3

My models.py has User and Business models that have this field:

location = PointField(geography=True)

I'm getting Google Maps coordinates (in EPSG 4326) via Geocode service from an address which the user specifies.
Then I'm saving it in the above field (also EPSG 4326).

Now, what I want is to get all Business objects within a specified radius (of 1km for example), based on the user location:

Business.gis.filter(location__distance_lt=(request.user.location, D(km=1)))

But it doesn't work, it gives me this error:

ValueError: SpatiaLite does not support distance queries on geometry fields with a geodetic coordinate system. Distance objects; use a numeric value of your distance in degrees instead.

So, does anyone know how to properly solve this? Thanks in advance!

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
softzer0
  • 445
  • 2
  • 7
  • 25
  • I resorted to converting it to projected system before doing query. It works that way, although it's not so precise but it my case that's not a concern. – softzer0 Dec 09 '16 at 15:33
  • Hey @MikiSoft, I posted a bit of a late answer, have a look. – John Moutafis Aug 29 '17 at 15:17
  • @JohnMoutafis Hey, I solved it long time ago in the way I described above, but I've marked your answer as solution nevertheless to thank you for the effort. :) – softzer0 Aug 30 '17 at 21:01

1 Answers1

0

Theory:

This is a pretty obscure error related to those two:

Since you are using geodetic coordinate systems (like the EPSG 4326) you need to provide the distance in degrees.

Here is a very good explanation on how to transform km to degrees accurately enough: How do I convert kilometres to degrees in Geodjango/GEOS?

Finally I would suggest to use the dwithin.


Praxis:

  • 1km ~= 0.008 deg
  • The query now should look:

    Business.gis.filter(location__dwithin=(request.user.location, 0.008))
    
John Moutafis
  • 22,254
  • 11
  • 68
  • 112