Book Image

Python Geospatial Analysis Essentials

By : Erik Westra
Book Image

Python Geospatial Analysis Essentials

By: Erik Westra

Overview of this book

Table of Contents (13 chapters)
Python Geospatial Analysis Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up your Python installation


To start analyzing geospatial data using Python, we are going to make use of two freely available third-party libraries:

  • GDAL: The Geospatial Data Abstraction Library makes it easy for you to read and write geospatial data in both vector and raster format.

  • Shapely: As the name suggests, this is a wonderful library that enables you to perform various calculations on geometric shapes. It also allows you to manipulate shapes, for example, by joining shapes together or by splitting them up into their component pieces.

Let's go ahead and get these two libraries installed into your Python setup so we can start using them right away.

Installing GDAL

GDAL, or more accurately the GDAL/OGR library, is a project by the Open Source Geospatial Foundation to provide libraries to read and write geospatial data in a variety of formats. Historically, the name GDAL referred to the library to read and write raster-format data, while OGR referred to the library to access vector-format data. The two libraries have now merged, though the names are still used in the class and function names, so it is important to understand the difference between the two.

A default installation of GDAL/OGR allows you to read raster geospatial data in 100 different formats, and write raster data in 71 different formats. For vector data, GDAL/OGR allows you read data in 42 different formats, and write in 39 different formats. This makes GDAL/OGR an extremely useful tool to access and work with geospatial data.

GDAL/OGR is a C++ library with various bindings to allow you to access it from other languages. After installing it on your computer, you typically use the Python bindings to access the library using your Python interpreter. The following diagram illustrates how these various pieces all fit together:

Let's go ahead and install the GDAL/OGR library now. The main website of GDAL (and OGR) can be found at http://gdal.org.

How you install it depends on which operating system your computer is using:

  • For MS Windows machines, you can install GDAL/OGR using the FWTools installer, which can be downloaded from http://fwtools.maptools.org.

    Alternatively, you can install GDAL/OGR and Shapely using the OSGeo installer, which can be found at http://trac.osgeo.org/osgeo4w.

  • For Mac OS X, you can download the complete installer for GDAL and OGR from http://www.kyngchaos.com/software/frameworks.

  • For Linux, you can download the source code to GDAL/OGR from the main GDAL site, and follow the instructions on the site to build it from source. You may also need to install the Python bindings for GDAL and OGR.

Once you have installed it, you can check that it's working by firing up your Python interpreter and typing import osgeo.gdal and then import osgeo.ogr. If the Python command prompt reappears each time without an error message, then GDAL and OGR were successfully installed and you're all ready to go:

>>>import osgeo.gdal
>>>import osgeo.ogr
>>>

Installing Shapely

Shapely is a geometry manipulation and analysis library. It is based on the Geometry Engine, Open Source (GEOS) library, which implements a wide range of geospatial data manipulations in C++. Shapely provides a Pythonic interface to GEOS, making it easy to use these manipulations directly within your Python programs. The following illustration shows the relationship between your Python code, the Python interpreter, Shapely, and the GEOS library:

The main website for Shapely can be found at http://pypi.python.org/pypi/Shapely.

The website has everything you need, including complete documentation on how to use the library. Note that to install Shapely, you need to download both the Shapely Python package and the underlying GEOS library. The website for the GEOS library can be found at http://trac.osgeo.org/geos.

How you go about installing Shapely depends on which operating system your computer is using:

  • For MS Windows, you should use one of the prebuilt installers available on the Shapely website. These installers include their own copy of GEOS, so there is nothing else to install.

  • For Mac OS X, you should use the prebuilt GEOS framework available at http://www.kyngchaos.com/software/frameworks.

    Tip

    Note that if you install the GDAL Complete package from the preceding website, you will already have GEOS installed on your computer.

    Once GEOS has been installed, you can install Shapely using pip, the Python package manager:

    pip install shapely
    

    If you don't have pip installed on your computer, you can install it by following the instructions at https://pip.pypa.io/en/latest/installing.html.

  • For Linux machines, you can either download the source code from the GEOS website and compile it yourself, or install a suitable RPM or APT package which includes GEOS. Once this has been done, you can use pip install shapely to install the Shapely library itself.

Once you have installed it, you can check that the Shapely library is working by running the Python command prompt and typing the following command:

>>> import shapely.geos
>>>

If you get the Python command prompt again without any errors, as in the preceding example, then Shapely has been installed successfully and you're all set to go.