# Finding a geo point inside a geoshape rectangle

**URL:** <https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [October 21, 2022, 2:28am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160 "2022-10-21T02:28:22Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonpious](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antonpious/32/31885_2.png) [@antonpious](https://discuss.elastic.co/u/antonpious)\
**Post date:** [October 21, 2022, 2:28am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/1 "2022-10-21T02:28:22Z")

</div>

All the documentation seems to point to either having geo points and then having a bounding box to select these geo points.  
Is the reverse feature possible in Elastic Search?  
The only closest I could find in this form is  
[Geo Center and Rectangle Box](https://discuss.elastic.co/t/please-help-with-querying-geospatial-data-geopoint-and-geoshape-with-nest-api/311546)  
Where is the contains keyword in elastic?

I have a mapping which says its a geo shape

```auto
"geoLocationBox": {
	"type": "geo_shape"
}

```

I am inserting the data

```auto
	"name": "wanted record",
	"geoLocationBox": {
		"type": "envelope",
		"coordinates": [
			[
				18.2503,
				-66.6593
			],
			[
				18.1119,
				-66.8364
			]
			
		]

```

How to get this record given a geopoint which is within this box?

```auto
"geoLocationPoint": {
	"lat": 18.1779,
	"lon": -66.7644
}

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 21, 2022, 2:33am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/2 "2022-10-21T02:33:58Z")

</div>

Hi @antonpious

I am struggling a bit to follow.

Is this the use case?

Given a point return all polygons that the given point is contained within?

Is that what you are asking?

Do you have an index with various geo\_shape(s) and you want to provide a point and return all the shapes that contain the point

---

<div class="post-metadata">

**Author:** ![antonpious](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antonpious/32/31885_2.png) [@antonpious](https://discuss.elastic.co/u/antonpious)\
**Post date:** [October 21, 2022, 3:43am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/3 "2022-10-21T03:43:11Z")

</div>

Yes. your understanding is correct.

The polygon is a rectangle. So each record would have a rectangle stating the top left and bottom right co-ordinate.

Now the query would get only a geo point.  
The goal is to find all records where this point is contained within these rectangles.

---

<div class="post-metadata">

**Author:** ![antonpious](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antonpious/32/31885_2.png) [@antonpious](https://discuss.elastic.co/u/antonpious)\
**Post date:** [October 21, 2022, 4:27am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/4 "2022-10-21T04:27:04Z")

</div>

I guess you don't have this feature... I might as well convert this field into an object with min max of latitude and longitude and then check the point is within these values as described in this thread

> <https://stackoverflow.com/questions/30378755/determine-if-a-geopoint-fits-inside-a-rectangle>

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 21, 2022, 4:56am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/5 "2022-10-21T04:56:28Z")

</div>

So odd I thought we did.. I have been experimenting... will keep trying...

Yes certainly I think do a range query... but I thought a `geo_shape` query would work but no luck so far!

---

<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 21, 2022, 5:19am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/6 "2022-10-21T05:19:11Z")

</div>

I think the issue here is the understanding on how to define an Envelope. From the documentation the coordinates needs to be provided in the format `[[minLon, maxLat], [maxLon, minLat]]`.

Therefore your envelope example has the following coordinates:

```auto
minLon=18.2503
maxLon=18.1119
minLat=-66.6593
maxLat=-66.8364

```

Note that the minLat \> maxLAt, therefore this is an envelope that crosses the dateline, so an inside point would be for example:

```auto
GET my_index/_search
{
  "query": {
    "geo_shape": {
      "geoLocationBox": {
        "shape": {
          "type": "Point",
          "coordinates": [180.0, -66.7644]
        }
      }
    }
  }
}

```

Hope this helps.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 21, 2022, 5:30am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/7 "2022-10-21T05:30:55Z")

</div>

Hi @Ignacio_Vera

Can you tell me why this does not work... I tried with relationships `contains`, `within` and `intersects`

```auto
DELETE discuss-shape

PUT discuss-shape/
{
  "mappings": {
    "properties": {
      "name" : {"type": "keyword"},
      "geoLocationBox": {"type": "geo_shape"}
    }
  }
}

POST discuss-shape/_doc
{
  "name": "10x10 Box",
  "geoLocationBox": {
    "type": "envelope",
    "coordinates": [
      [
        10.0,
        10.0
      ],
      [
        -10.0,
        -10.0
      ]
    ]
  }
}

POST discuss-shape/_doc
{
  "name": "5x5 Box",
  "geoLocationBox": {
    "type": "envelope",
    "coordinates": [
      [
        5.0,
        5.0
      ],
      [
        -5.0,
        -5.0
      ]
    ]
  }
}

POST discuss-shape/_doc
{
  "name": "5x5 Offset Box",
  "geoLocationBox": {
    "type": "envelope",
    "coordinates": [
      [
        10.0,
        10.0
      ],
      [
        5.0,
        5.0
      ]
    ]
  }
}

POST discuss-shape/_search
{
  "query": {
    "geo_shape": {
      "geoLocationBox": {
        "relation": "contains",
        "shape": {
          "coordinates": [0.0, 0.0],
          "type": "point"
        }
      }
    }
  }
}

#results

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }
}

```

I even put these on the maps so I could see it would seem / I would expect the query above with the 0.0, 0.0 point should return the 2 centered boxes but it does not .... I can not get it too work...

 ![Screen Shot 2022-10-20 at 10.30.05 PM](https://us1.discourse-cdn.com/elastic/original/3X/7/5/75174be0fdf274d6ed236de214ca9847a10d48ee.jpeg)

1.0, 1.0 does not work either etc

```auto
POST discuss-shape/_search
{
  "query": {
    "geo_shape": {
      "geoLocationBox": {
        "relation": "contains",
        "shape": {
          "coordinates": [1.0, 1.0],
          "type": "point"
        }
      }
    }
  }
}

