-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Python Geospatial Analysis Cookbook
By :

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).
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.
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.
To globally install GDAL into our Python site packages, we will proceed with the following steps:
$ sudo apt-get install -y build-essentiallibxml2-dev libxslt1-dev
$ sudo apt-get install libgdal-dev # install is 125MB
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
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.
toggleglobalsitepackages
Make sure you have your virtual environment activated as follows:
mdiener@mdiener-VirtualBox:~$ workon pygeoan_cb (pygeoan_cb)mdiener@mdiener-VirtualBox:~$
(pygeoan_cb)mdiener@mdiener-VirtualBox:~$ toggleglobalsitepackages enable global site-packages
$ python >>> import gdal >>>
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.
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.
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.
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.
Change the font size
Change margin width
Change background colour