我正在使用 geodjango 并在我的数据库中有一组点。要获取某个区域内的点查询集,我使用以下命令:
queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m))
我的问题是我如何才能从我经过的点返回一个点(距离最短的点)?
编辑
我应该提到我正在传递坐标并想Point
用它们创建一个对象。然后将该点作为原点传递并对其进行过滤。例如我试过:
from spots.models import *
from django.contrib.gis.geos import *
origin = Point(28.011030, -26.029430)
distance_m = 1000
queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m))
for q in queryset:
print q.distance
这段代码给了我这个错误:
Traceback (most recent call last):
File "<console>", line 2, in <module>
AttributeError: 'Spot' object has no attribute 'distance'
有趣的是,如果我执行以下操作:
origin = Spot.objects.get(name='Montecasino').point
distance_m = 1000
for city in Spot.objects.distance(origin):
print(city.name, city.distance)
(u'Design Quarter Parking', Distance(m=677.347841801))
(u'Montecasino', Distance(m=0.0))
(u'Fourways', Distance(m=1080.67723755))