How to check if latitude and longitude inside geo boundary in Django ?

Published 7 Dec, 2021


How do we actually check if a given place may be geo location coordinate inside a boundary in Django?

In my previous blog post I talked about how do we convert a place or zone into a bounding box and how do we efficiently store that inside a Django model. Right now I’m explaining how do we efficiently check if given latitude and longitude inside a bounding box. This is super helpful to check whether given location coordinates inside the zone or not. For example you may define a zone as San Francisco or New York and you have the location that needs to check if the location is really inside the geo region or not. Without further due let”s see some code in action.

from django.contrib.gis.geos import Point
point = Point(lat, lon)
Zone.objects.filter(geo__contains=point).all()

Above command will return all of the places that are in the geo boundary.

This article published under development on django tags. If you wish to receive email from me when I post a new blog post then please subscribe to my newsletter.


You might also like