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