Returns the rows that have locations within the specified box, defined by latitude, longitude corners

Works with the following datatypes:

This function works with 2.1 endpoint(s). What does this mean? »

The within_polygon(...) function is used in the $where parameter filter for Point , Line , and Polygon values within a polygon defined by a sequence points. It accepts two parameters:

  • The field name of a Location or Point
  • A multipolygon in Well-Known Text format

Well-Known Text is a standard way of encoding geospatial data in a textual manner, and is more compact than GeoJSON:

MULTIPOLYGON ((-87.637714 41.887275, -87.613681 41.886892, -87.625526 41.871555, -87.637714 41.887275))

Heads up! Contrary to the normal convention of "latitude, longitude" ordering in the coordinates property, Well-known text orders the coordinates as "longitude, latitude" (X coordinate, Y coordinate), as other GIS coordinate systems are encoded.

For example, to query for all of the Chicago 311 service requests for vacant and abandoned buildings within that polygon:

https://data.cityofchicago.org/resource/yama-9had.json?$where=within_polygon(location, 'MULTIPOLYGON (((-87.637714 41.887275, -87.613681 41.886892, -87.625526 41.871555, -87.637714 41.887275)))')

Cool Tool! Formatting shapes as WKT for testing is tedious and error-prone - I don't recommend it. Instead, use a library that can generate them for you, or a tool like Wicket to draw them in your browser.