按距离查询注释以查找带有Postgis的Django 1.11中最接近的距离似乎不正确

时间:2018-11-06 02:30:35

标签: django postgresql postgis django-1.11

这是我的模特:

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)

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

1 个答案:

答案 0 :(得分:0)

还需要点类信息

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