问题描述
我的models.py
的用户和业务模型具有以下字段:
My models.py
has User and Business models that have this field:
location = PointField(geography=True)
我正在通过Geocode服务从用户指定的地址获取Google Maps坐标(在 EPSG 4326 中).
然后,将其保存在上述字段中(也是 EPSG 4326 ).
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).
现在,我要根据用户位置获取指定半径(例如1公里)内的所有Business对象:
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:
那么,有人知道如何正确解决这个问题吗?预先感谢!
So, does anyone know how to properly solve this? Thanks in advance!
推荐答案
理论:
这是与这两个有关的非常模糊的错误:
This is a pretty obscure error related to those two:
- GeoDjango dwithin errors when using django.contrib.gis.measure.D
- Geodjango distance query not retrieving correct results
由于您使用的是大地坐标系(例如 EPSG 4326 ),因此需要以度为单位提供距离.
Since you are using geodetic coordinate systems (like the EPSG 4326) you need to provide the distance in degrees.
关于如何足够精确地将km转换为度,这是一个很好的解释:
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?
最后,我建议使用 dwithin
.
Finally I would suggest to use the dwithin
.
实践:
-
1km ~= 0.008 deg
-
查询现在应显示为:
1km ~= 0.008 deg
The query now should look:
Business.gis.filter(location__dwithin=(request.user.location, 0.008))
这篇关于GeoDjango& SpatiaLite-过滤附近的物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!