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

Preface

There are several powerful Python libraries for reading, processing, analyzing, and viewing geospatial data. There are also a number of websites that provide high-quality geospatial data, which you can use freely in your own projects. This data will often be the basis for your analysis, providing the shapes of countries, the positions of cities, the outlines of roads, and so on. Using this data in conjunction with the available geospatial libraries gives you a powerful toolkit for performing your own geospatial analysis using Python.

What this book covers

Chapter 1, Geospatial Analysis and Techniques, walks the reader through the process of downloading sample geospatial data, before writing a simple Python program to read and analyze that sample data.

Chapter 2, Geospatial Data, focuses on the data used for geospatial analysis: how to obtain it, why good data is important, the different formats that geospatial data can come in, and how to generate your own spatial datasets.

Chapter 3, Spatial Databases, provides a brief introduction to creating geospatial databases, how to store data in a spatially-enabled database, and how to perform efficient queries against that data.

Chapter 4, Creating Maps, looks at how to use the Mapnik library to produce great-looking maps.

Chapter 5, Analyzing Geospatial Data, guides the reader through the process of writing spatial analysis programs using Python. Based on the datasets downloaded in Chapter 2, Geospatial Data, and using the major Python libraries for geospatial analysis, this chapter uses a recipe-like format to solve a range of typical spatial analysis problems.

Chapter 6, Building a Complete Geospatial Analysis System, uses all the various libraries and techniques covered in the earlier chapters to build a complete geospatial analysis system.

What you need for this book

The code examples in this book use Python 2 to analyze geospatial data. Any reasonably powerful computer running Windows, Mac OS X, or Linux will be suitable. You will need to download and install the following software onto your computer:

  • Python version 2.7 or later, excluding Python 3.x

  • GDAL/OGR version 1.11 or later

  • GEOS version 3.4.2 or later

  • Shapely version 1.5.7 or later

  • PostgreSQL version 9.3 or later

  • PostGIS version 2.1.4 or later

  • psycopg2 version 2.5 or later

  • Mapnik version 2.2 or later

  • PROJ version 4.0 or later

  • PyProj version 1.9.4 or later

  • NetworkX version 1.9.1 or later

Full instructions for downloading, installing, and using these various tools and libraries are included in this book.

Who this book is for

If you are an experienced Python developer wishing to come up to speed with geospatial programming, or have specific spatial programming needs, then this book is for you. While familiarity with installing third-party Python libraries will be an advantage, no prior knowledge of geospatial programming concepts or techniques is required.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "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."

A block of code is set as follows:

import osgeo.ogr
shapefile = osgeo.ogr.Open("TM_WORLD_BORDERS-0.3.shp")
layer = shapefile.GetLayer(0)
for i in range(layer.GetFeatureCount()):
    feature = layer.GetFeature(i)
    feature_name = feature.GetField("NAME")
    geometry = feature.GetGeometryRef()
    geometry_type = geometry.GetGeometryName()
    print i, feature_name, geometry_type

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

from osgeo import ogr
driver = ogr.GetDriverByName("ESRI Shapefile")
dstFile = driver.CreateDataSource("test-shapefile")

Any command-line input or output is written as follows:

% python readRaster.py
-500 53081919
-84 1
-83 8
-82 9
-81 17
...
5241 1
5295 1
5300 1
5443 1

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking on the Next button moves you to the next screen."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.