OGR
We touched on OGR
as a way to handle WKT strings, but its real power is as a universal vector library. This book strives for pure Python solutions, but no single library even comes close to the variety of formats that OGR
can process.
OGR
is contained in the GDAL
library, which we can install with conda
:
conda install gdal
Let’s read an example point shapefile using the OGR
Python API. The example shapefile can be downloaded as a ZIP file from https://github.com/GeospatialPython/Learn/raw/master/point.zip.
This point shapefile has five points with single-digit, positive coordinates. The attributes list the order in which the points were created, making it useful for testing. This simple example will read in the point shapefile and loop through each feature; then, it will print the x
and y
values of each point, plus the value of the first attribute field:
from osgeo import ogr shp = ogr.Open("point.shp") layer = shp.GetLayer() for feature in layer...