我遇到以下问题:
我创建了两个点,例如:
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)
<小时/> 从我的答案的第二部分中确认了类似问题的解决方案:3d distance calculations with GeoDjango
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)
andp_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)
关于python - geodjango(postgis)中两个3D点之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40024322/