So, you need to store and retrieve location data, map data or various kinds of geographical features in your application's database?
Well, then, Geodjango and Postgis is your friend. Geo-WHAT, Post-WHAT? Panic not, let's figure this out together, shall we?
Following django's batteries-included philosophy, you can find in django contrib a well-written and fantastically documented gis (GIS or Geographical Information Systems) application. In order to use this powerful library to manage and manipulate location, map and geographical features and data, there's 4 simple steps that we have to go through.
1. Postgis Installation
On your server or your local development machine, we will ensure that we have the postgis library installed. In this short article, I will specifically talk about postgis installation on Mac OS X.
2. Postgis in PostgreSQL db
Once postgis is installed, we want to be sure update our existing PostgreSQL db with the additional postgis spatial reference tables and geometry columns.
3. Change django db backend
Use 'ENGINE': 'django.contrib.gis.db.backends.postgis' in our settings.py file from now on.
4. Write django models
For django apps that we want to store spatial data in, we will now swap out:
from django.db import models
with
from django.contrib.gis.db import models
1. Postgis Installation
Assuming you have already familiar with PostgreSQL installation and set up on Mac OS X, our Postgis Installation involves exactly one command:
sudo port -v install postgis
This is going to take a while, so go take a walk. ;-)
2. Postgis in PostgreSQL db
Once we have postgis installed via MacPorts, we can now run the "createlang plpgsql" command on our target database and followed by a series of SQL commands in .sql files, like this:
sudo -u postgres createlang plpgsql your_database_name
sudo -u postgres psql -d your_database_name -f /opt/local/var/macports/software/postgis/1.5.2_1+postgresql90/opt/local/share/postgresql90/contrib/postgis-1.5/postgis.sql
sudo -u postgres psql -d your_database_name -f /opt/local/var/macports/software/postgis/1.5.2_1+postgresql90/opt/local/share/postgresql90/contrib/postgis-1.5/postgis_comments.sql
sudo -u postgres psql -d your_database_name -f /opt/local/var/macports/software/postgis/1.5.2_1+postgresql90/opt/local/share/postgresql90/contrib/postgis-1.5/spatial_ref_sys.sql
And if you are curious, "createlang" is a utility for adding a new programming language to the PostgreSQL database. In our context above, we are creating the "plpgsql" language in our database. This is required by Postgis for spatial queries and sql calls made by postgis.sql. If you would like to find out more about what plpgsql does, here's the page with all the gory details -
http://www.postgresql.org/docs/9.0/static/plpgsql-overview.html.
The subsequent 3 commands executing "postgis.sql", "postgis_comments.sql" and "spatial_ref_sys.sql" completes our Postgis set-up for our specific database named "your_database_name" and "your_database_name" is now a Postgis-enabled persistent data store! (Hip Hip Hooray)
Steps 3 and 4
Once we have gotten the two simple steps above working, steps 3 and 4 are trivial if you are familiar with conventional django.
In settings.py, we now make sure that our database engine is set to use our postgis-enabled database backend.
Correspondingly, for specific models where we want to introduce geographical attributes, we will no longer use "from django.db import models" and instead, use "from django.contrib.gis.db import models" as the basis for writing our custom class in models.py.
Now, we are ready to explore maps and store related map objects and features in our datastore! Well done. :-)
Discussion
Looks like Django updated the docs on how to use it with Postgis and they even provide a script that will add all dependencies to PostgreSQL (eq. creates a db template too)
https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#creating-a-spatial-database-template-for-postgis
Leave a Comment :
Leave a Comment