Django application to allow looking up objects by distance.
- Python (2.6, 2.7, 3.3)
- Django (1.5, 1.6)
On Debian or Ubuntu install the geos and proj libraries with...
apt-get install libgeos-c1 proj
pip install django-distance
If you are using django 1.6+ you will need to install django-localflavor with...
pip install django-localflavor
'distance' to your
INSTALLED_APPS = ( ... 'distance', )
python manage.py syncdb
Import zip data
Import zip data with
python manage.py import_zipdata [path to csv data file]
You will find two files in the data folder CivicSpace US and GeoPostcodes. The first being a free data-set derived from the US Census 1999, 2000 and 2003. The latter is a sample data-set from http://www.geopostcodes.com/ where you can purchase the full data-set.
Import models and accessories:
from distance.models import Zip from django.contrib.gis.measure import D
Query zip codes within 50 miles:
myzip = Zip.distance.get(code="94129") zips = Zip.distance.within(myzip.location, D(mi=50))
zips = myzip.within(D(mi=50))
Query zip codes between 50 and 100 miles:
myzip = Zip.distance.get(code="94129") zips = Zip.distance.within(myzip.location, D(mi=50), D(mi=100))
zips = myzip.within(D(mi=50), D(mi=100))
Zip object methods and attr
The state the zipcode is within.
The city the zipcode is within.
A distance attribute is added to each Zip object when queried using the managers
within method. The attribute indicates the distance in meters.
Wraps the distance attribute in a D (Distance) object.
Returns the zip's latitude and longitude as a dict.
Returns a D (Distance) object representing the distance between this zip and the
other_zip parsed in.
Is a short cut to the managers within method. The method expects either a
D (Distance) object or an int. If passed an int its assumed the measurement is
That's it, we're done!