gpt4 book ai didi

django - 按距离查询注释以在 Django 1.11 中使用 postgis 查找最近的距离似乎不正确

转载 作者:行者123 更新时间:2023-11-29 11:37:18 25 4
gpt4 key购买 nike

这是我的模型:

class Coast(TimeStampedModel):
position = PointField(null=True, srid=4326)
shape_id = models.IntegerField(null=True)
state = models.CharField(max_length=4, null=True, blank=True, choices=US_STATES)
point_type = models.CharField(max_length=25, choices=cc.POINT_TYPES, default=cc.POINT_TYPES.COAST, db_index=True)
interpolated = models.BooleanField(default=False)

这是我正在运行的代码:

def get_distances(current_address):
return (models.Coast.objects
.filter(point_type='COAST', position__distance_lte=(current_address, D(m=10000)))
.annotate(distance=Distance("position", current_address))
.order_by("distance"))

current_address = Point(x=26.3960373, y=-80.0755588, srid=4326)
distances = get_distances(current_address)
for dd in distances[0:10]:
print 'distance:', dd.distance.m, current_address.distance(dd.position)

这是输出:

distance: 1090.96923342 2.9609226587
distance: 1091.50068042 1.80716304192
distance: 1120.14014531 1.31372843777
distance: 1129.40394238 3.86868793486
distance: 1153.68196704 1.1093495446
distance: 1193.25809435 5.09212206614
distance: 1217.1136617 1.16301011591
distance: 1231.50011729 5.09984181242
distance: 1251.77367236 6.0540028636
distance: 1301.92838504 6.5265317962

我想弄清楚为什么要过滤查询集并按距离排序

.order_by('distance')

返回的顺序与手动计算两点之间的距离不同?

current_address.distance(dd.position)

我正在尝试找到离当前地址最近的点。

最佳答案

还需要点类的信息

需要查看 Point.distance 应该是一个函数吗?

关于django - 按距离查询注释以在 Django 1.11 中使用 postgis 查找最近的距离似乎不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53165009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com