Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Geospatial Analysis Cookbook

You're reading from   Python Geospatial Analysis Cookbook Over 60 recipes to work with topology, overlays, indoor routing, and web application analysis with Python

Arrow left icon
Product type Paperback
Published in Nov 2015
Publisher
ISBN-13 9781783555079
Length 310 pages
Edition 1st Edition
Languages
Arrow right icon
Toc

Table of Contents (15) Chapters Close

Preface 1. Setting Up Your Geospatial Python Environment FREE CHAPTER 2. Working with Projections 3. Moving Spatial Data from One Format to Another 4. Working with PostGIS 5. Vector Analysis 6. Overlay Analysis 7. Raster Analysis 8. Network Routing Analysis 9. Topology Checking and Data Validation 10. Visualizing Your Analysis 11. Web Analysis with GeoDjango A. Other Geospatial Python Libraries
B. 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

You have been reading a chapter from
Python Geospatial Analysis Cookbook
Published in: Nov 2015
Publisher:
ISBN-13: 9781783555079
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime