I would like to:

  1. Save pairs of srid 4326 (latitude, longitude) points in a PostGIS Geodjango 1.8 Database. (Maximum distance between the edge points: 2.000km)

  2. Given a srid 4326 (latitude, longitude) point, filter the closer entries in a radius of 500m.

My approach:


    class PointModel(geomodels.Model):
        point = geomodels.PointField(

        geoobjects = geomodels.GeoManager()
        objects = models.Manager()

Entering the data

>>> from django.contrib.gis.geos import Point
>>> latitude = float(...)
>>> longitude = float (...)
>>> entry_one = PointModel(point=Point(longitude, latitude))
>>> entry_one.save()
>>> entry_ten.save()

Performing the query:

>>> from django.contrib.gis.measure import D
>>> from django.contrib.gis.geos import GEOSGeometry
>>> pnt = GEOSGeometry('POINT(longitude latitude)', srid=4326)
>>> result = PointModel.geoobjects.filter(point__distance_lte=(pnt, D(m=500)))


  • Since the coordinates provided are geographic, I have set geography=True. Considering that this application can quickly get thousands of entries and queries, is this a good choice in terms of performance? Is there a more robust approach?

  • Entering the data, the given (latitude, longitude) point is entered as Point(longitude, latitude). If I entered all points as Point(latitude, longitude) would I have taken wrong results?

  • 1
    In response to question 1, yes I think this approach is probably fine. In response to question 2, yes, you would either get the wrong results, or an out of bounds error, depending on the coordinates involved. – Alexander Mar 1 '16 at 13:32
  • @alex Thank you for your answer. Would projecting the geographic coordinates to Cartesian be a premature optimization? – raratiru Mar 1 '16 at 19:03
  • 1
    It would depend on your needs, if you need good accuracy with distance queries and within-radius queries that cover a large distance then Geography type is probably best but you won't have quite so many spatial lookups available. You could easily test the speed differences between the types, I think a comparison of the two with different spatial queries would be very interesting. – Alexander Mar 2 '16 at 9:32
  • @alex I see your point. Theoretically, such a comparison is very interesting, I am not sure I will avoid it in the near future! However, I understand from your answer that the most "expensive" geography feature, suits best for accuracy in large distances. In my case, the maximum measured distances among the data can certainly be 2000 km but the "query of interest" will be searching among those data for distances of 0,5km - 1km: "Find the relevant entries of data in a radius of 750m from 'these' coordinates with accuracy of 20 meters". Would "geography" still worth the overhead? – raratiru Mar 3 '16 at 14:48
  • 1
    It's impossible to say without knowing your requirements, why not test it? It wouldn't take long to set up a typical use case and find out. If you do please share! – Alexander Mar 3 '16 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.