Book Image

Python Geospatial Analysis Cookbook

Book Image

Python Geospatial Analysis Cookbook

Overview of this book

Table of Contents (20 chapters)
Python Geospatial Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Other Geospatial Python Libraries
Mapping Icon Libraries
Index

Installing GeoDjango and PostgreSQL with PostGIS


This is our final installation recipe and if you have followed along so far, you are ready for a simple, straightforward start to Django. Django is a web framework for professionals with deadlines, according to the Django homepage. The spatial part of it can be found in GeoDjango. GeoDjango is a contrib module installed with every Django installation therefore, you only need to install Django to get GeoDjango running. Of course, "geo" has its dependencies that were met in the previous sections. For reference purposes, take a look at this great documentation on the Django homepage at

https://docs.djangoproject.com/en/dev/ref/contrib/gis/install/#ref-gis-install.

We will use PostgreSQL and PostGIS since they are the open source industry go-to spatial databases. The installations are not 100% necessary, but without them there is no real point because you then limit your operations, and they're definitely needed if you plan to store your spatial data in a spatial database. The combination of PostgreSQL and PostGIS is the most common spatial database setup for GeoDjango. This installation is definitely more involved and can lead to some hook-ups depending on your system.

Getting ready

To use GeoDjango, we will need to have a spatial database installed, and in our case, we will be using PostgreSQL with the PostGIS extension. GeoDjango also supports Oracle, Spatialite, and MySQL. The dependencies of PostGIS include GDAL, GEOS, PROJ.4, LibXML2, and JSON-C.

Start up your Python virtual environment as follows:

mdiener@mdiener-VirtualBox:~$ workon pygeoan_cb
(pygeoan_cb)mdiener@mdiener-VirtualBox:~$

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How to do it...

Follow these steps. These are taken from the PostgreSQL homepage for Ubuntu Linux:

  1. Create a new file called pgdg.list using the standard gedit text editor. This stores the command to fire up your Ubuntu installer package:

    $ sudo gedit /etc/apt/sources.list.d/pgdg.list
    
  2. Add this line to the file, save, and then close it:

    $ deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main
    
  3. Now, run the wget command for add the key:

    $ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \ sudo apt-key add -
    
  4. Run the update command to actualize your installer packages:

    $ sudo apt-get update
    
  5. Run the install command to actually install PostgreSQL 9.3:

    $ sudo apt-get install postgresql-9.3
    
  6. To install PostGIS 2.1, we will have one unmet dependency, libgdal1, so go ahead and install it:

    $ sudo apt-get install libgdal1
    
  7. Now we can install PostGIS 2.1 for PostgreSQL 9.3 on our machine:

    $ sudo apt-get install postgresql-9.3-postgis-2.1
    
  8. Install the PostgreSQL header files:

    $ sudo apt-get install libpq-dev
    
  9. Finally, install the contrib module with contributions:

    $ sudo apt-get install postgresql-contrib
    
  10. Install the Python database adapter, psycopg2, to connect to your PostgreSQL database from Python:

    $ sudo apt-get install python-psycopg2
    
  11. Now we can create a standard PostgreSQL database as follows:

    (pygeoan_cb)mdiener@mdiener-VirtualBox:~$ createdb [NewDatabaseName]
    
  12. Using the psql command-line tool, we can create a PostGIS extension to our newly created database to give it all the PostGIS functions as follows:

    (pygeoan_cb)mdiener@mdiener-VirtualBox:~$ psql -d [NewDatabaseName] -c "CREATE EXTENSION postgis;"
    
  13. Moving on, we can finally install Django in one line directly in our activated virtual environment:

    $ pip install django
    
  14. Test out your install of Django and GDAL and, as always, try to import them as follows:

    >>> from django.contrib.gis import gdal
    >>> gdal.HAS_GDAL
    True
    

Windows users should be directed to the PostgreSQL Windows (http://www.postgresql.org/download/windows/) binaries provided by EnterpriseDB (http://www.enterprisedb.com/products-services-training/pgdownload#windows). Download the correct version and follow the installer instructions. PostGIS is also included in the list of extensions that you can directly install using the installer.

How it works...

Installations using the apt-get Ubuntu installer and the Windows installers are simple enough in order to have PostgreSQL, PostGIS, and Django up and running. However, the inner workings of the installers are beyond the scope of this book.

There's more...

To summarize all the installed libraries, take a look at this table:

Library name

Description

Reason to install

NumPy

This adds support for large multidimensional arrays and matrices

It is a requirement for many other libraries

pyproj

This handles projections

It transforms projections

shapely

This handles geospatial operations

It performs fast geometry manipulations and operations

matplotlib

This plots libraries

It provides a quick visualization of results

descartes

This uses Shapely or GeoJSON objects as matplotlib paths and patches

It speedily plots geo-data

pandas

This provides high-performance data structures and data analysis

It performs data manipulation, CSV creation, and data manipulation

SciPy

This provides a collection of Python libraries for scientific computing

It has the best collection of necessary tools

PySAL

This contains a geospatial analysis library

It performs a plethora of spatial operations (optional)

IPython

This provides interactive Python computing

It is a helpful notebook to store and save your scripts (optional)

Django

This contains a web application framework

It is used for our demo web application in Chapter 11, Web Analysis with GeoDjango

pyshp

This provides pure Python shapefile manipulation and generation

It helps input and output shapefiles

GeoJSON

This contains the JSON format for spatial data

It facilitates the exchange and publication of this format

PostgreSQL

This is a relational database

It helps store spatial data

PostGIS

This is the spatial extension to PostgreSQL

It stores and performs spatial operations on geographic data in PostgreSQL