Skip to main content

Using PostgreSQL’s EarthDistance extension for django 1.11, 2.2 and 3.2 (for older versions see with_djorm_expressions branch)

Earthdistance allows to do fast geolocalized queries without using PostGIS

Usage

Cube and EarthDistance extensions must be enabled in postgreSQL BD, so log in database using pgsql and install extensions:

=> create extension cube;
=> create extension earthdistance;

Filter by rows inside a circunference of radius r

from django.db import models

from django_earthdistance.models import EarthDistanceQuerySet

class MyModel(models.Model):
    latitute = models.FloatField()
    longitude = models.FloatField()

    objects = EarthDistanceQuerySet.as_manager()

# Define fields to query in DistanceExpression initialization
# search with lat=0.2546 and lon=-38.25 and distance 1500 meters
# use param `annotate` to set a custom field for the distance, `_ed_distance` as default

MyModel.objects.in_distance(1500, fields=['latitude', 'longitude'], points=[0.2546, -38.25])

Annotate each row returned by a query with distance between two points

from django_earthdistance.models import EarthDistance, LlToEarth

MyModel.objects.filter(....).annotate(
    distance=EarthDistance([
        LlToEarth([0.2546, -38.25]),
        LlToEarth(['latitude', 'longitude'])
    ]))

Optimizing perfomance with indexes

PostgreSQL allow to use GiST indexes with functions results, a good perfomance improvement is to store ll_to_earth results in an index, ll_to_earth is a function that calculates the position of a point on the surface of the earth (assuming earth is perfectly spherical)

-- Example MyModel table is app_mymodel and points columns are latitude and longitude
CREATE INDEX mymodel_location ON app_mymodel USING gist (ll_to_earth(latitude, longitude));

For django < 1.7

Also, using south is preferred, just add this migration to migrations/ folder and edit it to your needs, index will be created

class Migration(SchemaMigration):

    def forwards(self, orm):
        cursor = connection.cursor()
        cursor.execute("CREATE INDEX mymodel_location ON app_mymodel USING gist (ll_to_earth(latitude, longitude));")


    def backwards(self, orm):
        # Deleting field 'Venue.coords'
        cursor = connection.cursor()
        cursor.execute("DROP INDEX mymodel_location ON app_mymodel;")

Metadata

Release files for django-earthdistance 1.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-earthdistance 1.1.3
File Size Uploaded Provenance Actions
django-earthdistance-1.1.3.tar.gz 8.2 kB Without provenance Details for django-earthdistance-1.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-earthdistance 1.1.3
File Interpreter ABI Platform Actions
django_earthdistance-1.1.3-py3-none-any.whl Python 3 none any Details for django_earthdistance-1.1.3-py3-none-any.whl

Total release size: 17.7 kB

Release files / django-earthdistance-1.1.3.tar.gz

Download URL django-earthdistance-1.1.3.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8a167e308a1676a7513fee429dad653065a0e087c5dc8146731a52661bec330a
BLAKE2b-256 checksum
How to use checksums
1b237147b745a4c0095db91c273c1acd8a811f6d8800e69579134a8ff3747bd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Release files / django_earthdistance-1.1.3-py3-none-any.whl

Download URL django_earthdistance-1.1.3-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
23826d6c1b816b8231809bd48f03eec8ae46a6118812e0dc79d86207a257330f
BLAKE2b-256 checksum
How to use checksums
acb5aa27515ed1b33bdb7457f6990dc32502957e38f0269ad4a4187b81c0abc0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.12

Security for django-earthdistance 1.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page