示例#1
0
        public Uri GetImageAddress(Wms wms)
        {
            // This object is readonly.
            // WMS is readonly.
            Debug.Assert(wms != null, "Every layer must have a parent layer!");

            if (layers.Length > 0 && (crsId >= 0 && crsId < wms.CRSCount))
            {
                // Coordinates of requested map, left ...
                var left = viewport.LeftCorner.ToString(CultureInfo.InvariantCulture);
                // ... bottom ...
                var bottom = viewport.BottomCorner.ToString(CultureInfo.InvariantCulture);
                // ... right ...
                var right = viewport.RightCorner.ToString(CultureInfo.InvariantCulture);
                var top   = viewport.TopCorner.ToString(CultureInfo.InvariantCulture);    // ... and top

                var isReversed = wms.IsCrsReversed;
                var minx       = isReversed ? bottom : left;
                var miny       = isReversed ? left : bottom;
                var maxx       = isReversed ? top : right;
                var maxy       = isReversed ? right : top;

                var address = string.Format(CultureInfo.InvariantCulture, IMAGE_ADDRESS_TEMPLATE,
                                            wms.ServiceAddress,                                      // Address of the service
                                            wms.Version,                                             // Version of the service
                                            layers,                                                  // Ordered list of layers (the first is the top-most)
                                            wms.CRSVariableName,                                     // Variable name for CRS
                                            wms.GetCRSName(crsId),                                   // CRS of requested map
                                            minx, miny, maxx, maxy,
                                            viewport.Width,                                          // Width of image in pixels
                                            viewport.Height,                                         // Height of image in pixels
                                            wms.GetImageFormat(format),                              // Format of the map image
                                            isTransparent.ToString(),                                // String representation of transparency
                                                                                                     // Transparent color in hexadecimal: red ...
                                            bgColor.R.ToString("X2", CultureInfo.InvariantCulture),
                                            bgColor.G.ToString("X2", CultureInfo.InvariantCulture),  // ... green ...
                                            bgColor.B.ToString("X2", CultureInfo.InvariantCulture)); // ... and blue

                try
                {
                    var uri = new Uri(address);
                    return(uri);
                }
                catch (UriFormatException)
                {
                    Debug.Fail("The address template is incorrect!");
                    return(null);
                }
            }

            return(null);
        }