Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Python Geospatial Analysis Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
Python Geospatial Analysis Cookbook

Python Geospatial Analysis Cookbook

By : Diener
4.4 (5)
close
close
Python Geospatial Analysis Cookbook

Python Geospatial Analysis Cookbook

4.4 (5)
By: Diener

Overview of this book

Geospatial development links your data to places on the Earth’s surface. Its analysis is used in almost every industry to answer location type questions. Combined with the power of the Python programming language, which is becoming the de facto spatial scripting choice for developers and analysts worldwide, this technology will help you to solve real-world spatial problems. This book begins by tackling the installation of the necessary software dependencies and libraries needed to perform spatial analysis with Python. From there, the next logical step is to prepare our data for analysis; we will do this by building up our tool box to deal with data preparation, transformations, and projections. Now that our data is ready for analysis, we will tackle the most common analysis methods for vector and raster data. To check or validate our results, we will explore how to use topology checks to ensure top-quality results. This is followed with network routing analysis focused on constructing indoor routes within buildings, over different levels. Finally, we put several recipes together in a GeoDjango web application that demonstrates a working indoor routing spatial analysis application. The round trip will provide you all the pieces you need to accomplish your own spatial analysis application to suit your requirements.
Table of Contents (15 chapters)
close
close
12
A. Other Geospatial Python Libraries
13
B. Mapping Icon Libraries
14
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.

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY