Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Python Geospatial Analysis Cookbook
  • Toc
  • feedback
Python Geospatial Analysis Cookbook

Python Geospatial Analysis Cookbook

By : Diener
4.4 (5)
close
Python Geospatial Analysis Cookbook

Python Geospatial Analysis Cookbook

4.4 (5)
By: Diener

Overview of this book

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.
Table of Contents (15 chapters)
close
12
A. Other Geospatial Python Libraries
13
B. Mapping Icon Libraries
14
Index

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.

bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete