/// <summary> /// Returns a List of the tile BoundingBoxes which cover the complete area of the map BoundingBox /// </summary> /// <param name="tileSet">The TileSet that provides the tiles</param> /// <param name="extent">The BoundingBox of the map</param> /// <param name="mapResolution">The resolution of the map</param> /// <returns></returns> public static List<BoundingBox> GetTileExtents(TileSet tileSet, BoundingBox extent, double mapResolution) { tileSet.Resolutions.Sort(); double tileResolution = GetTileResolution(tileSet.Resolutions, mapResolution); List<BoundingBox> tileExtents = new List<BoundingBox>(); double xOrigin = tileSet.BoundingBox.Left; double yOrigin = tileSet.BoundingBox.Bottom; double tileWorldUnits = tileResolution*tileSet.Width; BoundingBox tileBbox = new BoundingBox( Math.Floor((extent.Left - xOrigin)/tileWorldUnits)*tileWorldUnits + xOrigin, Math.Floor((extent.Bottom - yOrigin)/tileWorldUnits)*tileWorldUnits + yOrigin, Math.Ceiling((extent.Right - xOrigin)/tileWorldUnits)*tileWorldUnits + xOrigin, Math.Ceiling((extent.Top - yOrigin)/tileWorldUnits)*tileWorldUnits + yOrigin); int tileCountX = (int) Math.Round((tileBbox.Right - tileBbox.Left)/tileWorldUnits); int tileCountY = (int) Math.Round((tileBbox.Top - tileBbox.Bottom)/tileWorldUnits); for (int x = 0; x < tileCountX; x++) { for (int y = 0; y < tileCountY; y++) { double x1 = tileBbox.Left + x*tileWorldUnits; double y1 = tileBbox.Bottom + y*tileWorldUnits; double x2 = x1 + tileWorldUnits; double y2 = y1 + tileWorldUnits; BoundingBox tileExtent = new BoundingBox(x1, y1, x2, y2); if (CheckForBounds(tileSet.BoundingBox, tileExtent)) { tileExtents.Add(tileExtent); } } } return tileExtents; }
/// <summary> /// Returns a List of the tile BoundingBoxes which cover the complete area of the map BoundingBox /// </summary> /// <param name="tileSet">The TileSet that provides the tiles</param> /// <param name="extent">The BoundingBox of the map</param> /// <param name="mapResolution">The resolution of the map</param> /// <returns></returns> public static List<Envelope> GetTileExtents(TileSet tileSet, Envelope extent, double mapResolution) { tileSet.Resolutions.Sort(); double tileResolution = GetTileResolution(tileSet.Resolutions, mapResolution); var tileExtents = new List<Envelope>(); double xOrigin = tileSet.BoundingBox.MinX; double yOrigin = tileSet.BoundingBox.MinY; double tileWorldUnits = tileResolution*tileSet.Width; var tileBbox = new Envelope( Math.Floor((extent.MinX - xOrigin)/tileWorldUnits)*tileWorldUnits + xOrigin, Math.Floor((extent.MinY - yOrigin)/tileWorldUnits)*tileWorldUnits + yOrigin, Math.Ceiling((extent.MaxX - xOrigin)/tileWorldUnits)*tileWorldUnits + xOrigin, Math.Ceiling((extent.MaxY - yOrigin)/tileWorldUnits)*tileWorldUnits + yOrigin); int tileCountX = (int) Math.Round((tileBbox.MaxX - tileBbox.MinX)/tileWorldUnits); int tileCountY = (int) Math.Round((tileBbox.MaxY - tileBbox.MinY)/tileWorldUnits); for (int x = 0; x < tileCountX; x++) { for (int y = 0; y < tileCountY; y++) { double x1 = tileBbox.MinX + x*tileWorldUnits; double y1 = tileBbox.MinY + y*tileWorldUnits; double x2 = x1 + tileWorldUnits; double y2 = y1 + tileWorldUnits; var tileExtent = new Envelope(x1, x2, y1, y2); if (CheckForBounds(tileSet.BoundingBox, tileExtent)) { tileExtents.Add(tileExtent); } } } return tileExtents; }
private Bitmap WmsGetMap(Envelope extent, TileSet tileSet) { Stream responseStream = null; Bitmap bitmap = null; Client.WmsOnlineResource resource = GetPreferredMethod(); string requestUrl = GetRequestUrl(extent, tileSet); Uri myUri = new Uri(requestUrl); WebRequest webRequest = WebRequest.Create(myUri); webRequest.Method = resource.Type; webRequest.Timeout = TimeOut; if (Credentials != null) webRequest.Credentials = Credentials; else webRequest.Credentials = CredentialCache.DefaultCredentials; if (Proxy != null) webRequest.Proxy = Proxy; HttpWebResponse webResponse = null; try { webResponse = (HttpWebResponse) webRequest.GetResponse(); if (webResponse.ContentType.StartsWith("image")) { responseStream = webResponse.GetResponseStream(); bitmap = (Bitmap) 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 '" + LayerName + "'. Was expecting image but received this: " + stringWriter.ToString(); HandleGetMapException(message, null); ; return null; } } catch (WebException webEx) { string message = "There was a problem connecting to the WMS server when rendering layer '" + LayerName + "'"; HandleGetMapException(message, webEx); } catch (Exception ex) { string message = "There was a problem while retrieving the image from the WMS in layer '" + LayerName + "'"; HandleGetMapException(message, ex); } finally { if (webResponse != null) { webResponse.Close(); } if (responseStream != null) { responseStream.Close(); responseStream.Dispose(); } } return bitmap; }
private string GetRequestUrl(Envelope box, TileSet tileSet) { Client.WmsOnlineResource resource = GetPreferredMethod(); StringBuilder strReq = new StringBuilder(resource.OnlineResource); if (!resource.OnlineResource.Contains("?")) strReq.Append("?"); if (!strReq.ToString().EndsWith("&") && !strReq.ToString().EndsWith("?")) strReq.Append("&"); strReq.AppendFormat(Map.NumberFormatEnUs, "&REQUEST=GetMap&BBOX={0},{1},{2},{3}", box.MinX, box.MinY, box.MaxX, box.MaxY); 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(); }
private static TileSet ParseTileSetNode(XmlNode xnlTileSet, XmlNamespaceManager nsmgr) { var tileSet = new TileSet(); var xnLayers = xnlTileSet.SelectSingleNode("sm:Layers", nsmgr); if (xnLayers != null) tileSet.Layers.AddRange(xnLayers.InnerText.Split(new[] {','})); tileSet.Name = CreateDefaultName(tileSet._layers); var xnSrs = xnlTileSet.SelectSingleNode("sm:SRS", nsmgr); if (xnSrs != null) tileSet.Srs = xnSrs.InnerText; var xnWidth = xnlTileSet.SelectSingleNode("sm:Width", nsmgr); if (xnWidth != null) { int width; if (!Int32.TryParse(xnWidth.InnerText, NumberStyles.Any, Map.NumberFormatEnUs, out width)) throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); tileSet.Width = width; } var xnHeight = xnlTileSet.SelectSingleNode("sm:Height", nsmgr); if (xnHeight != null) { int height; if (!Int32.TryParse(xnHeight.InnerText, NumberStyles.Any, Map.NumberFormatEnUs, out height)) throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); tileSet.Height = height; } var xnFormat = xnlTileSet.SelectSingleNode("sm:Format", nsmgr); if (xnFormat != null) tileSet.Format = xnFormat.InnerText; var xnStyles = xnlTileSet.SelectSingleNode("sm:Styles", nsmgr); if (xnStyles != null) tileSet.Styles.AddRange(xnStyles.InnerText.Split(new[] {','})); var xnBoundingBox = xnlTileSet.SelectSingleNode("sm:BoundingBox", nsmgr); if (xnBoundingBox != null) { var att = xnBoundingBox.Attributes; double minx, miny, maxx, maxy; if (att == null || ( !double.TryParse(att["minx"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out minx) & !double.TryParse(att["miny"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out miny) & !double.TryParse(att["maxx"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out maxx) & !double.TryParse(att["maxy"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out maxy))) { throw new ArgumentException("Invalid LatLonBoundingBox on tileset '" + tileSet.Name + "'"); } tileSet.BoundingBox = new Envelope(minx, maxx, miny, maxy); } var xnResolutions = xnlTileSet.SelectSingleNode("sm:Resolutions", nsmgr); if (xnResolutions != null) { var resolutions = xnResolutions.InnerText.TrimEnd(' ').Split(new[] {' '}); foreach (string resolutionStr in resolutions) { if (resolutionStr != "") { double resolution; if (!Double.TryParse(resolutionStr, NumberStyles.Any, Map.NumberFormatEnUs, out resolution)) throw new ArgumentException("Invalid resolution on tileset '" + tileSet.Name + "'"); tileSet.Resolutions.Add(resolution); } } } return tileSet; }
private static TileSet ParseTileSetNode(XmlNode xnlTileSet, XmlNamespaceManager nsmgr) { TileSet tileSet = new TileSet(); XmlNode xnLayers = xnlTileSet.SelectSingleNode("sm:Layers", nsmgr); if (xnLayers != null) { tileSet.Layers.AddRange(xnLayers.InnerText.Split(new char[] { ',' })); } tileSet.Name = CreateDefaultName(tileSet.layers); XmlNode xnSRS = xnlTileSet.SelectSingleNode("sm:SRS", nsmgr); if (xnSRS != null) { tileSet.Srs = xnSRS.InnerText; } XmlNode xnWidth = xnlTileSet.SelectSingleNode("sm:Width", nsmgr); if (xnWidth != null) { int width; if (!Int32.TryParse(xnWidth.InnerText, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out width)) { throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); } tileSet.Width = width; } XmlNode xnHeight = xnlTileSet.SelectSingleNode("sm:Height", nsmgr); if (xnHeight != null) { int height; if (!Int32.TryParse(xnWidth.InnerText, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out height)) { throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); } tileSet.Height = height; } XmlNode xnFormat = xnlTileSet.SelectSingleNode("sm:Format", nsmgr); if (xnFormat != null) { tileSet.Format = xnFormat.InnerText; } XmlNode xnStyles = xnlTileSet.SelectSingleNode("sm:Styles", nsmgr); if (xnStyles != null) { tileSet.Styles.AddRange(xnStyles.InnerText.Split(new char[] { ',' })); } XmlNode xnBoundingBox = xnlTileSet.SelectSingleNode("sm:BoundingBox", nsmgr); if (xnBoundingBox != null) { double minx = 0; double miny = 0; double maxx = 0; double maxy = 0; if ( !double.TryParse(xnBoundingBox.Attributes["minx"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out minx) & !double.TryParse(xnBoundingBox.Attributes["miny"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out miny) & !double.TryParse(xnBoundingBox.Attributes["maxx"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out maxx) & !double.TryParse(xnBoundingBox.Attributes["maxy"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out maxy) ) { throw new ArgumentException("Invalid LatLonBoundingBox on tileset '" + tileSet.Name + "'"); } tileSet.BoundingBox = new SharpMap.Geometries.BoundingBox(minx, miny, maxx, maxy); } XmlNode xnResolutions = xnlTileSet.SelectSingleNode("sm:Resolutions", nsmgr); if (xnResolutions != null) { double resolution; string[] resolutions = xnResolutions.InnerText.Split(new char[] { ' ' }); foreach (string resolutionStr in resolutions) { if (!Double.TryParse(resolutionStr, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out resolution)) { throw new ArgumentException("Invalid resolution on tileset '" + tileSet.Name + "'"); } tileSet.Resolutions.Add(resolution); } } return(tileSet); }
private static TileSet ParseTileSetNode(XmlNode xnlTileSet, XmlNamespaceManager nsmgr) { TileSet tileSet = new TileSet(); XmlNode xnLayers = xnlTileSet.SelectSingleNode("sm:Layers", nsmgr); if (xnLayers != null) tileSet.Layers.AddRange(xnLayers.InnerText.Split(new char[] { ',' })); tileSet.Name = CreateDefaultName(tileSet.layers); XmlNode xnSRS = xnlTileSet.SelectSingleNode("sm:SRS", nsmgr); if (xnSRS != null) tileSet.Srs = xnSRS.InnerText; XmlNode xnWidth = xnlTileSet.SelectSingleNode("sm:Width", nsmgr); if (xnWidth != null) { int width; if (!Int32.TryParse(xnWidth.InnerText, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out width)) throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); tileSet.Width = width; } XmlNode xnHeight = xnlTileSet.SelectSingleNode("sm:Height", nsmgr); if (xnHeight != null) { int height; if (!Int32.TryParse(xnWidth.InnerText, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out height)) throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); tileSet.Height = height; } XmlNode xnFormat = xnlTileSet.SelectSingleNode("sm:Format", nsmgr); if (xnFormat != null) tileSet.Format = xnFormat.InnerText; XmlNode xnStyles = xnlTileSet.SelectSingleNode("sm:Styles", nsmgr); if (xnStyles != null) tileSet.Styles.AddRange(xnStyles.InnerText.Split(new char[] { ',' })); XmlNode xnBoundingBox = xnlTileSet.SelectSingleNode("sm:BoundingBox", nsmgr); if (xnBoundingBox != null) { double minx = 0; double miny = 0; double maxx = 0; double maxy = 0; if ( !double.TryParse(xnBoundingBox.Attributes["minx"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out minx) & !double.TryParse(xnBoundingBox.Attributes["miny"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out miny) & !double.TryParse(xnBoundingBox.Attributes["maxx"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out maxx) & !double.TryParse(xnBoundingBox.Attributes["maxy"].Value, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out maxy) ) { throw new ArgumentException("Invalid LatLonBoundingBox on tileset '" + tileSet.Name + "'"); } tileSet.BoundingBox = new SharpMap.Geometries.BoundingBox(minx, miny, maxx, maxy); } XmlNode xnResolutions = xnlTileSet.SelectSingleNode("sm:Resolutions", nsmgr); if (xnResolutions != null) { double resolution; string[] resolutions = xnResolutions.InnerText.Split(new char[] { ' ' }); foreach (string resolutionStr in resolutions) { if (!Double.TryParse(resolutionStr, System.Globalization.NumberStyles.Any, SharpMap.Map.numberFormat_EnUS, out resolution)) throw new ArgumentException("Invalid resolution on tileset '" + tileSet.Name + "'"); tileSet.Resolutions.Add(resolution); } } return tileSet; }
private static TileSet ParseTileSetNode(XmlNode xnlTileSet, XmlNamespaceManager nsmgr) { var tileSet = new TileSet(); var xnLayers = xnlTileSet.SelectSingleNode("sm:Layers", nsmgr); if (xnLayers != null) { tileSet.Layers.AddRange(xnLayers.InnerText.Split(new[] { ',' })); } tileSet.Name = CreateDefaultName(tileSet._layers); var xnSrs = xnlTileSet.SelectSingleNode("sm:SRS", nsmgr); if (xnSrs != null) { tileSet.Srs = xnSrs.InnerText; } var xnWidth = xnlTileSet.SelectSingleNode("sm:Width", nsmgr); if (xnWidth != null) { int width; if (!Int32.TryParse(xnWidth.InnerText, NumberStyles.Any, Map.NumberFormatEnUs, out width)) { throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); } tileSet.Width = width; } var xnHeight = xnlTileSet.SelectSingleNode("sm:Height", nsmgr); if (xnHeight != null) { int height; if (!Int32.TryParse(xnHeight.InnerText, NumberStyles.Any, Map.NumberFormatEnUs, out height)) { throw new ArgumentException("Invalid width on tileset '" + tileSet.Name + "'"); } tileSet.Height = height; } var xnFormat = xnlTileSet.SelectSingleNode("sm:Format", nsmgr); if (xnFormat != null) { tileSet.Format = xnFormat.InnerText; } var xnStyles = xnlTileSet.SelectSingleNode("sm:Styles", nsmgr); if (xnStyles != null) { tileSet.Styles.AddRange(xnStyles.InnerText.Split(new[] { ',' })); } var xnBoundingBox = xnlTileSet.SelectSingleNode("sm:BoundingBox", nsmgr); if (xnBoundingBox != null) { var att = xnBoundingBox.Attributes; double minx, miny, maxx, maxy; if (att == null || ( !double.TryParse(att["minx"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out minx) & !double.TryParse(att["miny"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out miny) & !double.TryParse(att["maxx"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out maxx) & !double.TryParse(att["maxy"].Value, NumberStyles.Any, Map.NumberFormatEnUs, out maxy))) { throw new ArgumentException("Invalid LatLonBoundingBox on tileset '" + tileSet.Name + "'"); } tileSet.BoundingBox = new Envelope(minx, maxx, miny, maxy); } var xnResolutions = xnlTileSet.SelectSingleNode("sm:Resolutions", nsmgr); if (xnResolutions != null) { var resolutions = xnResolutions.InnerText.TrimEnd(' ').Split(new[] { ' ' }); foreach (string resolutionStr in resolutions) { if (resolutionStr != "") { double resolution; if (!Double.TryParse(resolutionStr, NumberStyles.Any, Map.NumberFormatEnUs, out resolution)) { throw new ArgumentException("Invalid resolution on tileset '" + tileSet.Name + "'"); } tileSet.Resolutions.Add(resolution); } } } return(tileSet); }