# Failed to create valid geo\_shape

**URL:** https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975
**Category:** Elasticsearch
**Created:** [April 9, 2019, 7:52am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975 "2019-04-09T07:52:48Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 9, 2019, 7:52am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/1 "2019-04-09T07:52:48Z")

</div>

Appreciate if someone can shed some light on why ES complains about the seemingly valid geo\_shape I'm trying to create.

The question has been asked on SO (Discuss whined about the length of the body)

> <https://stackoverflow.com/questions/55587629/failed-to-create-valid-geo-shape>

Thank you very much in advance

---

<div class="post-metadata">

### Author: ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)
#### Post date: [April 9, 2019, 8:18am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/2 "2019-04-09T08:18:51Z")

</div>

The message is opaque, granted, but I think it will be a problem that this point is repeated so that this shape is self-intersecting.

```nohighlight
[-116.96965590466098, 32.65769347876227]

```

The shape is really an outline with a hole, and if you represent it as such a shape then it seems to index ok:

> <https://gist.github.com/DaveCTurner/d50d549f49bd380dcee7ef501a887da6>

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 9, 2019, 8:23am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/3 "2019-04-09T08:23:49Z")

</div>

Thanks @DavidTurner, I was going to update the post with another observation that goes in the same direction as yours, i.e. that all other polygons that also exhibit the same issue are concave polygons whose path contains two identical points (except the first and last) that are not following each other, which indeed creates a hole.

I wasn't aware of anything in the GeoJson spec that prevents self-intersecting polygons (at least nothing that [mapbox/geojsonhint](https://github.com/mapbox/geojsonhint) complains about), but that might explain the issue. I think the error message could be less opaque, indeed.

---

<div class="post-metadata">

### Author: ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)
#### Post date: [April 9, 2019, 8:45am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/4 "2019-04-09T08:45:35Z")

</div>

It's possible that this is permitted by GeoJSON. Nonetheless I think the response could be better so I opened [https://github.com/elastic/elasticsearch/issues/40998](https://github.com/elastic/elasticsearch/issues/40998).

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 9, 2019, 8:47am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/5 "2019-04-09T08:47:16Z")

</div>

Thanks @DavidTurner, much appreciated!

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 9, 2019, 11:36am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/6 "2019-04-09T11:36:57Z")

</div>

@DavidTurner here is another interesting one with another error message that seems intuitive at first, but isn't when you look at the shape:

```
PUT index/doc/1630656
{
  "parcel": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          -122.64482060784113,
          38.56711793465956
        ],
        [
          -122.64494911515231,
          38.56806649867188
        ],
        [
          -122.64646104699145,
          38.569267483716594
        ],
        [
          -122.64665226614191,
          38.56929059387256
        ],
        [
          -122.64662994320358,
          38.56928589940636
        ],
        [
          -122.64668841957221,
          38.5692949639317
        ],
        [
          -122.64665226614191,
          38.56929059387256
        ],
        [
          -122.64675499746104,
          38.56931222105709
        ],
        [
          -122.64687327352003,
          38.56935284573999
        ],
        [
          -122.64692832584761,
          38.56936744190402
        ],
        [
          -122.64698558991063,
          38.569375184019115
        ],
        [
          -122.64704369004176,
          38.569375886094306
        ],
        [
          -122.64710123048759,
          38.56936953126357
        ],
        [
          -122.6471568289413,
          38.56935627219081
        ],
        [
          -122.64724333535125,
          38.56934103930251
        ],
        [
          -122.64733109987546,
          38.569331195304905
        ],
        [
          -122.64741957872974,
          38.5693268011908
        ],
        [
          -122.64750822370347,
          38.5693278841858
        ],
        [
          -122.64759648555793,
          38.56933443757932
        ],
        [
          -122.64768381742691,
          38.569346420767744
        ],
        [
          -122.6477696782066,
          38.56936375950291
        ],
        [
          -122.64784262002463,
          38.56937339405759
        ],
        [
          -122.64791649753268,
          38.56937624952554
        ],
        [
          -122.64799029207013,
          38.56937228653462
        ],
        [
          -122.64806298612233,
          38.569361559728456
        ],
        [
          -122.64813357734766,
          38.56934421701336
        ],
        [
          -122.6481796239189,
          38.56932949668995
        ],
        [
          -122.64822706056755,
          38.56931780933501
        ],
        [
          -122.64827556379203,
          38.56930923465239
        ],
        [
          -122.64691737383271,
          38.56652470841123
        ],
        [
          -122.64482060784113,
          38.56711793465956
        ]
      ]
    ]
  }
} 

```

The shape looks like this (no intersection and shape is closed)

 ![25](https://us1.discourse-cdn.com/elastic/original/3X/9/e/9e3bd1503c94255412c236e730e7cd221da2e0d0.jpeg)

The error message now says:

```
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "failed to parse field [parcel] of type [geo_shape]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "failed to parse field [parcel] of type [geo_shape]",
    "caused_by": {
      "type": "illegal_argument_exception",
      "reason": "first and last points of the polygon must be the same (it must close itself): polyLats[0]=38.56806649867188 polyLats[4]=38.56935284573999"
    }
  },
  "status": 400
}

```

From all the points in the coordinates array, ES seems to have only kept 5 since it's comparing point 0 with point 4, which are in reality the second, respectively the ninth of the polygon array given in input.

Appreciate if you have any idea why this happens.

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 9, 2019, 11:44am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/7 "2019-04-09T11:44:14Z")

</div>

Never mind, again data quality seems to be responsible for this. There are overlapping points at the top right of the figure (points, 5, 6, 7), which might explain the issue.

---

<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: [April 9, 2019, 11:47am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/8 "2019-04-09T11:47:23Z")

</div>

yes, it seems there is a loop in one side:

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

The error is misleading because in those cases the logic tries to incorrectly divide the polygon into two components. One of the components becomes invalid.

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 9, 2019, 11:49am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/9 "2019-04-09T11:49:37Z")

</div>

Thank you @Ignacio_Vera, shall I file an issue to improve the message as David did for the earlier issue?

---

<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: [April 9, 2019, 11:50am UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/10 "2019-04-09T11:50:45Z")

</div>

If you don't mind, yes so we keep track of this issues.

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [April 9, 2019, 12:17pm UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/12 "2019-04-09T12:17:59Z")

</div>

Actually, it's the same type of shape as the very first one, i.e. a hole that touches the outline. Yet, the error message is different.

```
PUT index/doc/1
{
  "parcel": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          0,
          0
        ],
        [
          0,
          2
        ],
        [
          1,
          1.9
        ],
        [
          0.5,
          1.8
        ],
        [
          1.5,
          1.8
        ],
        [
          1,
          1.9
        ],
        [
          2,
          2
        ],
        [
          2,
          0
        ],
        [
          0,
          0
        ]
      ]
    ]
  }
}

```

 ![46](https://us1.discourse-cdn.com/elastic/original/3X/9/0/90797322b1059dadfefa6fe6eecfd4d26e8fea61.png)

---

<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: [May 7, 2019, 12:18pm UTC](https://discuss.elastic.co/t/failed-to-create-valid-geo-shape/175975/13 "2019-05-07T12:18:07Z")

</div>

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