0

I am trying to read in an OSM file with OGR.

import ogr
driverOSM = ogr.GetDriverByName('OSM')
roadsDs = driverOSM.Open('data/map.osm', 0)
roadsLayer = roadsDs.GetLayer()

The feature count returns -1

print roadsLayer.GetFeatureCount()

When I loop over the features only points are returned, eventhough I know there are ways too (tested the data in QGIS).

featureStand = roadsLayer.GetNextFeature()
while featureStand:
  geomStand = featureStand.GetGeometryRef()
  print geomStand
  featureStand.Destroy()
  featureStand = roadsLayer.GetNextFeature()

Can anybody reproduce that or sees what I am doing wrong?


Python 2.7.5, GDAL 1.10.1, did not modify osmconf.ini

  • 1
    -1 means feature count not known – Mike T Oct 24 '13 at 20:32
  • @MikeToews, that makes sense. Still wondering why I GetGeometryRef only finds points. – ustroetz Oct 24 '13 at 20:47
  • 1
    There are other geometry types, but they are under different layers. Try roadsLayer.GetLayerCount() or possibly roadsDs.GetLayer(3) and you will see other geometry types. – Mike T Oct 24 '13 at 22:04
  • That's interesting! roadsLayer.GetLayerCount() acutally gives me five layers. GetLayer(0) has the points and GetLayer(1) has the ways. Since I just used GetLayer() it chooesed the first layer (0) which are the points. Thanks @MikeToews! Do you want to post it as an answer? – ustroetz Oct 24 '13 at 22:15
1

An OSM file according to OGR contains five layers, one for each category:

  1. points : "node" features that have significant tags attached.
  2. lines : "way" features that are recognized as non-area.
  3. multilinestrings : "relation" features that form a multilinestring(type = 'multilinestring' or type = 'route').
  4. multipolygons : "relation" features that form a multipolygon (type = 'multipolygon' or type = 'boundary'), and "way" features that are recognized as area.
  5. other_relations : "relation" features that do not belong to the above 2 layers.

The GetLayer() function actually takes a base-0 argument, so for the second layer, lines:

roadsLayer = roadsDs.GetLayer(1)
print(roadsLayer.GetName())  # lines

Although it isn't documented, it appears this driver is unable to get the feature count from a flat file, as -1 means feature count not known. Using a while-loop is probably the best way to go through the elements of each layer, as you have discovered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.