4

I'm trying to index geojson data using elasticsearch.

There is some kind of polygons that causing an error: "Points of LinearRing do not form a closed linestring".

Looks like it happens because these polygons have intersection in one point: enter image description here intersection

Is there some way to index such polygons or fix it programmatically?

3
  • There isn't anything to fix here -- Inversions are a valid part of a polygon geometry. It would only be invalid if the ring crossed over the gap at the location where the ring touches itself. If you were to "repair" his "hole", the polygon assembly code should reintroduce an inversion in lieu of an interior ring. See also this and this
    – Vince
    Jul 7, 2015 at 18:08
  • Is there a vertex in the left side segment where the right hand peak seems to touch the ring? If not then the computation may give a result showing that the peak vertex is not on the line but slightly inside.
    – user30184
    Jul 7, 2015 at 21:20
  • It is not so clear if a polygon with outer ring touching itself at one point is valid or not: springer.com/cda/content/document/cda_downloaddocument/…
    – user30184
    Jul 7, 2015 at 21:47

1 Answer 1

4

Here is a simple polygon that is touching its own ring at one point:

enter image description here

WKT of the polygon is

POLYGON ((76 224, 84 224, 84 216, 78 216, 78 221, 79 219, 79 223, 78 221, 76 224))

PostGIS reports that this polygon is not valid:

select ST_IsValid(ST_GeomFromText('
POLYGON ((76 224, 84 224, 84 216, 78 216, 78 221, 79 219, 79 223, 78 221, 76 224))
'))
===
f

The ST_MakeValid function corrects the polygon by converting the "inversion" into a hole that is touching the outer ring:

select ST_AsText(ST_MakeValid(ST_GeomFromText('
POLYGON ((76 224, 84 224, 84 216, 78 216, 78 221, 79 219, 79 223, 78 221, 76 224))')))

"POLYGON((76 224,84 224,84 216,78 216,78 221,76 224),(78 221,79 219,79 223,78 221))"

If you were using PostGIS the answer to your question would be to use ST_MakeValid. Correcting shapefiles should be possible with ogr2ogr and sqlite dialect but only if GDAL is compiled with most recent Spatialite which supports ST_MakeValid taken from LWGEOM library https://www.gaia-gis.it/gaia-sins/spatialite-sql-latest.html. Usage would be then

ogr2ogr -f "ESRI shapefile" corrected.shp input.shp -dialect sqlite -sql "SELECT ST_MakeValid(geometry), attribute1, attribute2, ... FROM input"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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