/// <summary> /// Renders the layer /// </summary> /// <param name="g">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void Render(IGraphics g, Map map) { if (BaseLayer != null) { BaseLayer.Render(g, map); } var fds = new FeatureCollectionSet(); var box = map.Envelope; ExecuteIntersectionQuery(box, fds); if (fds.Count == 0 || fds[0].Count == 0) { base.Render(g, map); return; } var zoomIndex = GetZoomIndex(map.Zoom); var dot = _bitmaps[zoomIndex]; var opacity = _opacity[zoomIndex]; using (var image = new Bitmap(map.Size.Width + dot.Width, map.Size.Height + dot.Height, PixelFormat.Format32bppArgb)) { using (IGraphics gr = Graphics.FromImage(image).G()) gr.Clear(Color.White); DrawPoints(map, fds[0], dot, image); Colorize(image, HeatColorBlend, opacity); g.DrawImage(image, -dot.Width / 2, -dot.Height / 2); } }
/// <summary> /// Check if the layer can be queried and retrieve data, if there is any. /// </summary> protected bool TryGetData(Map map, float x, float y, int pixelSensitivity, WmsServer.InterSectDelegate intersectDelegate, ICanQueryLayer queryLayer, string cqlFilter, out IFeatureCollectionSet fds) { if (!queryLayer.IsQueryEnabled) { fds = null; return(false); } float queryBoxMinX = x - pixelSensitivity; float queryBoxMinY = y - pixelSensitivity; float queryBoxMaxX = x + pixelSensitivity; float queryBoxMaxY = y + pixelSensitivity; Coordinate minXY = map.ImageToWorld(new PointF(queryBoxMinX, queryBoxMinY)); Coordinate maxXY = map.ImageToWorld(new PointF(queryBoxMaxX, queryBoxMaxY)); Envelope queryBox = new Envelope(minXY, maxXY); fds = new FeatureCollectionSet(); queryLayer.ExecuteIntersectionQuery(queryBox, fds); if (fds.Count == 0) { return(false); } var table = fds[0]; if (intersectDelegate != null) { fds.Remove(table); fds.Add(intersectDelegate(table, queryBox)); table = fds[0]; } // filter the rows with the CQLFilter if one is provided if (cqlFilter != null) { var toKeep = table.Clone(); foreach (var feature in table) { if (CqlFilter(feature, cqlFilter)) { toKeep.Add(feature); } } fds.Remove(table); fds.Add(toKeep); } return(fds[0].Count > 0); }
public JsonResult GetData(float w, float n, float e, float s, int z) { string format = String.Format("~/App_Data/berlin/{0}", "osmbuildings.shp"); string path = this.HttpContext.Server.MapPath(format); if (!System.IO.File.Exists(path)) throw new FileNotFoundException("file not found", path); Point start = this.GeoToPixel(n, w, z); var meta = new { n, w, s, e, x = start.X, y = start.Y, z }; Envelope bbox = new Envelope(); bbox.ExpandToInclude(new Coordinate(n, w)); bbox.ExpandToInclude(new Coordinate(s, e)); var ds = new FeatureCollectionSet(); using (ShapeFile provider = new ShapeFile(path)) { provider.DoTrueIntersectionQuery = true; provider.Open(); provider.ExecuteIntersectionQuery(bbox, ds); provider.Close(); } int zz = MaxZoom - z; List<object> data = new List<object>(); var table = ds[0]; foreach (var row in table) { int c = (short)(row.Attributes["height"]); if (c == 0) c = 5; // default value for "null" (zero) heights int h = c * ScaleZ >> zz; if (h <= 1) h = 1; IGeometry geometry = row.Geometry; Coordinate[] coords = geometry.Coordinates; int total = coords.Length; double[] values = new double[total * 2]; int i = 0; foreach (Coordinate curr in coords) { Point p = this.GeoToPixel(curr.X, curr.Y, z); values[i++] = p.X - start.X; values[i++] = p.Y - start.Y; } data.Add(new object[] { h, values }); } return this.Json(new { meta, data }, JsonRequestBehavior.AllowGet); }
public JsonResult GetData(string layer, int z, int x, int y) { if (String.IsNullOrEmpty(layer)) { throw new ArgumentNullException("layer"); } Map map = ShapefileHelper.Spherical(); IQueryable <VectorLayer> coll = map.Layers .AsQueryable() .OfType <VectorLayer>() .Where(l => l.Enabled && l.IsQueryEnabled) .Where(l => String.Equals(l.LayerName, layer)); VectorLayer query = coll.SingleOrDefault(); if (query == null) { throw new ArgumentException("Layer not found: " + layer); } if (query.SRID != 4326) { throw new ArgumentException("Only EPSG:4326 supported"); } using (Utf8Grid grid = new Utf8Grid(UtfGridResolution, x, y, z)) { Envelope bbox = this.GetBoundingBoxInLatLngWithMargin(x, y, z); var ds = new FeatureCollectionSet(); query.ExecuteIntersectionQuery(bbox, ds); IEnumerable <GeoJSON> data = GeoJSONHelper.GetData(ds); int i = 1; foreach (GeoJSON val in data) { IGeometry geom = val.Geometry; IDictionary <string, object> dict = val.Values; grid.FillPolygon(geom, i, dict); i = i + 1; } Utf8GridResults results = grid.CreateUtfGridJson(); return(this.Json(new { keys = results.Keys, data = results.Data, grid = results.Grid, }, JsonRequestBehavior.AllowGet)); } }
public void linq_should_work_with_feature_collections() { Envelope world = new Envelope(-180, 180, -90, 90); ILayer layer = Map.GetLayerByName("poly_landmarks"); Assert.That(layer, Is.Not.Null); Assert.That(layer, Is.InstanceOf<ICanQueryLayer>()); IFeatureCollectionSet data = new FeatureCollectionSet(); ICanQueryLayer query = (ICanQueryLayer)layer; query.ExecuteIntersectionQuery(world, data); foreach (IFeatureCollection table in data) { Assert.That(table, Is.Not.Null); Assert.That(table, Is.Not.Empty); IList<IFeature> list = table.ToList(); Assert.That(list, Is.Not.Null); } }
public void linq_should_work_with_feature_collections() { Envelope world = new Envelope(-180, 180, -90, 90); ILayer layer = Map.GetLayerByName("poly_landmarks"); Assert.That(layer, Is.Not.Null); Assert.That(layer, Is.InstanceOf <ICanQueryLayer>()); IFeatureCollectionSet data = new FeatureCollectionSet(); ICanQueryLayer query = (ICanQueryLayer)layer; query.ExecuteIntersectionQuery(world, data); foreach (IFeatureCollection table in data) { Assert.That(table, Is.Not.Null); Assert.That(table, Is.Not.Empty); IList <IFeature> list = table.ToList(); Assert.That(list, Is.Not.Null); } }
public JsonResult GetData(string layer, int z, int x, int y) { if (String.IsNullOrEmpty(layer)) throw new ArgumentNullException("layer"); Map map = ShapefileHelper.Spherical(); IQueryable<VectorLayer> coll = map.Layers .AsQueryable() .OfType<VectorLayer>() .Where(l => l.Enabled && l.IsQueryEnabled) .Where(l => String.Equals(l.LayerName, layer)); VectorLayer query = coll.SingleOrDefault(); if (query == null) throw new ArgumentException("Layer not found: " + layer); if (query.SRID != 4326) throw new ArgumentException("Only EPSG:4326 supported"); using (Utf8Grid grid = new Utf8Grid(UtfGridResolution, x, y, z)) { Envelope bbox = this.GetBoundingBoxInLatLngWithMargin(x, y, z); var ds = new FeatureCollectionSet(); query.ExecuteIntersectionQuery(bbox, ds); IEnumerable<GeoJSON> data = GeoJSONHelper.GetData(ds); int i = 1; foreach (GeoJSON val in data) { IGeometry geom = val.Geometry; IDictionary<string, object> dict = val.Values; grid.FillPolygon(geom, i, dict); i = i + 1; } Utf8GridResults results = grid.CreateUtfGridJson(); return this.Json(new { keys = results.Keys, data = results.Data, grid = results.Grid, }, JsonRequestBehavior.AllowGet); } }
/// <summary> /// Renders the layer /// </summary> /// <param name="g">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void Render(IGraphics g, Map map) { if (Style.Enabled && Style.MaxVisible >= map.Zoom && Style.MinVisible < map.Zoom) { if (DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'")); } g.TextRenderingHint = TextRenderingHint; g.SmoothingMode = SmoothingMode; var envelope = map.Envelope; //View to render var lineClipping = new CohenSutherlandLineClipping(envelope.MinX, envelope.MinY, envelope.MaxX, envelope.MaxY); if (CoordinateTransformation != null) { #if !DotSpatialProjections if (ReverseCoordinateTransformation != null) { envelope = GeometryTransform.TransformBox(envelope, ReverseCoordinateTransformation.MathTransform); } else { CoordinateTransformation.MathTransform.Invert(); envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform); CoordinateTransformation.MathTransform.Invert(); } #else envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source); #endif } var ds = new FeatureCollectionSet(); DataSource.Open(); DataSource.ExecuteIntersectionQuery(envelope, ds); DataSource.Close(); if (ds.Count == 0) { base.Render(g, map); return; } var features = ds[0]; //Initialize label collection List <BaseLabel> labels = new List <BaseLabel>(); //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection //Render labels foreach (var feature in features) { var featureGeometry = (IGeometry)feature.Geometry.Clone(); if (CoordinateTransformation != null) #if !DotSpatialProjections { featureGeometry = GeometryTransform.TransformGeometry( featureGeometry, CoordinateTransformation.MathTransform, GeometryServiceProvider.Instance.CreateGeometryFactory((int)CoordinateTransformation.TargetCS.AuthorityCode) ); } #else { featuresGeometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation.Source, CoordinateTransformation.Target, CoordinateTransformation.TargetFactory); } #endif LabelStyle style; if (Theme != null) //If thematics is enabled, lets override the style { style = Theme.GetStyle(feature) as LabelStyle; } else { style = Style; } float rotationStyle = style != null ? style.Rotation : 0f; float rotationColumn = 0f; if (!String.IsNullOrEmpty(RotationColumn)) { Single.TryParse(feature.Attributes[RotationColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs, out rotationColumn); } float rotation = rotationStyle + rotationColumn; int priority = Priority; if (_getPriorityMethod != null) { priority = _getPriorityMethod(feature); } else if (!String.IsNullOrEmpty(PriorityColumn)) { Int32.TryParse(feature.Attributes[PriorityColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs, out priority); } string text; if (_getLabelMethod != null) { text = _getLabelMethod(feature); } else { text = feature.Attributes[LabelColumn].ToString(); } if (!String.IsNullOrEmpty(text)) { // for lineal geometries, try clipping to ensure proper labeling if (featureGeometry is ILineal) { if (featureGeometry is ILineString) { featureGeometry = lineClipping.ClipLineString(featureGeometry as ILineString); } else if (featureGeometry is IMultiLineString) { featureGeometry = lineClipping.ClipLineString(featureGeometry as IMultiLineString); } } if (featureGeometry is IGeometryCollection) { if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.All) { foreach (var geom in (featureGeometry as IGeometryCollection)) { BaseLabel lbl = CreateLabel(feature, geom, text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) { labels.Add(lbl); } } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.CommonCenter) { BaseLabel lbl = CreateLabel(feature, featureGeometry, text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) { labels.Add(lbl); } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.First) { if ((featureGeometry as IGeometryCollection).NumGeometries > 0) { BaseLabel lbl = CreateLabel(feature, (featureGeometry as IGeometryCollection).GetGeometryN(0), text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.Largest) { var coll = (featureGeometry as IGeometryCollection); if (coll.NumGeometries > 0) { var largestVal = 0d; var idxOfLargest = 0; for (var j = 0; j < coll.NumGeometries; j++) { var geom = coll.GetGeometryN(j); if (geom is ILineString && ((ILineString)geom).Length > largestVal) { largestVal = ((ILineString)geom).Length; idxOfLargest = j; } if (geom is IMultiLineString && ((IMultiLineString)geom).Length > largestVal) { largestVal = ((IMultiLineString)geom).Length; idxOfLargest = j; } if (geom is IPolygon && ((IPolygon)geom).Area > largestVal) { largestVal = ((IPolygon)geom).Area; idxOfLargest = j; } if (geom is IMultiPolygon && ((IMultiPolygon)geom).Area > largestVal) { largestVal = ((IMultiPolygon)geom).Area; idxOfLargest = j; } } BaseLabel lbl = CreateLabel(feature, coll.GetGeometryN(idxOfLargest), text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) { labels.Add(lbl); } } } } else { BaseLabel lbl = CreateLabel(feature, featureGeometry, text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) { labels.Add(lbl); } } } } if (labels.Count > 0) //We have labels to render... { if (Style.CollisionDetection && _labelFilter != null) { _labelFilter(labels); } for (int i = 0; i < labels.Count; i++) { // Don't show the label if not necessary if (!labels[i].Show) { continue; } if (labels[i] is Label) { var label = labels[i] as Label; if (label.Style.IsTextOnPath == false || label.TextOnPathLabel == null) { Renderer.Draw(map, g, label); } else { if (label.Style.BackColor != null && label.Style.BackColor != System.Drawing.Brushes.Transparent) { //draw background if (label.TextOnPathLabel.RegionList.Count > 0) { g.FillRectangles(labels[i].Style.BackColor, labels[i].TextOnPathLabel.RegionList.ToArray()); //g.FillPolygon(labels[i].Style.BackColor, labels[i].TextOnPathLabel.PointsText.ToArray()); } } label.TextOnPathLabel.DrawTextOnPath(); } } else if (labels[i] is PathLabel) { var plbl = labels[i] as PathLabel; var lblStyle = plbl.Style; g.DrawString(lblStyle.Halo, new SolidBrush(lblStyle.ForeColor), plbl.Text, lblStyle.Font.FontFamily, (int)lblStyle.Font.Style, lblStyle.Font.Size, lblStyle.GetStringFormat(), lblStyle.IgnoreLength, plbl.Location); } } } } base.Render(g, map); }
/// <summary> /// Method to render this layer to the map, applying <paramref name="theme"/>. /// </summary> /// <param name="g">The graphics object</param> /// <param name="map">The map object</param> /// <param name="envelope">The envelope to render</param> /// <param name="theme">The theme to apply</param> protected void RenderInternal(IGraphics g, Map map, Envelope envelope, ITheme theme) { IFeatureCollectionSet ds; lock (_dataSource) { ds = new FeatureCollectionSet(); DataSource.Open(); DataSource.ExecuteIntersectionQuery(envelope, ds); DataSource.Close(); } foreach (var features in ds) { if (CoordinateTransformation != null) { for (int i = 0; i < features.Count; i++) #if !DotSpatialProjections { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation. MathTransform, GeometryServiceProvider.Instance.CreateGeometryFactory((int)CoordinateTransformation.TargetCS.AuthorityCode)); } } #else { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation.Source, CoordinateTransformation.Target, CoordinateTransformation.TargetFactory); } #endif //Linestring outlines is drawn by drawing the layer once with a thicker line //before drawing the "inline" on top. if (Style.EnableOutline) { for (int i = 0; i < features.Count; i++) { var feature = features[i]; var outlineStyle = theme.GetStyle(feature) as VectorStyle; if (outlineStyle == null) { continue; } if (!(outlineStyle.Enabled && outlineStyle.EnableOutline)) { continue; } if (!(outlineStyle.MinVisible <= map.Zoom && map.Zoom <= outlineStyle.MaxVisible)) { continue; } using (outlineStyle = outlineStyle.Clone()) { if (outlineStyle != null) { //Draw background of all line-outlines first Renderer.DrawOutline(map, g, feature.Geometry, outlineStyle); } } } } for (int i = 0; i < features.Count; i++) { var feature = features[i]; var style = theme.GetStyle(feature); if (style == null) { continue; } if (!style.Enabled) { continue; } if (!(style.MinVisible <= map.Zoom && map.Zoom <= style.MaxVisible)) { continue; } IStyle[] stylesToRender = GetStylesToRender(style); if (stylesToRender == null) { return; } foreach (var vstyle in stylesToRender) { if (!(vstyle is VectorStyle) || !vstyle.Enabled) { continue; } using (var clone = (vstyle as VectorStyle).Clone()) { if (clone != null) { RenderGeometry(g, map, feature.Geometry, clone); } } } } } }
/// <summary> /// Gets FeatureInfo as text/plain /// </summary> /// <param name="map">The map</param> /// <param name="requestedLayers">The requested layers</param> /// <param name="x">The x-ordinate</param> /// <param name="y">The y-ordinate</param> /// <param name="featureCount"></param> /// <param name="cqlFilter">The code query language</param> /// <param name="context">The <see cref="HttpContext"/> to use. If not specified or <value>null</value>, <see cref="HttpContext.Current"/> is used.</param> /// <exception cref="InvalidOperationException">Thrown if this function is used without a valid <see cref="HttpContext"/> at hand</exception> /// <returns>Plain text string with featureinfo results</returns> public static string CreateFeatureInfoPlain(Map map, string[] requestedLayers, Single x, Single y, int featureCount, string cqlFilter, HttpContext context = null) { if (context == null) context = HttpContext.Current; if (context == null) throw new InvalidOperationException("Cannot use CreateFeatureInfoPlain without a valid HttpContext"); var vstr = "GetFeatureInfo results: \n"; foreach (string requestLayer in requestedLayers) { bool found = false; foreach (var mapLayer in map.Layers) { if (String.Equals(mapLayer.LayerName, requestLayer, StringComparison.InvariantCultureIgnoreCase)) { found = true; var queryLayer = mapLayer as ICanQueryLayer; if (queryLayer == null || !queryLayer.IsQueryEnabled) continue; var queryBoxMinX = x - (_pixelSensitivity); var queryBoxMinY = y - (_pixelSensitivity); var queryBoxMaxX = x + (_pixelSensitivity); var queryBoxMaxY = y + (_pixelSensitivity); var minXY = map.ImageToWorld(new PointF(queryBoxMinX, queryBoxMinY)); var maxXY = map.ImageToWorld(new PointF(queryBoxMaxX, queryBoxMaxY)); var queryBox = new Envelope(minXY, maxXY); var fds = new FeatureCollectionSet(); queryLayer.ExecuteIntersectionQuery(queryBox, fds); if (fds.Count == 0) { vstr = vstr + "\nSearch returned no results on layer: " + requestLayer; } if (_intersectDelegate != null) { var fdt = fds[0]; fds.RemoveAt(0); fds.Add(_intersectDelegate(fdt, queryBox)); } else { if (fds[0].Count == 0) { vstr = vstr + "\nSearch returned no results on layer: " + requestLayer + " "; } else { //filter the rows with the CQLFilter if one is provided if (cqlFilter != null) { var toKeep = fds[0].Clone(); foreach (var f in fds[0]) { if (!CqlFilter(f, cqlFilter)) { toKeep.Add(f); } } fds[0] = toKeep; } //if featurecount < fds...count, select smallest bbox, because most likely to be clicked vstr = vstr + "\n Layer: '" + requestLayer + "'\n Featureinfo:\n"; var keys = new object[fds[0].Count]; var area = new double[fds[0].Count]; var l = 0; foreach (var f in fds[0]) { area[l] = f.Geometry.EnvelopeInternal.Area; keys[l] = f.Attributes[0]; } Array.Sort(area, keys); if (fds[0].Count < featureCount) { featureCount = fds[0].Count; } for (int k = 0; k < featureCount; k++) { var f = fds[0][keys[k]]; var att = f.Factory.AttributesDefinition; for (int j = 0; j < att.Count; j++) { vstr = vstr + " '" + f.Attributes[j] + "'"; } if ((k + 1) < featureCount) vstr = vstr + ",\n"; } } } } } if (found == false) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotDefined, "Unknown layer '" + requestLayer + "'", context); return string.Empty; } } return vstr; }
/// <summary> /// Gets FeatureInfo as GeoJSON /// </summary> /// <param name="map">The map to create the feature info from</param> /// <param name="requestedLayers">The layers to create the feature info for</param> /// <param name="x">The x-Ordinate</param> /// <param name="y">The y-Ordinate</param> /// <param name="featureCount">The number of features</param> /// <param name="cqlFilterString">The CQL Filter string</param> /// <param name="context">The <see cref="HttpContext"/> to use. If not specified or <value>null</value>, <see cref="HttpContext.Current"/> is used.</param> /// <exception cref="InvalidOperationException">Thrown if this function is used without a valid <see cref="HttpContext"/> at hand</exception> /// <returns>GeoJSON string with featureinfo results</returns> public static string CreateFeatureInfoGeoJSON(Map map, string[] requestedLayers, Single x, Single y, int featureCount, string cqlFilterString, HttpContext context = null) { if (context == null) context = HttpContext.Current; if (context == null) throw new InvalidOperationException("Cannot use CreateFeatureInfoGeoJSON without a valid HttpContext"); var items = new List<Converters.GeoJSON.GeoJSON>(); foreach (var requestLayer in requestedLayers) { var found = false; foreach (var mapLayer in map.Layers) { if (String.Equals(mapLayer.LayerName, requestLayer, StringComparison.InvariantCultureIgnoreCase)) { found = true; var queryLayer = mapLayer as ICanQueryLayer; if (queryLayer == null || !queryLayer.IsQueryEnabled) continue; var queryBoxMinX = x - (_pixelSensitivity); var queryBoxMinY = y - (_pixelSensitivity); var queryBoxMaxX = x + (_pixelSensitivity); var queryBoxMaxY = y + (_pixelSensitivity); var minXY = map.ImageToWorld(new PointF(queryBoxMinX, queryBoxMinY)); var maxXY = map.ImageToWorld(new PointF(queryBoxMaxX, queryBoxMaxY)); var queryBox = new Envelope(minXY, maxXY); var fds = new FeatureCollectionSet(); queryLayer.ExecuteIntersectionQuery(queryBox, fds); // if (_intersectDelegate != null) { var fc = fds[0]; fds.RemoveAt(0); fds.Add(_intersectDelegate(fc, queryBox)); } //filter the rows with the CQLFilter if one is provided if (cqlFilterString != null) { var toKeep = fds[0].Clone(); foreach (var f in fds[0].Clone()) { if (CqlFilter(f, cqlFilterString)) { toKeep.Add(f); } } fds[0] = toKeep; } var data = Converters.GeoJSON.GeoJSONHelper.GetData(fds); #if DotSpatialProjections throw new NotImplementedException(); #else // Reproject geometries if needed IMathTransform transform = null; if (queryLayer is VectorLayer) { ICoordinateTransformation transformation = (queryLayer as VectorLayer).CoordinateTransformation; transform = transformation == null ? null : transformation.MathTransform; } if (transform != null) { data = data.Select(d => { var converted = GeometryTransform.TransformGeometry(d.Geometry, transform, map.Factory); d.SetGeometry(converted); return d; }); } #endif items.AddRange(data); } } if (found == false) { WmsException.ThrowWmsException(WmsException.WmsExceptionCode.LayerNotDefined, "Unknown layer '" + requestLayer + "'", context); return string.Empty; } } var writer = new StringWriter(); Converters.GeoJSON.GeoJSONWriter.Write(items, writer); return writer.ToString(); }
/// <summary> /// Renders the layer /// </summary> /// <param name="g">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void Render(IGraphics g, Map map) { if (BaseLayer != null) { BaseLayer.Render(g, map); } var fds = new FeatureCollectionSet(); var box = map.Envelope; ExecuteIntersectionQuery(box, fds); if (fds.Count == 0 || fds[0].Count == 0) { base.Render(g, map); return; } var zoomIndex = GetZoomIndex(map.Zoom); var dot = _bitmaps[zoomIndex]; var opacity = _opacity[zoomIndex]; using (var image = new Bitmap(map.Size.Width + dot.Width, map.Size.Height + dot.Height, PixelFormat.Format32bppArgb)) { using (IGraphics gr = Graphics.FromImage(image).G()) gr.Clear(Color.White); DrawPoints(map, fds[0], dot, image); Colorize(image, HeatColorBlend, opacity); g.DrawImage(image, -dot.Width/2, -dot.Height/2); } }
/// <summary> /// Method to render this layer to the map, applying <paramref name="theme"/>. /// </summary> /// <param name="g">The graphics object</param> /// <param name="map">The map object</param> /// <param name="envelope">The envelope to render</param> /// <param name="theme">The theme to apply</param> protected void RenderInternal(Graphics g, Map map, Envelope envelope, ITheme theme) { IFeatureCollectionSet ds; lock (_dataSource) { ds = new FeatureCollectionSet(); DataSource.Open(); DataSource.ExecuteIntersectionQuery(envelope, ds); DataSource.Close(); } foreach (var features in ds) { if (CoordinateTransformation != null) for (int i = 0; i < features.Count; i++) #if !DotSpatialProjections features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation. MathTransform, GeometryServiceProvider.Instance.CreateGeometryFactory((int)CoordinateTransformation.TargetCS.AuthorityCode)); #else features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation.Source, CoordinateTransformation.Target, CoordinateTransformation.TargetFactory); #endif //Linestring outlines is drawn by drawing the layer once with a thicker line //before drawing the "inline" on top. if (Style.EnableOutline) { for (int i = 0; i < features.Count; i++) { var feature = features[i]; var outlineStyle = theme.GetStyle(feature) as VectorStyle; if (outlineStyle == null) continue; if (!(outlineStyle.Enabled && outlineStyle.EnableOutline)) continue; if (!(outlineStyle.MinVisible <= map.Zoom && map.Zoom <= outlineStyle.MaxVisible)) continue; using (outlineStyle = outlineStyle.Clone()) { if (outlineStyle != null) { //Draw background of all line-outlines first if (feature.Geometry is ILineString) { VectorRenderer.DrawLineString(g, feature.Geometry as ILineString, outlineStyle.Outline, map, outlineStyle.LineOffset); } else if (feature.Geometry is IMultiLineString) { VectorRenderer.DrawMultiLineString(g, feature.Geometry as IMultiLineString, outlineStyle.Outline, map, outlineStyle.LineOffset); } } } } } for (int i = 0; i < features.Count; i++) { var feature = features[i]; var style = theme.GetStyle(feature); if (style == null) continue; if (!style.Enabled) continue; if (!(style.MinVisible <= map.Zoom && map.Zoom <= style.MaxVisible)) continue; IStyle[] stylesToRender = GetStylesToRender(style); if (stylesToRender == null) return; foreach (var vstyle in stylesToRender) { if (!(vstyle is VectorStyle) || !vstyle.Enabled) continue; using (var clone = (vstyle as VectorStyle).Clone()) { if (clone != null) { RenderGeometry(g, map, feature.Geometry, clone); } } } } } }
public JsonResult GetData(float w, float n, float e, float s, int z) { string format = String.Format("~/App_Data/berlin/{0}", "osmbuildings.shp"); string path = this.HttpContext.Server.MapPath(format); if (!System.IO.File.Exists(path)) { throw new FileNotFoundException("file not found", path); } Point start = this.GeoToPixel(n, w, z); var meta = new { n, w, s, e, x = start.X, y = start.Y, z }; Envelope bbox = new Envelope(); bbox.ExpandToInclude(new Coordinate(n, w)); bbox.ExpandToInclude(new Coordinate(s, e)); var ds = new FeatureCollectionSet(); using (ShapeFile provider = new ShapeFile(path)) { provider.DoTrueIntersectionQuery = true; provider.Open(); provider.ExecuteIntersectionQuery(bbox, ds); provider.Close(); } int zz = MaxZoom - z; List <object> data = new List <object>(); var table = ds[0]; foreach (var row in table) { int c = (short)(row.Attributes["height"]); if (c == 0) { c = 5; // default value for "null" (zero) heights } int h = c * ScaleZ >> zz; if (h <= 1) { h = 1; } IGeometry geometry = row.Geometry; Coordinate[] coords = geometry.Coordinates; int total = coords.Length; double[] values = new double[total * 2]; int i = 0; foreach (Coordinate curr in coords) { Point p = this.GeoToPixel(curr.X, curr.Y, z); values[i++] = p.X - start.X; values[i++] = p.Y - start.Y; } data.Add(new object[] { h, values }); } return(this.Json(new { meta, data }, JsonRequestBehavior.AllowGet)); }
/// <summary> /// Renders the layer /// </summary> /// <param name="g">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void Render(Graphics g, Map map) { if (Style.Enabled && Style.MaxVisible >= map.Zoom && Style.MinVisible < map.Zoom) { if (DataSource == null) throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'")); g.TextRenderingHint = TextRenderingHint; g.SmoothingMode = SmoothingMode; var envelope = map.Envelope; //View to render var lineClipping = new CohenSutherlandLineClipping(envelope.MinX, envelope.MinY, envelope.MaxX, envelope.MaxY); if (CoordinateTransformation != null) { #if !DotSpatialProjections if (ReverseCoordinateTransformation != null) { envelope = GeometryTransform.TransformBox(envelope, ReverseCoordinateTransformation.MathTransform); } else { CoordinateTransformation.MathTransform.Invert(); envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform); CoordinateTransformation.MathTransform.Invert(); } #else envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source); #endif } var ds = new FeatureCollectionSet(); DataSource.Open(); DataSource.ExecuteIntersectionQuery(envelope, ds); DataSource.Close(); if (ds.Count == 0) { base.Render(g, map); return; } var features = ds[0]; //Initialize label collection List<BaseLabel> labels = new List<BaseLabel>(); //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection //Render labels foreach(var feature in features) { var featureGeometry = (IGeometry)feature.Geometry.Clone(); if (CoordinateTransformation != null) #if !DotSpatialProjections featureGeometry = GeometryTransform.TransformGeometry( featureGeometry, CoordinateTransformation.MathTransform, GeometryServiceProvider.Instance.CreateGeometryFactory((int)CoordinateTransformation.TargetCS.AuthorityCode) ); #else featuresGeometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation.Source, CoordinateTransformation.Target, CoordinateTransformation.TargetFactory); #endif LabelStyle style; if (Theme != null) //If thematics is enabled, lets override the style style = Theme.GetStyle(feature) as LabelStyle; else style = Style; float rotationStyle = style != null ? style.Rotation : 0f; float rotationColumn = 0f; if (!String.IsNullOrEmpty(RotationColumn)) Single.TryParse(feature.Attributes[RotationColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs, out rotationColumn); float rotation = rotationStyle + rotationColumn; int priority = Priority; if (_getPriorityMethod != null) priority = _getPriorityMethod(feature); else if (!String.IsNullOrEmpty(PriorityColumn)) Int32.TryParse(feature.Attributes[PriorityColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs, out priority); string text; if (_getLabelMethod != null) text = _getLabelMethod(feature); else text = feature.Attributes[LabelColumn].ToString(); if (!String.IsNullOrEmpty(text)) { // for lineal geometries, try clipping to ensure proper labeling if (featureGeometry is ILineal) { if (featureGeometry is ILineString) featureGeometry = lineClipping.ClipLineString(featureGeometry as ILineString); else if (featureGeometry is IMultiLineString) featureGeometry = lineClipping.ClipLineString(featureGeometry as IMultiLineString); } if (featureGeometry is IGeometryCollection) { if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.All) { foreach (var geom in (featureGeometry as IGeometryCollection)) { BaseLabel lbl = CreateLabel(feature, geom, text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) labels.Add(lbl); } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.CommonCenter) { BaseLabel lbl = CreateLabel(feature, featureGeometry, text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) labels.Add(lbl); } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.First) { if ((featureGeometry as IGeometryCollection).NumGeometries > 0) { BaseLabel lbl = CreateLabel(feature, (featureGeometry as IGeometryCollection).GetGeometryN(0), text, rotation, style, map, g); if (lbl != null) labels.Add(lbl); } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.Largest) { var coll = (featureGeometry as IGeometryCollection); if (coll.NumGeometries > 0) { var largestVal = 0d; var idxOfLargest = 0; for (var j = 0; j < coll.NumGeometries; j++) { var geom = coll.GetGeometryN(j); if (geom is ILineString && ((ILineString) geom).Length > largestVal) { largestVal = ((ILineString) geom).Length; idxOfLargest = j; } if (geom is IMultiLineString && ((IMultiLineString) geom).Length > largestVal) { largestVal = ((IMultiLineString)geom).Length; idxOfLargest = j; } if (geom is IPolygon && ((IPolygon) geom).Area > largestVal) { largestVal = ((IPolygon) geom).Area; idxOfLargest = j; } if (geom is IMultiPolygon && ((IMultiPolygon) geom).Area > largestVal) { largestVal = ((IMultiPolygon) geom).Area; idxOfLargest = j; } } BaseLabel lbl = CreateLabel(feature, coll.GetGeometryN(idxOfLargest), text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) labels.Add(lbl); } } } else { BaseLabel lbl = CreateLabel(feature, featureGeometry, text, rotation, priority, style, map, g, _getLocationMethod); if (lbl != null) labels.Add(lbl); } } } if (labels.Count > 0) //We have labels to render... { if (Style.CollisionDetection && _labelFilter != null) _labelFilter(labels); for (int i = 0; i < labels.Count; i++) { // Don't show the label if not necessary if (!labels[i].Show) { continue; } if (labels[i] is Label) { var label = labels[i] as Label; if (label.Style.IsTextOnPath == false || label.TextOnPathLabel==null) { VectorRenderer.DrawLabel(g, label.Location, label.Style.Offset, label.Style.Font, label.Style.ForeColor, label.Style.BackColor, label.Style.Halo, label.Rotation, label.Text, map, label.Style.HorizontalAlignment, label.LabelPoint); } else { if (label.Style.BackColor != null && label.Style.BackColor != System.Drawing.Brushes.Transparent) { //draw background if (label.TextOnPathLabel.RegionList.Count > 0) { g.FillRectangles(labels[i].Style.BackColor, labels[i].TextOnPathLabel.RegionList.ToArray()); //g.FillPolygon(labels[i].Style.BackColor, labels[i].TextOnPathLabel.PointsText.ToArray()); } } label.TextOnPathLabel.DrawTextOnPath(); } } else if (labels[i] is PathLabel) { var plbl = labels[i] as PathLabel; var lblStyle = plbl.Style; g.DrawString(lblStyle.Halo, new SolidBrush(lblStyle.ForeColor), plbl.Text, lblStyle.Font.FontFamily, (int) lblStyle.Font.Style, lblStyle.Font.Size, lblStyle.GetStringFormat(), lblStyle.IgnoreLength, plbl.Location); } } } } base.Render(g, map); }
/// <summary> /// Check if the layer can be queried and retrieve data, if there is any. /// </summary> protected bool TryGetData(Map map, float x, float y, int pixelSensitivity, WmsServer.InterSectDelegate intersectDelegate, ICanQueryLayer queryLayer, string cqlFilter, out IFeatureCollectionSet fds) { if (!queryLayer.IsQueryEnabled) { fds = null; return false; } float queryBoxMinX = x - pixelSensitivity; float queryBoxMinY = y - pixelSensitivity; float queryBoxMaxX = x + pixelSensitivity; float queryBoxMaxY = y + pixelSensitivity; Coordinate minXY = map.ImageToWorld(new PointF(queryBoxMinX, queryBoxMinY)); Coordinate maxXY = map.ImageToWorld(new PointF(queryBoxMaxX, queryBoxMaxY)); Envelope queryBox = new Envelope(minXY, maxXY); fds = new FeatureCollectionSet(); queryLayer.ExecuteIntersectionQuery(queryBox, fds); if (fds.Count == 0) return false; var table = fds[0]; if (intersectDelegate != null) { fds.Remove(table); fds.Add(intersectDelegate(table, queryBox)); table = fds[0]; } // filter the rows with the CQLFilter if one is provided if (cqlFilter != null) { var toKeep = table.Clone(); foreach (var feature in table) { if (CqlFilter(feature, cqlFilter)) toKeep.Add(feature); } fds.Remove(table); fds.Add(toKeep); } return fds[0].Count > 0; }