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);
 }
Esempio n. 2
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);
        }
 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);
 }
		internal virtual void Run()
		{
			WKTReader rdr = new WKTReader();
			IList<IGeometry> lines = new List<IGeometry>
			                             {
			                                 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)")
			                             };

		    Polygonizer polygonizer = new Polygonizer();
			polygonizer.Add(lines);
			
			var polys = polygonizer.GetPolygons();
			
			Console.WriteLine("Polygons formed (" + polys.Count + "):");
            foreach(var obj in polys)
			    Console.WriteLine(obj);
		}
Esempio n. 5
0
        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.Count(); z++)
                        {
                            IList<IGeometry> cont = GetContours(ref iRst, x, y, lev[z]);

                            foreach (Geometry g in cont)
                            {
                                DotSpatial.Data.Feature 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 (Geometry 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 (LineString c in geomNoder.Node(Contours))
                        {
                            NodedContours.Add(c);
                        }

                        Polygonizer polygonizer = new Polygonizer();
                        polygonizer.Add(NodedContours);

                        foreach (Polygon 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;
        }
        /*
                [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());
        }
Esempio n. 7
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);
            var wkts = new[] { wkt1, wkt2, wkt3,};            
            var polygonizer = new Polygonizer();
            foreach (var 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);
            }
        }
 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);
 }