# Polygon rejected as self-intersecting, but it does not self-intersect

**URL:** <https://discuss.elastic.co/t/polygon-rejected-as-self-intersecting-but-it-does-not-self-intersect/368714>\
**Category:** Elasticsearch\
**Created:** [October 12, 2024, 4:19pm UTC](https://discuss.elastic.co/t/polygon-rejected-as-self-intersecting-but-it-does-not-self-intersect/368714 "2024-10-12T16:19:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesdiacono](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jamesdiacono/32/138316_2.png) [@jamesdiacono](https://discuss.elastic.co/u/jamesdiacono)\
**Post date:** [October 12, 2024, 4:19pm UTC](https://discuss.elastic.co/t/polygon-rejected-as-self-intersecting-but-it-does-not-self-intersect/368714/1 "2024-10-12T16:19:02Z")

</div>

Elasticsearch 8.15.2 is refusing to index a polygon which, as far as I can tell, is perfectly valid. Here is an image of the polygon on a map. Notice that it is just a trapezoid with nothing funny going on, and it does not cross the anti-meridian.

![image](https://us1.discourse-cdn.com/elastic/original/3X/9/2/9234ad6b36ccb2673aec86d30122df0b492cf1da.png)

To reproduce, run the following cURL commands.

```auto
curl "localhost:9200/self_intersection?pretty" -X PUT --json '{
    "mappings": {"properties": {"shape": {"type": "geo_shape"}}}
}'
curl "localhost:9200/self_intersection/_doc/?pretty" --json '{
    "shape": {
        "type": "Polygon",
        "coordinates": [
            [
                [-30, 50],
                [160, 50],
                [160, 0],
                [0, 0],
                [-30, 50]
            ]
        ]
    }
}'
curl -X DELETE "localhost:9200/self_intersection"

```

The second cURL call produces this error:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "document_parsing_exception",
        "reason" : "[13:5] failed to parse field [shape] of type [geo_shape]"
      }
    ],
    "type" : "document_parsing_exception",
    "reason" : "[13:5] failed to parse field [shape] of type [geo_shape]",
    "caused_by" : {
      "type" : "illegal_argument_exception",
      "reason" : "Polygon self-intersection at lat=24.24242424242424 lon=160.0"
    }
  },
  "status" : 400
}

```

Is this a bug in Elasticsearch?

---

<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:** [October 13, 2024, 7:11am UTC](https://discuss.elastic.co/t/polygon-rejected-as-self-intersecting-but-it-does-not-self-intersect/368714/2 "2024-10-13T07:11:11Z")

</div>

The problem is the orientation of the geojson polygon. This is what Elasticsearch is trying to do:

Your polygon is wide as it expands more than half of the world (max longitude - min longitude \> 180) and therefore Elasticsearch needs to decide if the polygon crosses the dateline or not.

For doing so, it uses the orientation of the polygon's coordinates. In case of Geojson, the [standard](https://datatracker.ietf.org/doc/html/rfc7946) is very clear about it:

```auto
Polygon rings MUST follow the right-hand rule for orientation
(counterclockwise external rings, clockwise internal rings).

```

And therefore Elasticsearch tries to build a CCW polygon. The coordinates of your polygon are in clockwise order, therefore Elasticsearch tries to build the polygon across the dateline but it fails because in that case the polygon is self-intersecting.

If you build the polygon with points in counterclockwise order, then it succeeds:

```auto
curl "localhost:9200/self_intersection/_doc/?pretty" --json '{
    "shape": {
        "type": "Polygon",
        "coordinates": [
            [
                [-30, 50],
                [0, 0],
                [160, 0],
                [160, 50],
                [-30, 50]
            ]
        ]
    }
}'

```

In summary, if you want to index wide Geojson polygons, then you need to make sure the coordinates are in counterclockwise order.

---

<div class="post-metadata">

**Author:** ![jamesdiacono](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jamesdiacono/32/138316_2.png) [@jamesdiacono](https://discuss.elastic.co/u/jamesdiacono)\
**Post date:** [October 13, 2024, 8:54am UTC](https://discuss.elastic.co/t/polygon-rejected-as-self-intersecting-but-it-does-not-self-intersect/368714/3 "2024-10-13T08:54:32Z")

</div>

I can't believe I missed that. Thanks very much!
