0

I've been using PostGIS via GeoDjango (Django v3.0.7), happily until just now. I have a query (Django .filter()) that looks like this:

.filter(point__distance_lte=(dlPt, D(m=CloseRadius)))

when it invokes the PostgreSQL query I get this error:

django.db.utils.ProgrammingError: function ST_DistanceSphere(geometry, geometry) does not exist

dlPt is indeed a Point, <class 'django.contrib.gis.geos.point.Point'>, and so are the point attributes in the objects being searched.

Any ideas what the issue might be?

3
  • Have you created PostGIS extension in the database? Commented Sep 28, 2020 at 19:03
  • If created, what version of PostGIS are you using? Commented Sep 28, 2020 at 19:05
  • Looks like distance_lte uses ST_Distance_Sphere (note the _; not sure if typo, though), which is deprecated in PostGIS 3.x. Test with direct SQL; if that's the case, it's tivial to write a wrapper function. Also, if possible, use dwithin (it calls the index driven ST_DWithin, which is much faster).
    – geozelot
    Commented Sep 28, 2020 at 19:17

1 Answer 1

4

Function st_distancesphere is avaialble for PostGIS version 2.2 and greater. So make sure that while using PostGIS with Geodjango it has a version that is greater then 2.2, otherwise it won't be able to find it in the database.

https://postgis.net/docs/ST_DistanceSphere.html

And you can check your PostGIS version by using this query

SELECT PostGIS_Version();
2
  • yup, that was it! SELECT PostGIS_Version() returns: "2.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1". thanks for your help!
    – rikb
    Commented Sep 29, 2020 at 15:54
  • No problem! Glad to help! Commented Sep 29, 2020 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.