首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >经纬度/纬度PointFields距离使用几何而不是地理

经纬度/纬度PointFields距离使用几何而不是地理
EN

Stack Overflow用户
提问于 2016-06-13 07:25:07
回答 1查看 758关注 0票数 0

我正在尝试使用GeoDjango在PointField中存储经度/经度,然后查询两个PointField之间的距离(以公里为单位)。导致距离计算返回几何距离而不是地理距离的某些因素不起作用。我在模型中使用了geography=True,但它似乎没有帮助。我使用的是postgispostgresql

型号:

代码语言:javascript
复制
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)

测试:

代码语言:javascript
复制
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)

输出:

代码语言:javascript
复制
Customer 1:  SRID=4326;POINT (-79.2999999999999972 43.6000000000000014)  
Customer 2:  SRID=4326;POINT (-89.2000000000000028 48.3999999999999986)  
Distance: 11.002272492535353

这只返回两点之间的几何距离,而不是地理距离。有谁有任何建议,我可以让它返回KMs距离上的椭球体?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-06-14 02:41:43

通过阅读文档,我发现GEOSGeometry类中的距离计算不考虑SRID,并且与PostGIS数据库直接提供的距离计算不同。

我使用了python libray geopy,它提供了计算距离的distance方法。

代码语言:javascript
复制
from geopy.distance import distance as geopy_distance
geopy_distance(customer1.gis_location, customer2.gis_location).kilometers

不幸的是,我仍然不知道如何使用Django的GIS库来计算两个Point的距离。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37780072

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档