This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Add support for PostgreSQL earthdistance extension to Django

Project Description
django-earthdistance
=====================

Using PostgreSQL's EarthDistance extension for django >= 1.8 (for older versions see *with_djorm_expressions* branch)*

Earthdistance allows to do fast geolocalized queries without using PostGIS

.. image:: https://pypip.in/v/django-earthdistance/badge.png
:target: https://crate.io/packages/django-earthdistance

.. image:: https://pypip.in/d/django-earthdistance/badge.png
:target: https://crate.io/packages/django-earthdistance

**Tested with python 2.7 and 3.3**


Usage
---------------

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

.. code:: sql

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



Filter by rows inside a circunference of radius r
--------------------------------------------------

.. code:: python

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
----------------------------------------------------------------------

.. code:: python

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)


.. code:: sql

-- 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));


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

.. code:: python

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;")
Release History

Release History

This version
History Node

1

History Node

0.6

History Node

0.5

History Node

0.4

History Node

0.3

History Node

0.1a

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
django-earthdistance-1.tar.gz (8.2 kB) Copy SHA256 Checksum SHA256 Source Aug 14, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting