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

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