【发布时间】:2016-06-12 23:25:06
【问题描述】:
我正在尝试使用 GeoDjango 将纬度/经度存储在 PointField 中,然后以公里为单位查询两个 PointFields 之间的距离。某些东西无法导致距离计算返回几何距离而不是地理距离。我在模型中使用geography=True,但它似乎没有帮助。我正在使用postgis 和postgresql
型号:
from django.contrib.gis.db import models
class Customer(models.Model):
location = models.CharField(max_length=100)
gis_location = models.PointField(u"longitude/latitude",
geography=True,
blank=True,
null=True)
测试:
from django.contrib.gis.geos import Point
# some set up
customer1.gis_location = Point(-79.3, 43.6)
print('Customer 1:', customer1.gis_location)
customer2.gis_location = Point(-89.2, 48.4)
print('Customer 2:', customer2.gis_location)
distance = customer1.gis_location.distance(customer2.gis_location)
print('Distance: ', distance)
输出:
Customer 1: SRID=4326;POINT (-79.2999999999999972 43.6000000000000014)
Customer 2: SRID=4326;POINT (-89.2000000000000028 48.3999999999999986)
Distance: 11.002272492535353
这只是返回两点之间的几何距离,而不是地理距离。有没有人对我如何让它在球体上返回 KMs 距离有任何建议?
谢谢!
【问题讨论】:
标签: python django geolocation distance geodjango