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.
Let's read a sample point
shapefile using the OGR Python API. The sample shapefile can be downloaded as a ZIP file here: 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'll print the x and y value 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: ... geometry = feature.GetGeometryRef() ... print(geometry.GetX(), geometry...