GeoDjango distance queries returning incorrect results

I just got GeoDjango on my development machine. The problem is that I cannot get the distance request to work correctly. No matter which SRID I use, the distance results are completely disabled. Here is an example.

>>> from django.contrib.gis.measure import D >>> from app.models import Place >>> from django.contrib.gis.geos import Point >>> qs = Place.objects.all() >>> point = Point(-118, 34) >>> qs.filter(coordinates__distance_lte=(point, D(m=1))) [<Place: 7-Eleven>, <Place: Arthur Murray Dance Studio>, <Place: Costco>, <Place: AMC Century City 15>, <Place: 24 Hour Fitness>, <Place: Ralphs>, <Place: Houston Restaurant>, <Place: CVS/pharmacy>, <Place: Shaky Alibi>, <Place: Sephora>, <Place: Trader Joe's>] 

The problem is that these places are much farther than 1 m from the point .

I tried to play with him, but I had no luck. Here is an example with a different SRID.

 >>> qs = Place.objects.all().transform(3786) >>> point = Point(-118, 34, srid=3786) >>> qs.filter(coordinates__distance_lte=(point, D(m=1))) [<Place: 7-Eleven>, <Place: Arthur Murray Dance Studio>, <Place: Costco>, <Place: AMC Century City 15>, <Place: 24 Hour Fitness>, <Place: Ralphs>, <Place: Houston Restaurant>, <Place: CVS/pharmacy>, <Place: Shaky Alibi>, <Place: Sephora>, <Place: Trader Joe's>] 

I have the feeling that I’m just choosing the wrong SRIDs, but not one of them that I worked with on the Internet, didn’t work or didn’t answer a single answer, which is even moderately useful.

Any help is much appreciated!

+7
source share
1 answer

I hate answering my question, but no one else approached the plate, and I understood the solution.

I switched to PostGIS to isolate the problem from either the location database itself or Django. Therefore, I converted the Django ORM request that I used above to the ideal request that I would expect to receive from a PostGIS server. To my surprise, the request that was made was equivalent:

 SELECT id FROM app_place WHERE ST_DWithin(coordinates, ST_SetSRID(ST_Point(-118, 34), 3768), 1); 

That was the problem. I needed a request:

 SELECT id FROM app_place WHERE ST_Distance_Sphere(ST_SetSRID(ST_Point(-118, 34), 3768), coordinates) < 1; 

I read the source of Django to find out what is going on here, and I realized that I set geography=True in the coordinates field. This seems to modify the PostGIS functions created by Django when creating the SQL query. This is undocumented, but here is the relevant section .

In short, if you are having this problem, remove geography=True from your model and you should be good to go.

+8
source

All Articles