# How to find self-intersecting polylines using ArcObjects?

If I have a polyline, how do I figure out whether it is self-intersecting? I tried to use ITopologicalOperator.IsSimple Property, but it always returns true.

Are there any ways of doing these kind of topological checks over individual geometries?

-
As a last resort you can split polyline into segments, and check intersection between each segment, but this should be done only if there is no other way to do that, because it most likely will be quite slow. – Sunder Nov 20 '12 at 10:44

I think you can do with with the ITopologicalOperator2 interface:

``````dim pTopoLogicalOp as ITopologicalOperator2
Set pPolyline = pFeature.ShapeCopy
Set pTopoLogicalOp = pPolyline
Let pTopoLogicalOp.IsKnownSimple = False
If pTopoLogicalOp.IsSimple = False Then
' Polyline is self intersecting
End If
``````
-
doesn't work. pTopoLogicalOp.IsSimple is always true – Devdatta Tengshe Nov 20 '12 at 10:57
I played around a little bit more, and found that your code works in VBA, but the corresponding code in C# still gives IsSimple as true. There are also quite a few issues mentioned with ITopologicalOperator, so we'll have to chalk it down to that. – Devdatta Tengshe Nov 20 '12 at 11:38

The Polyline should be converted to a Polygon, and then the ITopologicalOperator3:get_IsSimpleEx() method can be used

Here is the sample code

``````private bool IsLineSelfIntersecting(IPolyline line)
{
bool isSelfIntersecting = false;

IPointCollection polygonVertices = new PolygonClass();
IPointCollection lineVertices = line as IPointCollection;

ITopologicalOperator3 topo = polygonVertices as ITopologicalOperator3;
esriNonSimpleReasonEnum reason =esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections;
topo.IsKnownSimple_2 = false;
if (!topo.get_IsSimpleEx(out reason))
{
if (reason == esriNonSimpleReasonEnum.esriNonSimpleSelfIntersections)
{
isSelfIntersecting = true;
}
}

return isSelfIntersecting;
}
``````
-