Esempio n. 1
0
        public void PolygonizerTest()
        {
            const string wkt = @"MULTILINESTRING(
(595435.592 663003.43, 595329.952 663070.947), (595435.592 663003.43, 595422.128 662982.365), 
(595422.128 662982.365, 595408.755 662961.281), (595408.755 662961.281, 595324.534 663015.109), 
(595324.534 663015.109, 595324.534 663015.109), (595326.792 663018.656, 595324.534 663015.109), 
(595324.534 663015.109, 595324.534 663015.109), (595324.534 663015.109, 595311.111 662994.018), 
(595311.111 662994.018, 595299.143 662975.215), (595299.143 662975.215, 595297.687 662972.927), 
(595297.687 662972.927, 595284.264 662951.837), (595284.264 662951.837, 595270.782 662930.654), 
(595299.143 662975.215, 595302.092 663071.747), (595302.092 663071.747, 595302.098 663071.926), 
(595329.289 663070.966, 595327.933 663042.567), (595327.933 663042.567, 595327.933 663042.567), 
(595327.933 663042.567, 595326.792 663018.656), (595327.933 663042.567, 595422.128 662982.365), 
(595355.101 662876.893, 595341.661 662855.813), (595341.661 662855.813, 595232.906 662925.154), 
(595232.906 662925.154, 595257.509 662928.727), (595257.509 662928.727, 595257.509 662928.727), 
(595257.509 662928.727, 595270.782 662930.654), (595270.782 662930.654, 595355.101 662876.893), 
(595301.269 662789.679, 595290.846 662792.619), (595290.846 662792.619, 595229.883 662829.806), 
(595229.883 662829.806, 595231.001 662867.129), (595314.822 662813.628, 595314.822 662813.628), 
(595314.822 662813.628, 595231.001 662867.129), (595301.269 662789.679, 595314.822 662813.628), 
(595314.822 662813.628, 595314.822 662813.628), (595237.314 663073.714, 595237.311 663073.607), 
(595237.311 663073.607, 595234.348 662973.758), (595234.348 662973.758, 595232.906 662925.154), 
(595232.906 662925.154, 595231.955 662896.179), (595231.955 662896.179, 595231.955 662896.179), 
(595231.955 662896.179, 595231.001 662867.129), (595395.292 662940.216, 595311.111 662994.018), 
(595381.829 662919.151, 595297.687 662972.927), (595368.365 662898.086, 595284.264 662951.837), 
(595328.272 662834.701, 595231.955 662896.179), (595231.955 662896.179, 595231.955 662896.179), 
(595408.755 662961.281, 595395.292 662940.216), (595395.292 662940.216, 595395.292 662940.216), 
(595395.292 662940.216, 595381.829 662919.151), (595381.829 662919.151, 595381.829 662919.151), 
(595381.829 662919.151, 595368.365 662898.086), (595368.365 662898.086, 595355.101 662876.893), 
(595341.661 662855.813, 595328.272 662834.701), (595328.272 662834.701, 595328.272 662834.701), 
(595328.272 662834.701, 595314.822 662813.628), (595314.822 662813.628, 595314.822 662813.628), 
(595229.059 662774.178, 595229.883 662829.806), (595301.269 662789.679, 595292.309 662772.342), 
(595210.495 662868.477, 595216.033 662860.79), (595217.835 662853.417, 595216.033 662860.79), 
(595208.232 662974.501, 595234.348 662973.758), (595212.331 663074.324, 595237.311 663073.607),
(595237.311 663073.607, 595262.33 663072.888), (595262.33 663072.888, 595302.092 663071.747), 
(595302.092 663071.747, 595329.289 663070.966), (595329.289 663070.966, 595329.952 663070.947),
(595329.952 663070.947, 595404.571 663068.804), (595404.571 663068.804, 595455.568 663034.686),
(595455.568 663034.686, 595435.592 663003.43), (595257.509 662928.727, 595257.509 662928.727), 
(595257.509 662928.727, 595262.33 663072.888), (595262.33 663072.888, 595262.333 663073.001),
(595177.856 662775.665, 595229.059 662774.178), (595229.059 662774.178, 595229.059 662774.178),
(595229.059 662774.178, 595292.309 662772.342), (595217.835 662853.417, 595215.34 662774.577))";

            // Arrange
            var geom = new WKTReader(new GeometryFactory(new PrecisionModel(1000))).Read(wkt);

            var op = new NetTopologySuite.Operation.Polygonize.Polygonizer();

            op.Add(geom);

            // Act
            var res = op.GetGeometry();

            // Assert
            Assert.That(res, Is.Not.Null, "geom != null");

            Assert.That(res.IsValid, Is.True, "IsValid");
            Assert.That(res.IsSimple, Is.True, "IsSimple");
            Assert.That(new RepeatedPointTester().HasRepeatedPoint(res), Is.False, "No repeated points");

            Assert.That(CoordinateArrays.HasRepeatedPoints(res.Coordinates), Is.False, "No repeated points");
        }
 public static IGeometry PolygonizeInvalidRingLines(IGeometry g)
 {
     var lines = LineStringExtracter.GetLines(g);
     var 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);
     var 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(CollectionUtil.Cast<ILineString, IGeometry>(lines));
     var geom = polygonizer.GetInvalidRingLines();
     return g.Factory.BuildGeometry(geom);
 }
 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);
 }
 public static IGeometry PolygonizeAllErrors(IGeometry g)
 {
     var lines = LineStringExtracter.GetLines(g);
     var 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);
     */
 }
