# Geo shape self-intersection

**URL:** <https://discuss.elastic.co/t/geo-shape-self-intersection/242584>\
**Category:** Elasticsearch\
**Created:** [July 25, 2020, 3:00am UTC](https://discuss.elastic.co/t/geo-shape-self-intersection/242584 "2020-07-25T03:00:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DenisDeis](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@DenisDeis](https://discuss.elastic.co/u/DenisDeis)\
**Post date:** [July 25, 2020, 3:00am UTC](https://discuss.elastic.co/t/geo-shape-self-intersection/242584/1 "2020-07-25T03:00:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Ignacio\_Vera](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ignacio_vera/32/36674_2.png) [@Ignacio\_Vera](https://discuss.elastic.co/u/Ignacio_Vera)\
**Post date:** [July 27, 2020, 9:15am UTC](https://discuss.elastic.co/t/geo-shape-self-intersection/242584/2 "2020-07-27T09:15:40Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![kkolivas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kkolivas/32/73021_2.png) [@kkolivas](https://discuss.elastic.co/u/kkolivas)\
**Post date:** [July 29, 2020, 3:22pm UTC](https://discuss.elastic.co/t/geo-shape-self-intersection/242584/3 "2020-07-29T15:22:10Z")

</div>

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

```auto
{
	"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:

 ![Screenshot 2020-07-29 at 5.34.50 PM](https://us1.discourse-cdn.com/elastic/original/3X/d/c/dc514dd7118d21244e81e8ca8153ba04ac508880.png)

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](https://github.com/elastic/elasticsearch/issues/59386), though not exactly the same)

---

<div class="post-metadata">

**Author:** ![DenisDeis](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@DenisDeis](https://discuss.elastic.co/u/DenisDeis)\
**Post date:** [July 31, 2020, 12:42am UTC](https://discuss.elastic.co/t/geo-shape-self-intersection/242584/4 "2020-07-31T00:42:56Z")

</div>

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())
```

---

<div class="post-metadata">

**Author:** ![DenisDeis](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@DenisDeis](https://discuss.elastic.co/u/DenisDeis)\
**Post date:** [July 31, 2020, 3:56am UTC](https://discuss.elastic.co/t/geo-shape-self-intersection/242584/5 "2020-07-31T03:56:13Z")

</div>

To keep it linked:

> <https://github.com/elastic/elasticsearch/issues/60495>
>
> Elasticsearch version (bin/elasticsearch --version):
> Version: 7.8.1
> Plugins installed: \[\]
> JVM version (java -version):
> JVM: 14.0.1
> OS version (uname -a if on a Unix-like system):
> Ubuntu 20.04.1 LTS
> Description...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 28, 2020, 3:56am UTC](https://discuss.elastic.co/t/geo-shape-self-intersection/242584/6 "2020-08-28T03:56:13Z")

</div>

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