GIS burdGIS logo

A Django - Postgres GIS server on Windows

The Curse of Windows

The more I dig into development and building dev environments the more I feel the tug of Linux. I've never tried a Linux environment but after endless searching for Windows specific instructions and tutorials (and then discovering how much more laborious everything is) the more I want to make the switch. I haven't yet; I don't like change... I'm still on Windows 8.

After deciding it would be a good idea to build a dev environment for a GIS server in Django (AKA GeoDjango) I thought I'd pull all the resources I've used together in order to save other Windows users some search time and provide myself with a guide the next time I think this would be a "good idea". I hope this helps and if you have anything to add or something doesn't work for you please get in touch.

The Virtual Env

I have several Python installations on my machine. Some programs depend on specific ones and dislike it if I start messing around with them and updating / installing new packages. As such, for development I always use a virtual environment through Windows Powershell. I'm not going to repeat what has already been done so well previously, so if you don't have a virtual environment set up already, this post by Tyler Butler will get you ready. Follow that and when you can "workon" your virtual env, come back here.

Postgres Installation

I'm more familiar with using MySQL in Django projects. Working in GIS it's about time I set up Postgres. How hard can it be? Well, it's a bit of a faff but not insurmountable. I've opted for postgres 9.2 32 bit simply because Openshift provide that version and if I ever want to put this live I'll be using Openshift. If you have a Django app that you'd like to host on Openshift this Stack Exchange post will help you out... a lot (a huge and continued thanks to Luis Masuelli).

To install postgres I found this tutorial on the excellent Django Girls site. It will direct you to the following links to download (remember I'm going with postgres 9.2) and install postgres. A couple of notes on the installation:

I did set up a new user in Windows with admin rights to install postgres.

I also allowed Stack Builder to run at the end of the installation. This was so I could install PostGIS (which you'll be needing) and you can find that in categories>spatial extensions>PostGIS 2.1:

Application Stack Builder

Congrats! You should now have postgres installed. It's probably a good idea to test it.

Testing postgres

Because I had set up a new Windows user with admin rights to install postgres I ran into trouble when trying to access it from my normal Windows user. The tutorial at Django Girls goes into this but I'll repeat it here for completeness.

With postgres installed you should be able to open up command prompt and type psql:

cmd psql

You'll be asked for a password here, most likely one that you don't have. And so, you'll need to circumnavigate that with the following command:

psql -U <username> -W

For <username> you should be able to use postgres (postgres creates that user for you on install) and the password you created on the install of postgres. Then you'll see the prompt change from < to #. This means we're in psql:

psql Access

Now we can create a new user and then create a database owned by that user:

# CREATE USER yourUsername;

# CREATE DATABASE yourDatabaseName OWNER youUsername;

Don't forget to use the semicolon at the end of your postgres command. Remember, we're dealing with postgreSQL and you need to end a command with ";". You can check that this has worked by using the pgAdmin GUI. Just go to your start screen and type "pgAdmin". Open the app and have a look at your users and databases:

pgAdmin III

The GEO bit

It would be great fun now to dive into building a Django project and playing around with our sparkly new database. Buuuuuuuut.... we need a few more bits to add the Geo to our Django. First up we need to allow Python to talk to the database. For this we'll use psycopg2. You'll need to get powershell open for this and activate your virtual env. My virtual env here is call "rtm". I've run "pip freeze" to see what I have installed (nothing so far) and then changed directory to my Scripts directory:

ps pip freeze

I read a lot about how to install psycopg2 in a windows virtual env and then realised it was simpler than anticipated. A quick:

pip install psycopg2

seemed to do the trick. Bonus!

Next we need numpy. Again a "pip install numpy" works fine. One part of the "Geo" in GeoDjango is a library called GDAL. Now pip won't work for this. Instead we have to download the package and the install it. That's pretty easy though thanks to Cristoph Gohlke. Visit that site and download the package you need. I went with GDAL-1.11.4-cp27-none-win32.whl. The cp2.7 means it is for Python 2.7 and the win32 means it's 32 bit. I saved this to my rtm folder (rather than Scripts) so I need to back out of the Scripts folder "cd ../" and then I can simply use "pip install GDAL-1.11.4-cp27-none-win32.whl"

So that's the Geo.... What about the Django? Well, that's as easy as "pip install django". If you run a pip freeze once that's completed you should see all the libraries happily installed. I've left all the commands mentioned above in this screenshot for reference:

The Geo Bit

Start a Django project

Now for the fun bit. Let's start a Django project, connect to our database and then test a couple of things to make sure "the Geo bit" works. To start a Django project the docs are very good. You can find them here. Once started we'll need to make a couple of changes to the settings.py file. The first thing to do is add the following at the bottom of the INSTALLED_APPS:

'django.contrib.gis',

and then we need to set the database. I've seen tutorials suggest using psycopg2 as the backend but I found postgis to work. Find the DATABASE settings and replace what's there with this:

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

All done? Well not quite. If you go back to the virtual env and start the server you may find a problem. With your virtual env activated (and in the right location; the same folder as django's manage.py file) start the server with "python manage.py runserver". You may find that django is not happy because we don't have a password. This is a job for psql in the command window. We'll need to alter our user, set a password and to be on the safe side create superuser privileges and make sure our password is valid. Sign in as postgres with the password you set at install and use the following commands:

psql ALTER USER yourUserName WITH SUPERUSER PASSWORD 'password' VALID UNTIL 'infinity';

Postgres Alter User

Now you can add your 'password' to the Django settings, cross you fingers (and perhaps your toes) and try the "python manage.py runserver" again.

Did it work? If you're getting error messages and feel like you're pounding your head against a brick wall drop me a line and I'll be happy to help you troubleshoot. I'd be very interested to know how you get on and errors will help me refine this tutorial.

The astute among you may notice I've made no mention of GEOS and Proj4. I have QGIS and OSGeo4W installed. However, I've removed all mention of GDAL, GEOS and Proj4 from my environment variables and GeoDjango still appears to work. If your installation is working at this stage then head over to the GeoDjango Tutorial and get mapping.

Next time we'll look at getting some real-time abilities added in with redis and potentially adding Torque capabilities. Thanks for reading and good luck!

...archive

all articles
tutorials
map stories

sign-up...

Sign-up for our monthly newsletter.

What now?

If you enjoyed this post and would like to learn more about GIS or improve your skills check out our training courses. If you'd like to contribute a story, tutorial or anything else, please contact us.

Thanks for reading and if you'd like to support our content please feel free to buy us a coffee.