
Python Geospatial Analysis Cookbook
By :

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.
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:~$
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.
Follow these steps. These are taken from the PostgreSQL homepage for Ubuntu Linux:
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
$ deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main
wget
command for add the key:$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \ sudo apt-key add -
update
command to actualize your installer packages:$ sudo apt-get update
install
command to actually install PostgreSQL 9.3:$ sudo apt-get install postgresql-9.3
libgdal1
, so go ahead and install it:$ sudo apt-get install libgdal1
$ sudo apt-get install postgresql-9.3-postgis-2.1
$ sudo apt-get install libpq-dev
contrib
module with contributions:$ sudo apt-get install postgresql-contrib
psycopg2
, to connect to your PostgreSQL database from Python:$ sudo apt-get install python-psycopg2
(pygeoan_cb)mdiener@mdiener-VirtualBox:~$ createdb [NewDatabaseName]
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;"
$ pip install django
>>> 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.
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.
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 |
Change the font size
Change margin width
Change background colour