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've been looking for a function that can tell me if a LineString intersects itself. I've tried calling st_intersects with the same LineString twice, but obviously identical linestrings will intersect. My plan if there is no function would be to get all the points in the line string, and create individual linestrings and then check each new linestring against each other one with st_intersects. I do not want it to come to this, but I'm afraid it will.

So are there any PostGIS functions for checking if a linestring is self-intersecting? It should be similar to finding out whether a polygon is complex I'd assume

share|improve this question

migrated from stackoverflow.com Oct 31 '11 at 2:40

This question came from our site for professional and enthusiast programmers.

    
There was a second answer here that didn't quite help me PostGIS wise, but it did contain a link to some helpful information on the problem as a whole. It seems that it has been deleted. Does anyone have that link? –  Jeff Oct 31 '11 at 17:26
add comment

1 Answer 1

up vote 9 down vote accepted

You can test for a self-intersecting linestring with ST_IsSimple(geom):

SELECT ST_IsSimple('LINESTRING (50 50, 150 150, 50 150, 150 50)');
 st_issimple
-------------
 f
(1 row)

Self-intersection at POINT ( 100.0 100.0 )

Above image and below caption are from JTS TestBuilder (click "Simple?")

Self-intersection at POINT ( 100.0 100.0 )

This can be fixed with ST_UnaryUnion(geom) (since PostGIS 2.0), which returns a valid/simple three piece multilinestring:

MULTILINESTRING((50 50, 100 100), 
  (100 100, 150 150, 50 150, 100 100), 
  (100 100, 150 50))
share|improve this answer
    
This is perfect! I knew the answer had to be so simple. You said you used JTS Testbuilder to create the image. Is there any way you know of in Postgis to get the point/points of intersection? –  Jeff Oct 31 '11 at 11:19
    
That's the challenging detail. JTS TestBuilder has CheckNoding, but this only finds one node coordinate per call, rather than a MULTIPOINT. I'm not sure exactly sure how to get the noding point(s) with PostGIS. –  Mike T Oct 31 '11 at 17:23
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.