Пример #1
0
        private string GetRequestUrl(SharpMap.Geometries.BoundingBox box, TileSet tileSet)
        {
            SharpMap.Web.Wms.Client.WmsOnlineResource resource = GetPreferredMethod();
            System.Text.StringBuilder strReq = new StringBuilder(resource.OnlineResource);
            if (!resource.OnlineResource.Contains("?"))
            {
                strReq.Append("?");
            }
            if (!strReq.ToString().EndsWith("&") && !strReq.ToString().EndsWith("?"))
            {
                strReq.Append("&");
            }

            strReq.AppendFormat(SharpMap.Map.numberFormat_EnUS, "&REQUEST=GetMap&BBOX={0},{1},{2},{3}",
                                box.Min.X, box.Min.Y, box.Max.X, box.Max.Y);
            strReq.AppendFormat("&WIDTH={0}&Height={1}", tileSet.Width, tileSet.Height);
            strReq.Append("&LAYERS="); // LAYERS is set in caps because the current version of tilecache.py does not accept mixed case (a little bug)
            if (tileSet.Layers != null && tileSet.Layers.Count > 0)
            {
                foreach (string layer in tileSet.Layers)
                {
                    strReq.AppendFormat("{0},", layer);
                }
                strReq.Remove(strReq.Length - 1, 1);
            }
            strReq.AppendFormat("&FORMAT={0}", tileSet.Format);

            if (_WmsClient.WmsVersion == "1.3.0")
            {
                strReq.AppendFormat("&CRS={0}", tileSet.Srs);
            }
            else
            {
                strReq.AppendFormat("&SRS={0}", tileSet.Srs);
            }
            strReq.AppendFormat("&VERSION={0}", _WmsClient.WmsVersion);

            if (tileSet.Styles != null && tileSet.Styles.Count > 0)
            {
                strReq.Append("&STYLES=");
                foreach (string style in tileSet.Styles)
                {
                    strReq.AppendFormat("{0},", style);
                }
                strReq.Remove(strReq.Length - 1, 1);
            }

            if (_CustomParameters != null && _CustomParameters.Count > 0)
            {
                foreach (string name in _CustomParameters.Keys)
                {
                    string value = _CustomParameters[name];
                    strReq.AppendFormat("&{0}={1}", name, value);
                }
            }

            return(strReq.ToString());
        }
Пример #2
0
        /// <summary>
        /// Gets the URL for a map request base on current settings, the image size and boundingbox
        /// </summary>
        /// <param name="box">Area the WMS request should cover</param>
        /// <param name="size">Size of image</param>
        /// <returns>URL for WMS request</returns>
        public string GetRequestUrl(IEnvelope box, System.Drawing.Size size)
        {
            SharpMap.Web.Wms.Client.WmsOnlineResource resource = GetPreferredMethod();
            System.Text.StringBuilder strReq = new StringBuilder(resource.OnlineResource);
            if (!resource.OnlineResource.Contains("?"))
            {
                strReq.Append("?");
            }
            if (!strReq.ToString().EndsWith("&") && !strReq.ToString().EndsWith("?"))
            {
                strReq.Append("&");
            }

            strReq.AppendFormat(SharpMap.Map.numberFormat_EnUS, "REQUEST=GetMap&BBOX={0},{1},{2},{3}",
                                box.MinX, box.MinY, box.MaxX, box.MaxY);
            strReq.AppendFormat("&WIDTH={0}&Height={1}", size.Width, size.Height);
            strReq.Append("&Layers=");
            if (_LayerList != null && _LayerList.Count > 0)
            {
                foreach (string layer in _LayerList)
                {
                    strReq.AppendFormat("{0},", layer);
                }
                strReq.Remove(strReq.Length - 1, 1);
            }
            strReq.AppendFormat("&FORMAT={0}", _MimeType);
            if (_SpatialReferenceSystem == string.Empty)
            {
                throw new ApplicationException("Spatial reference system not set");
            }
            if (wmsClient.WmsVersion == "1.3.0")
            {
                strReq.AppendFormat("&CRS={0}", _SpatialReferenceSystem);
            }
            else
            {
                strReq.AppendFormat("&SRS={0}", _SpatialReferenceSystem);
            }
            strReq.AppendFormat("&VERSION={0}", wmsClient.WmsVersion);
            strReq.Append("&Styles=");
            if (_StylesList != null && _StylesList.Count > 0)
            {
                foreach (string style in _StylesList)
                {
                    strReq.AppendFormat("{0},", style);
                }
                strReq.Remove(strReq.Length - 1, 1);
            }
            return(strReq.ToString());
        }