# Results
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }
}

```

---

<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 21, 2022, 5:40am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/8 "2022-10-21T05:40:11Z")

</div>

Remember the format: `[[minLon, maxLat], [maxLon, minLat]]`

So your envelope:

```auto
{
    "type": "envelope",
    "coordinates": [
      [
        10.0,
        10.0
      ],
      [
        -10.0,
        -10.0
      ]
    ]
  }

```

contains minLon \> maxLon, therefore crosses the dateline (it is not the shape you have drawn). Try swapping the longitudes:

```auto
{
    "type": "envelope",
    "coordinates": [
      [
        -10.0,
        10.0
      ],
      [
        10.0,
        -10.0
      ]
    ]
  }

```

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 21, 2022, 5:40am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/9 "2022-10-21T05:40:39Z")

</div>

Ohhhhhh I see... YAY!!!!!

@Ignacio_Vera It is weird the malformed envelopes still show up on the map... and still ingest ... seems like some validation is in order 😉

But now it Works when I define the envelopes correct!!!

@antonpious You should be able to do this..

```auto
DELETE discuss-shape

PUT discuss-shape/
{
  "mappings": {
    "properties": {
      "name" : {"type": "keyword"},
      "geoLocationBox": {"type": "geo_shape"}
    }
  }
}

POST discuss-shape/_doc
{
  "name": "10x10 Box Other",
  "geoLocationBox": {
    "type": "envelope",
    "coordinates": [
      [
        -10.0,
        10.0
      ],
      [
        10.0,
        -10.0
      ]
    ]
  }
}

POST discuss-shape/_doc
{
  "name": "5x5 Box",
  "geoLocationBox": {
    "type": "envelope",
    "coordinates": [
      [
        -5.0,
        5.0
      ],
      [
        5.0,
        -5.0
      ]
    ]
  }
}

POST discuss-shape/_doc
{
  "name": "5x5 Offset Box",
  "geoLocationBox": {
    "type": "envelope",
    "coordinates": [
      [
        5.0,
        10.0
      ],
      [
        10.0,
        5.0
      ]
    ]
  }
}

POST discuss-shape/_search
{
  "query": {
    "geo_shape": {
      "geoLocationBox": {
        "relation": "intersects",
        "shape": {
          "coordinates": [0.0, 0.0],
          "type": "point"
        }
      }
    }
  }
}

# Correct Results!!!

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0,
    "hits": [
      {
        "_index": "discuss-shape",
        "_id": "8HoK-YMBWq97sHQ4avvA",
        "_score": 0,
        "_source": {
          "name": "10x10 Box Other",
          "geoLocationBox": {
            "type": "envelope",
            "coordinates": [
              [
                -10,
                10
              ],
              [
                10,
                -10
              ]
            ]
          }
        }
      },
      {
        "_index": "discuss-shape",
        "_id": "anoM-YMBWq97sHQ4dv38",
        "_score": 0,
        "_source": {
          "name": "5x5 Box",
          "geoLocationBox": {
            "type": "envelope",
            "coordinates": [
              [
                -5,
                5
              ],
              [
                5,
                -5
              ]
            ]
          }
        }
      }
    ]
  }
}

```

I KNEW we did this!!! 🙂

---

<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 21, 2022, 6:07am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/10 "2022-10-21T06:07:35Z")

</div>

I open: [Envelopes crossing the dateline are not handled properly in vector tiles API · Issue #91060 · elastic/elasticsearch · GitHub](https://github.com/elastic/elasticsearch/issues/91060)

I think there is a bug on how we handle those kind of envelopes in vector tiles.

---

<div class="post-metadata">

**Author:** ![antonpious](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antonpious/32/31885_2.png) [@antonpious](https://discuss.elastic.co/u/antonpious)\
**Post date:** [October 22, 2022, 1:18am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/12 "2022-10-22T01:18:58Z")

</div>

Thanks @stephenb it works.  
The issue seems to be the order of the rectangle  
|latitude\_min|latitude\_max|longitude\_min|longitude\_max|

|18.1119|18.2503|-66.8364|-66.6593|

To `[[minLon, maxLat], [maxLon, minLat]]`  
This would give Left Top, Right Bottom

```auto
[
	-66.8364,
	18.2503
],
[
	-66.6593,
	18.1119
]

```

This was  
[[maxLon, maxLat], [minLon, minLat]] before which would give Right Top, Left Bottom

Thanks for the syntax correction too @Ignacio_Vera

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 22, 2022, 4:48am UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/13 "2022-10-22T04:48:17Z")

</div>

Just for everyone Envelope Definition [Here](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-shape.html#_envelope)

> ##### Envelope
> 
> Elasticsearch supports an `envelope` type, which consists of coordinates for upper left and lower right points of the shape to represent a bounding rectangle in the format `[[minLon, maxLat], [maxLon, minLat]]`:

---

<div class="post-metadata">

**Author:** ![antonpious](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antonpious/32/31885_2.png) [@antonpious](https://discuss.elastic.co/u/antonpious)\
**Post date:** [October 22, 2022, 2:40pm UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/14 "2022-10-22T14:40:39Z")

</div>

@stephenb is it possible to add this sample in this page [Geo Shape Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-shape-query.html) so that people know Elasticsearch have this feature.

---

<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:** [November 19, 2022, 2:40pm UTC](https://discuss.elastic.co/t/finding-a-geo-point-inside-a-geoshape-rectangle/317160/15 "2022-11-19T14:40:46Z")

</div>

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