如何使用geodjango从一个点的最低距离返回一条记录? [英] How to return a record with the lowest distance from a point using geodjango?

查看:117
本文介绍了如何使用geodjango从一个点的最低距离返回一条记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用geodjango并在我的数据库中收集积分。要获得一定区域内的积分,请使用以下内容:

  queryset = Spot.objects.filter(point__distance_lte = ,distance_m))

我的问题是如何只返回一个点(与最低距离)从我过去的那一点起?



编辑


$ b $我应该提到我正在坐标坐标,并希望与他们一起创建一个 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

此代码段的代码给我这个错误:

 追溯(最近的最后一次呼叫):
文件<控制台& ,第2行,< module>
AttributeError:'Spot'对象没有属性'distance'

有趣的是,如果我做以下:

  origin = Spot.objects.get(name ='Montecasino')。point 
distance_m = 1000

Spot.objects.distance(origin)中的城市
print(city.name,city.distance)

(u'Design Quarter Parking'距离(m = 677.347841801))
(u'Montecasino',距离(m = 0.0))
(u'Fourways',距离(m = 1080.67723755))


解决方案

最后一个从 GeoDjango距离过滤器,距离值存储在模型中 - 查询,导致我 post 。根据这些信息,我可以收集您在距离查询中必须 measure参数
您将在下面的代码段中看到,导入度量为D 。然后在查询中使用它。
如果你没有指定,你会得到这个错误:

  ValueError:`distance_lte`查询所需的元组类型。 

要使用我使用的最低距离的点 order_by('distance ')[:1] [0]

  from spots.models import * 
来自django.contrib.gis.geos import *
from django.contrib.gis.measure import D

distance_m = 20000
origin = Point(28.011030,-26.029430)

nearest_spot = Spot.objects.filter(point__distance_lte =(origin,D(m = distance_m)))。distance(origin).order_by('distance')[:1] [0]


I am using geodjango and have a collection of points in my database. To get a queryset of points within a certain area I use this:

queryset = Spot.objects.filter(point__distance_lte=(origin, distance_m))

My question is how can I return only one point(the point with the lowest distance)from the point I have passed it?

EDIT

I should mention that I am passing in coordinates and wanting to create a Point object with them. Then pass that point in as the origin and filter against that. For example I have tried:

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

This snippet of code gives me this error:

Traceback (most recent call last):
  File "<console>", line 2, in <module>
AttributeError: 'Spot' object has no attribute 'distance'

Interestingly enough if I do the following:

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))

解决方案

Finally a solution gathered from clues from GeoDjango distance filter with distance value stored within model - query which lead me to this post. From this information I was able to gather that you MUST SPECIFY the measure parameter in your distance query. You will see in the below snippet, I import measure as D. Then use it in the query. If you don't specify it you will get this error:

ValueError: Tuple required for `distance_lte` lookup type.

To take just the point with the lowest distance I used order_by('distance')[:1][0]

from spots.models import *
from django.contrib.gis.geos import *
from django.contrib.gis.measure import D

distance_m = 20000
origin = Point(28.011030, -26.029430)

closest_spot = Spot.objects.filter(point__distance_lte=(origin, D(m=distance_m))).distance(origin).order_by('distance')[:1][0]

这篇关于如何使用geodjango从一个点的最低距离返回一条记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