我正在使用Geodjango,并且数据库中有一些点的集合。为了获取在特定区域内的点的查询集,我使用以下代码:
queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m))
我的问题是如何只返回我已经通过的点中距离最近的那个点?
编辑
我应该提到,我正在传递坐标,并希望创建一个带有这些坐标的点对象。然后将该点作为原点传递并筛选。 例如,我尝试过:
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))
[:1][0]
吗? - Robert Johnstone