public bool TryGetMap(IViewport viewport, ref IRaster raster) { int width; int height; try { width = Convert.ToInt32(viewport.Width); height = Convert.ToInt32(viewport.Height); } catch (OverflowException) { Trace.Write("Could not conver double to int (ExportMap size)"); return(false); } Client.WmsOnlineResource resource = GetPreferredMethod(); var uri = new Uri(GetRequestUrl(viewport.Extent, width, height)); WebRequest webRequest = WebRequest.Create(uri); webRequest.Method = resource.Type; webRequest.Timeout = TimeOut; webRequest.Credentials = Credentials ?? CredentialCache.DefaultCredentials; try { using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) using (var dataStream = webResponse.GetResponseStream()) { if (!webResponse.ContentType.StartsWith("image")) { return(false); } byte[] bytes = BruTile.Utilities.ReadFully(dataStream); raster = new Raster(new MemoryStream(bytes), viewport.Extent); } return(true); } catch (WebException webEx) { if (!ContinueOnError) { throw (new RenderException( "There was a problem connecting to the WMS server", webEx)); } Trace.Write("There was a problem connecting to the WMS server: " + webEx.Message); } catch (Exception ex) { if (!ContinueOnError) { throw (new RenderException("There was a problem while attempting to request the WMS", ex)); } Trace.Write("There was a problem while attempting to request the WMS" + ex.Message); } return(false); }
/// <summary> /// Gets the URL for a map request base on current settings, the image size and boundingbox /// </summary> /// <returns>URL for WMS request</returns> public string GetRequestUrl(BoundingBox box, int width, int height) { Client.WmsOnlineResource resource = GetPreferredMethod(); var strReq = new StringBuilder(resource.OnlineResource); if (!resource.OnlineResource.Contains("?")) { strReq.Append("?"); } if (!strReq.ToString().EndsWith("&") && !strReq.ToString().EndsWith("?")) { strReq.Append("&"); } strReq.AppendFormat(CultureInfo.InvariantCulture, "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}", width, 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"); } strReq.AppendFormat(_wmsClient.WmsVersion != "1.3.0" ? "&SRS={0}" : "&CRS={0}", SpatialReferenceSystem); strReq.AppendFormat("&VERSION={0}", _wmsClient.WmsVersion); strReq.Append("&TRANSPARENT=true"); strReq.Append("&Styles="); if (StylesList != null && StylesList.Count > 0) { foreach (string style in StylesList) { strReq.AppendFormat("{0},", style); } strReq.Remove(strReq.Length - 1, 1); } if (ExtraParams != null) { foreach (var extraParam in ExtraParams) { strReq.AppendFormat("&{0}={1}", extraParam.Key, extraParam.Value); } } return(strReq.ToString()); }