3D distance calculation with GeoDjango

I use

  • python 2.7.12
  • django 1.10.6
  • postgreSQL 9.5.6
  • postGIS 2.2.2

First question

I need to use GeoDjango to calculate the distance between two points. When I checked the documentation , it says that GeoQuerySet.distance () is deprecated and uses Distance () from django.contrib.gis.db instead. models.functions.

The following code works fine:

from django.contrib.gis.db.models.functions import Distance

p1 = Instrument.objects.get(pk=151071000).coordinates
p2 = Instrument.objects.filter(pk=151071008)

for i in p2.annotate(distance=Distance('coordinates', p1)):
    print i.distance
    print i.distance.__class__

Conclusion:

461.10913945 m
<class 'django.contrib.gis.measure.Distance'>

My model:

class Instrument(models.Model):
    ...
    coordinates = gis_models.PointField(null=True, blank=True, dim=3)

But I only have two points, so when I try to use Distance () without annotate (), it returns an instance of the django.contrib.gis.db.models.functions.Distance () rathen class than django.contrib.gis. measure.Distance ():

p1 = Instrument.objects.get(pk=151071000).coordinates
p2 = Instrument.objects.get(pk=151071008).coordinates
print Distance(p1, p2)

Conclusion:

Distance(Value(SRID=4326;POINT Z (-76.48623600000001 44.260223 0)), GeomValue(SRID=4326;POINT Z (-76.490923 44.262658 0)))

How to get the same result as using annotate ()?

Second question

, /. , , 2d. 200 :

p1 = Instrument.objects.get(pk=151071000)
p1.coordinates = 'SRID=4326;POINT Z (-76.48623600000001 44.260223 200)'
p2 = Instrument.objects.filter(pk=151071008)

for i in p2.annotate(distance=Distance('coordinates', p1.coordinates)):
    print i.distance

:

461.10913945 m
+3
1

:

  • Distance :

    .

    , Distance(p1, p2) Distance.
    :

    p1 = Instrument.objects.get(pk=151071000).coordinates
    p2 = Instrument.objects.get(pk=151071008).coordinates
    d = Distance(m=p1.distance(p2))
    print d.m
    

    .

    annotate, ! ( )

  • Distance . 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)
      
+1

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


All Articles