public void PolygonArea() { Coordinate[] coords = new Coordinate[20]; Random rnd = new Random(); Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); GeoAPI.Geometries.ICoordinate[] coordscheck = new GeoAPI.Geometries.ICoordinate[20]; for (int i = 0; i < 19; i++) { coords[i] = new Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 10), center.Y + (i * 10) * Math.PI / 10); double x = coords[i].X; double y = coords[i].Y; GisSharpBlog.NetTopologySuite.Geometries.Coordinate c = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(x, y); coordscheck[i] = c; } coordscheck[19] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(coordscheck[0].X, coordscheck[0].Y); coords[19] = new Coordinate(coords[0].X, coords[0].Y); GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory(); GeoAPI.Geometries.ILinearRing ring = gf.CreateLinearRing(coordscheck); GeoAPI.Geometries.IPolygon polygonCheck = gf.CreatePolygon(ring, null); Polygon pg = new Polygon(coords); double areaCheck = polygonCheck.Area; double area = pg.Area; Assert.AreEqual(area, areaCheck); }
public void Multipg() { Random rnd = new Random(); Polygon[] pg = new Polygon[50]; GeoAPI.Geometries.IPolygon[] pgcheck = new GeoAPI.Geometries.IPolygon[50]; GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory(); for (int i = 0; i < 50; i++) { Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); Coordinate[] coord = new Coordinate[36]; GeoAPI.Geometries.ICoordinate[] coordscheck = new GeoAPI.Geometries.ICoordinate[36]; for (int ii = 0; ii < 36; ii++) { coord[ii] = new Coordinate(center.X + Math.Cos((ii * 10) * Math.PI / 10), center.Y + (ii * 10) * Math.PI / 10); double x = coord[ii].X; double y = coord[ii].Y; GisSharpBlog.NetTopologySuite.Geometries.Coordinate c = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(x, y); coordscheck[ii] = c; } coord[35] = new Coordinate(coord[0].X, coord[0].Y); coordscheck[35] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(coordscheck[0].X, coordscheck[0].Y); GeoAPI.Geometries.ILinearRing ring = gf.CreateLinearRing(coordscheck); pgcheck[i] = gf.CreatePolygon(ring, null); pg[i] = new Polygon(coord); } MultiPolygon mpg = new MultiPolygon(pg); GeoAPI.Geometries.IMultiPolygon mpgcheck = gf.CreateMultiPolygon(pgcheck); for (int ii = 0; ii < mpg.Coordinates.Count; ii++) { Assert.AreEqual(mpg.Coordinates[ii].X, mpgcheck.Coordinates[ii].X); Assert.AreEqual(mpg.Coordinates[ii].Y, mpgcheck.Coordinates[ii].Y); } }
public void Overlaps() { var rnd = new Random(); var pg = new Polygon[50]; var pgcheck = new GeoAPI.Geometries.IPolygon[50]; var gf = new NetTopologySuite.Geometries.GeometryFactory(); var center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); for (var i = 0; i < 50; i++) { var coord = new Coordinate[36]; var coordscheck = new GeoAPI.Geometries.Coordinate[36]; for (var ii = 0; ii < 36; ii++) { coord[ii] = new Coordinate(center.X + Math.Cos((ii * 10) * Math.PI / 10), center.Y + (ii * 10) * Math.PI / 10); var x = coord[ii].X; var y = coord[ii].Y; var c = new GeoAPI.Geometries.Coordinate(x, y); coordscheck[ii] = c; } coord[35] = new Coordinate(coord[0].X, coord[0].Y); coordscheck[35] = new GeoAPI.Geometries.Coordinate(coordscheck[0].X, coordscheck[0].Y); GeoAPI.Geometries.ILinearRing ring = gf.CreateLinearRing(coordscheck); pgcheck[i] = gf.CreatePolygon(ring, null); pg[i] = new Polygon(coord); } for (var t = 0; t < 49; t++) { var g = pg[t].Overlaps(pg[t + 1]); var gcheck = pgcheck[t].Overlaps(pgcheck[t + 1]); Assert.AreEqual(g, gcheck); } }
public void Multipg() { var rnd = new Random(); var pg = new Polygon[50]; var pgcheck = new GeoAPI.Geometries.IPolygon[50]; var gf = new NetTopologySuite.Geometries.GeometryFactory(); for (var i = 0; i < 50; i++) { var center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); var coord = new Coordinate[36]; var coordscheck = new GeoAPI.Geometries.Coordinate[36]; for (var ii = 0; ii < 36; ii++) { coord[ii] = new Coordinate(center.X + Math.Cos((ii * 10) * Math.PI / 10), center.Y + (ii * 10) * Math.PI / 10); var x = coord[ii].X; var y = coord[ii].Y; var c = new GeoAPI.Geometries.Coordinate(x, y); coordscheck[ii] = c; } coord[35] = new Coordinate(coord[0].X, coord[0].Y); coordscheck[35] = new GeoAPI.Geometries.Coordinate(coordscheck[0].X, coordscheck[0].Y); var ring = gf.CreateLinearRing(coordscheck); pgcheck[i] = gf.CreatePolygon(ring, null); pg[i] = new Polygon(coord); } var mpg = new MultiPolygon(pg); var mpgcheck = gf.CreateMultiPolygon(pgcheck); for (var ii = 0; ii < mpg.Coordinates.Count; ii++) { Assert.AreEqual(mpg.Coordinates[ii].X, mpgcheck.Coordinates[ii].X); Assert.AreEqual(mpg.Coordinates[ii].Y, mpgcheck.Coordinates[ii].Y); } }
public void PolygonEnvelopeArea() { var coords = new Coordinate[20]; var rnd = new Random(); var center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); var coordscheck = new GeoAPI.Geometries.Coordinate[20]; for (var i = 0; i < 19; i++) { coords[i] = new Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 10), center.Y + (i * 10) * Math.PI / 10); var x = coords[i].X; var y = coords[i].Y; var c = new GeoAPI.Geometries.Coordinate(x, y); coordscheck[i] = c; } coordscheck[19] = new GeoAPI.Geometries.Coordinate(coords[0].X, coords[0].Y); coords[19] = new Coordinate(coords[0].X, coords[0].Y); var gf = new NetTopologySuite.Geometries.GeometryFactory(); GeoAPI.Geometries.ILinearRing ring = gf.CreateLinearRing(coordscheck); GeoAPI.Geometries.IPolygon pgcheck = gf.CreatePolygon(ring, null); var pg = new Polygon(coords); var area = pg.Envelope.Area(); AssertExt.AreEqual15(area, pgcheck.Envelope.Area); }
public void Overlaps() { Random rnd = new Random(); Polygon[] pg = new Polygon[50]; GeoAPI.Geometries.IPolygon[] pgcheck = new GeoAPI.Geometries.IPolygon[50]; GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory(); Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); for (int i = 0; i < 50; i++) { Coordinate[] coord = new Coordinate[36]; GeoAPI.Geometries.ICoordinate[] coordscheck = new GeoAPI.Geometries.ICoordinate[36]; for (int ii = 0; ii < 36; ii++) { coord[ii] = new Coordinate(center.X + Math.Cos((ii * 10) * Math.PI / 10), center.Y + (ii * 10) * Math.PI / 10); double x = coord[ii].X; double y = coord[ii].Y; GisSharpBlog.NetTopologySuite.Geometries.Coordinate c = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(x, y); coordscheck[ii] = c; } coord[35] = new Coordinate(coord[0].X, coord[0].Y); coordscheck[35] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(coordscheck[0].X, coordscheck[0].Y); GeoAPI.Geometries.ILinearRing ring = gf.CreateLinearRing(coordscheck); pgcheck[i] = gf.CreatePolygon(ring, null); pg[i] = new Polygon(coord); } for (int t = 0; t < 49; t++) { bool g = pg[t].Overlaps(pg[t + 1]); bool gcheck = pgcheck[t].Overlaps(pgcheck[t + 1]); Assert.AreEqual(g, gcheck); } }
public void PolygonEnvelopeMaxMin() { Coordinate[] coords = new Coordinate[20]; Random rnd = new Random(); Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); GeoAPI.Geometries.ICoordinate[] coordscheck = new GeoAPI.Geometries.ICoordinate[20]; for (int i = 0; i < 19; i++) { coords[i] = new Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 10), center.Y + (i * 10) * Math.PI / 10); double x = coords[i].X; double y = coords[i].Y; GisSharpBlog.NetTopologySuite.Geometries.Coordinate c = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(x, y); coordscheck[i] = c; } coordscheck[19] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(coords[0].X, coords[0].Y); coords[19] = new Coordinate(coords[0].X, coords[0].Y); GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory(); GeoAPI.Geometries.ILinearRing ring = gf.CreateLinearRing(coordscheck); GeoAPI.Geometries.IPolygon pgcheck = gf.CreatePolygon(ring, null); Polygon pg = new Polygon(coords); AssertExt.AreEqual15(pg.Envelope.Maximum.X, pgcheck.EnvelopeInternal.MaxX); AssertExt.AreEqual15(pg.Envelope.Maximum.Y, pgcheck.EnvelopeInternal.MaxY); AssertExt.AreEqual15(pg.Envelope.Minimum.X, pgcheck.EnvelopeInternal.MinX); AssertExt.AreEqual15(pg.Envelope.Minimum.Y, pgcheck.EnvelopeInternal.MinY); }
protected override void OnRenderInternal(SharpMap.Map map, GeoAPI.Geometries.IPolygon polygon, System.Drawing.Graphics g) { var pt = polygon.Centroid; g.RenderingOrigin = System.Drawing.Point.Truncate(SharpMap.Utilities.Transform.WorldtoMap(pt.Coordinate, map)); base.OnRenderInternal(map, polygon, g); }
protected override void OnRenderInternal(SharpMap.MapViewport map, GeoAPI.Geometries.IPolygon polygon, System.Drawing.Graphics g) { var pt = polygon.Centroid; g.RenderingOrigin = System.Drawing.Point.Truncate(map.WorldToImage(pt.Coordinate)); base.OnRenderInternal(map, polygon, g); }
public void PolygonHoles() { Coordinate[] coords = new Coordinate[20]; Random rnd = new Random(); Coordinate center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); // Shell Coordinates GeoAPI.Geometries.ICoordinate[] coordscheck = new GeoAPI.Geometries.ICoordinate[20]; for (int i = 0; i < 19; i++) { double x = center.X + Math.Cos((i * 10) * Math.PI / 10); double y = center.Y + (i * 10) * Math.PI / 10; coords[i] = new Coordinate(x, y); coordscheck[i] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(x, y); } coordscheck[19] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(coords[0].X, coords[0].Y); coords[19] = new Coordinate(coords[0].X, coords[0].Y); // Shell Rings LinearRing ring = new LinearRing(coords); GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory(); GeoAPI.Geometries.ILinearRing ringCheck = gf.CreateLinearRing(coordscheck); // Hole Coordinates GeoAPI.Geometries.ICoordinate[] coordsholecheck = new GeoAPI.Geometries.ICoordinate[20]; Coordinate[] coordshole = new Coordinate[20]; for (int i = 0; i < 20; i++) { double x = center.X + Math.Cos((i * 10) * Math.PI / 20); double y = center.Y + (i * 10) * Math.PI / 20; coordshole[i] = new Coordinate(x, y); coordsholecheck[i] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(x, y); } coordshole[19] = new Coordinate(coordshole[0].X, coordshole[0].Y); coordsholecheck[19] = new GisSharpBlog.NetTopologySuite.Geometries.Coordinate(coordshole[0].X, coordshole[0].Y); // Hole LinearRing Arrays LinearRing hole = new LinearRing(coordshole); ILinearRing[] holes = new ILinearRing[1]; GeoAPI.Geometries.ILinearRing holeCheck = gf.CreateLinearRing(coordsholecheck); GeoAPI.Geometries.ILinearRing[] holescheck = new GeoAPI.Geometries.ILinearRing[1]; holes[0] = hole; holescheck[0] = holeCheck; Polygon pg = new Polygon(ring, holes); GeoAPI.Geometries.IPolygon polygonCheck = gf.CreatePolygon(ringCheck, holescheck); double areaCheck = polygonCheck.Area; double area = pg.Area; Assert.AreEqual(area, areaCheck); }
private static GeoAPIMultiPolygon FromMultiPolygon(IMultiPolygon geometry, GeoAPIGeometryFactory factory, bool copyUserData) { var dsPolygons = new GeoAPI.Geometries.IPolygon[geometry.NumGeometries]; for (var i = 0; i < dsPolygons.Length; i++) { dsPolygons[i] = FromPolygon((IPolygon)geometry.GetGeometryN(i), factory, copyUserData); } var result = factory.CreateMultiPolygon(dsPolygons); if (copyUserData) { result.UserData = geometry.UserData; } return(result); }
public void Intersection() { var rnd = new Random(); var pg = new Polygon[50]; var pgcheck = new GeoAPI.Geometries.IPolygon[50]; var gf = new NetTopologySuite.Geometries.GeometryFactory(); var center = new Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90); for (var i = 0; i < 50; i++) { var coord = new Coordinate[36]; var coordscheck = new GeoAPI.Geometries.Coordinate[36]; for (var ii = 0; ii < 36; ii++) { coord[ii] = new Coordinate(center.X + Math.Cos((ii * 10) * Math.PI / 10), center.Y + (ii * 10) * Math.PI / 10); var x = coord[ii].X; var y = coord[ii].Y; var c = new GeoAPI.Geometries.Coordinate(x, y); coordscheck[ii] = c; } coord[35] = new Coordinate(coord[0].X, coord[0].Y); coordscheck[35] = new GeoAPI.Geometries.Coordinate(coordscheck[0].X, coordscheck[0].Y); GeoAPI.Geometries.ILinearRing ring = gf.CreateLinearRing(coordscheck); pgcheck[i] = gf.CreatePolygon(ring, null); pg[i] = new Polygon(coord); } for (var t = 0; t < 49; t++) { var g = pg[t].Intersection(pg[t + 1]); var gcheck = pgcheck[t].Intersection(pgcheck[t + 1]); for (var j = 0; j < g.Coordinates.Count; j++) { Assert.AreEqual(g.Coordinates[j].X, gcheck.Coordinates[j].X); Assert.AreEqual(g.Coordinates[j].Y, gcheck.Coordinates[j].Y); } } }
private void LoadParcels(List <PointD> pt, Color color, string labelname) { SharpMap.Map map = new SharpMap.Map(); List <GeoAPI.Geometries.Coordinate> vertices = new List <GeoAPI.Geometries.Coordinate>(); foreach (PointD i in pt) { GeoAPI.Geometries.Coordinate p = new GeoAPI.Geometries.Coordinate(); p.X = i.X; p.Y = i.Y; vertices.Add(p); } GeoAPI.Geometries.Coordinate l = new GeoAPI.Geometries.Coordinate(); l.X = pt[0].X; l.Y = pt[0].Y; vertices.Add(l); //Collection<GeoAPI.Geometries.IGeometry> geom = new Collection<GeoAPI.Geometries.IGeometry>(); GeometryFactory gf = new NetTopologySuite.Geometries.GeometryFactory(); GeoAPI.Geometries.ILinearRing shell = gf.CreateLinearRing(vertices.ToArray()); GeoAPI.Geometries.IPolygon polygon = gf.CreatePolygon(shell, null); SharpMap.Data.Providers.GeometryProvider geomProvider = new SharpMap.Data.Providers.GeometryProvider(polygon); SharpMap.Layers.VectorLayer layerParcels = new SharpMap.Layers.VectorLayer("Parcels"); SharpMap.Styles.VectorStyle style = new SharpMap.Styles.VectorStyle(); style.Fill = new SolidBrush(Color.FromArgb(200, color)); style.Outline = new Pen(new SolidBrush(color)); layerParcels.Style = style; layerParcels.Style.EnableOutline = true; layerParcels.DataSource = geomProvider; layerParcels.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; mapBox1.ActiveTool = SharpMap.Forms.MapBox.Tools.Pan; var fdt = new SharpMap.Data.FeatureDataTable(); fdt.Columns.Add(new System.Data.DataColumn("id", typeof(uint))); fdt.Columns.Add(new System.Data.DataColumn("label", typeof(string))); var fdr = (SharpMap.Data.FeatureDataRow)fdt.NewRow(); fdr.ItemArray = new object[] { 1, labelname }; fdr.Geometry = polygon; fdt.AddRow(fdr); var dataprovider = new SharpMap.Data.Providers.GeometryFeatureProvider(fdt); var ll = new SharpMap.Layers.LabelLayer("llayer"); ll.DataSource = dataprovider; ll.LabelColumn = "label"; ll.Style.Font = new Font("Eurostile Extended", 16, FontStyle.Bold); mapBox1.Map.Layers.Add(layerParcels); mapBox1.Map.Layers.Add(ll); mapBox1.Map.ZoomToExtents(); mapBox1.Refresh(); }
private static GeoAPIMultiPolygon FromMultiPolygon(IMultiPolygon geometry, GeoAPIGeometryFactory factory, bool copyUserData) { var dsPolygons = new GeoAPI.Geometries.IPolygon[geometry.NumGeometries]; for (var i = 0; i < dsPolygons.Length; i++) dsPolygons[i] = FromPolygon((IPolygon)geometry.GetGeometryN(i), factory, copyUserData); var result = factory.CreateMultiPolygon(dsPolygons); if (copyUserData) result.UserData = geometry.UserData; return result; }
public static void Test() { string s1 = "POLYGON((7.5999034 47.5506347,7.5997595 47.5507183,7.5998959 47.5508256,7.5999759 47.5508885,7.6001195 47.550805,7.5999034 47.5506347))"; string s2 = "POLYGON((7.6003356 47.5509754,7.6001195 47.550805,7.5999759 47.5508885,7.6000322 47.5509328,7.6001926 47.551059,7.6003356 47.5509754))"; s1 = "POLYGON((7.5999034 47.5506347,7.6001195 47.550805,7.5999759 47.5508885,7.5998959 47.5508256,7.5997595 47.5507183,7.5999034 47.5506347))"; s2 = "POLYGON((7.6003356 47.5509754,7.6001926 47.551059,7.6000322 47.5509328,7.5999759 47.5508885,7.6001195 47.550805,7.6003356 47.5509754))"; // NetTopologySuite.Geometries.Implementation.CoordinateArraySequenceFactory // GeoAPI.Geometries.IGeometryFactory geoFactory = new NetTopologySuite.Geometries.GeometryFactory(); NetTopologySuite.IO.WKTReader wr = new NetTopologySuite.IO.WKTReader(); Wgs84Coordinates[] coords1 = PolygonParsingExtensions.PolygonStringToCoordinates(s1); Wgs84Coordinates[] coords2 = PolygonParsingExtensions.PolygonStringToCoordinates(s2); var lr = new NetTopologySuite.Geometries.LinearRing(coords1.ToNetTopologyCoordinates()); System.Console.WriteLine(lr.IsValid); var x = new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(coords1.ToNetTopologyCoordinates())); System.Console.WriteLine(x.IsValid); NetTopologySuite.Geometries.GeometryFactory geomFactory = new NetTopologySuite.Geometries.GeometryFactory(); GeoAPI.Geometries.IPolygon poly1 = geomFactory.CreatePolygon(coords1.ToNetTopologyCoordinates()); GeoAPI.Geometries.IPolygon poly2 = geomFactory.CreatePolygon(coords2.ToNetTopologyCoordinates()); /* * GeoAPI.Geometries.IPolygon poly1 = (GeoAPI.Geometries.IPolygon)wr.Read(s1); * GeoAPI.Geometries.IPolygon poly2 = (GeoAPI.Geometries.IPolygon)wr.Read(s2); */ poly1.SRID = 4326; poly2.SRID = 4326; CalculateArea2(coords1); CalculateArea2(coords2); System.Console.WriteLine(poly1.Area); System.Console.WriteLine(poly2.Area); GeoAPI.Geometries.IPolygon poly3quick = (GeoAPI.Geometries.IPolygon)poly1.Union(poly2); System.Console.WriteLine(poly3quick.IsValid); // https://gis.stackexchange.com/questions/209797/how-to-get-geometry-points-using-geo-api System.Console.Write(poly1.IsValid); System.Console.Write(poly2.IsValid); System.Collections.Generic.List <GeoAPI.Geometries.IGeometry> lsPolygons = new System.Collections.Generic.List <GeoAPI.Geometries.IGeometry>(); lsPolygons.Add(poly1); lsPolygons.Add(poly2); GeoAPI.Geometries.IGeometry ig = NetTopologySuite.Operation.Union.CascadedPolygonUnion.Union(lsPolygons); System.Console.WriteLine(ig.GetType().FullName); GeoAPI.Geometries.IPolygon poly3 = (GeoAPI.Geometries.IPolygon)ig; System.Console.WriteLine(poly3); // POLYGON ((7.5997595 47.5507183, 7.5999034 47.5506347, 7.6001195 47.550805, 7.6003356 47.5509754 // , 7.6001926 47.551059, 7.6000322 47.5509328, 7.5999759 47.5508885 // , 7.5998959 47.5508256, 7.5997595 47.5507183)) System.Console.WriteLine(poly3.Shell.Coordinates); /* * // GeoAPI.Geometries.IPolygon poly3 = (GeoAPI.Geometries.IPolygon)ig; * NetTopologySuite.Geometries.MultiPolygon poly3a = (NetTopologySuite.Geometries.MultiPolygon)ig; * GeoAPI.Geometries.IGeometry ig2 = poly3a.ConvexHull(); * System.Console.WriteLine(ig2.GetType().FullName); */ // GeoAPI.Geometries.IPolygon poly4 = (GeoAPI.Geometries.IPolygon)ig2; // System.Console.WriteLine(poly4); System.Console.WriteLine("--- Press any key to continue --- "); System.Console.ReadKey(); } // End Sub Test
public static void TestUnion() { string s1 = "POLYGON((8.3038582 47.0506309,8.3038611 47.050588,8.3038772 47.0504833,8.3041581 47.0505083,8.3041532 47.0506229,8.303965 47.0506089,8.3039616 47.0506342,8.3038582 47.0506309))"; string s2 = "POLYGON((8.3041532 47.0506229,8.3041581 47.0505083,8.3042898 47.0505392,8.3042879 47.050571,8.3042854 47.0506139,8.3041532 47.0506229))"; string s3 = "POLYGON((8.3042879 47.050571,8.30442 47.05058,8.3044327 47.0507439,8.3043001 47.0507637,8.3042885 47.0506777,8.3042854 47.0506139,8.3042879 47.050571))"; string s4 = "POLYGON((8.304174 47.050784,8.3041695 47.0507507,8.3041592 47.0506835,8.3041585 47.0506448,8.3042166 47.0506438,8.3042225 47.0506777,8.3042885 47.0506777,8.3043001 47.0507637,8.304174 47.050784))"; Wgs84Coordinates[] coords1 = PolygonParsingExtensions.PolygonStringToCoordinates(s1); Wgs84Coordinates[] coords2 = PolygonParsingExtensions.PolygonStringToCoordinates(s2); Wgs84Coordinates[] coords3 = PolygonParsingExtensions.PolygonStringToCoordinates(s3); Wgs84Coordinates[] coords4 = PolygonParsingExtensions.PolygonStringToCoordinates(s4); NetTopologySuite.Geometries.GeometryFactory geomFactory = new NetTopologySuite.Geometries.GeometryFactory(); GeoAPI.Geometries.IPolygon poly1 = geomFactory.CreatePolygon(coords1.ToNetTopologyCoordinates()); GeoAPI.Geometries.IPolygon poly2 = geomFactory.CreatePolygon(coords2.ToNetTopologyCoordinates()); GeoAPI.Geometries.IPolygon poly3 = geomFactory.CreatePolygon(coords3.ToNetTopologyCoordinates()); GeoAPI.Geometries.IPolygon poly4 = geomFactory.CreatePolygon(coords4.ToNetTopologyCoordinates()); System.Collections.Generic.List <GeoAPI.Geometries.IGeometry> lsPolygons = new System.Collections.Generic.List <GeoAPI.Geometries.IGeometry>(); lsPolygons.Add(poly1); lsPolygons.Add(poly2); lsPolygons.Add(poly3); lsPolygons.Add(poly4); GeoAPI.Geometries.IGeometry ig = NetTopologySuite.Operation.Union.CascadedPolygonUnion.Union(lsPolygons); System.Console.WriteLine(ig.GetType().FullName); GeoAPI.Geometries.IPolygon unionPolygon = (GeoAPI.Geometries.IPolygon)ig; System.Console.WriteLine(poly3); System.Console.WriteLine(unionPolygon.Shell.Coordinates); System.Collections.Generic.List <Wgs84Coordinates> coords = new System.Collections.Generic.List <Wgs84Coordinates>(); for (int i = 0; i < unionPolygon.Shell.Coordinates.Length; ++i) { coords.Add(new Wgs84Coordinates(unionPolygon.Shell.Coordinates[i].X, unionPolygon.Shell.Coordinates[i].Y)); } coords = PolygonParsingExtensions.ToCounterClockWise(coords); // Close polygon if unclosed if (coords[0].Latitude != coords[coords.Count - 1].Latitude || coords[0].Longitude != coords[coords.Count - 1].Longitude) { coords.Add(coords[0]); } string insertString = @" DECLARE @GB_UID AS uniqueidentifier; DECLARE @SO_UID AS uniqueidentifier; SET @GB_UID = NULLIF('abc', ''); SET @SO_UID = NULLIF('', ''); DELETE FROM T_ZO_Objekt_Wgs84Polygon WHERE ZO_OBJ_WGS84_GB_UID = @GB_UID; /* INSERT INTO T_ZO_Objekt_Wgs84Polygon ( ZO_OBJ_WGS84_UID ,ZO_OBJ_WGS84_GB_UID ,ZO_OBJ_WGS84_SO_UID ,ZO_OBJ_WGS84_Sort ,ZO_OBJ_WGS84_GM_Lat ,ZO_OBJ_WGS84_GM_Lng ) */"; for (int i = 0; i < coords.Count; ++i) { if (i != 0) { insertString += " \r\n\r\n\r\nUNION ALL \r\n\r\n"; } insertString += $@" SELECT NEWID() AS ZO_OBJ_WGS84_UID ,CAST(@GB_UID AS uniqueidentifier) AS ZO_OBJ_WGS84_GB_UID ,CAST(@SO_UID AS uniqueidentifier) AS ZO_OBJ_WGS84_SO_UID ,CAST({i.ToString(System.Globalization.CultureInfo.InvariantCulture)} AS integer) + 1 AS ZO_OBJ_WGS84_Sort ,{coords[i].Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture)} AS ZO_OBJ_WGS84_GM_Lat -- decimal(23, 20) ,{coords[i].Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture)} AS ZO_OBJ_WGS84_GM_Lng -- decimal(23, 20) "; } insertString += " \r\n; \r\n\r\n"; System.Console.WriteLine(insertString); System.Console.WriteLine("--- Press any key to continue --- "); System.Console.ReadKey(); }