【问题标题】:GeoDjango - lon/lat PointFields distance are using geometry instead of geographyGeoDjango - lon/lat PointFields 距离使用几何而不是地理
【发布时间】:2016-06-12 23:25:06
【问题描述】:

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

型号:

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


    【解决方案1】:

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

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

    from geopy.distance import distance as geopy_distance
    geopy_distance(customer1.gis_location, customer2.gis_location).kilometers
    

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

    【讨论】:

      猜你喜欢
      • 2013-01-26
      • 2014-05-02
      • 1970-01-01
      • 2012-04-04
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 2012-04-23
      • 2016-08-05
      相关资源
      最近更新 更多