How to calculate 3D distance (including height) between two points in GeoDjango

Prologue:

This often raises the question in SO:

I wanted to make an example in SO Documentation, but the chapter was geodjangonever removed, and after the Documentation was turned off on August 8, 2017, I will follow up on this widely-spread and discussed meta-answer and write my example as an answer to my question.

Of course, I would be more than happy to see any other approach.


Question:

Suppose the model:

class MyModel(models.Model):
    name = models.CharField()
    coordinates = models.PointField()

Where I save a point in a variable coordinateas a point lan, lng, alt:

MyModel.objects.create(
    name='point_name', 
    coordinates='SRID=3857;POINT Z (100.00 10.00 150)')

:

p1 = MyModel.objects.get(name='point_1').coordinates
p2 = MyModel.objects.get(name='point_2').coordinates

d = Distance(m=p1.distance(p2))

d=X .

:

:

p1.coordinates = 'SRID=3857;POINT Z (100.00 10.00 200)'

150 , :

d = Distance(m=p1.distance(p2))

d=X , , .
3D- ?

+4
2

GeoDjango Distance .
3D-, , :

:

latitude, longitude altitude , (x, y, z), Euclidean Formula 3D-.

  • :
    polar_point_1 = (long_1, lat_1, alt_1)

    polar_point_2 = (long_2, lat_2, alt_2)

  • , :

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

  • , :

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

numpy:

np.linalg.norm(point_1 - point_2)
+1

Source: https://habr.com/ru/post/1683447/


All Articles