Esempio n. 8
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. 11
0
        public void WriteShapeFiles(DataAccessLayer DAL)
        {
            #if !(PocketPC || WindowsCE || Mobile)
            List<TtPolygon> polys = DAL.GetPolygons();
            Dictionary<string, TtMetaData> metas = DAL.GetMetaData().ToDictionary(m => m.CN, m => m);

            string folder = SelectedPath + "\\GIS";
            Directory.CreateDirectory(folder);

            foreach (TtPolygon poly in polys)
            {
                string _CurrDir = String.Format("{0}\\{1}\\", folder, poly.Name.ScrubFileName());
                string _File = String.Format("{0}\\{1}\\{1}", folder, poly.Name.ScrubFileName());
                Directory.CreateDirectory(_CurrDir);

                string CurrFileName = System.IO.Path.Combine(folder, poly.Name);

                List<TtPoint> points = DAL.GetPointsInPolygon(poly.CN).ToList();

                List<WayPoint> wayPoints = points.FilterOnly(OpType.WayPoint).Cast<WayPoint>().ToList();
                points = points.FilterOut(OpType.WayPoint).ToList();

                TtMetaData md;

                if (points.Count > 0 || wayPoints.Count > 0)
                {
                    if (points.Count > 0)
                        md = DAL.GetMetaDataByCN(points[0].MetaDefCN);
                    else
                        md = DAL.GetMetaDataByCN(wayPoints[0].MetaDefCN);
                    if (md == null)
                        continue;
                }
                else
                    continue;

                if (wayPoints.Count > 0)
                    WriteWayPoints(_File, wayPoints, md);

                if (points.Count < 1)
                    continue;

                CoordinateList BAdjCoords = new CoordinateList();
                CoordinateList BUnAdjCoords = new CoordinateList();

                CoordinateList NavAdjCoords = new CoordinateList();
                CoordinateList NavUnAdjCoords = new CoordinateList();

                bool hasWayPoints = false;

                TtPoint firstBndPoint = null, lastBntPoint = null;

                foreach (TtPoint point in points)
                {
                    if (point.IsNavPoint())
                    {
                        NavAdjCoords.Add(new Coordinate(point.AdjX, point.AdjY, point.AdjZ));
                        NavUnAdjCoords.Add(new Coordinate(point.UnAdjX, point.UnAdjY, point.UnAdjZ));
                    }

                    if (point.IsBndPoint())
                    {
                        BAdjCoords.Add(new Coordinate(point.AdjX, point.AdjY, point.AdjZ));
                        BUnAdjCoords.Add(new Coordinate(point.UnAdjX, point.UnAdjY, point.UnAdjZ));

                        if (firstBndPoint == null)
                            firstBndPoint = point;

                        lastBntPoint = point;
                    }

                    if (point.op == OpType.WayPoint)
                    {
                        hasWayPoints = true;
                    }
                }

                double polyLinePerim = -1;
                if (firstBndPoint != null)
                    polyLinePerim = poly.Perimeter - TtUtils.Distance(firstBndPoint, lastBntPoint, true);

                #region Navigation

                #region Adj
                string FileName = _File + "_NavAdj";
                GeometryFactory geoFac = new GeometryFactory();
                ShapefileDataWriter sdw;
                Polygonizer polyizer = new Polygonizer();

                ArrayList features = new ArrayList();
                AttributesTable attTable = new AttributesTable();

                attTable.AddAttribute("Poly_Name", poly.Name);
                attTable.AddAttribute("Desc", poly.Description);
                attTable.AddAttribute("Poly", "Navigation Adjusted");
                attTable.AddAttribute("CN", poly.CN);
                attTable.AddAttribute("Perim_M", poly.Perimeter);
                attTable.AddAttribute("PerimLine_M", polyLinePerim);

                Feature feat = new Feature();
                DbaseFileHeader dbh;

                if (NavAdjCoords.Count > 1)
                {
                    sdw = new ShapefileDataWriter(FileName, geoFac);

                    feat.Geometry = new NetTopologySuite.Geometries.LineString(NavAdjCoords.ToArray());

                    feat.Attributes = attTable;

                    features.Add(feat);

                    dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);

                    sdw.Header = dbh;
                    sdw.Write(features);
                    WriteProjection(FileName, md.Zone);

                    //points
                    FileName = _File + "_NavAdj_Points";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Navigation Adjusted Points";

                    features = GetPointFeatures(points.Where(p => p.IsNavPoint()), true, DAL, metas);

                    if (features.Count > 0)
                    {
                        dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                        sdw.Header = dbh;
                        sdw.Write(features);
                        WriteProjection(FileName, md.Zone);
                    }
                }
                #endregion

                #region UnAdj
                if (NavUnAdjCoords.Count > 1)
                {
                    FileName = _File + "_NavUnAdj";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Navigation UnAdjusted";
                    feat = new Feature();
                    feat.Geometry = new NetTopologySuite.Geometries.LineString(NavUnAdjCoords.ToArray());
                    //feat.Geometry = new NetTopologySuite.Geometries.LinearRing(NavUnAdjCoords.ToArray());
                    feat.Attributes = attTable;

                    features.Add(feat);

                    dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                    sdw.Header = dbh;
                    sdw.Write(features);
                    WriteProjection(FileName, md.Zone);

                    //points
                    FileName = _File + "_NavUnAdj_Points";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Navigation UnAdjusted Points";

                    features = GetPointFeatures(points.Where(p => p.IsNavPoint()), false, DAL, metas);

                    if (features.Count > 0)
                    {
                        dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                        sdw.Header = dbh;
                        sdw.Write(features);
                        WriteProjection(FileName, md.Zone);
                    }
                }
                #endregion

                #endregion

                #region Boundary

                #region Adj Line
                if (BAdjCoords.Count > 1)
                {
                    FileName = _File + "_BndAdjLine";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Boundary Adjusted Line";
                    feat = new Feature();
                    feat.Geometry = new NetTopologySuite.Geometries.LineString(BAdjCoords.ToArray());
                    feat.Attributes = attTable;

                    features.Add(feat);

                    dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                    sdw.Header = dbh;
                    sdw.Write(features);
                    WriteProjection(FileName, md.Zone);
                }
                #endregion

                #region UnAdj Line
                if (BUnAdjCoords.Count > 1)
                {
                    FileName = _File + "_BndUnAdjLine";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Boundary UnAdjusted Line";
                    feat = new Feature();
                    feat.Geometry = new NetTopologySuite.Geometries.LineString(BUnAdjCoords.ToArray());
                    feat.Attributes = attTable;

                    features.Add(feat);

                    dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                    sdw.Header = dbh;
                    sdw.Write(features);
                    WriteProjection(FileName, md.Zone);
                }
                #endregion

                #region Adj
                if (BAdjCoords.Count > 3)
                {
                    FileName = _File + "_BndAdj";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable.AddAttribute("Area_MtSq", poly.Area);
                    attTable["Poly"] = "Boundary Adjusted";
                    feat = new Feature();

                    if (BAdjCoords[0] != BAdjCoords[BAdjCoords.Count - 1])
                        BAdjCoords.Add(BAdjCoords[0]);

                    feat.Geometry = new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(BAdjCoords.ToArray()));
                    feat.Attributes = attTable;

                    features.Add(feat);

                    dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                    sdw.Header = dbh;
                    sdw.Write(features);
                    WriteProjection(FileName, md.Zone);

                    //points
                    FileName = _File + "_BndAdj_Points";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Boundary Adjusted Points";

                    features = GetPointFeatures(points.Where(p => p.IsBndPoint()), true, DAL, metas);

                    if (features.Count > 0)
                    {
                        dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                        sdw.Header = dbh;
                        sdw.Write(features);
                        WriteProjection(FileName, md.Zone);
                    }
                }
                #endregion

                #region UnAdj
                if (BUnAdjCoords.Count > 3)
                {
                    FileName = _File + "_BndUnAdj";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Boundary UnAdjusted";
                    feat = new Feature();

                    if (BUnAdjCoords[0] != BUnAdjCoords[BUnAdjCoords.Count - 1])
                        BUnAdjCoords.Add(BUnAdjCoords[0]);

                    feat.Geometry = new NetTopologySuite.Geometries.Polygon(new LinearRing(BUnAdjCoords.ToArray()));
                    feat.Attributes = attTable;

                    features.Add(feat);

                    dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                    sdw.Header = dbh;
                    sdw.Write(features);
                    WriteProjection(FileName, md.Zone);

                    //points
                    FileName = _File + "_BndUnAdj_Points";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "Boundary UnAdjusted Points";

                    features = GetPointFeatures(points.Where(p => p.IsBndPoint()), false, DAL, metas);

                    if (features.Count > 0)
                    {
                        dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                        sdw.Header = dbh;
                        sdw.Write(features);
                        WriteProjection(FileName, md.Zone);
                    }
                }
                #endregion

                #region WayPoints
                if (hasWayPoints)
                {
                    //points
                    FileName = _File + "_WayPoints";
                    geoFac = new GeometryFactory();
                    sdw = new ShapefileDataWriter(FileName, geoFac);
                    features = new ArrayList();
                    attTable["Poly"] = "WayPoints";

                    features = GetPointFeatures(points.Where(p => p.op == OpType.WayPoint), false, DAL, metas);

                    if (features.Count > 0)
                    {
                        dbh = ShapefileDataWriter.GetHeader((Feature)features[0], features.Count);
                        sdw.Header = dbh;
                        sdw.Write(features);
                        WriteProjection(FileName, md.Zone);
                    }
                }
                #endregion
                #endregion
            }
            #endif
        }
 public LineStringAdder(Polygonizer container)
 {
     _container = container;
 }
Esempio n. 13
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());
        }
Esempio n. 14
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);
            }
        }
Esempio n. 15
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;
        }
Esempio n. 16
0
 public LineStringAdder(Polygonizer container)
 {
     _container = container;
 }
 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);
 }