GeoDjango: How can I get the distance between two points?

2024/9/24 11:24:52

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.

  1. How can I calculate this distance with GeoDjango?
  2. What units are the results in?
  3. Is there a 'best' way to store this data? Float? Decimal?

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!

Answer

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!

https://en.xdnf.cn/q/71708.html

Related Q&A

Reading direct access binary file format in Python

Background:A binary file is read on a Linux machine using the following Fortran code:parameter(nx=720, ny=360, nday=365) c dimension tmax(nx,ny,nday),nmax(nx,ny,nday)dimension tmin(nx,ny,nday),nmin(nx,…

Fabric/Python: AttributeError: NoneType object has no attribute partition

Have the following function in fabric for adding user accounts.~/scripts #fab -lPython source codeAvailable commands:OS_TYPEadduser_createcmd Create command line for adding useradduser_getinfo Prom…

Python object @property

Im trying to create a point class which defines a property called "coordinate". However, its not behaving like Id expect and I cant figure out why. class Point:def __init__(self, coord=None…

Tkinter - Inserting text into canvas windows

I have a Tkinter canvas populated with text and canvas windows, or widgets, created using the create_text and create_window methods. The widgets I place on the canvas are text widgets, and I want to in…

How to run nosetests without showing of my matplotlibs graph?

I try to run my test without any messages displaying from my main program. I only want verbose messages from nosetests to display.For example: nosetests -v --nologcaptureAll of my printout messages fro…

Variable name to string in Python

I have written some code in Python and I would like to save some of the variables into files having the name of the variable. The following code:variableName1 = 3.14 variableName2 = 1.09 save_variable_…

In Python, how to print FULL ISO 8601 timestamp, including current timezone

I need to print the FULL local date/time in ISO 8601 format, including the local timezone info, eg:2007-04-05T12:30:00.0000-02:00I can use datetime.isoformat() to print it, if I have the right tzinfo o…

TextVariable not working

I am trying to get the Text out of an Entry widget in Tkinter. It works with Entry1.get(), but it does not work using textvariableWhat am I doing wrong ? from Tkinter import * master = Tk() v = String…

Is there a Python module to get next runtime from a crontab-style time definition?

Im writing a dashboard application and I need a way to figure out how long an item is "valid", i.e. when should it have been superseded by a new value (its possible to have an error such that…

Django Generic Relations error: cannot resolve keyword content_object into field

Im using Djangos Generic Relations to define Vote model for Question and Answer models. Here is my vote model:models.pyclass Vote(models.Model):user_voted = models.ForeignKey(MyUser)is_upvote = models.…