Polygon.Intersects exception

Jun 28, 2010 at 9:54 PM

Hi i am trying to use NTS to cut off a bit of a LineString that intersects a polygon

but i have no luck only get an exception
i am new to NTS and don't realy know how to use it (still learning)

i am first trying to check if there is an intersection

if (!myNtPolygon.Intersects(myLineString))

i get an exception

System.ArgumentException was unhandled by user code
  Message=At least one object must implement IComparable.
       på System.Collections.Comparer.Compare(Object a, Object b)
       på System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
       på System.Linq.EnumerableSorter`2.CompareKeys(Int32 index1, Int32 index2)
       på System.Linq.EnumerableSorter`1.QuickSort(Int32[] map, Int32 left, Int32 right)
       på System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
       på System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
       på GisSharpBlog.NetTopologySuite.GeometriesGraph.GeometryGraph.AddSelfIntersectionNodes(Int32 argIndex)
       på GisSharpBlog.NetTopologySuite.GeometriesGraph.GeometryGraph.ComputeSelfNodes(LineIntersector li, Boolean computeRingSelfNodes)
       på GisSharpBlog.NetTopologySuite.Operation.Relate.RelateComputer.ComputeIM()
       på GisSharpBlog.NetTopologySuite.Operation.Relate.RelateOp.get_IntersectionMatrix()
       på GisSharpBlog.NetTopologySuite.Operation.Relate.RelateOp.Relate(IGeometry a, IGeometry b)
       på GisSharpBlog.NetTopologySuite.Geometries.Geometry.Relate(IGeometry g)
       på GisSharpBlog.NetTopologySuite.Geometries.Geometry.Intersects(IGeometry g)
       på Gis.GPX.ExclusionFilter.Filter(ILineString myLineString)
       på GPXUpload.Model.Editor.ApplyFilter()
       på Gis.GPX.FilterBase.set_IsEnabled(Boolean value)

is ther a bug in NTS or am i doing something wrong

Jun 28, 2010 at 10:30 PM

The NTS is certainly not complete or completely robust but it was the best Spatial library we could find. One of the guys on this project ported it to Silverlight and there is a small possibility that we could have introduced the bug. We also rercently updated to SL4 in the "branch" folder of the latest source.

I've only used the WKB serialisation to create the geometries myself, how are you creating yours?

If you want to verifiy if the bug is with our port you could run up your code in the full .net version of the library. I must admit I'm still using the server side (SQL2008) to perform my more complex spatial queries. Their is a rumor that a SL version of their library maybe on its way.

Jun 28, 2010 at 11:07 PM
Edited Jun 28, 2010 at 11:08 PM

i create all geometries by code, UI interaction or by reading gpx files

i have just tried to make a test in the trunk code from svn (1.7.1)
and there it works fine

have tryed in the sl4 branch in DeepEarth and there i get the exception

the test case


IGeometry g1 = new WKTReader().Read("LINESTRING(-6.770287 62.00991,-6.770051 62.00975,-6.769663 62.00953,-6.769495 62.00947,-6.769223 62.00936,-6.769066 62.00927,-6.768713 62.00924,-6.768526 62.00907,-6.768113 62.00881,-6.76716 62.00862,-6.767598 62.00894,-6.768164 62.0091,-6.767548 62.00908,-6.766623 62.00893,-6.766685 62.00906)");

IGeometry g2 = new WKTReader().Read("POLYGON((-6.7707870788201179 62.01018724771437,-6.7693064994438972 62.009739111702125,-6.7690275497063492 62.009029129912683,-6.7703901118859129 62.009336287186436,-6.7707870788201179 62.01018724771437))");

bool overlaps = g1.Intersects(g2);
Debug.WriteLine("by wkt works" + overlaps);



Jun 29, 2010 at 12:31 AM

ok i have been digging around and found out that it is an bug introduced when you have replaced "Wintellect.PowerCollections.OrderedDictionary" by "System.Collections.Generic.Dictionary"

i have only tested with the testcase above and some small code in another app and it works with the modifications below
there are maby some other cases that are broken

public IEnumerator GetEnumerator()
  //return nodeMap.OrderBy(x => x).GetEnumerator();
  return nodeMap.Values.OrderBy(x => x).GetEnumerator();

public IEnumerator GetEnumerator() 
  // return nodeMap.OrderBy(x => x).GetEnumerator(); // bug
  return nodeMap.Values.OrderBy(x => x).GetEnumerator();
public IList Edges
    if (edgeList == null)
	edgeList = new List<EdgeEnd>();
	//foreach (var obj in edgeMap.OrderBy(x => x))
	foreach (var obj in edgeMap.Values.OrderBy(x => x))
   return edgeList;
public override void Insert(EdgeEnd e)
  EdgeEndBundle eb = null;
  if (edgeMap.ContainsKey(e))
	eb = (EdgeEndBundle)edgeMap[e];
//EdgeEndBundle eb = (EdgeEndBundle) edgeMap[e];
if (eb == null)
  eb = new EdgeEndBundle(e);
  InsertEdgeEnd(e, eb);