Пример #3
0
        /// <summary>
        /// Renders the layer
        /// </summary>
        /// <param name="g">Graphics object reference</param>
        /// <param name="map">Map which is rendered</param>
        public override void Render(System.Drawing.Graphics g, Map map)
        {
            SharpMap.Web.Wms.Client.WmsOnlineResource resource = GetPreferredMethod();
            Uri myUri = new Uri(GetRequestUrl(map.Envelope, map.Size));

            System.Net.WebRequest myWebRequest = System.Net.WebRequest.Create(myUri);
            myWebRequest.Method  = resource.Type;
            myWebRequest.Timeout = _TimeOut;
            if (_Credentials != null)
            {
                myWebRequest.Credentials = _Credentials;
            }
            else
            {
                myWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }

            if (_Proxy != null)
            {
                myWebRequest.Proxy = _Proxy;
            }

            try
            {
                System.Net.HttpWebResponse myWebResponse = (System.Net.HttpWebResponse)myWebRequest.GetResponse();
                System.IO.Stream           dataStream    = myWebResponse.GetResponseStream();

                if (myWebResponse.ContentType.StartsWith("image"))
                {
                    System.Drawing.Image img = System.Drawing.Image.FromStream(myWebResponse.GetResponseStream());
                    if (_ImageAttributes != null)
                    {
                        g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0,
                                    img.Width, img.Height, GraphicsUnit.Pixel, this.ImageAttributes);
                    }
                    else
                    {
                        g.DrawImageUnscaled(img, 0, 0, map.Size.Width, map.Size.Height);
                    }
                }
                dataStream.Close();
                myWebResponse.Close();
            }
            catch (System.Net.WebException webEx)
            {
                if (!_ContinueOnError)
                {
                    throw (new SharpMap.Rendering.Exceptions.RenderException("There was a problem connecting to the WMS server when rendering layer '" + this.LayerName + "'", webEx));
                }
                else
                {
                    //Write out a trace warning instead of throwing an error to help debugging WMS problems
                    System.Diagnostics.Trace.Write("There was a problem connecting to the WMS server when rendering layer '" + this.LayerName + "': " + webEx.Message);
                }
            }
            catch (System.Exception ex)
            {
                if (!_ContinueOnError)
                {
                    throw (new SharpMap.Rendering.Exceptions.RenderException("There was a problem rendering layer '" + this.LayerName + "'", ex));
                }
                else
                {
                    //Write out a trace warning instead of throwing an error to help debugging WMS problems
                    System.Diagnostics.Trace.Write("There was a problem connecting to the WMS server when rendering layer '" + this.LayerName + "': " + ex.Message);
                }
            }
            base.Render(g, map);
        }
Пример #4
0
        private Bitmap WmsGetMap(BoundingBox extent, TileSet tileSet)
        {
            Stream responseStream = null;

            System.Drawing.Bitmap bitmap = null;

            SharpMap.Web.Wms.Client.WmsOnlineResource resource = GetPreferredMethod();
            string requestUrl = GetRequestUrl(extent, tileSet);
            Uri    myUri      = new Uri(requestUrl);

            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(myUri);
            webRequest.Method  = resource.Type;
            webRequest.Timeout = this.TimeOut;

            if (this.Credentials != null)
            {
                webRequest.Credentials = this.Credentials;
            }
            else
            {
                webRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }

            if (this.Proxy != null)
            {
                webRequest.Proxy = this.Proxy;
            }

            System.Net.HttpWebResponse webResponse = null;

            try
            {
                webResponse = (System.Net.HttpWebResponse)webRequest.GetResponse();

                if (webResponse.ContentType.StartsWith("image"))
                {
                    responseStream = webResponse.GetResponseStream();
                    bitmap         = (Bitmap)System.Drawing.Bitmap.FromStream(responseStream);
                    return((Bitmap)bitmap);
                }
                else
                {
                    //if the result was not an image retrieve content anyway for debugging.
                    responseStream = webResponse.GetResponseStream();
                    StreamReader readStream   = new StreamReader(responseStream, Encoding.UTF8);
                    StringWriter stringWriter = new StringWriter();
                    stringWriter.Write(readStream.ReadToEnd());
                    string message = "Failed to retrieve image from the WMS in layer '" + this.LayerName + "'. Was expecting image but received this: " + stringWriter.ToString();
                    HandleGetMapException(message, null);;
                    return(null);
                }
            }
            catch (System.Net.WebException webEx)
            {
                string message = "There was a problem connecting to the WMS server when rendering layer '" + this.LayerName + "'";
                HandleGetMapException(message, webEx);
            }
            catch (System.Exception ex)
            {
                string message = "There was a problem while retrieving the image from the WMS in layer '" + this.LayerName + "'";
                HandleGetMapException(message, ex);
            }
            finally
            {
                if (webResponse != null)
                {
                    webResponse.Close();
                }
                if (responseStream != null)
                {
                    responseStream.Close();
                    responseStream.Dispose();
                }
            }
            return(bitmap);
        }