Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Understanding GeoJSON polygons for geopoint queries in Sanity.io

3 replies
Last updated: Feb 1, 2022
After reading the docs I am not sure I fully understand regions for geopoint queries. The placeholder name is geopolygon, so are we just doing four points that are themselves sets of lat/long?
Jan 31, 2022, 8:18 PM
Hi User. The polygon type comes from the GeoJSON spec. For example, here is a very crudely drawn polygon around London:

{
  "type": "Polygon",
  "coordinates": [
    [
      [
        -0.479747,
        51.652658
      ],
      [
        0.441217,
        51.698629
      ],
      [
        0.309875,
        51.337763
      ],
      [
        -0.5435,
        51.366871
      ],
      [
        -0.479747,
        51.652658
      ]
    ]
  ]
}
In GROQ, you can wrap this in the
geo()
function to construct a polygon Sanity understands.
Here is an example GROQ query to find places in the London polygon:


*[_type == "place" && geo::contains(
  geo({
    "type": "Polygon",
    "coordinates": [
      [
        [
          -0.479747,
          51.652658
        ],
        [
          0.441217,
          51.698629
        ],
        [
          0.309875,
          51.337763
        ],
        [
          -0.5435,
          51.366871
        ],
        [
          -0.479747,
          51.652658
        ]
      ]
    ]
  }),
  geo::latLng(lat, lng)
)]{
  name
}
You can try this out in the Sanity Vision plugin with test data, like this:


[
  {
    "_type": "place",
    "name": "Tate Modern",
    "lat": 51.507621,
    "lng": -0.098852
  },
  {
    "_type": "place",
    "name": "Louvre",
    "lat": 48.861598,
    "lng": 2.335093
  }
][_type == "place" && geo::contains(
  geo({
    "type": "Polygon",
    "coordinates": [
      [
        [
          -0.479747,
          51.652658
        ],
        [
          0.441217,
          51.698629
        ],
        [
          0.309875,
          51.337763
        ],
        [
          -0.5435,
          51.366871
        ],
        [
          -0.479747,
          51.652658
        ]
      ]
    ]
  }),
  geo::latLng(lat, lng)
)]{
  name
}
Here are two examples showing queries for places in London, and
not in London:
Feb 1, 2022, 10:46 AM
user E
Ah, I whiffed on connecting the dots to a spec like that in my head. That's so completely awesome. Sanity is like doing an Easter egg hunt at the plastic Easter egg factory. I really appreciate the thorough examples! (That goes for everyone here, actually)
Feb 1, 2022, 1:22 PM
No problem! The docs are a little lacking on the geo functions; I had to go hunting myself to find useful info. We should expand the docs, and something geo-related content would also be awesome to blog about/publish on Exchange .
Feb 1, 2022, 1:47 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?