Since we will be using the GDAL/OGR library to access geospatial data, let's take a closer look at how you can read and write both vector-format and raster-format data using this library.
In the previous chapter, we wrote a simple program that reads the features out of a shapefile. Here is a copy of that program:
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
Let's take a closer look at how this program ...