/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="box">Geometry to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> public void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds) { if (CoordinateTransformation != null) { #if !DotSpatialProjections if (ReverseCoordinateTransformation != null) { box = GeometryTransform.TransformBox(box, ReverseCoordinateTransformation.MathTransform); } else { CoordinateTransformation.MathTransform.Invert(); box = GeometryTransform.TransformBox(box, CoordinateTransformation.MathTransform); CoordinateTransformation.MathTransform.Invert(); } #else box = GeometryTransform.TransformBox(box, CoordinateTransformation.Target, CoordinateTransformation.Source); #endif } lock (_dataSource) { _dataSource.Open(); int tableCount = ds.Tables.Count; _dataSource.ExecuteIntersectionQuery(box, ds); if (ds.Tables.Count > tableCount) { //We added a table, name it according to layer ds.Tables[ds.Tables.Count - 1].TableName = LayerName; } _dataSource.Close(); } }
public static BoundingBox GetMinimalExtent(string connectionString, long?countryCode, long?regionCode, long?districtCode, long?settlementCode) { using (var connection = new SqlConnection(connectionString)) { connection.Open(); Geometry geom = null; if (districtCode != null) { geom = GetGeomById(connection, "gisWKBRayon", districtCode.Value); } else if (regionCode != null) { geom = GetGeomById(connection, "gisWKBRegion", regionCode.Value); } else if (countryCode != null) { geom = GetGeomById(connection, "gisWKBCountry", countryCode.Value); } //else if (settlementCode != null) // geom = GetGeomById(connection, "gisWKBSettlement", settlementCode.Value); if (geom != null) { //Geoms in DB in spherical mercator. Transform to wgs var bbox = geom.GetBoundingBox(); //if (bbox.Width < 1) bbox = bbox.Grow(10); bbox = GeometryTransform.TransformBox(bbox, CoordinateSystems.SphericalMercatorCS, CoordinateSystems.WGS84); return(bbox); } connection.Close(); } return(null); }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// </summary> /// <param name="box">Geometry to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> public void ExecuteIntersectionQuery(BoundingBox box, FeatureDataSet ds) { if (CoordinateTransformation != null) { #if !DotSpatialProjections if (ReverseCoordinateTransformation != null) { box = GeometryTransform.TransformBox(box, ReverseCoordinateTransformation.MathTransform); } else { CoordinateTransformation.MathTransform.Invert(); box = GeometryTransform.TransformBox(box, CoordinateTransformation.MathTransform); CoordinateTransformation.MathTransform.Invert(); } #else box = GeometryTransform.TransformBox(box, CoordinateTransformation.Target, CoordinateTransformation.Source); #endif } lock (_dataSource) { _dataSource.Open(); _dataSource.ExecuteIntersectionQuery(box, ds); _dataSource.Close(); } }
/// <summary> /// Renders the layer to a graphics object /// </summary> /// <param name="g">Graphics object reference</param> /// <param name="map">Map which is rendered</param> public override void OnRender(Graphics g, Map map) // TODO: remove map as parameter { if (map.Center == null) { throw (new ApplicationException("Cannot render map. View center not specified")); } if (g == null) { return; } g.SmoothingMode = SmoothingMode; //View to render IEnvelope envelope = map.Envelope; if (CoordinateTransformation != null) { envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform.Inverse()); } if (DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + Name + "'")); } RenderFeatures(g, envelope, map); }
public static Envelope ToSource(this Layer self, Envelope envelope) { #if !DotSpatialProjections if (self.ReverseCoordinateTransformation != null) { return(GeometryTransform.TransformBox(envelope, self.ReverseCoordinateTransformation.MathTransform)); } #endif if (self.CoordinateTransformation != null) { #if !DotSpatialProjections var mt = self.CoordinateTransformation.MathTransform; mt.Invert(); var res = GeometryTransform.TransformBox(envelope, mt); mt.Invert(); return(res); #else return(GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source)); #endif } // no transformation return(envelope); }
protected Envelope ToSource(Envelope envelope) { if (ReverseCoordinateTransformation == null) { if (CoordinateTransformation == null) { return(envelope); } var mt = CoordinateTransformation.MathTransform; #if !DotSpatialProjections mt.Invert(); var res = GeometryTransform.TransformBox(envelope, mt); mt.Invert(); #else return(GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source)); #endif return(res); } #if !DotSpatialProjections return(GeometryTransform.TransformBox(envelope, ReverseCoordinateTransformation.MathTransform)); #else return(GeometryTransform.TransformBox(box, ReverseCoordinateTransformation.Source, ReverseCoordinateTransformation.Target)); #endif }
private void Form2_Load(object sender, EventArgs e) { var tileLayer = new TileAsyncLayer(KnownTileSources.Create(KnownTileSource.BingRoadsStaging), "TileLayer"); this.mapBox1.Map.BackgroundLayer.Add(tileLayer); GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 3857); IMathTransform mathTransform = LayerTools.Wgs84toGoogleMercator.MathTransform; Envelope geom = GeometryTransform.TransformBox( new Envelope(-9.205626, -9.123736, 38.690993, 38.740837), mathTransform); //Adds a pushpin layer VectorLayer pushPinLayer = new VectorLayer("PushPins"); List <IGeometry> geos = new List <IGeometry>(); geos.Add(gf.CreatePoint(geom.Centre)); GeometryProvider geoProvider = new GeometryProvider(geos); pushPinLayer.DataSource = geoProvider; //this.mapBox1.Map.Layers.Add(pushPinLayer); this.mapBox1.Map.ZoomToBox(geom); this.mapBox1.Map.Zoom = 8500; this.mapBox1.Refresh(); }
/// <summary> /// Utility function to transform given envelope using a specific transformation /// </summary> /// <param name="envelope">The source envelope</param> /// <param name="coordinateTransformation">The <see cref="GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation"/> to use.</param> /// <returns>The target envelope</returns> protected virtual Envelope ToTarget(Envelope envelope, ICoordinateTransformation coordinateTransformation) { if (coordinateTransformation == null) { return(envelope); } return(GeometryTransform.TransformBox(envelope, coordinateTransformation.MathTransform)); }
public void TestMap2() { var m = new Map(_mapSize); m.BackgroundLayer.Add(new TileLayer(new OsmTileSource(new OsmRequest(KnownOsmTileServers.MapQuest)), "MapQuest")); var codeBase = Path.GetDirectoryName(GetType().Assembly.CodeBase); var cn = string.Format("Data Source={0};", Path.Combine(new Uri(codeBase).LocalPath, "TestData", "osm_aurich.sqlite")); var ct = Wgs84ToWebMercator; //Env[7,45731445821406 : 7,53454260528903, 53,4342695512313 : 53,478793942147] var box = new Envelope(7.45731445821406, 7.53454260528903, 53.4342695512313, 53.478793942147); var box3857 = GeometryTransform.TransformBox(box, ct.MathTransform); m.ZoomToBox(box3857); foreach (var msp in ManagedSpatiaLite.GetSpatialTables(cn)) { var l = new VectorLayer(msp.Table, msp); switch (msp.Table.Substring(0, 2).ToLower()) { case "pt": l.Style = VectorStyle.CreateRandomPuntalStyle(); break; case "ln": l.Style = VectorStyle.CreateRandomLinealStyle(); break; case "pg": l.Style = VectorStyle.CreateRandomPolygonalStyle(); break; default: l.Style = VectorStyle.CreateRandomStyle(); break; } l.CoordinateTransformation = ct; m.Layers.Add(l); } var f = GetFormatter(); BruTile.Utility.AddBruTileSurrogates(GetFormatter()); Map mD = null; Assert.DoesNotThrow(() => mD = SandD(m, f)); TestMaps("Test2", m, mD); }
/// <summary> /// Utility function to transform given envelope using a specific transformation /// </summary> /// <param name="envelope">The source envelope</param> /// <param name="coordinateTransformation">The <see cref="GeoAPI.CoordinateSystems.Transformations.ICoordinateTransformation"/> to use.</param> /// <returns>The target envelope</returns> protected virtual Envelope ToTarget(Envelope envelope, ICoordinateTransformation coordinateTransformation) { if (coordinateTransformation == null) { return(envelope); } #if !DotSpatialProjections return(GeometryTransform.TransformBox(envelope, coordinateTransformation.MathTransform)); #else return(GeometryTransform.TransformBox(envelope, coordinateTransformation.Source, coordinateTransformation.Target)); #endif }
/// <summary> /// Returns the data associated with all the geometries that are intersected by 'geom' /// /// Note! The table added should be named according to the LayerName! /// </summary> /// <param name="box">Bounding box to intersect with</param> /// <param name="ds">FeatureDataSet to fill data into</param> public void ExecuteIntersectionQuery(Envelope box, FeatureDataSet ds) { if (CoordinateTransformation != null) { box = GeometryTransform.TransformBox(box, CoordinateTransformation.MathTransform.Inverse()); } DataSource.ExecuteIntersectionQuery(box, ds); if (ds.Tables.Count > 0) { ds.Tables[0].TableName = LayerName; } }
/// <summary> /// Renders the layer to a graphics object /// </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 (map.Center == null) { throw (new ApplicationException("Cannot render map. View center not specified")); } g.SmoothingMode = SmoothingMode; BoundingBox envelope = map.Envelope; //View to render 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 } //List<SharpMap.Geometries.Geometry> features = this.DataSource.GetGeometriesInView(map.Envelope); if (DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'")); } //If thematics is enabled, we use a slighty different rendering approach if (Theme != null) { RenderInternal(g, map, envelope, Theme); } else { RenderInternal(g, map, envelope); } base.Render(g, map); }
public void TestMap2() { var m = new Map(_mapSize); m.BackgroundLayer.Add(new TileLayer(KnownTileSources.Create(KnownTileSource.BingHybridStaging), "BingHybridStaging")); string cn = $"Data Source={TestUtility.GetPathToTestFile("osm_aurich.sqlite")};"; var ct = Wgs84ToWebMercator; //Env[7,45731445821406 : 7,53454260528903, 53,4342695512313 : 53,478793942147] var box = new Envelope(7.45731445821406, 7.53454260528903, 53.4342695512313, 53.478793942147); var box3857 = GeometryTransform.TransformBox(box, ct.MathTransform); m.ZoomToBox(box3857); foreach (var msp in ManagedSpatiaLite.GetSpatialTables(cn)) { var l = new VectorLayer(msp.Table, msp); switch (msp.Table.Substring(0, 2).ToLower()) { case "pt": l.Style = VectorStyle.CreateRandomPuntalStyle(); break; case "ln": l.Style = VectorStyle.CreateRandomLinealStyle(); break; case "pg": l.Style = VectorStyle.CreateRandomPolygonalStyle(); break; default: l.Style = VectorStyle.CreateRandomStyle(); break; } l.CoordinateTransformation = ct; m.Layers.Add(l); } var f = GetFormatter(); //BruTile.Utility.AddBruTileSurrogates(GetFormatter()); Map mD = null; Assert.DoesNotThrow(() => mD = SandD(m, f)); TestMaps("Test2", m, mD); }
public static Envelope ToTarget(this Layer self, Envelope envelope) { if (self.CoordinateTransformation == null) { return(envelope); } #if !DotSpatialProjections return(GeometryTransform.TransformBox(envelope, self.CoordinateTransformation.MathTransform)); #else return(GeometryTransform.TransformBox(envelope, self.CoordinateTransformation.Source, self.coordinateTransformation.Target)); #endif }
public override void Render(Graphics g, Map map, RenderType rendertype) { if (map.Center == null) { throw (new ApplicationException("Cannot render map. View center not specified")); } g.SmoothingMode = SmoothingMode; BoundingBox envelope = map.Envelope; //View to render 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 } if (DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'")); } if (Theme != null) { RenderInternal(g, map, envelope, Theme); } else { RenderInternal(g, map, envelope, rendertype); } base.Render(g, map, rendertype); }
/// <summary> /// Method called to initialize the rendering process /// </summary> /// <param name="graphics">The graphics object to render upon</param> /// <param name="map">The map</param> protected virtual void OnRender(Graphics graphics, Map map) { // Get query envelope var envelope = map.Envelope; // Convert bounding box to datasource's coordinate system 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 } lock (_dataSource) { var wasOpen = _dataSource.IsOpen; if (!_dataSource.IsOpen) { _dataSource.Open(); } _geometries = DataSource.GetGeometriesInView(envelope); if (logger.IsDebugEnabled) { logger.DebugFormat("Layer {0}, NumGeometries {1}", LayerName, _geometries.Count); } if (!wasOpen) { _dataSource.Close(); } } //Setting up the Symbolizer Symbolizer.Begin(graphics, map, 0); }
/// <summary> /// Method called to initialize the rendering process /// </summary> /// <param name="graphics"></param> /// <param name="map"></param> protected virtual void OnRender(Graphics graphics, Map map) { // Get query envelope BoundingBox envelope = map.Envelope; // Convert bounding box to datasource's coordinate system 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 } lock (_dataSource) { bool wasOpen = _dataSource.IsOpen; if (!_dataSource.IsOpen) { _dataSource.Open(); } _geometries = DataSource.GetGeometriesInView(envelope); Console.Out.WriteLine(string.Format("Layer {0}, NumGeometries {1}", LayerName, _geometries.Count)); if (!wasOpen) { _dataSource.Close(); } } _oldSmoothingMode = graphics.SmoothingMode; graphics.SmoothingMode = SmoothingMode; //Setting up the Symbolizer Symbolizer.Begin(graphics, map, 0); }
private void Form2_Load(object sender, EventArgs e) { var tileLayer = new TileAsyncLayer(KnownTileSources.Create(KnownTileSource.BingRoadsStaging, userAgent: TileLayerSample.DefaultUserAgent), "TileLayer"); this.mapBox1.Map.BackgroundLayer.Add(tileLayer); GeometryFactory gf = new GeometryFactory(new PrecisionModel(), 3857); IMathTransform mathTransform = LayerTools.Wgs84toGoogleMercator.MathTransform; Envelope geom = GeometryTransform.TransformBox( new Envelope(-9.205626, -9.123736, 38.690993, 38.740837), mathTransform); //Adds a pushpin layer VectorLayer pushPinLayer = new VectorLayer("PushPins"); List <IGeometry> geos = new List <IGeometry>(); geos.Add(gf.CreatePoint(geom.Centre)); GeometryProvider geoProvider = new GeometryProvider(geos); pushPinLayer.DataSource = geoProvider; //this.mapBox1.Map.Layers.Add(pushPinLayer); // ScaleBar this.mapBox1.Map.SRID = 3857; this.mapBox1.Map.Decorations.Add( new ScaleBar { Anchor = MapDecorationAnchor.RightCenter, Enabled = chkScaleBar.Checked }); // Graticule this.mapBox1.Map.Decorations.Add(new Graticule() { Enabled = chkGraticule.Checked, PcsGraticuleMode = PcsGraticuleMode.WebMercatorScaleLines, PcsGraticuleStyle = { NumSubdivisions = 2 } }); this.mapBox1.Map.ZoomToBox(geom); this.mapBox1.Map.Zoom = 8500; this.mapBox1.Refresh(); }
/// <summary> /// Utility function to transform given envelope to the source envelope /// </summary> /// <param name="envelope">The target envelope</param> /// <returns>The source envelope</returns> protected virtual Envelope ToSource(Envelope envelope) { if (ReverseCoordinateTransformation != null) { return(GeometryTransform.TransformBox(envelope, ReverseCoordinateTransformation.MathTransform)); } if (CoordinateTransformation != null) { var mt = CoordinateTransformation.MathTransform; mt.Invert(); var res = GeometryTransform.TransformBox(envelope, mt); mt.Invert(); return(res); } // no transformation return(envelope); }
protected void ddlProjection_SelectedIndexChanged(object sender, EventArgs e) { //Transform current view to new coordinate system and zoom to the transformed box string PreviousProj = ViewState["currentProj"].ToString(); string SelectedProj = ddlProjection.SelectedValue; //Points defining the current view Point left = new Point(myMap.Envelope.MinX, myMap.Center.Y); Point right = new Point(myMap.Envelope.MaxX, myMap.Center.Y); Point center = myMap.Center; if (PreviousProj != "Pseudo") { //Transform current view back to geographic coordinates ICoordinateTransformation trans = GetTransform(PreviousProj); left = GeometryTransform.TransformCoordinate(new Point(myMap.Envelope.MinX, myMap.Center.Y), trans.MathTransform.Inverse()); right = GeometryTransform.TransformCoordinate(new Point(myMap.Envelope.MaxX, myMap.Center.Y), trans.MathTransform.Inverse()); center = GeometryTransform.TransformCoordinate(myMap.Center, trans.MathTransform.Inverse()); } //If both PreviousSRID and SelectedSRID are projected coordsys, first transform to geographic if (SelectedProj == "Pseudo") { myMap.Center = center; myMap.Zoom = Math.Abs(right.X - left.X); } else //Project coordinates to new projection { //Transform back to geographic and over to new projection ICoordinateTransformation trans = GetTransform(SelectedProj); left = GeometryTransform.TransformCoordinate(left, trans.MathTransform); right = GeometryTransform.TransformCoordinate(right, trans.MathTransform); center = GeometryTransform.TransformCoordinate(center, trans.MathTransform); myMap.Center = center; myMap.Zoom = Math.Abs(right.X - left.X); var envelopeGcs = GeometryTransform.TransformBox(myMap.Envelope, trans.MathTransform.Inverse()); litEnvelopeLatLong.Text = envelopeGcs.ToString(); } GenerateMap(); }
protected override void OnRenderInternal(VectorLayer vl, Graphics g, GdiRenderingArguments ra, IProgressHandler handler) { g.SmoothingMode = vl.SmoothingMode; var envelope = ra.Map.Envelope; //View to render if (vl.CoordinateTransformation != null) { #if !DotSpatialProjections if (vl.ReverseCoordinateTransformation != null) { envelope = GeometryTransform.TransformBox(envelope, vl.ReverseCoordinateTransformation.MathTransform); } else { vl.CoordinateTransformation.MathTransform.Invert(); envelope = GeometryTransform.TransformBox(envelope, vl.CoordinateTransformation.MathTransform); vl.CoordinateTransformation.MathTransform.Invert(); } #else envelope = GeometryTransform.TransformBox(envelope, vl.CoordinateTransformation.Target, vl.CoordinateTransformation.Source); #endif } if (vl.DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + vl.LayerName + "'")); } //If thematics is enabled, we use a slighty different rendering approach if (vl.Theme != null) { OnRenderInternalTheme(vl, g, ra.Map, envelope, vl.Theme); } else { OnRenderInternalGeometries(vl, g, ra.Map, envelope); } }
private Map InitMap() { var res = new SharpMap.Map() { SRID = 3857, BackColor = System.Drawing.Color.AliceBlue, }; //Lisbon... var mathTransform = LayerTools.Wgs84toGoogleMercator.MathTransform; var geom = GeometryTransform.TransformBox( new Envelope(-9.205626, -9.123736, 38.690993, 38.740837), mathTransform); //geom.ExpandBy(2500); res.ZoomToBox(geom); res.Decorations.Add(new NorthArrow { ForeColor = Color.DarkSlateBlue }); return(res); }
/// <summary> /// Utility function to transform given envelope to the source envelope /// </summary> /// <param name="envelope">The target envelope</param> /// <returns>The source envelope</returns> protected virtual Envelope ToSource(Envelope envelope) { #if !DotSpatialProjections if (ReverseCoordinateTransformation != null) { return(GeometryTransform.TransformBox(envelope, ReverseCoordinateTransformation.MathTransform)); } #endif if (CoordinateTransformation != null) { #if !DotSpatialProjections var mt = CoordinateTransformation.MathTransform; mt.Invert(); var res = GeometryTransform.TransformBox(envelope, mt); mt.Invert(); return(res); #else return(GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source)); #endif } // no transformation return(envelope); }
public void RenderLayer(IVectorLayer layer, Map map, System.Drawing.Graphics g) { if (map.Center == null) { throw (new ApplicationException("Cannot render map. View center not specified")); } g.SmoothingMode = layer.SmoothingMode; SharpMap.Geometries.BoundingBox envelope = map.Envelope; //View to render if (layer.CoordinateTransformation != null) { envelope = GeometryTransform.TransformBox( envelope, layer.CoordinateTransformation.MathTransform.Inverse()); } //List<SharpMap.Geometries.Geometry> features = this.DataSource.GetGeometriesInView(map.Envelope); if (layer.DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + layer.LayerName + "'")); } //If thematics is enabled, we use a slighty different rendering approach if (layer.Theme != null) { SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet(); layer.DataSource.Open(); layer.DataSource.ExecuteIntersectionQuery(envelope, ds); layer.DataSource.Close(); FeatureDataTable features = (FeatureDataTable)ds.Tables[0]; if (layer.CoordinateTransformation != null) { for (int i = 0; i < features.Count; i++) { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, layer.CoordinateTransformation.MathTransform); } } //Linestring outlines is drawn by drawing the layer once with a thicker line //before drawing the "inline" on top. if (layer.Style.EnableOutline) { //foreach (SharpMap.Geometries.Geometry feature in features) for (int i = 0; i < features.Count; i++) { SharpMap.Data.FeatureDataRow feature = features[i]; //Draw background of all line-outlines first if (feature.Geometry is SharpMap.Geometries.LineString) { IVectorStyle outlinestyle1 = layer.Theme.GetStyle(feature); if (outlinestyle1.Enabled && outlinestyle1.EnableOutline) { SharpMap.Rendering.VectorRenderer.DrawLineString(g, feature.Geometry as LineString, outlinestyle1.Outline, map); } } else if (feature.Geometry is SharpMap.Geometries.MultiLineString) { IVectorStyle outlinestyle2 = layer.Theme.GetStyle(feature); if (outlinestyle2.Enabled && outlinestyle2.EnableOutline) { SharpMap.Rendering.VectorRenderer.DrawMultiLineString(g, feature.Geometry as MultiLineString, outlinestyle2.Outline, map); } } } } for (int i = 0; i < features.Count; i++) { SharpMap.Data.FeatureDataRow feature = features[i]; IVectorStyle style = layer.Theme.GetStyle(feature); RenderGeometry(g, map, layer.ClippingEnabled, feature.Geometry, style); } } else { layer.DataSource.Open(); Collection <Geometry> geoms = layer.DataSource.GetGeometriesInView(envelope); layer.DataSource.Close(); if (layer.CoordinateTransformation != null) { for (int i = 0; i < geoms.Count; i++) { geoms[i] = GeometryTransform.TransformGeometry(geoms[i], layer.CoordinateTransformation.MathTransform); } } //Linestring outlines is drawn by drawing the layer once with a thicker line //before drawing the "inline" on top. if (layer.Style.EnableOutline) { foreach (SharpMap.Geometries.Geometry geom in geoms) { if (geom != null) { //Draw background of all line-outlines first switch (geom.GetType().FullName) { case "SharpMap.Geometries.LineString": SharpMap.Rendering.VectorRenderer.DrawLineString(g, geom as LineString, layer.Style.Outline, map); break; case "SharpMap.Geometries.MultiLineString": SharpMap.Rendering.VectorRenderer.DrawMultiLineString(g, geom as MultiLineString, layer.Style.Outline, map); break; default: break; } } } } for (int i = 0; i < geoms.Count; i++) { if (geoms[i] != null) { RenderGeometry(g, map, layer.ClippingEnabled, geoms[i], layer.Style); } } } //base.Render(g, map); }
/// <summary> /// Renders the layer to a graphics object /// </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 (map.Center == null) { throw (new ApplicationException("Cannot render map. View center not specified")); } g.SmoothingMode = SmoothingMode; BoundingBox envelope = map.Envelope; //View to render if (CoordinateTransformation != null) { #if !DotSpatialProjections CoordinateTransformation.MathTransform.Invert(); envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform); CoordinateTransformation.MathTransform.Invert(); #else envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source); #endif } //List<SharpMap.Geometries.Geometry> features = this.DataSource.GetGeometriesInView(map.Envelope); if (DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + LayerName + "'")); } //If thematics is enabled, we use a slighty different rendering approach if (Theme != null) { FeatureDataSet ds = new FeatureDataSet(); lock (_dataSource) { DataSource.Open(); DataSource.ExecuteIntersectionQuery(envelope, ds); DataSource.Close(); } foreach (FeatureDataTable features in ds.Tables) { if (CoordinateTransformation != null) { for (int i = 0; i < features.Count; i++) #if !DotSpatialProjections { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation. MathTransform); } } #else { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation.Source, CoordinateTransformation.Target); } #endif //Linestring outlines is drawn by drawing the layer once with a thicker line //before drawing the "inline" on top. if (Style.EnableOutline) { //foreach (SharpMap.Geometries.Geometry feature in features) for (int i = 0; i < features.Count; i++) { FeatureDataRow feature = features[i]; VectorStyle 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; } //Draw background of all line-outlines first if (feature.Geometry is LineString) { VectorRenderer.DrawLineString(g, feature.Geometry as LineString, outlineStyle.Outline, map); } else if (feature.Geometry is MultiLineString) { VectorRenderer.DrawMultiLineString(g, feature.Geometry as MultiLineString, outlineStyle.Outline, map); } } } for (int i = 0; i < features.Count; i++) { FeatureDataRow feature = features[i]; VectorStyle style = Theme.GetStyle(feature) as VectorStyle; if (style == null) { continue; } if (!style.Enabled) { continue; } if (!(style.MinVisible <= map.Zoom && map.Zoom <= style.MaxVisible)) { continue; } RenderGeometry(g, map, feature.Geometry, style); } } } else { //if style is not enabled, we don't need to render anything if (!Style.Enabled) { return; } Collection <Geometry> geoms = null; // Is datasource already open? lock (_dataSource) { bool alreadyOpen = DataSource.IsOpen; // If not open yet, open it if (!alreadyOpen) { DataSource.Open(); } // Read data geoms = DataSource.GetGeometriesInView(envelope); // If was not open, close it if (!alreadyOpen) { DataSource.Close(); } } if (CoordinateTransformation != null) { for (int i = 0; i < geoms.Count; i++) #if !DotSpatialProjections { geoms[i] = GeometryTransform.TransformGeometry(geoms[i], CoordinateTransformation.MathTransform); } } #else { geoms[i] = GeometryTransform.TransformGeometry(geoms[i], CoordinateTransformation.Source, CoordinateTransformation.Target); } #endif //Linestring outlines is drawn by drawing the layer once with a thicker line //before drawing the "inline" on top. if (Style.EnableOutline) { foreach (Geometry geom in geoms) { if (geom != null) { //Draw background of all line-outlines first if (geom is LineString) { VectorRenderer.DrawLineString(g, geom as LineString, Style.Outline, map); } else if (geom is MultiLineString) { VectorRenderer.DrawMultiLineString(g, geom as MultiLineString, Style.Outline, map); } } } } for (int i = 0; i < geoms.Count; i++) { if (geoms[i] != null) { RenderGeometry(g, map, geoms[i], Style); } } } base.Render(g, map); }
/// <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); }
protected override void OnShown(EventArgs e) { base.OnShown(e); using (new TemporaryWaitCursor()) { //create ok/cancel buttons var okButton = new BarButtonItem(mapControl.barManager, Resources.gis_SetCaseMapForm_Ok);// "OK"); okButton.ItemClick += okButton_Click; okButton.Border = BorderStyles.Default; okButton.Appearance.Font = new Font(okButton.Appearance.Font, FontStyle.Bold); var cancelButton = new BarButtonItem(mapControl.barManager, Resources.gis_SetCaseMapForm_Cancel); cancelButton.ItemClick += cancelButton_Click; cancelButton.Border = BorderStyles.Default; cancelButton.Appearance.Font = new Font(cancelButton.Appearance.Font, FontStyle.Bold); mapControl.barManager.Bars["StatusBar"].AddItem(okButton).BeginGroup = true; mapControl.barManager.Bars["StatusBar"].AddItem(cancelButton); //IT IS UGLY HACK! mapControl.RemoveUglyButton(); //set default tool mapControl.InputTool.IsActive = true; //init map mapControl.MapSpatRef = CoordinateSystems.SphericalMercatorCS; DateTime start = DateTime.Now; //mapControl.LoadMap(MapProjectsStorage.DefaultMapPath); //var defPath = BaseSettings.DefaultMapProject ?? MapProjectsStorage.DefaultMapPath; var defPath = string.IsNullOrEmpty(BaseSettings.DefaultMapProject) ? MapProjectsStorage.DefaultMapPath : BaseSettings.DefaultMapProject; mapControl.LoadMap(defPath); mapControl.MapSelector.UpdateValue(GisInterface.GetMapName(defPath)); Debug.WriteLine("Loaded:" + (DateTime.Now - start).TotalMilliseconds); Debug.WriteLine("Loaded:" + DateTime.Now); //set case point, if exists if (InitWgsPoint != null) { var point = InitWgsPoint.Clone(); if (mapControl.MapSpatRef != CoordinateSystems.WGS84) { point = GeometryTransform.TransformPoint(point, CoordinateSystems.WGS84, mapControl.MapSpatRef); } mapControl.InputTool.Point = point; } //zoom to box if (InitAdminBBox != null) { var adminBox = InitAdminBBox.Clone(); //reproject if need. NotNeed!!!! if (mapControl.MapSpatRef != CoordinateSystems.WGS84) { adminBox = GeometryTransform.TransformBox(adminBox, CoordinateSystems.WGS84, mapControl.MapSpatRef); } mapControl.Map.ZoomToBox(adminBox); } } }
public static Bitmap GetPrintMap (long countryId, long regionId, long rayonId, long settlementId, decimal x, decimal y) { var setCaseMapControl = new SetCaseMapControl(); setCaseMapControl.MapSpatRef = CoordinateSystems.SphericalMercatorCS; Point initWgsPoint; if (x != 0 && y != 0) { initWgsPoint = new Point((double)x, (double)y); } else { initWgsPoint = null; } if (initWgsPoint != null) { var point = initWgsPoint.Clone(); if (setCaseMapControl.MapSpatRef != CoordinateSystems.WGS84) { point = GeometryTransform.TransformPoint(point, CoordinateSystems.WGS84, setCaseMapControl.MapSpatRef); } setCaseMapControl.InputTool.Point = point; } else { double _x, _y; if (GetSettlementCoordinates(settlementId, out _x, out _y)) { initWgsPoint = new Point(_x, _y); var point = initWgsPoint.Clone(); if (setCaseMapControl.MapSpatRef != CoordinateSystems.WGS84) { point = GeometryTransform.TransformPoint(point, CoordinateSystems.WGS84, setCaseMapControl.MapSpatRef); } setCaseMapControl.InputTool.Point = point; } } var defPath = string.IsNullOrEmpty(BaseSettings.DefaultMapProject) ? MapProjectsStorage.DefaultMapPath : BaseSettings.DefaultMapProject; setCaseMapControl.LoadMap(defPath);//MapProjectsStorage.DefaultMapPath); // get min WGS-extent var extent = Extents.GetMinimalExtent(ConnectionManager.DefaultInstance.ConnectionString, countryId == 0 ? null : (long?)countryId, regionId == 0 ? null : (long?)regionId, rayonId == 0 ? null : (long?)rayonId, settlementId == 0 ? null : (long?)settlementId); var extentPrj = GeometryTransform.TransformBox(extent, CoordinateSystems.WGS84, CoordinateSystems.SphericalMercatorCS); var k = extentPrj.Width / extentPrj.Height; const double w = 180; var h = w / k; setCaseMapControl.Width = (int)(w * 10); setCaseMapControl.Height = (int)(h * 10); setCaseMapControl.ZoomToBox(extentPrj); setCaseMapControl.InputTool.TemporaryLayerName = "Vet. Case Location"; setCaseMapControl.InputTool.TemporaryLayerStyle.MarkerType = MarkerTypes.Cross; var result = setCaseMapControl.m_mapImage.GetMapImage(w, h, 300); //setCaseMapControl.Dispose(); return(result); }
/// <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; BoundingBox envelope = map.Envelope; //View to render var lineClipping = new CohenSutherlandLineClipping(envelope.Min.X, envelope.Min.Y, envelope.Max.X, envelope.Max.Y); if (CoordinateTransformation != null) { #if !DotSpatialProjections CoordinateTransformation.MathTransform.Invert(); envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.MathTransform); CoordinateTransformation.MathTransform.Invert(); #else envelope = GeometryTransform.TransformBox(envelope, CoordinateTransformation.Target, CoordinateTransformation.Source); #endif } FeatureDataSet ds = new FeatureDataSet(); DataSource.Open(); DataSource.ExecuteIntersectionQuery(envelope, ds); DataSource.Close(); if (ds.Tables.Count == 0) { base.Render(g, map); return; } FeatureDataTable features = ds.Tables[0]; //Initialize label collection List <BaseLabel> labels = new List <BaseLabel>(); //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection //Render labels for (int i = 0; i < features.Count; i++) { FeatureDataRow feature = features[i]; if (CoordinateTransformation != null) #if !DotSpatialProjections { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation. MathTransform); } #else { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, CoordinateTransformation.Source, CoordinateTransformation.Target); } #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[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[PriorityColumn].ToString(), NumberStyles.Any, Map.NumberFormatEnUs, out priority); } string text; if (_getLabelMethod != null) { text = _getLabelMethod(feature); } else { text = feature[LabelColumn].ToString(); } if (!String.IsNullOrEmpty(text)) { // for lineal geometries, try clipping to ensure proper labeling if (feature.Geometry is ILineal) { if (feature.Geometry is LineString) { feature.Geometry = lineClipping.ClipLineString(feature.Geometry as LineString); } else if (feature.Geometry is MultiLineString) { feature.Geometry = lineClipping.ClipLineString(feature.Geometry as MultiLineString); } } if (feature.Geometry is GeometryCollection) { if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.All) { foreach (Geometry geom in (feature.Geometry as GeometryCollection)) { BaseLabel lbl = CreateLabel(geom, text, rotation, priority, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.CommonCenter) { BaseLabel lbl = CreateLabel(feature.Geometry, text, rotation, priority, style, map, g); if (lbl != null) { labels.Add(lbl); } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.First) { if ((feature.Geometry as GeometryCollection).Collection.Count > 0) { BaseLabel lbl = CreateLabel((feature.Geometry as GeometryCollection).Collection[0], text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum.Largest) { GeometryCollection coll = (feature.Geometry as GeometryCollection); if (coll.NumGeometries > 0) { double largestVal = 0; int idxOfLargest = 0; for (int j = 0; j < coll.NumGeometries; j++) { Geometry geom = coll.Geometry(j); if (geom is LineString && ((LineString)geom).Length > largestVal) { largestVal = ((LineString)geom).Length; idxOfLargest = j; } if (geom is MultiLineString && ((MultiLineString)geom).Length > largestVal) { largestVal = ((MultiLineString)geom).Length; idxOfLargest = j; } if (geom is Polygon && ((Polygon)geom).Area > largestVal) { largestVal = ((Polygon)geom).Area; idxOfLargest = j; } if (geom is MultiPolygon && ((MultiPolygon)geom).Area > largestVal) { largestVal = ((MultiPolygon)geom).Area; idxOfLargest = j; } } BaseLabel lbl = CreateLabel(coll.Geometry(idxOfLargest), text, rotation, priority, style, map, g); if (lbl != null) { labels.Add(lbl); } } } } else { BaseLabel lbl = CreateLabel(feature.Geometry, text, rotation, priority, style, map, g); 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; VectorRenderer.DrawLabel(g, label.Location, label.Style.Offset, label.Style.Font, label.Style.ForeColor, label.Style.BackColor, Style.Halo, label.Rotation, label.Text, map); } 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); }
public void RenderLayer(ILabelLayer layer, Map map, Graphics g) { if (layer.Style.Enabled && layer.Style.MaxVisible >= map.Zoom && layer.Style.MinVisible < map.Zoom) { if (layer.DataSource == null) { throw (new ApplicationException("DataSource property not set on layer '" + layer.LayerName + "'")); } g.TextRenderingHint = layer.TextRenderingHint; g.SmoothingMode = layer.SmoothingMode; SharpMap.Geometries.BoundingBox envelope = map.Envelope; //View to render if (layer.CoordinateTransformation != null) { envelope = GeometryTransform.TransformBox(envelope, layer.CoordinateTransformation.MathTransform.Inverse()); } FeatureDataSet ds = new FeatureDataSet(); layer.DataSource.Open(); layer.DataSource.ExecuteIntersectionQuery(envelope, ds); layer.DataSource.Close(); if (ds.Tables.Count == 0) { //base.Render(g, map); return; } FeatureDataTable features = (FeatureDataTable)ds.Tables[0]; //Initialize label collection List <Label> labels = new List <Label>(); LabelLayer.GetLabelMethod lblDelegate = layer.LabelStringDelegate; //List<System.Drawing.Rectangle> LabelBoxes; //Used for collision detection //Render labels for (int i = 0; i < features.Count; i++) { FeatureDataRow feature = features[i]; if (layer.CoordinateTransformation != null) { features[i].Geometry = GeometryTransform.TransformGeometry(features[i].Geometry, layer.CoordinateTransformation.MathTransform); } ILabelStyle style = null; if (layer.Theme != null) //If thematics is enabled, lets override the style { style = layer.Theme.GetStyle(feature); } else { style = layer.Style; } float rotation = 0; if (!String.IsNullOrEmpty(layer.RotationColumn)) { float.TryParse(feature[layer.RotationColumn].ToString(), NumberStyles.Any, Map.numberFormat_EnUS, out rotation); } string text; if (lblDelegate != null) { text = lblDelegate(feature); } else { text = feature[layer.LabelColumn].ToString(); } if (text != null && text != String.Empty) { if (feature.Geometry is GeometryCollection) { if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.All) { foreach (SharpMap.Geometries.Geometry geom in (feature.Geometry as GeometryCollection)) { SharpMap.Rendering.Label lbl = CreateLabel(layer, geom, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.CommonCenter) { Label lbl = CreateLabel(layer, feature.Geometry, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } else if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.First) { if ((feature.Geometry as GeometryCollection).Collection.Count > 0) { SharpMap.Rendering.Label lbl = CreateLabel(layer, (feature.Geometry as GeometryCollection).Collection[0], text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } else if (layer.MultipartGeometryBehaviour == SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest) { GeometryCollection coll = (feature.Geometry as GeometryCollection); if (coll.NumGeometries > 0) { double largestVal = 0; int idxOfLargest = 0; for (int j = 0; j < coll.NumGeometries; j++) { SharpMap.Geometries.Geometry geom = coll.Geometry(j); if (geom is Geometries.LineString && ((Geometries.LineString)geom).Length > largestVal) { largestVal = ((Geometries.LineString)geom).Length; idxOfLargest = j; } if (geom is Geometries.MultiLineString && ((Geometries.MultiLineString)geom).Length > largestVal) { largestVal = ((Geometries.LineString)geom).Length; idxOfLargest = j; } if (geom is Geometries.Polygon && ((Geometries.Polygon)geom).Area > largestVal) { largestVal = ((Geometries.Polygon)geom).Area; idxOfLargest = j; } if (geom is Geometries.MultiPolygon && ((Geometries.MultiPolygon)geom).Area > largestVal) { largestVal = ((Geometries.MultiPolygon)geom).Area; idxOfLargest = j; } } SharpMap.Rendering.Label lbl = CreateLabel(layer, coll.Geometry(idxOfLargest), text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } } else { SharpMap.Rendering.Label lbl = CreateLabel(layer, feature.Geometry, text, rotation, style, map, g); if (lbl != null) { labels.Add(lbl); } } } } if (labels.Count > 0) //We have labels to render... { if (layer.Style.CollisionDetection && layer.LabelFilter != null) { layer.LabelFilter(labels); } for (int i = 0; i < labels.Count; i++) { VectorRenderer.DrawLabel(g, labels[i].LabelPoint, labels[i].Style.Offset, labels[i].Style.Font, labels[i].Style.ForeColor, labels[i].Style.BackColor, layer.Style.Halo, labels[i].Rotation, labels[i].Text, map); } } labels = null; } //base.Render(g, map); }