internal virtual void Run() { var rdr = new WKTReader(); IList <Geometry> lines = new List <Geometry> { rdr.Read("LINESTRING (0 0 , 10 10)"), rdr.Read("LINESTRING (185 221, 100 100)"), rdr.Read("LINESTRING (185 221, 88 275, 180 316)"), rdr.Read("LINESTRING (185 221, 292 281, 180 316)"), rdr.Read("LINESTRING (189 98, 83 187, 185 221)"), rdr.Read("LINESTRING (189 98, 325 168, 185 221)") }; var polygonizer = new Polygonizer(); polygonizer.Add(lines); var polys = polygonizer.GetPolygons(); Console.WriteLine("Polygons formed (" + polys.Count + "):"); foreach (var obj in polys) { Console.WriteLine(obj); } }
/* * [TestAttribute] * public void Test2() { * doTest(new String[]{ * * "LINESTRING(20 20, 20 100)", * "LINESTRING (20 100, 20 180, 100 180)", * "LINESTRING (100 180, 180 180, 180 100)", * "LINESTRING (180 100, 180 20, 100 20)", * "LINESTRING (100 20, 20 20)", * "LINESTRING (100 20, 20 100)", * "LINESTRING (20 100, 100 180)", * "LINESTRING (100 180, 180 100)", * "LINESTRING (180 100, 100 20)" * }, * new String[]{}); * } */ private void DoTest(String[] inputWKT, String[] expectedOutputWKT) { var polygonizer = new Polygonizer(); polygonizer.Add(ToGeometries(inputWKT)); Compare(ToGeometries(expectedOutputWKT), polygonizer.GetPolygons()); }
public void IsPointInRingTest() { const string wkt1 = @"LINESTRING(159084.138183715 215384.465334836,159085.255551952 215382.299118879,159084.287081382 215380.125720536,159081.909029527 215380.063184668,159080.685184241 215382.030015885,159081.870080819 215384.260803017,159084.138183715 215384.465334836)"; const string wkt2 = @"LINESTRING(159081.417 215432.901,159081.484 215412.286,159069.343 215411.729,159063.929 215411.79,159063.765 215441.011,159055.84 215440.909,159055.756 215439.794,159050.254 215439.759,159050.231 215426.571,159029.91 215426.438,159029.894 215420.862,159028.749 215420.841,159028.787 215412.904,159050.376 215412.995,159050.394 215404.475,159051.839 215404.512,159051.721 215397.907,159050.448 215397.876,159050.48 215385.756,159037.29 215385.669,159037.274 215380.139,159036.129 215380.118,159036.18 215372.161,159050.58 215372.256,159050.641 215357.846,159061.806 215357.884,159061.764 215355.578,159063.703 215355.583,159063.834 215344.331,159062.797 215344.264,159062.878 215338.481,159063.885 215338.48,159063.94 215333.569,159062.002 215333.52,159062.061 215329.565,159063.973 215329.565,159064.019 215324.529,159063.008 215324.569,159062.948 215318.85,159064.078 215318.847,159064.229 215304.453,159074.999 215304.543,159074.895 215315.988,159082.789 215316.117,159082.751291265 215325.067329746,159076.257853729 215325.010585012,159076.145672787 215330.246673065,159077.726943351 215330.292687136,159077.64265916 215336.096838446,159075.46670601 215336.028874838,159075.17015073 215349.460814847,159091.770139291 215349.583804507,159091.835913025 215356.268745225,159114.649 215356.642,159114.529 215396.632,159109.671 215396.625,159109.501 215398.902,159112.021 215398.92,159111.982 215404.407,159112.999 215404.421,159113.001 215412.415,159095.019 215412.366,159094.928 215434.091,159086.987 215433.984,159086.928 215432.972,159081.417 215432.901)"; const string wkt3 = @"LINESTRING(159063.929280482 215399.247659686,159103.333615111 215398.947801304,159103.342074891 215397.380179598,159101.054815857 215397.403687265,159101.283398228 215370.145108237,159064.458615271 215370.009119945,159063.929280482 215399.247659686)"; var reader = new WKTReader(GeometryFactory.Fixed); string[] wkts = new[] { wkt1, wkt2, wkt3, }; var polygonizer = new Polygonizer(); foreach (string wkt in wkts) { var geom = (ILineString)reader.Read(wkt); Assert.IsNotNull(geom); Assert.IsTrue(geom.IsValid); Assert.AreEqual(geom.Factory.PrecisionModel, GeometryFactory.Fixed.PrecisionModel); polygonizer.Add(geom); } var polys = polygonizer.GetPolygons(); Console.WriteLine("Polygons formed (" + polys.Count + "):"); foreach (var obj in polys) { Assert.IsNotNull(obj); Console.WriteLine(obj); } }
public static ICollection <IGeometry> Polygonize(IGeometry geometry) { var polygonizer = new Polygonizer(false); polygonizer.Add(geometry); return(polygonizer.GetPolygons()); }
public void RunDisjointCircles() { const bool extractOnlyPolygonal = false; var polygonizer = new Polygonizer(extractOnlyPolygonal); polygonizer.Add(_testCircles); var output = polygonizer.GetPolygons(); }
private static Geometry polygonize(Geometry g, bool extractOnlyPolygonal) { var lines = LinearComponentExtracter.GetLines(g); var polygonizer = new Polygonizer(extractOnlyPolygonal); polygonizer.Add(lines); return(polygonizer.GetGeometry()); }
/// <summary> /// Applies the Filter to the specified geometry /// </summary> /// <param name="g"></param> public virtual void Filter(IGeometry g) { ILineString l = g as ILineString; if (l != null) { _container.Add(l); } }
public static Geometry PolygonizeCutEdges(Geometry g) { var lines = LineStringExtracter.GetLines(g); var polygonizer = new Polygonizer(); polygonizer.Add(lines); var geom = polygonizer.GetCutEdges(); return(g.Factory.BuildGeometry(geom.ToArray <Geometry>())); }
private void CheckPolygonize(bool extractOnlyPolygonal, String[] inputWKT, String[] expectedWKT) { var polygonizer = new Polygonizer(extractOnlyPolygonal); polygonizer.Add(ReadList(inputWKT)); var expected = ReadList(expectedWKT); var actual = polygonizer.GetPolygons(); CheckEqual(expected, actual); }
public static IGeometry polygonizeCutEdges(IGeometry g) { var lines = LineStringExtracter.GetLines(g); Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(lines); var geom = polygonizer.GetCutEdges(); return(g.Factory.BuildGeometry(CollectionUtil.Cast <ILineString, IGeometry>(geom))); }
public static IGeometry polygonizeInvalidRingLines(IGeometry g) { var lines = LineStringExtracter.GetLines(g); Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(lines); var geom = polygonizer.GetInvalidRingLines(); return(g.Factory.BuildGeometry(geom)); }
public static Dictionary <ObjectId, IList <Point3d> > FindDanglingLine(IList <ObjectId> objectIds) { var dictionary = new Dictionary <ObjectId, IList <Point3d> >(); //var points = new List<Point3d>(); var database = objectIds[0].Database; using (var tr = database.TransactionManager.StartTransaction()) { var reader = new DwgReader(); // var pmFixed3 = new PrecisionModel(3); // 读入多边形数据 foreach (ObjectId objectId in objectIds) { if (!objectId.IsValid) { continue; } IGeometry geom = reader.ReadEntityAsGeometry(tr, objectId); if (geom == null) { continue; } // 开始做Union var nodedLineString = UnaryUnionOp.Union(geom); var polygonizer = new Polygonizer(); polygonizer.Add(nodedLineString); var polygons = polygonizer.GetPolygons(); // 悬挂线 var points = new List <Point3d>(); foreach (ILineString lineString in polygons) { foreach (var coordinate in lineString.Coordinates) { // 如果是NaN直接设定为0 if (double.IsNaN(coordinate.Z)) { coordinate.Z = 0; } points.Add(new Point3d(coordinate.X, coordinate.Y, coordinate.Z)); } } if (points.Any()) { dictionary.Add(objectId, points); } } tr.Commit(); } return(dictionary); }
public static IGeometry polygonize(IGeometry g) { var lines = LineStringExtracter.GetLines(g); var polygonizer = new Polygonizer(); polygonizer.Add(lines); var polys = polygonizer.GetPolygons(); var polyArray = GeometryFactory.ToPolygonArray(polys); return(g.Factory.CreateGeometryCollection(polyArray)); }
internal static IGeometry Polygonize(IGeometry geometry) { var lines = LineStringExtracter.GetLines(geometry); var polygonizer = new Polygonizer(false); polygonizer.Add(lines); var polys = new List <IGeometry>(polygonizer.GetPolygons()); var polyArray = GeometryFactory.ToGeometryArray(polys); return(geometry.Factory.BuildGeometry(polyArray)); }
public static Geometry polygonizeCutEdges(Geometry g) { var lines = LineStringExtracter.GetLines(g); Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(lines); var geomList = polygonizer.GetCutEdges() .Select(x => (Geometry)x) .ToList(); return(g.Factory.BuildGeometry(geomList)); }
public static Geometry PolygonizeAllErrors(Geometry g) { var lines = LineStringExtracter.GetLines(g); var polygonizer = new Polygonizer(); polygonizer.Add(lines); var errs = new List <Geometry>(); errs.AddRange(polygonizer.GetDangles()); errs.AddRange(polygonizer.GetCutEdges()); errs.AddRange(polygonizer.GetInvalidRingLines()); return(g.Factory.BuildGeometry(errs)); }
public static IGeometry polygonizeAllErrors(IGeometry g) { var lines = LineStringExtracter.GetLines(g); Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(lines); var errs = new List <ILineString>(); errs.AddRange(polygonizer.GetDangles()); errs.AddRange(polygonizer.GetCutEdges()); errs.AddRange(CollectionUtil.Cast <IGeometry, ILineString>(polygonizer.GetInvalidRingLines())); return(g.Factory.BuildGeometry(CollectionUtil.Cast <ILineString, IGeometry>(errs))); }
private static IGeometry Polygonize(IGeometry g, bool extractOnlyPolygonal) { var lines = LineStringExtracter.GetLines(g); var polygonizer = new Polygonizer(extractOnlyPolygonal); polygonizer.Add(lines); return(polygonizer.GetGeometry()); /* * Collection polys = polygonizer.getPolygons(); * Polygon[] polyArray = GeometryFactory.toPolygonArray(polys); * return g.getFactory().createGeometryCollection(polyArray); */ }
public static IGeometry polygonizeAllErrors(IGeometry g) { var lines = LineStringExtracter.GetLines(g); Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(lines); var geomList = new List <IGeometry>(); geomList.AddRange(polygonizer.GetDangles()); geomList.AddRange(polygonizer.GetCutEdges()); geomList.AddRange(polygonizer.GetInvalidRingLines()); return(g.Factory.BuildGeometry(geomList)); }
/// <summary> /// Add the linestring given to the polygonizer /// </summary> /// <param name="lineString">The line string</param> /// <param name="polygonizer">The polygonizer</param> static void AddLineString(ILineString lineString, Polygonizer polygonizer) { if (lineString is ILinearRing) { // LinearRings are treated differently to line strings : we need a LineString NOT a LinearRing lineString = lineString.Factory.CreateLineString(lineString.CoordinateSequence); } // unioning the linestring with the point makes any self intersections explicit. var point = lineString.Factory.CreatePoint(lineString.GetCoordinateN(0)); var toAdd = lineString.Union(point); //Add result to polygonizer polygonizer.Add(toAdd); }
public static IGeometry Polygonize(IGeometry geometry) { if (geometry == null) { throw new ArgumentNullException("geometry"); } var lines = LineStringExtracter.GetLines(geometry); var geoms = lines.ToList(); var polygonizer = new Polygonizer(); polygonizer.Add(geoms); var polys = polygonizer.GetPolygons(); return(geometry.Factory.BuildGeometry(polys)); }
internal static IGeometry PolygonizeForClip(IGeometry geometry, IPreparedGeometry clip) { var lines = LineStringExtracter.GetLines(geometry); var clippedLines = new List <IGeometry>(); foreach (ILineString line in lines) { if (clip.Contains(line)) { clippedLines.Add(line); } } var polygonizer = new Polygonizer(); polygonizer.Add(clippedLines); var polys = polygonizer.GetPolygons(); var polyArray = GeometryFactory.ToGeometryArray(polys); return(geometry.Factory.CreateGeometryCollection(polyArray)); }
public void Should_Find_Correct_Number_Of_Polygons_From_Lines() { var paths = new List <IGeometry>(); var factory = GeometryFactory.Default; for (int x = 1; x < 10; x++) { var startPoint = new Coordinate(x * 40, 30); var endPoint = new Coordinate(x * 40, 440); paths.Add(factory.CreateLineString(new[] { startPoint, endPoint })); } for (int y = 1; y < 10; y++) { var startPoint = new Coordinate(30, y * 40); var endPoint = new Coordinate(450, y * 40); paths.Add(factory.CreateLineString(new[] { startPoint, endPoint })); } var noder = new Noding.Snapround.GeometryNoder(new PrecisionModel(1.0d)); var geomFactory = GeometryFactory.Default; var nodedLines = noder.Node(paths); var nodedDedupedLinework = geomFactory.BuildGeometry(nodedLines.ToArray()).Union(); // WKT committed by FObermaier in 01/31/2017 => test fail string expected = @"MULTILINESTRING ((40 30, 40 40), (40 40, 40 80), (40 80, 40 120), (40 120, 40 160), (40 160, 40 200), (40 200, 40 240), (40 240, 40 280), (40 280, 40 320), (40 320, 40 360), (40 360, 40 440), (80 30, 80 40), (80 40, 80 80), (80 80, 80 120), (80 120, 80 160), (80 160, 80 200), (80 200, 80 240), (80 240, 80 280), (80 280, 80 320), (80 320, 80 360), (80 360, 80 440), (120 30, 120 40), (120 40, 120 80), (120 80, 120 120), (120 120, 120 160), (120 160, 120 200), (120 200, 120 240), (120 240, 120 280), (120 280, 120 320), (120 320, 120 360), (120 360, 120 440), (160 30, 160 40), (160 40, 160 80), (160 80, 160 120), (160 120, 160 160), (160 160, 160 200), (160 200, 160 240), (160 240, 160 280), (160 280, 160 320), (160 320, 160 360), (160 360, 160 440), (200 30, 200 40), (200 40, 200 80), (200 80, 200 120), (200 120, 200 160), (200 160, 200 200), (200 200, 200 240), (200 240, 200 280), (200 280, 200 320), (200 320, 200 360), (200 360, 200 440), (240 30, 240 40), (240 40, 240 80), (240 80, 240 120), (240 120, 240 160), (240 160, 240 200), (240 200, 240 240), (240 240, 240 280), (240 280, 240 320), (240 320, 240 360), (240 360, 240 440), (280 30, 280 40), (280 40, 280 80), (280 80, 280 120), (280 120, 280 160), (280 160, 280 200), (280 200, 280 240), (280 240, 280 280), (280 280, 280 320), (280 320, 280 360), (280 360, 280 440), (320 30, 320 40), (320 40, 320 80), (320 80, 320 120), (320 120, 320 160), (320 160, 320 200), (320 200, 320 240), (320 240, 320 280), (320 280, 320 320), (320 320, 320 360), (320 360, 320 440), (360 30, 360 40), (360 40, 360 80), (360 80, 360 120), (360 120, 360 160), (360 160, 360 200), (360 200, 360 240), (360 240, 360 280), (360 280, 360 320), (360 320, 360 360), (360 360, 360 440), (30 40, 40 40), (40 40, 80 40), (80 40, 120 40), (120 40, 160 40), (160 40, 200 40), (200 40, 240 40), (240 40, 280 40), (280 40, 320 40), (320 40, 360 40), (360 40, 450 40), (30 80, 40 80), (40 80, 80 80), (80 80, 120 80), (120 80, 160 80), (160 80, 200 80), (200 80, 240 80), (240 80, 280 80), (280 80, 320 80), (320 80, 360 80), (360 80, 450 80), (30 120, 40 120), (40 120, 80 120), (80 120, 120 120), (120 120, 160 120), (160 120, 200 120), (200 120, 240 120), (240 120, 280 120), (280 120, 320 120), (320 120, 360 120), (360 120, 450 120), (30 160, 40 160), (40 160, 80 160), (80 160, 120 160), (120 160, 160 160), (160 160, 200 160), (200 160, 240 160), (240 160, 280 160), (280 160, 320 160), (320 160, 360 160), (360 160, 450 160), (30 200, 40 200), (40 200, 80 200), (80 200, 120 200), (120 200, 160 200), (160 200, 200 200), (200 200, 240 200), (240 200, 280 200), (280 200, 320 200), (320 200, 360 200), (360 200, 450 200), (30 240, 40 240), (40 240, 80 240), (80 240, 120 240), (120 240, 160 240), (160 240, 200 240), (200 240, 240 240), (240 240, 280 240), (280 240, 320 240), (320 240, 360 240), (360 240, 450 240), (30 280, 40 280), (40 280, 80 280), (80 280, 120 280), (120 280, 160 280), (160 280, 200 280), (200 280, 240 280), (240 280, 280 280), (280 280, 320 280), (320 280, 360 280), (360 280, 450 280), (30 320, 40 320), (40 320, 80 320), (80 320, 120 320), (120 320, 160 320), (160 320, 200 320), (200 320, 240 320), (240 320, 280 320), (280 320, 320 320), (320 320, 360 320), (360 320, 450 320), (30 360, 40 360), (40 360, 80 360), (80 360, 120 360), (120 360, 160 360), (160 360, 200 360), (200 360, 240 360), (240 360, 280 360), (280 360, 320 360), (320 360, 360 360), (360 360, 450 360))"; // WKT from previous (to 01/31/2017) commit => test ok expected = @"MULTILINESTRING ((40 30, 40 40), (40 40, 40 80), (40 80, 40 120), (40 120, 40 160), (40 160, 40 200), (40 200, 40 240), (40 240, 40 280), (40 280, 40 320), (40 320, 40 360), (40 360, 40 440), (80 30, 80 40), (80 40, 80 80), (80 80, 80 120), (80 120, 80 160), (80 160, 80 200), (80 200, 80 240), (80 240, 80 280), (80 280, 80 320), (80 320, 80 360), (80 360, 80 440), (120 30, 120 40), (120 40, 120 80), (120 80, 120 120), (120 120, 120 160), (120 160, 120 200), (120 200, 120 240), (120 240, 120 280), (120 280, 120 320), (120 320, 120 360), (120 360, 120 440), (160 30, 160 40), (160 40, 160 80), (160 80, 160 120), (160 120, 160 160), (160 160, 160 200), (160 200, 160 240), (160 240, 160 280), (160 280, 160 320), (160 320, 160 360), (160 360, 160 440), (200 30, 200 40), (200 40, 200 80), (200 80, 200 120), (200 120, 200 160), (200 160, 200 200), (200 200, 200 240), (200 240, 200 280), (200 280, 200 320), (200 320, 200 360), (200 360, 200 440), (240 30, 240 40), (240 40, 240 80), (240 80, 240 120), (240 120, 240 160), (240 160, 240 200), (240 200, 240 240), (240 240, 240 280), (240 280, 240 320), (240 320, 240 360), (240 360, 240 440), (280 30, 280 40), (280 40, 280 80), (280 80, 280 120), (280 120, 280 160), (280 160, 280 200), (280 200, 280 240), (280 240, 280 280), (280 280, 280 320), (280 320, 280 360), (280 360, 280 440), (320 30, 320 40), (320 40, 320 80), (320 80, 320 120), (320 120, 320 160), (320 160, 320 200), (320 200, 320 240), (320 240, 320 280), (320 280, 320 320), (320 320, 320 360), (320 360, 320 440), (360 30, 360 40), (360 40, 360 80), (360 80, 360 120), (360 120, 360 160), (360 160, 360 200), (360 200, 360 240), (360 240, 360 280), (360 280, 360 320), (360 320, 360 360), (360 360, 360 440), (30 40, 40 40), (40 40, 80 40), (80 40, 120 40), (120 40, 160 40), (160 40, 200 40), (200 40, 240 40), (240 40, 280 40), (280 40, 320 40), (320 40, 360 40), (360 40, 450 40), (30 80, 40 80), (40 80, 80 80), (80 80, 120 80), (120 80, 160 80), (160 80, 200 80), (200 80, 240 80), (240 80, 280 80), (280 80, 320 80), (320 80, 360 80), (360 80, 450 80), (30 120, 40 120), (40 120, 80 120), (80 120, 120 120), (120 120, 160 120), (160 120, 200 120), (200 120, 240 120), (240 120, 280 120), (280 120, 320 120), (320 120, 360 120), (360 120, 450 120), (30 160, 40 160), (40 160, 80 160), (80 160, 120 160), (120 160, 160 160), (160 160, 200 160), (200 160, 240 160), (240 160, 280 160), (280 160, 320 160), (320 160, 360 160), (360 160, 450 160), (30 200, 40 200), (40 200, 80 200), (80 200, 120 200), (120 200, 160 200), (160 200, 200 200), (200 200, 240 200), (240 200, 280 200), (280 200, 320 200), (320 200, 360 200), (360 200, 450 200), (30 240, 40 240), (40 240, 80 240), (80 240, 120 240), (120 240, 160 240), (160 240, 200 240), (200 240, 240 240), (240 240, 280 240), (280 240, 320 240), (320 240, 360 240), (360 240, 450 240), (30 280, 40 280), (40 280, 80 280), (80 280, 120 280), (120 280, 160 280), (160 280, 200 280), (200 280, 240 280), (240 280, 280 280), (280 280, 320 280), (320 280, 360 280), (360 280, 450 280), (30 320, 40 320), (40 320, 80 320), (80 320, 120 320), (120 320, 160 320), (160 320, 200 320), (200 320, 240 320), (240 320, 280 320), (280 320, 320 320), (320 320, 360 320), (360 320, 450 320), (30 360, 40 360), (40 360, 80 360), (80 360, 120 360), (120 360, 160 360), (160 360, 200 360), (200 360, 240 360), (240 360, 280 360), (280 360, 320 360), (320 360, 360 360), (360 360, 450 360))"; Assert.AreEqual(expected, nodedDedupedLinework.ToString()); var polygonizer = new Polygonizer(); polygonizer.Add(nodedDedupedLinework); var polygons = polygonizer.GetPolygons(); Assert.AreEqual(64, polygons.Count); }
/** * Node a LinearRing and return a MultiPolygon containing * <ul> * <li>a single Polygon if the LinearRing is simple</li> * <li>several Polygons if the LinearRing auto-intersects</li> * </ul> * This is used to repair auto-intersecting Polygons */ private NetTopologySuite.Geometries.Geometry getArealGeometryFromLinearRing(LinearRing ring) { if (ring.IsSimple) { return(ring.Factory.CreateMultiPolygon(new Polygon[] { ring.Factory.CreatePolygon(ring, EMPTY_RING_ARRAY) })); } else { // Node input LinearRing and extract unique segments ISet <LineString> lines = nodeLineString(ring.Coordinates, ring.Factory); lines = getSegments(lines); // Polygonize the line network Polygonizer polygonizer = new Polygonizer(); polygonizer.Add((ICollection <NetTopologySuite.Geometries.Geometry>)lines); // Computes intersections to determine the status of each polygon ICollection <NetTopologySuite.Geometries.Geometry> geoms = new List <NetTopologySuite.Geometries.Geometry>(); foreach (NetTopologySuite.Geometries.Geometry g in polygonizer.GetPolygons()) { Polygon polygon = (Polygon)g; Coordinate p = polygon.InteriorPoint.Coordinate; var location = RayCrossingCounter.LocatePointInRing(p, ring.CoordinateSequence); if (location == NetTopologySuite.Geometries.Location.Interior) { geoms.Add(polygon); } } NetTopologySuite.Geometries.Geometry unionPoly = UnaryUnionOp.Union(geoms); NetTopologySuite.Geometries.Geometry unionLines = UnaryUnionOp.Union(lines).Difference(unionPoly.Boundary); geoms.Clear(); decompose(unionPoly, geoms); decompose(unionLines, geoms); return(ring.Factory.BuildGeometry(geoms)); } }
internal virtual void Run() { WKTReader rdr = new WKTReader(); IList lines = new ArrayList(); lines.Add(rdr.Read("LINESTRING (0 0 , 10 10)")); // isolated edge lines.Add(rdr.Read("LINESTRING (185 221, 100 100)")); //dangling edge lines.Add(rdr.Read("LINESTRING (185 221, 88 275, 180 316)")); lines.Add(rdr.Read("LINESTRING (185 221, 292 281, 180 316)")); lines.Add(rdr.Read("LINESTRING (189 98, 83 187, 185 221)")); lines.Add(rdr.Read("LINESTRING (189 98, 325 168, 185 221)")); Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(lines); ICollection polys = polygonizer.Polygons; Console.WriteLine("Polygons formed (" + polys.Count + "):"); foreach (object obj in polys) { Console.WriteLine(obj); } }
/// <summary> /// Converts a flat path to a <see cref="IGeometry"/>. /// </summary> /// <param name="wpfGeometry">The geometry to convert</param> /// <returns>A Geometry representing the path</returns> public IGeometry Read(WpfGeometry wpfGeometry) { var pathGeometry = PathGeometry.CreateFromGeometry(wpfGeometry); /* * .Item1 ... Closed * .Item2 ... Filled * .Item3 ... List<Coordinate[]> */ var pathPtSeq = ToCoordinates(pathGeometry); var geoms = new List <IGeometry>(); var seqIndex = 0; while (seqIndex < pathPtSeq.Count) { // assume next seq is shell // TODO: test this var pts = pathPtSeq[seqIndex]; if (pts.Item3.Length == 1) { geoms.Add(_geometryFactory.CreatePoint(pts.Item3[0])); seqIndex++; } else if (!(pts.Item1 || pts.Item2)) // neither closed nor filled { geoms.Add(_geometryFactory.CreateLineString(pts.Item3)); seqIndex++; } else { if (!pts.Item2) { geoms.Add(_geometryFactory.CreateLineString(pts.Item3)); continue; } var ringPts = ClosedCoordinateRing(pts.Item3); var rings = new List <IGeometry>(new[] { _geometryFactory.CreateLinearRing(ringPts) }); seqIndex++; //if (seqIndex < pathPtSeq.Count) //{ // if (!(pathPtSeq[seqIndex].Item1 || pathPtSeq[seqIndex].Item2)) continue; Coordinate[] holePts; // add holes as long as rings are CCW while (seqIndex < pathPtSeq.Count && (pathPtSeq[seqIndex].Item1 || pathPtSeq[seqIndex].Item2) && IsHole(holePts = pathPtSeq[seqIndex].Item3)) { rings.Add(_geometryFactory.CreateLinearRing(holePts)); seqIndex++; } var noder = new NetTopologySuite.Noding.Snapround.GeometryNoder(new NetTopologySuite.Geometries.PrecisionModel(100000000.0)); var nodedLinework = noder.Node(rings); // Use the polygonizer var p = new Polygonizer(pathGeometry.FillRule == FillRule.EvenOdd); p.Add(nodedLinework.ToList <IGeometry>()); var tmpPolygons = p.GetPolygons(); if (pathGeometry.FillRule == FillRule.Nonzero) { var unionized = CascadedPolygonUnion.Union(NetTopologySuite.Geometries.GeometryFactory.ToPolygonArray(tmpPolygons)); tmpPolygons = new List <IGeometry>(new[] { unionized }); } geoms.AddRange(tmpPolygons); //} } } return(BuildGeometry(geoms)); }
public static void FindDanglingLine(Database database) { var polylineIds = CadUtils.FindAllPolylines(Application.DocumentManager.MdiActiveDocument); using (var tr = database.TransactionManager.StartTransaction()) { var pmFixed3 = new PrecisionModel(3); // 读入多边形数据 var lineStringList = new List <IGeometry>(); foreach (ObjectId polylineId in polylineIds) { var curve = tr.GetObject(polylineId, OpenMode.ForRead) as Curve; var reader = new DwgReader(); IGeometry lineString; try { lineString = reader.ReadCurveAsPolygon(tr, curve) as Polygon; } catch (Exception) { lineString = reader.ReadCurveAsLineString(tr, curve) as LineString; } if (lineString != null && lineString.IsEmpty == false) { lineString = SimpleGeometryPrecisionReducer.Reduce(lineString, pmFixed3); lineStringList.Add(lineString); } } // 开始做Union var nodedLineString = UnaryUnionOp.Union(lineStringList); var polygonizer = new Polygonizer(); polygonizer.Add(nodedLineString); var polys = polygonizer.GetPolygons(); var dangles = polygonizer.GetDangles(); var cuts = polygonizer.GetCutEdges(); var writer = new DwgWriter(); var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(database); var blockTableRecord = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForWrite, false); // 悬挂线 foreach (ILineString lineString in dangles) { //if (lineString != null) //{ // var polyline = writer.WritePolyline(lineString); // polyline.ColorIndex = 3; // //polyline.Layer = ""; // // 输出到CAD // blockTableRecord.AppendEntity(polyline); // tr.AddNewlyCreatedDBObject(polyline, true); //} } tr.Commit(); } }
/// <summary> /// Creates a featureset from the given raster. /// </summary> /// <param name="rst">Raster used for creation.</param> /// <param name="contourType">The contour type used for creation.</param> /// <param name="fieldName">Name of the field that gets added to the featureset to put the level values into.</param> /// <param name="levels">The levels to sort the features into.</param> /// <returns>The featureset that was created from the raster.</returns> public static FeatureSet Execute(Raster rst, ContourType contourType, string fieldName = "Value", double[] levels = null) { double[] lev = levels; noData = rst.NoDataValue; type = contourType; Raster iRst = RasterCheck(rst, lev); string field = fieldName ?? "Value"; double[] x = new double[rst.NumColumns]; double[] y = new double[rst.NumRows]; for (int i = 0; i < rst.NumColumns; i++) { x[i] = rst.Extent.MinX + (rst.CellWidth * i) + (rst.CellWidth / 2); } for (int i = 0; i < rst.NumRows; i++) { y[i] = rst.Extent.MaxY - (rst.CellHeight * i) - (rst.CellHeight / 2); } FeatureSet fs = null; switch (type) { case ContourType.Line: fs = new FeatureSet(FeatureType.Line); fs.DataTable.Columns.Add(field, typeof(double)); if (levels != null) { for (int z = 0; z < levels.Length; z++) { IList <IGeometry> cont = GetContours(ref iRst, x, y, lev[z]); foreach (var g in cont) { var f = (Feature)fs.AddFeature((ILineString)g); f.DataRow[field] = lev[z]; } } } break; case ContourType.Polygon: fs = new FeatureSet(FeatureType.Polygon); fs.DataTable.Columns.Add("Lev", typeof(int)); fs.DataTable.Columns.Add("Label", typeof(string)); Collection <IGeometry> contours = new Collection <IGeometry>(); if (levels != null) { for (int z = 0; z < levels.Length; z++) { IList <IGeometry> cont = GetContours(ref iRst, x, y, lev[z]); foreach (var g in cont) { contours.Add(new LineString(g.Coordinates)); } } Coordinate[] boundary = new Coordinate[5]; boundary[0] = new Coordinate(x[0], y[0]); boundary[1] = new Coordinate(x[0], y[rst.NumRows - 1]); boundary[2] = new Coordinate(x[rst.NumColumns - 1], y[rst.NumRows - 1]); boundary[3] = new Coordinate(x[rst.NumColumns - 1], y[0]); boundary[4] = new Coordinate(x[0], y[0]); contours.Add(new LineString(boundary)); Collection <IGeometry> nodedContours = new Collection <IGeometry>(); IPrecisionModel pm = new PrecisionModel(1000d); GeometryNoder geomNoder = new GeometryNoder(pm); foreach (var c in geomNoder.Node(contours)) { nodedContours.Add(c); } Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(nodedContours); foreach (IPolygon p in polygonizer.GetPolygons().OfType <IPolygon>()) { IPoint pnt = p.InteriorPoint; int c = (int)((pnt.X - iRst.Extent.MinX) / iRst.CellWidth); int r = (int)((iRst.Extent.MaxY - pnt.Y) / iRst.CellHeight); double z = iRst.Value[r, c]; int cls = GetLevel(z, lev); string label = "Undefined"; if (cls == -1) { label = "< " + lev[0]; } else if (cls == lev.Length) { label = "> " + lev[lev.Length - 1]; } else if (cls >= 0 & cls < lev.Length) { label = lev[cls] + " - " + lev[cls + 1]; } IFeature f = fs.AddFeature(p); f.DataRow["Lev"] = cls; f.DataRow["Label"] = label; } } break; } return(fs); }
public static List <ObjectId> PolygonizeLineStrings(Database database, IEnumerable <ObjectId> polylineIdCollection, String outLayerName = "0", AcadColor color = null, double precision = 0.00001) { var result = new List <ObjectId>(); using (var tr = database.TransactionManager.StartTransaction()) { // 读入多边形数据 var lineStringList = new List <IGeometry>(); foreach (ObjectId polylineId in polylineIdCollection) { var curve = tr.GetObject(polylineId, OpenMode.ForRead) as Curve; var reader = new DwgReader(); var lineString = reader.ReadGeometry(curve, tr) as LineString; if (lineString != null && lineString.IsEmpty == false) { lineStringList.Add(lineString); } } #if TryManualUnion // 创建节点 var nodedContours = new List <List <IGeometry> >(); var scale = 1.0 / precision; var geomNoder = new NetTopologySuite.Noding.Snapround.GeometryNoder(new PrecisionModel(scale)); var nodedList = geomNoder.Node(lineStringList); // 这里可能可以用nodedList.Count来计算一个gourp的size来限定线程数量 var maxCountInGroup = 200; foreach (var c in nodedList) { //Coordinate[] linePts = CoordinateArrays.RemoveRepeatedPoints(c.Coordinates); //if (linePts.Count() > 1) // nodedContours.Add(c.Factory.CreateLineString(linePts)); //c.Buffer(0.001); // 将所有的线段每maxCountInGroup个分成一组. var groupCount = nodedContours.Count; if (groupCount == 0) { nodedContours.Add(new List <IGeometry>()); } var itemCount = nodedContours[nodedContours.Count - 1].Count; if (itemCount < maxCountInGroup) { nodedContours[nodedContours.Count - 1].Add(c); } else { nodedContours.Add(new List <IGeometry>()); nodedContours[nodedContours.Count - 1].Add(c); } } var workers = new List <Worker>(); var threadList = new List <Thread>(); // 为每组geometry开一个线程做union. foreach (List <IGeometry> nodedContour in nodedContours) { // Start a thread. var worker = new Worker(nodedContour); workers.Add(worker); var thread = new Thread(worker.Execute); threadList.Add(thread); thread.Start(); } // 等待所有线程运行结束 foreach (Thread thread in threadList) { thread.Join(); } // 最后将每组union得到的IGeometry再做一次union var nodedLineString = workers[0].Geometry; for (int i = 1; i < workers.Count; i++) { nodedLineString = nodedLineString.Union(workers[i].Geometry); } #else // 开始做Union var nodedLineString = UnaryUnionOp.Union(lineStringList, new GeometryFactory(new PrecisionModel(0.9d))); //var nodedLineString = lineStringList[0]; //for (int i = 1; i < lineStringList.Count; i++) //{ // nodedLineString = nodedLineString.Union(lineStringList[i]); //} #endif //造区 Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(nodedLineString); var polys = polygonizer.GetPolygons(); var dangles = polygonizer.GetDangles(); var cuts = polygonizer.GetCutEdges(); var writer = new DwgWriter(); var modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(database); var blockTableRecord = (BlockTableRecord)tr.GetObject(modelSpaceId, OpenMode.ForWrite, false); // 多边形 foreach (IGeometry geometry in polys) { var polygon = geometry as Polygon; if (polygon != null) { var polylines = writer.WritePolyline(polygon); foreach (Polyline polyline in polylines) { if (color != null) { polyline.Color = color; } polyline.Layer = outLayerName; // 输出到CAD var polylineId = blockTableRecord.AppendEntity(polyline); result.Add(polylineId); tr.AddNewlyCreatedDBObject(polyline, true); } } } //// 悬挂线 //foreach (ILineString lineString in dangles) //{ // if (lineString != null) // { // var polyline = writer.WritePolyline(lineString); // if (color != null) // polyline.Color = color; // polyline.Layer = outLayerName; // // 输出到CAD // blockTableRecord.AppendEntity(polyline); // tr.AddNewlyCreatedDBObject(polyline, true); // } //} //// 裁剪线 //foreach (ILineString lineString in cuts) //{ // if (lineString != null) // { // var polyline = writer.WritePolyline(lineString); // if (color != null) // polyline.Color = color; // polyline.Layer = outLayerName; // // 输出到CAD // blockTableRecord.AppendEntity(polyline); // tr.AddNewlyCreatedDBObject(polyline, true); // } //} tr.Commit(); } return(result); }
public static DotSpatial.Data.FeatureSet Execute(DotSpatial.Data.Raster rst, ContourType contourType, string FieldName = "Value", double[] levels = null) { double[] lev = levels; noData = rst.NoDataValue; type = contourType; DotSpatial.Data.Raster iRst = RasterCheck(rst, lev);; string field; if (FieldName == null) { field = "Value"; } else { field = FieldName; } double[] x = new double[rst.NumColumns]; double[] y = new double[rst.NumRows]; for (int i = 0; i < rst.NumColumns; i++) { x[i] = rst.Extent.MinX + rst.CellWidth * i + rst.CellWidth / 2; } for (int i = 0; i < rst.NumRows; i++) { y[i] = rst.Extent.MaxY - rst.CellHeight * i - rst.CellHeight / 2; } DotSpatial.Data.FeatureSet fs = null; switch (type) { case ContourType.Line: { fs = new DotSpatial.Data.FeatureSet(DotSpatial.Topology.FeatureType.Line); fs.DataTable.Columns.Add(field, typeof(double)); for (int z = 0; z < levels.Length; z++) { IList <IGeometry> cont = GetContours(ref iRst, x, y, lev[z]); foreach (var g in cont) { var f = (DotSpatial.Data.Feature)fs.AddFeature(ToDotSpatialLineString((ILineString)g)); f.DataRow[field] = lev[z]; } } } break; case ContourType.Polygon: { fs = new DotSpatial.Data.FeatureSet(DotSpatial.Topology.FeatureType.Polygon); fs.DataTable.Columns.Add("Lev", typeof(int)); fs.DataTable.Columns.Add("Label", typeof(string)); Collection <IGeometry> Contours = new Collection <IGeometry>(); for (int z = 0; z < levels.Count(); z++) { IList <IGeometry> cont = GetContours(ref iRst, x, y, lev[z]); foreach (var g in cont) { Contours.Add(new LineString(g.Coordinates)); } } Coordinate[] Boundary = new Coordinate[5]; Boundary[0] = new Coordinate(x[0], y[0]); Boundary[1] = new Coordinate(x[0], y[rst.NumRows - 1]); Boundary[2] = new Coordinate(x[rst.NumColumns - 1], y[rst.NumRows - 1]); Boundary[3] = new Coordinate(x[rst.NumColumns - 1], y[0]); Boundary[4] = new Coordinate(x[0], y[0]); Contours.Add(new LineString(Boundary)); Collection <IGeometry> NodedContours = new Collection <IGeometry>(); GeometryNoder geomNoder = new GeometryNoder(new PrecisionModel(1000d)); foreach (var c in geomNoder.Node(Contours)) { NodedContours.Add(c); } Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(NodedContours); foreach (IPolygon p in polygonizer.GetPolygons()) { Point pnt = (Point)p.InteriorPoint; int c = (int)((pnt.X - iRst.Extent.MinX) / iRst.CellWidth); int r = (int)((iRst.Extent.MaxY - pnt.Y) / iRst.CellHeight); double z = ((DotSpatial.Data.Raster)iRst).Value[r, c]; int Cls = GetLevel(z, lev); string label = "Undefined"; if (Cls == -1) { label = "< " + lev[0].ToString(); } if (Cls == lev.Count()) { label = "> " + lev[lev.Count() - 1].ToString(); } if (Cls >= 0 & Cls < lev.Count()) { label = lev[Cls].ToString() + " - " + lev[Cls + 1].ToString(); } DotSpatial.Topology.Polygon dsp = ToDotSpatialPolygon(p); DotSpatial.Data.Feature f = (DotSpatial.Data.Feature)fs.AddFeature(dsp); f.DataRow["Lev"] = Cls; f.DataRow["Label"] = label; } } break; } return(fs); }