Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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();
        }
    }
}
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

I think the issue here is that the polygon you are trying to generate is not valid.

I tried your code using the NetTopologySuite NuGet packages, and while the syntax was slightly different and it didn't throw an error, I still got a count of 0 for polys.Count

The shape you are trying to generate the polygon for looks like this

Unclosed Shape

This works

//i changed the values here.. 
lines.Add(rdr.Read("LINESTRING (0 0 , 10 10)"));
lines.Add(rdr.Read("LINESTRING (10 0, 10 10)"));
lines.Add(rdr.Read("LINESTRING (10 0, 0 0)"));

Closed Shape

I couldn't find how to, but it might be worth running the geometry through some sort of validity check before trying to convert it to polygon if this sort of thing is going to come up often.

EDIT:

Code that I used to test the square case:

using System.Text;
using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Operation.Polygonize;
using NetTopologySuite.IO;

namespace polcreate
{
    /// <summary>  
    /// Example of using Polygonizer class to polygonize a set of fully noded linestrings.
    /// </summary>  
    public class PolygonizeExample
    {
        [STAThread]
        public static void Main(String[] args)
        {
            PolygonizeExample test = new PolygonizeExample();
            try
            {
                test.Run();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }

        public void Run()
        {

            IGeometryFactory geometryFactory = new GeometryFactory();
            List<IGeometry> lines = new List<IGeometry>();
            WKTReader rdr = new WKTReader(geometryFactory);

            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 10)"));
            lines.Add(rdr.Read("LINESTRING (0 10, 0 0)"));

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

            IList<IGeometry> polys = polygonizer.GetPolygons();

            Console.WriteLine("Polygons formed (" + polys.Count + "):");
            foreach (object obj in polys)
            {
                Console.WriteLine(obj);
            }
            Console.ReadLine();
        }
    }
}
share|improve this answer
    
yes i am trying to create a polygon as shown in your figure..the first line(0 0, 10 10) was a typo..i just edited my post..still the errors are same.. –  vinayan Mar 7 '12 at 4:17
    
The coordinates you reposted are different for two of the lines, both the first and the last. I re-ran with the new ones you posted, and it worked in my copy of your project using the NetTopologySuite off NuGet. –  Kelso Mar 7 '12 at 4:30
    
i just tried a square shape now..using below strings..still errors.. (0 0 , 10 0) (10 0, 10 10) (10 10, 0 10) (0 10, 0 0) @Kelso -are you getting a polygon output? the i may need to try NuGet –  vinayan Mar 7 '12 at 4:30
    
@vinyan, I have ammended my post with your linestrings above, still getting no errors, and a polys.Count of 1. I have posted my exact code, so if you can't get my version to run, then maybe look at using the NuGet packages rather than the ones you are currently using. –  Kelso Mar 7 '12 at 4:44
    
thank you Kelso, I am looking at NuGet now..also i remember getting some warnings during my NTS build .. –  vinayan Mar 7 '12 at 4:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.