Giter Site home page Giter Site logo

django-earthdistance's Introduction

CI

pypi version

pypi license

django-earthdistance

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]{.title-ref} results in an index, [ll_to_earth]{.title-ref} 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;")

django-earthdistance's People

Contributors

jneight avatar michaeljones avatar onipiing avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.