Reading and writing geospatial data using Python
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.
Reading vector data
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_typeLet's take a closer look at how this program works, and more generally, how to read vector-format data using the OGR library.
When reading geospatial data, the osgeo.ogr.Open() function takes just a single parameter: the name of the dataset to open. The OGR library loops through...