我正在尝试使用GeoDjango在PointField中存储经度/经度,然后查询两个PointField之间的距离(以公里为单位)。导致距离计算返回几何距离而不是地理距离的某些因素不起作用。我在模型中使用了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距离上的椭球体?
谢谢!
发布于 2016-06-14 02:41:43
通过阅读文档,我发现GEOSGeometry类中的距离计算不考虑SRID,并且与PostGIS数据库直接提供的距离计算不同。
我使用了python libray geopy,它提供了计算距离的distance方法。
from geopy.distance import distance as geopy_distance
geopy_distance(customer1.gis_location, customer2.gis_location).kilometers不幸的是,我仍然不知道如何使用Django的GIS库来计算两个Point的距离。
https://stackoverflow.com/questions/37780072
复制相似问题