python - geodjango(postgis)中两个3D点之间的距离

标签 python django postgis postgresql-9.4 geodjango

我遇到以下问题:
我创建了两个点,例如:

SRID=3857;POINT Z (62780.8532226825 5415035.177460473 100)
SRID=3857;POINT Z (62785.8532226825 5415035.177460473 70)

如您所见,X 坐标相差 5m,Z 坐标相差 30m。 当我在 django shell 中运行 a.distance(b) 时,它返回 5,这是错误的。

但是,当我在 psql shell 中运行时:

SELECT ST_3DDistance(a.coordinates, b.coordinates)
FROM restapi_entityxyz a, restapi_entityxyz b
WHERE a.external_id='6841zef1561' AND b.external_id='1G23Fzd';

它返回:

st_3ddistance
------------------
 30.4138126514911

哪个是正确答案。

这是geodjango中缺乏功能还是一个错误?
我应该使用自定义库来执行这样的计算吗?

我的环境如下:

  • Python 3.5,
  • Django ,
  • postgresql 9.4 + postgis
  • gdal 和许多 Python 库。

最佳答案

django距离方法不是用于计算3D点(具有高程)的距离,而是用于计算2D点的距离。

我们可以通过创建自定义 3d 距离计算方法来解决这个问题,如下所述: Calculating distance between two points using latitude longitude and altitude (elevation)

  • Let:
    polar_point_1 = (long_1, lat_1, alt_1)
    and
    polar_point_2 = (long_2, lat_2, alt_2)

  • Translate each point to it's Cartesian equivalent by utilizing this formula:

    x = alt * cos(lat) * sin(long)
    y = alt * sin(lat)
    z = alt * cos(lat) * cos(long)
    

    and you will have p_1 = (x_1, y_1, z_1) and p_2 = (x_2, y_2, z_2) points respectively.

  • Finally use the Euclidean formula:

    dist = sqrt((x_2-x_1)**2 + (y_2-y_1)**2 + (z_2-z_1)**2)
    

<小时/> 从我的答案的第二部分中确认了类似问题的解决方案:3d distance calculations with GeoDjango

关于python - geodjango(postgis)中两个3D点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40024322/

相关文章:

python - 多对多字段验证错误

python - Django 动态 url

python - Django URL 映射 : How to remove app name from URL paths?

python - 多个版本的python

python - 当我将 PostgreSQL 作为 Django 的数据库时,为什么要使用 Redis?

python - Django ORM - 具有两列的左外连接?

postgresql - Postgres 9.1.24 > fatal error : SSL is not supported by this build

sql - 最近邻居的 Postgis SQL

postgresql - 在插入时将几何字段转换为不同类型?

python - 在 Python 中使用 findContours 和 OpenCV