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
Python Geospatial Analysis Cookbook
Python Geospatial Analysis Cookbook

Python Geospatial Analysis Cookbook: Over 60 recipes to work with topology, overlays, indoor routing, and web application analysis with Python

eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Python Geospatial Analysis Cookbook

Chapter 1. Setting Up Your Geospatial Python Environment

In this chapter, we will cover the following topics:

  • Installing virtualenv and virtualenvwrapper
  • Installing pyproj and NumPy
  • Installing shapely, matplotlib, and descartes
  • Installing pyshp, geojson, and pandas
  • Installing SciPy, PySal, and IPython
  • Installing GDAL and OGR
  • Installing GeoDjango and PostgreSQL with PostGIS

Introduction

This chapter will get the grunt work done for you so that you can freely and actively complete all the recipes in this book. We will start off by installing, each of the libraries you will be using, one by one. Once each step is completed, we will test each library installation to make sure it works. Since this book is directed toward those of you already working with spatial data, you can skip this chapter if you have it installed already. If not, you will find the installation instructions here useful as a reference.

The choice of Python libraries is based on industry-proven reliability and functionality. The plethora of functions in Python libraries has led to a flourishing GIS support on many top desktop GIS systems, such as QGIS and ESRI ArcGIS.

Also included in this book is an installer.sh bash file. The installer.sh file can be used to install the Python libraries that are available for your virtual environment from pip and other dependencies via the apt-get command. The installer.sh bash file is executed from the command line and installs almost everything in one go, so please take a look at it. For those of you who are starting with Python for the first time, follow the instructions in this chapter and your machine will be set up to complete different recipes.

Installations can sometimes be tricky even for advanced users, so you will find some of the most common pitfalls and hook-ups described in this chapter.

The development of these recipes was completed on a fresh Linux/Ubuntu 14.04 machine. Therefore, the code examples, if not otherwise specified, are Linux/Ubuntu-specific with Windows notes wherever necessary, unless otherwise specified.

Installing virtualenv and virtualenvwrapper

This recipe will enable you to manage different versions of different libraries for multiple projects. We use virtualenv to create virtual Python environments to host collections of project-specific libraries in an isolated directory. For example, you may have an old legacy project using Django 1.4, whereas a new project requires you use Django version 1.8. With virtualenv, you can have both versions of Django installed on the same machine, and each project can access the appropriate version of Django without any conflicts or problems.

Without virtualenv, you are forced to either upgrade the old project or find a workaround to implement the new features of the other version, therefore limiting or complicating the new project.

The virtualenv allows you to simply switch between different Python virtual environments for your individual projects. This has the added benefit that you can easily and quickly set up a new machine for testing or help a new developer get their machine up and running as fast as possible.

Getting ready

Before anything, we are going to assume that you already have a Linux/Ubuntu machine or a virtualbox instance running Linux/Ubuntu so you can follow these instructions.

Tip

