Book Image

Python Geospatial Development - Second Edition - Second Edition

By : Erik Westra
Book Image

Python Geospatial Development - Second Edition - Second Edition

By: Erik Westra

Overview of this book

Geospatial development links your data to places on the Earth's surface. Writing geospatial programs involves tasks such as grouping data by location, storing and analyzing large amounts of spatial information, performing complex geospatial calculations, and drawing colorful interactive maps. In order to do this well, you'll need appropriate tools and techniques, as well as a thorough understanding of geospatial concepts such as map projections, datums and coordinate systems. Python Geospatial Development - Second Edition teaches you everything you need to know about writing geospatial applications using Python. No prior knowledge of geospatial concepts, tools or techniques is required. The book guides you through the process of installing and using various toolkits, obtaining geospatial data for use in your programs, and building complete and sophisticated geospatial applications in Python. Python Geospatial Development teaches you everything you need to know about writing geospatial applications using Python. No prior knowledge of geospatial concepts, tools or techniques is required. The book guides you through the process of installing and using various toolkits, obtaining geospatial data for use in your programs, and building complete and sophisticated geospatial applications in Python. This book provides an overview of the major geospatial concepts, data sources and toolkits. It teaches you how to store and access spatial data using Python, how to perform a range of spatial calculations, and how to store spatial data in a database. Because maps are such an important aspect of geospatial programming, the book teaches you how to build your own “slippy map” interface within a web application, and finishes with the detailed construction of a geospatial data editor using Geodjango. Whether you want to write quick utilities to solve spatial problems, or develop sophisticated web applications based around maps and geospatial data, this book includes everything you need to know.
Table of Contents (18 chapters)
Python Geospatial Development
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Applications of geospatial development


Let's take a brief look at some of the more common geospatial development tasks you might encounter.

Analyzing geospatial data

Imagine that you have a database containing a range of geospatial data for San Francisco. This database might include geographical features, roads, the location of prominent buildings, and other man-made features such as bridges, airports, and so on.

Such a database can be a valuable resource for answering various questions. For example:

  • What's the longest road in Sausalito?

  • How many bridges are there in Oakland?

  • What is the total area of the Golden Gate Park?

  • How far is it from the Pier 39 to the Moscone Center?

Many of these types of problems can be solved using tools such as the PostGIS spatially-enabled database. For example, to calculate the total area of the Golden Gate Park, you might use the following SQL query:

select ST_Area(geometry) from features
  where name = "Golden Gate Park";

To calculate the distance between two places, you first have to geocode the locations to obtain their latitude and longitude. There are various ways to do this; one simple approach is to use a free geocoding web service, such as this:

http://nominatim.openstreetmap.org/search?q=Pier 39, San Francisco,CA

This returns a latitude value of 37.82 and a longitude value of -122.42.

Note

These latitude and longitude values are in decimal degrees. If you don't know what these are, don't worry; we'll talk about decimal degrees in Chapter 2, GIS.

Similarly, we can find the location of the Moscone Center using this query:

http://nominatim.openstreetmap.org/search?q=Moscone Center, San Francisco,CA

This returns a latitude value of 37.80 and a longitude value of -122.44.

Now that we have the coordinates for the two desired locations, we can calculate the distance between them using the Proj Python library:

import pyproj

lat1,long1 = (37.82,-122.42)
lat2,long2 = (37.80,-122.44)

geod = pyproj.Geod(ellps="WGS84")
angle1,angle2,distance = geod.inv(long1, lat1, long2, lat2)

print "Distance is %0.2f meters" % distance

This prints the distance between the two points:

Distance is 2833.64 meters

Note

Don't worry about the "WGS84" reference at this stage; we'll look at what this means in Chapter 2, GIS.

Of course, you wouldn't normally do this sort of analysis on a one-off basis like this—it's much more common to create a Python program that will answer these sorts of questions for any desired set of data. You might, for example, create a web application that displays a menu of available calculations. One of the options in this menu might be to calculate the distance between two points; when this option is selected, the web application would prompt the user to enter the two locations, attempt to geocode them by calling an appropriate web service (and display an error message if a location couldn't be geocoded), then calculate the distance between the two points using Proj, and finally display the results to the user.

Alternatively, if you have a database containing useful geospatial data, you could let the user select the two locations from the database rather than typing in arbitrary location names or street addresses.

However you choose to structure it, performing calculations like this will usually be a major part of your geospatial application.

Visualizing geospatial data

Imagine that you wanted to see which areas of a city are typically covered by a taxi during an average working day. You might place a GPS recorder into a taxi and leave it to record the taxi's position over several days. The results would be a series of timestamps, latitude and longitude values as follows:

2010-03-21 9:15:23  -38.16614499  176.2336626
2010-03-21 9:15:27  -38.16608632  176.2335635
2010-03-21 9:15:34  -38.16604198  176.2334771
2010-03-21 9:15:39  -38.16601507  176.2333958
...

By themselves, these raw numbers tell you almost nothing. But when you display this data visually, the numbers start to make sense:

You can immediately see that the taxi tends to go along the same streets again and again. And if you draw this data as an overlay on top of a street map, you can see exactly where the taxi has been:

(Street map courtesy of http://openstreetmap.org).

While this is a very simple example, visualization is a crucial aspect of working with geospatial data. How data is displayed visually, how different data sets are overlaid, and how the user can manipulate data directly in a visual format are all going to be major topics of this book.

Creating a geospatial mash-up

The concept of a "mash-up" has become popular in recent years. Mash-ups are applications that combine data and functionality from more than one source. For example, a typical mash-up may combine details of houses for rent in a given city, and plot the location of each rental on a map, as follows:

This example comes from http://housingmaps.com.

The Google Maps API has been immensely popular in creating these types of mash-ups. However, Google Maps has some serious licensing and other limitations—as does Google's main competitor, Bing. Fortunately, these are not the only options; tools such as Mapnik, Openlayers, and MapServer, to name a few, also allow you to create mash-ups that overlay your own data onto a map.

Most of these mash-ups run as web applications across the Internet, running on a server that can be accessed by anyone who has a web browser. Sometimes the mash-ups are private, requiring password access, but usually they are publicly available and can be used by anyone. Indeed, many businesses (such as the housing maps site shown in the previous image) are based on freely-available geospatial mash-ups.