If you are interested in exploring the techniques used in this chapter further, you might like to challenge yourself with the following tasks:
Hint
You can start with the country bounding boxes we calculated earlier and then calculate the midpoint using this:
midLat = (minLat + maxLat) / 2 midLong = (minLong + maxLong) / 2
For an extra challenge, you could use Shapely's centroid() method to calculate a more accurate representation of each country's center. To do this, you would have to convert the country's outline into a Shapely geometry, calculate the centroid, and then convert the centroid back into an OGR geometry before saving it into the output shapefile.
Hint
Implementing this in an efficient way can be difficult. A good approach would be to identify the bounding box for each of the polygons that make up the country's outline and then iterate over the DEM coordinates within that bounding box. You could then check to see whether a given coordinate is actually inside the country's outline using polygon.contains(point) and only add the height to the histogram if the point is indeed within the country's outline.
Hint
One possibility might be to calculate the rectangular bounding box around each urban area and then expand that bounding box north, south, east, and west by the desired angular distance. You could then quickly exclude all the points which aren't in that bounding box before making the time-consuming call to polygon.contains(point).
Another way of optimizing this program is to exclude urban areas outside of California. To do this, you would have to find a shapefile of US states, download the outline for California, and only include urban areas which lie (fully or partially) inside the state boundary.
Hint
Remember that a country outline is a MultiPolygon, where each Polygon in the MultiPolygon represents a single island. You will need to extract the exterior ring from each of these individual island polygons and calculate the total length of the line segments within that exterior ring. You can then total the length of each individual island to get the length of the entire country's coastline.
pyproj to perform common operations such as those discussed in this chapter.Hint
Writing your own reusable library modules is a common programming tactic. Think about the various tasks we solved in this chapter and how they can be turned into generic library functions. For example, you might like to write a function named calcLineStringLength(), which takes a LineString and returns the total length of the LineString's segments, optionally transforming the LineString's coordinates into lat/long values before calling geod.inv().
You could then write a calcPolygonOutlineLength() function, which uses calcLineStringLength() to calculate the length of a polygon's outer ring.