geodjango(postgis)中两个3D点之间的距离 [英] Distance between two 3D point in geodjango (postgis)
问题描述
我有以下问题:
我创建了两点,例如:
I have the following problem:
I created two points, for example:
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 ,这是错误的.
As you can see, there is 5m difference in X coordinates, and 30m in Z coordinates.
When I run a.distance(b)
in django shell, it returns 5, which is wrong.
但是,当我在psql shell中运行时:
However, whenIi run in a 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
中缺少功能还是错误?
我应该使用自定义库来执行这样的计算吗?
Is it a lack of functionality in geodjango
or a bug?
Should I use a custom library to perform such a calculation?
我的环境如下:
- Python 3.5,
- Django,
- postgresql 9.4 + postgis
- gdal和许多python库.
推荐答案
django距离方法不是用于计算3D点(带有高程)的距离,而是用于计算2D.
The django distance method is not for calculating the distance of 3D points (with elevation) but of 2D.
我们可以通过创建自定义3d距离计算方法来解决此问题,例如此处所述: 使用纬度经度和海拔(海拔)
We can work around that by creating a custom 3d distance calculation method, like the one described here: Calculating distance between two points using latitude longitude and altitude (elevation)
让:
polar_point_1 = (long_1, lat_1, alt_1)
和
polar_point_2 = (long_2, lat_2, alt_2)
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)
,您将分别获得p_1 = (x_1, y_1, z_1)
和p_2 = (x_2, y_2, z_2)
点.
最后使用欧几里得公式:
Finally use the Euclidean formula:
dist = sqrt((x_2-x_1)**2 + (y_2-y_1)**2 + (z_2-z_1)**2)
在这里我回答的第二部分中确认了类似问题的解决方案:使用GeoDjango进行3d距离计算
Confirmed solution of a similar issue from the second part of my answer here: 3d distance calculations with GeoDjango
这篇关于geodjango(postgis)中两个3D点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!