Geo shape self-intersection

I have the following feature:

{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [[
[77.500, 13.500],
[77.600, 13.500],
[77.600, 13.400],
[77.590, 13.400],
[77.590, 13.390],
[77.600, 13.390],
[77.600, 13.400],
[77.700, 13.400],
[77.700, 13.000],
[77.500, 13.000],
[77.500, 13.500]
]] } }

It should be valid and it has no intersection. However, I get the following error from ES:

"error": {
        "root_cause": [
            {
                "type": "mapper_parsing_exception",
                "reason": "failed to parse field [geometry] of type [geo_shape]"
            }
        ],
        "type": "mapper_parsing_exception",
        "reason": "failed to parse field [geometry] of type [geo_shape]",
        "caused_by": {
            "type": "invalid_shape_exception",
            "reason": "Self-intersection at or near point [77.6,13.4,0.0]"
        }
    }

There is a hole in the figure, but I have an example with a hole that is parsed by ES correctly:

{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [[
[77.500, 13.500],
[77.550, 13.500],
[77.530, 13.470],
[77.570, 13.470],
[77.550, 13.500],
[77.600, 13.500],
[77.600, 13.400],
[77.500, 13.400],
[77.500, 13.500]
]] } }

Could someone explain what is the issue with the first feature?

I think this is actually a bug, but in the opposite way of your thinking.

Both polygons are illegal following OGC specification (e.g. exterior ring cannot have two equal non-consecutive points). Here is a good explanation of illegal polygons, and your polygons just lie on the Ring Self Intersection case:

https://knowledge.safe.com/articles/21674/invalid-ogc-geometry-examples.html

Therefore the error in the first polygon is correct but the processing successfully of the second it is not. It actually does not build the polygon you are expecting so there is the bug. Would you mind open an issue in the GitHub repository?

I have come across the same problem with the following query to the /<index_name>/_validate/query endpoint:

{
	"query": {
		"bool": {
			"filter": {
				"geo_shape": {
					"coordinates": {
						"shape": {
							"type": "polygon",
							"coordinates": [
							    [
								 [-72.2788918, 42.9274099],
								 [-72.2795677, 42.9267657],
								 [-72.2787201, 42.9268521],
								 [-72.2794497, 42.9272763],
								 [-72.2788918, 42.9274099]
							    ]
							]
						}
					}
				}
			}
		}
	}
}

The described polygon is self-intersecting as shown here:

I have tested this with many additional self-intersecting ones and the results are always the same.

The last version of ES I had access to try it on was 5.4.2.

If nothing comes of this thread, I will open an issue on Github (I can see a similar issue, though not exactly the same)

Thank you, Ignacio, you are right! Also, I've found a way to check the geometry internally and fix it on the fly. I'll post the bug to GitHub.

import ogr

polygon = """
{ "type": "Polygon", "coordinates": [[
[77.500, 13.500],
[77.550, 13.500],
[77.530, 13.470],
[77.570, 13.470],
[77.550, 13.500],
[77.600, 13.500],
[77.600, 13.400],
[77.500, 13.400],
[77.500, 13.500]
]] }"""

geom = ogr.CreateGeometryFromJson(polygon)
print(geom.IsValid())
geom = geom.MakeValid()
print(geom.IsValid())

To keep it linked:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.