3

I'm building an api to make distance lookups using Django 1.8, GeoDjango, Django REST Framework and PostGIS.

Locally I'm using PostGIS 2.1.7, my host (PythonAnywhere) uses 2.1.4... I'm not able to install an earlier version locally (not so strong with making and installing).

The problem is that locally an api request like http://localhost:8000/v1/test-entries/8.5513076999999988%2055.6684179999999955/ return nothing, as it should. But doing the same request (using the same coordinates) on the live version on PythonAnywhere return all entries in the DB. The code is the same both locally and on PA. The distance lookup code is like so:

return TestEntry.objects.filter(pnt__distance_lte=(pnt, 10))

The only difference I see is the PostGIS version, but it seems unlikely that such a basic functionality shouldn't work in an older version.

Does anyone have experience with these kinds of technologies and give me some hints to what could be wrong?

5
  • What's the definition of the table you're querying against? Is it geometry or geography? Please use ST_SRID() to check both have the same srid. Commented Aug 24, 2015 at 13:09
  • Hey, thanks for your comment. This is the online table http://pastebin.com/rzzfnXRz and this is the local table http://pastebin.com/WCRrFFKh. Using ST_SRID() in both databases returns st_srid 4326. And looking closer I can see now that the online table is geometry and the local table is geography. That might be the problem. :) I will try and fix this.
    – Gobias
    Commented Aug 24, 2015 at 13:53
  • The problem was the geometry field in the online environment, and additionally that I had forgotten to apply Django migrations. Thanks for pointing out how to investigate this.
    – Gobias
    Commented Aug 24, 2015 at 14:07
  • I've posted a short summary to wrap this up so others will understand what we're talking about too. Commented Aug 24, 2015 at 15:20
  • 1
    @Gobias. Please accept the answer if this has solved the problem. It helps to indicate that you found an answer and thanks the person who took time to help you. Commented Aug 25, 2015 at 5:59

1 Answer 1

4

To explain what happened in the comments above:

pnt__distance_lte according to the manual translates

pnt__distance_lte=(pnt, 10)

into

ST_Distance(poly, pnt) <= 10

ST_Distance in postgis behaves differently for geography and geometry:

For geometry type Returns the 2-dimensional cartesian minimum distance (based on spatial ref) between two geometries in projected units. For geography type defaults to return spheroidal minimum distance between two geographies in meters

The problem may lie either in one system operation on geom and the other on geog or both geom but with different SRIDs.

In the end one system was selecting objects in 10 meters radius and the other in 10 degrees radius (4326 units).

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.