My Profile
model has this field:
location = models.PointField(geography=True, dim=2, srid=4326)
I'd like to calculate the distance between the two of these locations
(taking into account that the Earth is a spheroid) using GeoDjango, so that I can store this distance in the database.
I've reviewed previous, similar questions, and haven't found them useful. No answer gives enough explanation of what's happening or why it works.
I'm using Django 1.8 and the latest versions of required libraries for GeoDjango.
Thanks!
Following Abhyudit Jain's comment, I'm using geopy to calculate distance. I'm adding it as a property as opposed to storing it, as per e4c5's advice:
from django.contrib.gis.measure import Distance, D
from geopy.distance import distance
@property
def distance(self):
return Distance(m=distance(self.user_a.profile.location, self.user_b.profile.location).meters)
Geopy defaults to Vincenty’s formulae, with an error of up to 0.5%, and contains a lot of other functionality I'll use in future.
The above returns a GeoDjango Distance object, ready for easy conversion between measurements.
Thanks for the help!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With