Using GeoDjango in your Django project

(Comments)

In this tutorial, I will explain to you how to use GeoDjango in Django project. Let us assume you have UserProfile in your Django project.

class UserProfile(models.Model):
    user = models.ForeignKey(User)

for using GeoDjango in our project we need PostgreSQL DB. Lets create our DB in PostgreSQL and extend to PostGIS.

> sudo -u postgres psql
postgres=# create user django_geo with password 'django_geo';
postgres=# create database djangogeo owner django_geo;
postgres=# ALTER USER django_geo SUPERUSER; //giving sudo permission to user.
postgres=# \c djangogeo //switch to djangogeo DB
djangogeo=# CREATE EXTENSION postgis;

We are ready with PostgreSQL DATABASE. Let's add our DB details in our Django settings.

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'djangogeo',
        'USER': 'django_geo',
        'PASSWORD': "django_geo",
        'HOST' : 'localhost',
        'PORT' : '',
    },
}

Add 'django.contrib.gis' in our INSTALLED_APPS

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',

#internal apps
'my_app',

]

We are done with GeoDjango setup in our project. Do migrations for creating DB tables for our new database.

> ./manage.py migrate

Let's start editing our UserProfile model where we want to save user geolocation details.

from django.contrib.gis.db import models

class UserProfile(models.Model):
    user = models.ForeignKey(User)
    geo_location = models.PointField(srid=4326, null=True,blank=True)
    geo_objects = models.GeoManager()

Adding location details to UserProfile:

Let's see how to add location details in UserProfile using Django shell

> python manage.py shell
> from my_app.models import UserProfile
> from django.contrib.gis.geos import Point #GeoDjango takes lat and lng values in Point object.
> lat = 17.5013714
> lng = 78.3878926
> location_point = Point(lng, lat) #creating point object.
> user_profile = UserProfile.objects.first() #Assume we are adding geo location for first userprofile object.
> user_profile.geo_location = location_point
> user_profile.save()

GeoDjango distance queries

Query users within a 10km/100meter's/10mile's distance from your location

> from my_app.models import UserProfile
> from django.contrib.gis.measure import D
> user_profile = UserProfile.objects.first() #Assume first object is required user profile
# Query for 10km
> UserProfile.objects.filter(geo_location__distance_lte=(user_profile.geo_location, D(km=10)))
# Query for 100meter's
> UserProfile.objects.filter(geo_location__distance_lte=(user_profile.geo_location, D(m=100)))
# Query for 10mile's
> UserProfile.objects.filter(geo_location__distance_lte=(user_profile.geo_location, D(mi=10)))

Query users between 5 to 10 km distance from your location

> from my_app.models import UserProfile
> from django.contrib.gis.measure import D
> from django.db.models import Q
> user_profile = UserProfile.objects.first()
> UserProfile.objects.filter(Q(geo_location__distance_gte=(user_profile.geo_location, D(km=5)))
    & Q(geo_location__distance_gte=(user_profile.geo_location, D(km=5))))

Comments

Recent Posts

Archive

2018
2017
2016
2015
2014

Tags

Authors

Feeds

RSS / Atom