public static ICollection <IGeometry> Polygonize(IGeometry geometry)
        {
            var polygonizer = new Polygonizer(false);

            polygonizer.Add(geometry);
            return(polygonizer.GetPolygons());
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        /*
         *      [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());
        }
Exemplo n.º 4
0
        public void Awake()
        {
            int       w  = texture.width;
            int       h  = texture.height;
            BitMatrix bm = new BitMatrix(w, h);

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    bm.Set(x, y, (texture.GetPixel(x, y).a >= 0.5f));
                }
            }
            this.polygons        = Polygonizer.Polygonize(bm);
            this.polygonCollider = GetComponent <PolygonCollider2D>();

            foreach (List <IntPoint> li in this.polygons)
            {
                for (int i = 0; i < li.Count; i++)
                {
                    li[i] = new IntPoint(li[i].X * MAP_SCALE_INV, li[i].Y * MAP_SCALE_INV);
                }
            }

            dirty = true;
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Get or create a valid version of the geometry given. If the geometry is a
        /// polygon or multi polygon, self intersections or inconsistencies are fixed.
        /// Otherwise the geometry is returned.
        /// </summary>
        /// <param name="geom">The geometry to be fixed</param>
        /// <returns>The fixed geometry</returns>
        public static IGeometry Validate(this IGeometry geom)
        {
            if (geom is IPolygon)
            {
                if (geom.IsValid)
                {
                    geom.Normalize(); // validate does not pick up rings in the wrong order - this will fix that
                    return(geom);     // If the polygon is valid just return it
                }
                var polygonizer = new Polygonizer();
                AddPolygon((IPolygon)geom, polygonizer);
                return(ToPolygonGeometry(polygonizer.GetPolygons(), geom.Factory));
            }

            if (geom is IMultiPolygon)
            {
                if (geom.IsValid)
                {
                    geom.Normalize(); // validate does not pick up rings in the wrong order - this will fix that
                    return(geom);     // If the multipolygon is valid just return it
                }
                var polygonizer = new Polygonizer();
                for (int n = geom.NumGeometries; n-- > 0;)
                {
                    AddPolygon((IPolygon)geom.GetGeometryN(n), polygonizer);
                }
                return(ToPolygonGeometry(polygonizer.GetPolygons(), geom.Factory));
            }

            // ToDo other validations

            // Only care about polygons
            return(geom);
        }
        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());
        }
Exemplo n.º 9
0
 /// <summary>
 /// Add all line strings from the polygon given to the polygonizer given
 /// </summary>
 /// <param name="polygon">The polygon from which to extract the line strings</param>
 /// <param name="polygonizer">The polygonizer</param>
 static void AddPolygon(IPolygon polygon, Polygonizer polygonizer)
 {
     AddLineString(polygon.ExteriorRing, polygonizer);
     for (int n = polygon.NumInteriorRings; n-- > 0;)
     {
         AddLineString(polygon.GetInteriorRingN(n), polygonizer);
     }
 }
        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>()));
        }
        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 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)));
        }
        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);
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 17
0
        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 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)));
        }
        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));
        }
Exemplo n.º 20
0
        /// <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);
        }
        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));
        }
Exemplo n.º 23
0
        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));
        }
Exemplo n.º 24
0
    void Start()
    {
        values    = new sbyte[size, size, size];
        extractor = new Polygonizer(this);

        for (int x = 0; x < size; x++)
        {
            for (int z = 0; z < size; z++)
            {
                float noise = (float)((SimplexNoise.noise(x * 1f, z * 1f) * 0.01f));
                for (int y = 0; y < size; y++)
                {
                    values [x, y, z] = (sbyte)Mathf.Clamp(((((noise + 1f) * 32f) + 0.5f) - (y + 0.5f)), -127, 127);
                }
            }
        }
        Polygonize();
    }
        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));
        }
Exemplo n.º 26
0
        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));
            }
        }
Exemplo n.º 28
0
        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);
            }
        }
Exemplo n.º 29
0
        /// <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));
        }
Exemplo n.º 30
0
        /// <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);
        }
Exemplo n.º 31
0
 /// <summary>
 /// Creates a new instance of LineStringAdder
 /// </summary>
 public LineStringAdder(Polygonizer container)
 {
     _container = container;
 }