I also suggest trying out Vagrant (http://www.vagrantup.com), which uses virtualbox to box and standardize your development environment.

Ubuntu 14.04 comes with Python 2.7.6 and Python 3.4 preinstalled; the other libraries are your responsibility as explained in the following sections.

Windows users need to download and install Python 2.7.x from the Python home page at https://www.python.org/downloads/windows/; please download the newest version of the 2.7.x series since this book is written with 2.7.X in mind. The installer includes a bundled version of pip, so make sure you install it!

Take a close look at the correct version to download, making sure that you get either the 32-bit or 64-bit download. You cannot mix and match the versions, so be careful and remember to install the correct version.

A great site for other kinds of Windows binaries can be found at http://www.lfd.uci.edu/~gohlke/pythonlibs/. Wheel files are the new norms of installations and can be executed from the command line as follows:

python pip install libraryName.whl

Note

On Windows, make sure that your Python interpreter is set up on your system path. This way, you can simply call Python directly from the command prompt using the C:\Users\Michael> python filename.py command. If you need more help, information can be found by following one of the online instructions at https://pypi.python.org/pypi/pip.

As of Python 2.7.9 and later versions, pip is available on installation.

Python 3 would be awesome to use, and for many Python GIS libraries, it is ready for show time. Unfortunately, not all GIS libraries jive with Python 3 (pyproj) as one would love to see at the time of writing this. If you want, feel free to go for Python 3.x and give it a go. A great webpage to check the compatibility of a library can be found at https://caniusepython3.com/.

To install virtualenv, you need to have a running installation of Python and pip. The pip package manager manages and installs Python packages, making our lives easier. Throughout this book, if we need to install a package, pip will be our tool of choice for this job. The official installation instructions for pip can be found at https://pip.pypa.io/en/latest/installing.html. To install pip from the command line, we first need to install easy_install. Let's try it out from the Terminal:

$ sudo apt-get install python-setuptools python-pip

With this one line, you have both pip and easy_install installed.

Note

What is sudo?

sudo is a program for Unix-like computer operating systems that allows users to run programs with the security privileges of another user (normally, the super user or root). Its name is a concatenation of su (substitute user) and do (take action). Take a look at http://en.wikipedia.org/wiki/Sudo for more information on this.

The command sudo means to run an execution as a super user. If this fails, you will need to get theez_setup.py file, which is available at https://bootstrap.pypa.io/ez_setup.py. After downloading the file, you can run it from the command line:

$ python ez_setup.py

Now pip should be up and running and you can execute commands to complete the installations of virtualenv and virtualenvwrapper. The virtualenvwrapper creates shortcuts that are faster ways to create or delete your virtual environments. You can test it as follows:

$ pip install virtualenv

How to do it...

The steps to install your Python virtualenv and virtualenvwrapper packages are as follows:

  1. Install virtualenv using the pip installer:
    $ sudo pip install virtualenv
    
  2. Install virtualenvwrapper using easy_install:
    $ sudo easy_install virtualenvwrapper
    

    Note

    We use easy_install instead of pip because with Ubuntu 14.04, the virtualenvwrapper.sh file is unfortunately not located at /usr/local/bin/virtualenvwrapper.sh where it should be according to the online documentation.

  3. Assign the WORKON_HOME variable to your home directory with the folder name venvs. Create a single folder where you want to store all your different Python virtual environments; in my case, the folder is located at /home/mdiener/venvs:
    $ export WORKON_HOME=~/venvs
    $ mkdir $WORKON_HOME
    
  4. Run the source command to execute the virtualenvrapper.sh bash file:
    $ source /usr/local/bin/virtualenvwrapper.sh
    
  5. Next, we create a new virtual environment called pygeoan_cb, and this is also the name of the new folder where the virtual environment is installed:
    $ mkvirtualenv pygeoan_cb
    

    To use virtualenvwrapper the next time you start up your machine, we need to set it up so that your bash terminal runs the virtualenvwrapper.sh script when your computer starts.

  6. First, put it in your ~/.bashrc file:
    $ echo "export WORKON_HOME=$WORKON_HOME" >> ~/.bashrc
    
  7. Next, we'll import the virtualenvwrapper function in our bash:
    $ echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc
    
  8. Now we can execute our bash:
    $ source ~/.bashrc
    

How it works...

Step one shows how pip installs the virtualenv package into your system-wide Python installation. Step two shows how the virtualenvwrapper helper package is installed with easy_install because the virtualenvwrapper.sh file is not created using the pip installer. This will help us create, enter, and generally, work or switch between Python virtual environments with ease. Step three assigns the WORKON_HOME variable to a directory where we want to have all of our virtual environments. Then, we'll create a new directory to hold all the virtual environments. In step four, the command source is used to execute the shell script to set up the virtualenvwrapper package. In step five, we see how to actually create a new virtualenv called pygeoan_cb in our /home/mdiener/venvs directory. This final step automatically starts our virtualenv session.

Once the virtualenv session starts, we can now see the name of virtualenv in brackets like this:

(pygeoan_cb)mdiener@mdiener-VirtualBox:~$

To exit virtualenv, simply type the following code:

$ deactivate

Now, your command line should be back to normal as shown here:

mdiener@mdiener-VirtualBox:~$

To reactivate virtualenv, simply type:

$ workon pygeoan_cb

Tip

The workon command has Tab completion. So, simply type workon, and then the first letter of the name of the virtual environment you want to enter, such as py. Hit Tab and it will autocomplete the name.

Inside the /venvs folder, you will find specific individual virtual environments for each project in the form of a subfolder. The virtualenvwrapper package will always create a new folder for each new project you create. You can, therefore, easily delete a folder and it will remove your virtual environment.

To quickly print a list all of the installed libraries to a file, we'll use the pip command:

$ pip freeze > requirements.txt

This will create a text file called requirements.txt in the current folder. The text file contains a list of all the installed Python packages inside the Python virtual environment currently running.

To create a new virtualenv from a requirements file, use the following command:

$ pip install -r /path/to/requirements.txt

There's more…

For those of you who are just starting out with geospatial Python development, it should be noted that you should keep your project-specific code at another location outside your Python virtual environment folder. For example, I always have each project-related code contained in a separate folder called 01_projects, which is my main folder. The path to my projects folder is /home/mdiener/01_projects, and the structure of two of my projects is as follows:

  • 01_projects/Name_project1
  • 01_projects/Name_project2

All virtual environments are located under /home/mdiener/venvs/. Usually, I give them the same name as a project to keep things organized, as follows:

  • /home/mdiener/venvs/Name_project1
  • /home/mdiener/venvs/Name_project2

Installing pyproj and NumPy

The pyproj is a wrapper around the PROJ.4 library that works with projections and performs transformations (https://pypi.python.org/pypi/pyproj/) in Python. All your geographic information should be projected into one of the many coordinate systems supported by the European Petroleum Survey Group (EPSG). This information is necessary for the systems to correctly place data at the appropriate location on Earth. The geographic data can then be placed on top of each other as layers upon layers of data in order to create maps or perform analysis. The data must be correctly positioned or we won't be able to add, combine, or compare it to other data sources spatially.

Data comes from many sources and, often, a projection is not the same as a dataset. Even worse, the data could be delivered with a description from a data provider stating it's in projection UTM31 when, in reality, the data is in projection UTM34! This can lead to big problems later on when trying to get your data to work together as programs will throw you some ugly error messages.

NumPy is the scientific backbone of number crunching arrays and complex numbers that are used to power several popular geospatial libraries including GDAL (geospatial abstraction library). The power of NumPy lies is in its support for large matrices, arrays, and math functions. The installation of NumPy is, therefore, necessary for the other libraries to function smoothly, but is seldom used directly in our quest for spatial analysis.

Getting ready

Fire up your virtual environment, if it is not already running, using the following standard start command:

$ workon pygeoan_cb

Your prompt should now look like this:

(pygeoan_cb)mdiener@mdiener-VirtualBox:~$

Note

If workon for some reason does not start your virtual environment, you can start it simply by executing source /home/mdiener/venvs/pygeoan_cb/bin/activate from the command line; try the steps listed in the Installing virtualenv and virtualenvwrapper recipe again to get it going.

Now, we need to install some Python tools for development that allow us to install NumPy, so run this command:

$ sudo apt-get install -y python-dev

You are now ready to move on and install pyproj and NumPy inside your running virtual environment.

How to do it...

Simply fire up virtualenv and we will use the pip installer to do all the heavy lifting as follows:

  1. Use pip to go ahead and install NumPy; this can take a couple of minutes as many lines of installation verbosity are written on screen:
    $ pip install numpy
    

    Windows users can grab the .whl file for NumPy and execute it using following command:

    pip install numpy -1.9.2+mkl-cp27-none-win32.whl
    
  2. Use pip one more time to install pyproj:
    $ pip install pyproj
    

    Windows users can use the following command to install pyproj:

    pip install pyproj-1.9.4-cp27-none-win_amd64.whl
    
  3. Wait a few minutes; NumPy should be now running along with pyproj. To test if it's worked out, enter the following command in the Python console. The output should look like this:
    (pygeoan_cb)mdiener@mdiener-VirtualBox:~/venv$ python
    Python 2.7.3 (default,  Feb 27 2014, 19:58:35)
    [GCC 4.6.3] on linux2
    Type “help”,  “copyright”, “credits”, or  “license” for more information.
    >> import numpy
    >> import pyproj
    

No errors, I hope. You have now successfully installed NumPy and pyproj.

Note

All sorts of errors could show up, so please take a look at the respective installation links to help you solve them:

For pyproj: https://pypi.python.org/pypi/pyproj/

For NumPy: http://www.numpy.org

How it works...

This easy installation works using the standard pip installation method. No tricks or special commands are needed. You need to simply execute the pip install <library_name> command and you are off to the races.

Tip

Library names can be found by visiting the https://pypi.python.org/pypi web page if you are unsure of the exact name you want to install.

Installing shapely, matplotlib, and descartes

A large part of geospatial analysis and visualization is made possible using Shapely, matplotlib, GDAL, OGR, and descartes, which are installed later. Most of the recipes here will use these libraries extensively so setting them up is necessary to complete our exercises.

Shapely (http://toblerity.org/shapely) provides pure spatial analysis of geometries using the Cartesian coordinate system as is used by AutoCAD, for those of you familiar with CAD-like programs. The benefit of using a flat coordinate system is that all the rules of Euclidean geometry and analytic geometry are applied. For a quick refresher in the coordinate systems that we all learned in school, here is a little image to quickly jolt your memory.

Installing shapely, matplotlib, and descartes

Note

Description: A Cartesian coordinate system demonstrating a flat plane to plot and measure geometry.

Illustration 1: Source: http://en.wikipedia.org/wiki/Cartesian_coordinate_system.

The classic overlay analysis and other geometric computations is where Shapely shines using the GEOS library as its workhorse in the background.

As for matplotlib (http://matplotlib.org/), it is the plotting engine that renders nice graphs and data to your screen as an image or scalable vector graphic (svg). The uses of matplotlib are only limited to your imagination. So, like the name partially implies, matplotlib enables you to plot your data on a graph or even on a map. For those of you familiar with MATLAB, you will find matplotlib quite similar in functionality.

The descartes library provides a nicer integration of Shapely geometry objects with Matplotlib. Here, you will see that descartes opens the fill and patch of matplotlib plots to work with the geometries from Shapely and saves you from typing them individually.

Getting ready

To prepare for installation, it is necessary to install some global packages, such as libgeos_c, as these are required by Shapely. NumPy is also a requirement that we have already met and is also used by Shapely.

Install the requirements of matplotlib from the command line like this:

$ sudo apt-get install freetype* libpng-dev libjpeg8-dev

These are the dependencies of matplotlib, which can be seen on a Ubuntu 14.04 machine.

How to do it...

Follow these instructions:

  1. Run pip to install shapely:
    $ pip install shapely
    
  2. Run pip to install matplotlib:
    $ pip install matplotlib
    
  3. Finally, run pip to install descartes:
    $ pip install descartes
    

Another test to see if all has gone well is to simply enter the Python console and try to import the packages, and if no errors occur, your console should show an empty Python cursor. The output should look like what is shown in the following code:

(pygeoan_cb)mdiener@mdiener-VirtualBox:~/venv$ python
Python 2.7.3 (default,  Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type “help”,  “copyright”, “credits”, or  “license” for more information.
>>> import shapely
>>> import matplotlib
>>> import descartes
>>>

# type exit() to return
>>> exit()

If any errors occur, Python usually provides some good clues as to where the problem is located and there is always Stack Overflow. For example, have a look at http://stackoverflow.com/questions/19742406/could-not-find-library-geos-c-or-load-any-of-its-variants/23057508#2305750823057508.

How it works...

Here, the order in which you install the packages is very important. The descartes package depends on matplotlib, and matplotlib depends on NumPy plus freetype and libpng. This narrows you down to installing NumPy first, then matplotlib and its dependencies, and finally, descartes.

The installation itself is simple with pip and should be quick and painless. The tricky parts occur if libgeos_c is not installed properly, and you might need to install the libgeos-dev library.

Installing pyshp, geojson, and pandas

These specific libraries are for specific formats that make our life easier and simpler than using GDAL for some projects. pyshp will work with shapefiles, geojson with GeoJSON, and pandas with all other textual data types in a structured manner.

pyshp is pure Python and is used to import and export shapefiles; you can find the source code for pyshp here at https://github.com/GeospatialPython/pyshp. The pyshp library's sole purpose is to work with shapefiles. GDAL will be used to do most of our data's in/out needs, but sometimes, a pure Python library is simpler when working with shapefiles.

geojson is the name of a Python library and also a format, making it a little confusing to understand. The GeoJSON format (http://geojson.org) is becoming ever more popular and to this extent, we use the Python geojson library to handle its creation. You will find it on Python Package Index (PyPI) if you search for geojson. As you would expect, this will help us create all the different geometry types supported in the GeoJSON specifications.

pandas (http://pandas.pydata.org) is a data analysis library that structures your data in a spreadsheet-like manner for further computations. Since our geospatial data comes from a broad set of sources and formats, such as CSV, pandas helps work with the data with minimal effort.

Getting ready

Enter your virtual environment using the following command:

$ workon pygeoan_cb

Your prompt should now look like this:

(pygeoan_cb)mdiener@mdiener-VirtualBox:~$

How to do it...

The three installations are as follows:

  1. Pyshp will first be installed by simply using pip as follows:
    $ pip install pyshp
    
  2. Next, the geojson library will be installed using pip:
    $ pip install geojson
    
  3. Finally, pip will install pandas:
    $ pip install pandas
    

To test your installation of pyshp, use the import shapefile type. The output should look like what is shown in the following output:

(pygeoan_cb)mdiener@mdiener-VirtualBox:~/venv$ python
Python 2.7.3 (default,  Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type “help”,  “copyright”, “credits”, or  “license” for more information.
>> import shapefile
>> import geojson
>> import pandas

Note

The import shapefile statement imports the pyshp library; unlike the other libraries, the import name is not the same as the installation name.

How it works...

As seen in the other modules, we've used the standard installation pip package to execute installations. There are no other dependencies to worry about, making for fast progress.

Installing SciPy, PySAL, and IPython

SciPy is a collection of Python libraries, including SciPy library, matplotlib, pandas, SymPy, and IPython. The SciPy library itself is used for many operations, but we are particularly interested in the spatial module. This module can do many things including running a nearest neighbor query.

PySAL is a geospatial computing library that's used for spatial analysis. Creating models and running simulations directly from Python code are some of the many library functions that PySAL offers. PySAL is a library that, when put together with our visualization tools such as matplotlib, gives us a great tool.

IPython is a Python interpreter for a console that replaces the normal Python console you may be used to when running and testing Python code from your terminal. This is really just an advanced interpreter with some cool features, such as Tab completion, which means that beginners can get commands quickly by typing a letter and hitting Tab. The IPython notebooks help share code in the form of a web page, including code, images, and more without any installation.

Getting ready

The dependency jungle we looked at earlier is back and we need three more universal installations to our Ubuntu system using apt-get install as follows:

$ sudo apt-get install libblas-dev liblapack-dev gfortran

Note

Windows and Mac users can use a full installer (http://www.scipy.org/install.html), such as Anaconda or Enthought Canopy, which will perform all the installation dependencies for you in one go.

Three dependencies are used for the SciPy installation. PySAL depends on SciPy so make sure to install SciPy first. Only IPython does not need any extra installations.

Start up your Python virtual environment with the following code:

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

How to do it...

Let's look at these steps:

  1. First, we'll install SciPy since PySAL depends on it. This will take a while to install; it took my machine 5 minutes to go through so take a break:
    $ pip install scipy
    
  2. PySAL can be installed super quickly using pip:
    $ pip install pysal
    
  3. As usual, we'd like to see whether everything's working, so let's fire up the Python shell as follows:
    (pygeoan_cb)mdiener@mdiener-VirtualBox:~$python
    >>> import scipy
    >>> import pysal
    >>>
    
  4. IPython is to be installed globally or inside the virtual environment using pip as follows:
    $ pip install ipython
    

How it works...

SciPy and PySAL libraries are both geared to help accomplish various spatial analysis duties. The choice of tool is based on the task at hand, so make sure that you check which library offers what function at the command prompt as follows:

>>> from scipy import spatial
>>> help(spatial)

The output should look like what is shown in the following screenshot:

How it works...

Installing GDAL and OGR

Converting formats is boring, repetitive, and is one of the many, many responsibilities that the GDAL library provides, not to mention format transformations. However, GDAL also shines with regard to other geospatial functions, such as getting the current projections of a Shapefile or generating contours from elevation data. So, to only say that GDAL is a transformation library would be wrong; it really is so much more. The father of GDAL, Frank Warmerdam, deserves credit for starting it all off, and the GDAL project is now part of the OSGEO (Open Source Geospatial Foundation, refer to www.osgeo.org).

Note

The GDAL installation includes OGR; there is no extra installation required.

Currently, GDAL covers working with raster data, and OGR covers working with vector data. With GDAL 2.x now here, the two sides, raster and vector, are merged under one hat. GDAL and OGR are the so-called Swiss Army knives of geospatial data transformations, covering over 200 different spatial data formats.

Getting ready

GDAL isn't known to be the friendliest beast to install on Windows, Linux, or OSX. There are many dependencies and even more ways to install them. The descriptions are not all very straightforward. Keep in mind that this description is just one way of doing things and will not always work on all machines, so please refer to the online instructions for the latest and best ways to get your system up and running.

To start with, we will install some dependencies globally on our machine. After the dependencies have been installed, we will go into the global installation of GDAL for Python in our global site packages.

How to do it...

To globally install GDAL into our Python site packages, we will proceed with the following steps:

  1. The following command is used when installing build and XML tools:
    $ sudo apt-get install -y build-essentiallibxml2-dev libxslt1-dev
    
  2. Install the GDAL development files using the following command:
    $ sudo apt-get install libgdal-dev # install is 125MB
    
  3. This following command will install GDAL package in the main Python package. This means that GDAL will be installed globally. The global installation of GDAL is usually not a bad thing since, as far as I am aware, there are no backward incompatible versions, which is very rare these days. The installation of GDAL directly and only in virtualenv is painful, to say the least, and if you are interested in attempting it, I've mentioned some links for you to try out.
    $ sudo apt-get install python-gdal
    

    Note

    If you would like to attempt the installation inside your virtual environment, please take a look at this Stack Overflow question at http://gis.stackexchange.com/questions/28966/python-gdal-package-missing-header-file-when-installing-via-pip.

  4. To get GDAL in the Python virtual environment, we only need to run a simple virtualevnwrapper command:
    toggleglobalsitepackages
    

    Make sure you have your virtual environment activated as follows:

    mdiener@mdiener-VirtualBox:~$ workon pygeoan_cb
    (pygeoan_cb)mdiener@mdiener-VirtualBox:~$
    
  5. Now, activate the global Python site packages in your current virtual environment:
    (pygeoan_cb)mdiener@mdiener-VirtualBox:~$ toggleglobalsitepackages
    enable global site-packages
    
  6. The final check is to see if GDAL is available as follows:
    $ python
    >>> import gdal
    >>>
    
  7. No errors have been found and GDAL is ready for action.

Windows 7 plus users should use the OSGeo4W windows installer (https://trac.osgeo.org/osgeo4w/).Find the following section on the web page and download your Windows version in 32-bit or 64-bit. Follow the graphical installer instructions and the GDAL installation will then be complete.

Tip

Windows users can also directly get binaries if all fails at http://www.gisinternals.com/sdk/. This installer should help avoid any other Windows specific problems that can arise and this site can help get you going in the right direction.

How it works...

The GDAL installation encompasses both the raster (GDAL) and vector (OGR) tools in one. Within the GDAL install are five modules that can be separately imported into your project depending on your needs:

>>> from osgeo import gdal
>>> from osgeo import ogr
>>> from osgeo import osr
>>> from osgeo import gdal_array
>>> from osgeo import gdalconst
>>> python
>>> import osgeo
>>> help(osgeo)

To see what packages are included with your Python GDAL installation, we use the Python built-in help function to list what the OSGeo module has to offer. This is what you should see:

NAME
    osgeo - # __init__ for osgeo package.
FILE
    /usr/lib/python2.7/dist-packages/osgeo/__init__.py
MODULE DOCS
    http://docs.python.org/library/osgeo
PACKAGE CONTENTS
    _gdal
    _gdal_array
    _gdalconst
    _ogr
    _osr
    gdal
    gdal_array
    gdalconst
    gdalnumeric
    ogr
    osr
DATA
    __version__ = '1.10.0'
    version_info = sys.version_info(major=2, minor=7, micro=3, releaseleve...
VERSION
    1.10.0
(END)

At the time of writing this, the GDAL version is now bumped up to 2.0, and in developer land, this is old even before it gets printed. Beware that the GDAL 2.0 has compatibility issues and for this book, version 1.x.x is recommended.

See also

The http://www.gdal.org homepage is always the best place for reference regarding any information about it. The OSGEO includes GDAL as a supported project, and you can find more information on it at http://www.osgeo.org.

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

Left arrow icon Right arrow icon

Description

Geospatial development links your data to places on the Earth’s surface. Its analysis is used in almost every industry to answer location type questions. Combined with the power of the Python programming language, which is becoming the de facto spatial scripting choice for developers and analysts worldwide, this technology will help you to solve real-world spatial problems. This book begins by tackling the installation of the necessary software dependencies and libraries needed to perform spatial analysis with Python. From there, the next logical step is to prepare our data for analysis; we will do this by building up our tool box to deal with data preparation, transformations, and projections. Now that our data is ready for analysis, we will tackle the most common analysis methods for vector and raster data. To check or validate our results, we will explore how to use topology checks to ensure top-quality results. This is followed with network routing analysis focused on constructing indoor routes within buildings, over different levels. Finally, we put several recipes together in a GeoDjango web application that demonstrates a working indoor routing spatial analysis application. The round trip will provide you all the pieces you need to accomplish your own spatial analysis application to suit your requirements.

Who is this book for?

If you are a student, teacher, programmer, geospatial or IT administrator, GIS analyst, researcher, or scientist looking to do spatial analysis, then this book is for you. Anyone trying to answer simple to complex spatial analysis questions will get a working demonstration of the power of Python with real-world data. Some of you may be beginners with GIS, but most of you will probably have a basic understanding of geospatial analysis and programming.

What you will learn

  • Discover the projection and coordinate system information of your data and learn how to transform that data into different projections
  • Import or export your data into different data formats to prepare it for your application or spatial analysis
  • Use the power of PostGIS with Python to take advantage of the powerful analysis functions
  • Execute spatial analysis functions on vector data including clipping, spatial joins, measuring distances, areas, and combining data to new results
  • Create your own set of topology rules to perform and ensure quality assurance rules in Python
  • Find the shortest indoor path with network analysis functions in easy, extensible recipes revolving around all kinds of network analysis problems
  • Visualize your data on a map using the visualization tools and methods available to create visually stunning results
  • Build an indoor routing web application with GeoDjango to include your spatial analysis tools built from the previous recipes
Estimated delivery fee Deliver to Lithuania

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 30, 2015
Length: 310 pages
Edition : 1st
Language : English
ISBN-13 : 9781783555079
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Lithuania

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

Product Details

Publication date : Nov 30, 2015
Length: 310 pages
Edition : 1st
Language : English
ISBN-13 : 9781783555079
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 125.97
Python Geospatial Development
€41.99
Python Geospatial Analysis Cookbook
€41.99
Learning Geospatial Analysis with Python-Second Edition
€41.99
Total 125.97 Stars icon

Table of Contents

14 Chapters
1. Setting Up Your Geospatial Python Environment Chevron down icon Chevron up icon
2. Working with Projections Chevron down icon Chevron up icon
3. Moving Spatial Data from One Format to Another Chevron down icon Chevron up icon
4. Working with PostGIS Chevron down icon Chevron up icon
5. Vector Analysis Chevron down icon Chevron up icon
6. Overlay Analysis Chevron down icon Chevron up icon
7. Raster Analysis Chevron down icon Chevron up icon
8. Network Routing Analysis Chevron down icon Chevron up icon
9. Topology Checking and Data Validation Chevron down icon Chevron up icon
10. Visualizing Your Analysis Chevron down icon Chevron up icon
11. Web Analysis with GeoDjango Chevron down icon Chevron up icon
A. Other Geospatial Python Libraries Chevron down icon Chevron up icon
B. Mapping Icon Libraries Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(5 Ratings)
5 star 40%
4 star 60%
3 star 0%
2 star 0%
1 star 0%
Christian S. Dec 14, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Chapter 1 does a good job in getting you up and running with Python and the main libraries used in the following chapters.Chapter 2 explains coordinate systems and introduces Shapefile and GeoJSON file formats. Chapter 3 continues explaining image formats such as raster and vector. Also we setup our PostgreSQL and PostGIS. The instruction are clear and easy to follow. An interesting example is the conversion of an OpenStreeMap to a Shapefile. In Chapter 4 the focus is on PostGIS.Chapter 5, 6 and 7 deal with vector and geometry analysis. There are examples to calculate intersections, distances, and operations between polygons.Chapter 8 explains network analysis. There are very interesting examples on how to calculate the shortest path and an example to calculate indoor route walk time. Definitely this was one of the most interesting chapters.Chapter 9 deals with topology and validations rules. It has several example algorithms to validate rules.Chapter 10 and 11 finalize the implementation of the previous chapters, dealing with the final presentation through visualizations for the web.In summary I would recommend this book, it has enough content to serve as a reference to find examples in which you can dig deeper. This is a more practical book in the sense that you will not find detailed explanations of the algorithms, and it uses several third party libraries in the examples to get the job done.One thing it could have been improved is the layout of the source code.
Amazon Verified review Amazon
DC_ Jan 05, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The first chapter regarding getting everything setup is quite thorough regarding setting up the various Python libraries that are needed throughout the book. I found chapter 8 on network analysis very useful and interesting. This books gives a good grounding with code examples on which to build.
Amazon Verified review Amazon
Amazon Kunde Jun 04, 2019
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
A very helpful book, gives practical directions tp script useful applications within the field of geospatial analyses. However, some scripts could be a bit better explained for real beginners.
Amazon Verified review Amazon
Amazon Customer Dec 28, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Great introduction to a variety of Python libraries available for geospatial analysis. Both vector and raster analysis is covered with interesting examples. Many of the recipes will act as a base for the reader to take snippets from and build upon to implement into their own workflows. Algorithms and code not heavily explained which might not be great for complete beginners. Overall I recommend this book as it will open your eyes to some of the geospatial analysis techniques that you may not have realised were easy to implement outside of a standard GIS software package.
Amazon Verified review Amazon
USHANT SUMAN Dec 07, 2015
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Nice book readymade recipes for many day to day work.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela