Skip to content

Instantly share code, notes, and snippets.

@CMCDragonkai
Last active August 26, 2023 08:46
Show Gist options
  • Save CMCDragonkai/e7b15bb6836a7687658ec2bb3abd2927 to your computer and use it in GitHub Desktop.
Save CMCDragonkai/e7b15bb6836a7687658ec2bb3abd2927 to your computer and use it in GitHub Desktop.
GDAL OGR Iterate over all layers, features, fields and geometry #python #gdal #ogr
from osgeo import ogr
data = ogr.Open('/path/to/vector/file')
print('Data Name:', data.GetName())
# get a layer with GetLayer('layername'/layerindex)
for layer in data:
print('Layer Name:', layer.GetName())
print('Layer Feature Count:', len(layer))
# each layer has a schema telling us what fields and geometric fields the features contain
print('Layer Schema')
layer_defn = layer.GetLayerDefn()
for i in range(layer_defn.GetFieldCount()):
print(layer_defn.GetFieldDefn(i).GetName())
# some layers have multiple geometric feature types
# most of the time, it should only have one though
for i in range(layer_defn.GetGeomFieldCount()):
# some times the name doesn't appear
# but the type codes are well defined
print(layer_defn.GetGeomFieldDefn(i).GetName(), layer_defn.GetGeomFieldDefn(i).GetType())
# get a feature with GetFeature(featureindex)
# this is the one where featureindex may not start at 0
layer.ResetReading()
for feature in layer:
print('Feature ID:', feature.GetFID())
# get a metadata field with GetField('fieldname'/fieldindex)
print('Feature Metadata Keys:', feature.keys())
print('Feature Metadata Dict:', feature.items())
print('Feature Geometry:', feature.geometry())

OGR Types

import pprint
from osgeo import ogr
pprint.pprint(list(map(lambda f: (f, getattr(ogr, f)), list(filter(lambda x: x.startswith('wkb'), dir(ogr))))))

This gives us:

[('wkb25Bit', -2147483648),
 ('wkb25DBit', -2147483648),
 ('wkbCircularString', 8),
 ('wkbCircularStringM', 2008),
 ('wkbCircularStringZ', 1008),
 ('wkbCircularStringZM', 3008),
 ('wkbCompoundCurve', 9),
 ('wkbCompoundCurveM', 2009),
 ('wkbCompoundCurveZ', 1009),
 ('wkbCompoundCurveZM', 3009),
 ('wkbCurve', 13),
 ('wkbCurveM', 2013),
 ('wkbCurvePolygon', 10),
 ('wkbCurvePolygonM', 2010),
 ('wkbCurvePolygonZ', 1010),
 ('wkbCurvePolygonZM', 3010),
 ('wkbCurveZ', 1013),
 ('wkbCurveZM', 3013),
 ('wkbGeometryCollection', 7),
 ('wkbGeometryCollection25D', -2147483641),
 ('wkbGeometryCollectionM', 2007),
 ('wkbGeometryCollectionZM', 3007),
 ('wkbLineString', 2),
 ('wkbLineString25D', -2147483646),
 ('wkbLineStringM', 2002),
 ('wkbLineStringZM', 3002),
 ('wkbLinearRing', 101),
 ('wkbMultiCurve', 11),
 ('wkbMultiCurveM', 2011),
 ('wkbMultiCurveZ', 1011),
 ('wkbMultiCurveZM', 3011),
 ('wkbMultiLineString', 5),
 ('wkbMultiLineString25D', -2147483643),
 ('wkbMultiLineStringM', 2005),
 ('wkbMultiLineStringZM', 3005),
 ('wkbMultiPoint', 4),
 ('wkbMultiPoint25D', -2147483644),
 ('wkbMultiPointM', 2004),
 ('wkbMultiPointZM', 3004),
 ('wkbMultiPolygon', 6),
 ('wkbMultiPolygon25D', -2147483642),
 ('wkbMultiPolygonM', 2006),
 ('wkbMultiPolygonZM', 3006),
 ('wkbMultiSurface', 12),
 ('wkbMultiSurfaceM', 2012),
 ('wkbMultiSurfaceZ', 1012),
 ('wkbMultiSurfaceZM', 3012),
 ('wkbNDR', 1),
 ('wkbNone', 100),
 ('wkbPoint', 1),
 ('wkbPoint25D', -2147483647),
 ('wkbPointM', 2001),
 ('wkbPointZM', 3001),
 ('wkbPolygon', 3),
 ('wkbPolygon25D', -2147483645),
 ('wkbPolygonM', 2003),
 ('wkbPolygonZM', 3003),
 ('wkbPolyhedralSurface', 15),
 ('wkbPolyhedralSurfaceM', 2015),
 ('wkbPolyhedralSurfaceZ', 1015),
 ('wkbPolyhedralSurfaceZM', 3015),
 ('wkbSurface', 14),
 ('wkbSurfaceM', 2014),
 ('wkbSurfaceZ', 1014),
 ('wkbSurfaceZM', 3014),
 ('wkbTIN', 16),
 ('wkbTINM', 2016),
 ('wkbTINZ', 1016),
 ('wkbTINZM', 3016),
 ('wkbUnknown', 0),
 ('wkbXDR', 0)]

Not all of the above are actual types. The ones that are mostly relevant are from 1 to 7.

@naught101
Copy link

line 25, for feature in layer: doesn't seem to work if there is only one feature in a layer. I get

*** TypeError: iter() returned non-iterator of type 'Layer'

@ThomasHaarlem
Copy link

ThomasHaarlem commented Aug 26, 2023

it appears it starts at indexnumber 1, not 0.
I have a ogr.Layer with 8 items, when I use the "for feature in layer": I only get 7, missing the first item/object at indexrow [0].

Use:
# this is the one where featureindex may not start at 0
layer.ResetReading()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment