Skip to content Skip to sidebar Skip to footer

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 7

Solution 1:

The django distance method is not for calculating the distance of 3D points (with elevation) but of 2D.

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)

  • 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)
    


Confirmed solution of a similar issue from the second part of my answer here: 3d distance calculations with GeoDjango

Post a Comment for "Distance Between Two 3d Point In Geodjango (postgis)"