7

For a Google Maps application, I need to create a query that selects all items in my database that fall within a certain radius from a given latitude/longitude, given the lat/lng of each point. Is there an efficient way to do this in the Django ORM?

The best way I have come up with thus far is to select all points that will fall within a bounding square of that circle (with __range) and then call a iterative function on all the selected listings to determine whether they really fall in the circle.

My feeling is there might be a more efficient way - how would you do it?

Herman Schaaf
  • 46,821
  • 21
  • 100
  • 139

1 Answers1

9

Django has GeoDjango for this

http://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#distance-queries

Galen
  • 29,976
  • 9
  • 71
  • 89
  • Nice, that's definitely the best answer. Just for interest sake, any idea how you would solve it efficiently in plain Django? – Herman Schaaf Mar 31 '11 at 22:47
  • you could jsut use a raw query like this post http://stackoverflow.com/questions/1916953/filter-zipcodes-by-proximity-in-django-with-the-spherical-law-of-cosines – Galen Apr 01 '11 at 00:28
  • 1
    Is it possible to use GeoDjango's methos in a normal Django application? – Gocht Aug 07 '15 at 15:59