I am trying to convert lines to polygons using NetTopologySuite(a C# port of Java Topology Suite JTS). I ran the sample console code succesfully and it generated two polylines.
I then tried to run the code using three line strings below which are noded at endpoints.
(0 0,10 0)
(10 0,10 10)
(10 10,0 0)
but this move is bringing the demons out of the code. A bunch of errors are popping up as listed below.
C:\Users\Home\Desktop\polgon\polcreate\polcreate\bin\Release>polcreate.exe
at NetTopologySuite.Geometries.LinearRing`1.ValidateConstruction() in E:\Programming
\JTS\JTS2.11\v2.11_cleanup\NetTopologySuite\Geometries\LinearRing.cs:line 119
at NetTopologySuite.Geometries.LinearRing`1..ctor(ICoordinateSequence`1 coordinates,
IGeometryFactory`1 factory) in E:\Programming\JTS\JTS2.11\v2.11_cleanup\NetTopologySui
te\Geometries\LinearRing.cs:line 43
at NetTopologySuite.Geometries.GeometryFactory`1.CreateLinearRing(ICoordinateSequenc
e`1 coordinates) in E:\Programming\JTS\JTS2.11\v2.11_cleanup\NetTopologySuite\Geometrie
s\GeometryFactory.cs:line 552
at NetTopologySuite.Geometries.GeometryFactory`1.CreateLinearRing(IEnumerable`1 coor
dinates) in E:\Programming\JTS\JTS2.11\v2.11_cleanup\NetTopologySuite\Geometries\Geomet
ryFactory.cs:line 535
and so on....
The full code is shown below. Can anyone guide me the right way?
#define simple
using System.Text;
using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using GeoAPI.IO.WellKnownText;
using NetTopologySuite.Geometries;
using NetTopologySuite.Operation.Polygonize;
#if simple
using NUnit.Framework;
using Coord = NetTopologySuite.Coordinates.Coordinate;
using CoordSeqFac = NetTopologySuite.Coordinates.CoordinateSequenceFactory;
#else
using Coord = NetTopologySuite.Coordinates.BufferedCoordinate;
using CoordSeqFac = NetTopologySuite.Coordinates.BufferedCoordinateSequenceFactory;
#endif
namespace polcreate
{
/// <summary>
/// Example of using Polygonizer class to polygonize a set of fully noded linestrings.
/// </summary>
[TestFixture]
public class PolygonizeExample
{
[STAThread]
public static void Main(String[] args)
{
PolygonizeExample test = new PolygonizeExample();
try
{
test.Run();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
[Test]
public void Run()
{
IGeometryFactory<Coord> geoFactory =
new GeometryFactory<Coord>(new CoordSeqFac());
WktReader<Coord> rdr
= new WktReader<Coord>(geoFactory, null);
List<IGeometry<Coord>> lines
= new List<IGeometry<Coord>>();
// old values which ran successfully
//lines.Add(rdr.Read("LINESTRING (0 0 , 10 10)"));
//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)"));
//i changed the values here..
lines.Add(rdr.Read("LINESTRING (0 0 , 10 0)"));
lines.Add(rdr.Read("LINESTRING (10 0, 10 10)"));
lines.Add(rdr.Read("LINESTRING (10 10, 0 0)"));
Polygonizer<Coord> polygonizer
= new Polygonizer<Coord>();
polygonizer.Add(lines);
IList<IPolygon<Coord>> polys = polygonizer.Polygons;
Console.WriteLine("Polygons formed (" + polys.Count + "):");
foreach (object obj in polys)
{
Console.WriteLine(obj);
}
Console.ReadLine();
}
}
}



