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 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.

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.