Shapely is a very capable library for performing various calculations on geo-spatial data. Let's put it through its paces with a complex, real-world problem.
The U.S. Census Bureau makes available a Shapefile containing something called Core Based Statistical Areas (CBSAs), which are polygons defining urban areas with a population of 10,000 or more. At the same time, the GNIS website provides lists of placenames and other details. Using these two datasources, we will identify any parks within or close to an urban area.
Because of the volume of data we are potentially dealing with, we will limit our search to California. Feel free to download the larger data sets if you want, though you will have to optimize the code or your program will take a very long time to check all the CBSA polygon/placename combinations.
tl_2009_06_cbsa.zip
. Once the file has downloaded, uncompress it and place the resulting Shapefile into a convenient location so that you can work with it. CA_Features_XXX.zip
file. Do so, then decompress it and place the resulting CA_Features_XXX.txt
file into a convenient place.shapefile = osgeo.ogr.Open("tl_2009_06_cbsa.shp") layer = shapefile.GetLayer(0) for i in range(layer.GetFeatureCount()): feature = layer.GetFeature(i) geometry = feature.GetGeometryRef() ...
Make sure you add directory paths to your osgeo.ogr.Open()
statement (and to the file()
statement below) to match where you've placed these files.
wkt = geometry.ExportToWkt() shape = shapely.wkt.loads(wkt)
CA_Features_XXX.txt
file to identify the features marked as a park. For each of these features, we want to extract the name of the feature and its associated latitude and longitude. Here's how we might do this:f = file("CA_Features_XXX.txt", "r") for line in f.readlines(): chunks = line.rstrip().split("|") if chunks[2] == "Park": name = chunks[1] latitude = float(chunks[9]) longitude = float(chunks[10]) ...
Remember that the GNIS placename database is a pipe-delimited text file. That's why we have to split the line up using line.rstrip().split("|")
.
# findNearbyParks.py import osgeo.ogr import shapely.geometry import shapely.wkt MAX_DISTANCE = 0.1 # Angular distance; approx 10 km. print "Loading urban areas..." urbanAreas = {} # Maps area name to Shapely polygon. shapefile = osgeo.ogr.Open("tl_2009_06_cbsa.shp") layer = shapefile.GetLayer(0) for i in range(layer.GetFeatureCount()): feature = layer.GetFeature(i) name = feature.GetField("NAME") geometry = feature.GetGeometryRef() shape = shapely.wkt.loads(geometry.ExportToWkt()) dilatedShape = shape.buffer(MAX_DISTANCE) urbanAreas[name] = dilatedShape print "Checking parks..." f = file("CA_Features_XXX.txt", "r") for line in f.readlines(): chunks = line.rstrip().split("|") if chunks[2] == "Park": parkName = chunks[1] latitude = float(chunks[9]) longitude = float(chunks[10]) pt = shapely.geometry.Point(longitude, latitude) for urbanName,urbanArea in urbanAreas.items(): if urbanArea.contains(pt): print parkName + " is in or near " + urbanName f.close()
Don't forget to change the name of the CA_Features_XXX.txt
file to match the actual name of the file you downloaded. You may also need to change the path names to the tl_2009_06_CBSA.shp
file and the CA_Features
file if you placed them in a different directory.
If you run this program, you will get a master list of all the parks that are in or close to an urban area:
% python findNearbyParks.py Loading urban areas... Checking parks... Imperial National Wildlife Refuge is in or near El Centro, CA TwinLakesStateBeach is in or near Santa Cruz-Watsonville, CA AdmiralWilliamStandleyState Recreation Area is in or near Ukiah, CA Agate Beach County Park is in or near San Francisco-Oakland-Fremont, CA ...
Note that our program uses angular distances to decide if a park is in or near a given urban area. We looked at angular distances in Chapter 2. An angular distance is the angle (in decimal degrees) between two rays going out from the center of the Earth to the Earth's surface. Because a degree of angular measurement (at least for the latitudes we are dealing with here) roughly equals 100 km on the Earth's surface, an angular measurement of 0.1 roughly equals a real distance of 10 km.
Using angular measurements makes the distance calculation easy and quick to calculate, though it doesn't give an exact distance on the Earth's surface. If your application requires exact distances, you could start by using an angular distance to filter out the features obviously too far away, and then obtain an exact result for the remaining features by calculating the point on the polygon's boundary that is closest to the desired point, and then calculating the linear distance between the two points. You would then discard the points that exceed your desired exact linear distance. Implementing this would be an interesting challenge, though not one we will examine in this book.
18.117.138.104