I try to read in an .osm-file with python and GDAL/OGR, and then I want to save way IDs and associated node coordinates in a dictionary. For some strange reason, OGR seems to skip either entire ways or some nodes of a way.

I get different results when using an ordinary XML-Parser to read in the file - no ways or nodes of ways are skipped.

Code used with OGR:

count = 0
for wayFeature in layer2:
    processWay(wayFeature) # do smthg with the feature

Code used with ElementTree to parse xml:

for way in root.findall('way'):
    id = way.get('id')
    processWay(way) #do smthg with the way
    count = count+1

With ogr,I get 253 ways, with the XMLParser, I get 794 - a huge difference. 794 is what I expect,as I did a find&count on my .osm file.

How can that be?

I already set the osmconf.ini file to not ignore any nodes or ways,no matter if they have relevant tags or not.

Does it maybe have to do with the way's IDs in the file? I already played around with a dummy file, OGR sometimes seems skip ways where ID's are not ascending...but not always.

I could use the XML parser, but OGR has some nice features like filters and intersection tests, which I would like to use.

  • Do you have configured your osmconf.ini file so that it creates the "other_tags" field? Otherwise features with no matching tags are dropped. It may also be that you are counting also the ways which are used for building polygons but GDAL does not make linestrings from those. Could you make some statistics of your sample and check how many nodes, ways, and polygon relations there are. – user30184 Sep 6 '16 at 14:40
  • 1
    You should put some sample data available and comparison of the result that GDAL gives you and what you consider correct. Have you already made a test with JOSM? Also a quick look to documentation and sources of Osmium would for sure be informative osmcode.org/libosmium. – user30184 Sep 6 '16 at 16:09
  • @user30184: Yes, the "other_tags" field is created, but I did not think of the polygons, maybe that solves the mystery. I will check it out tomorrow and give feedback. And also thanks for the tip with JOSM! – Luigi_Papardelle Sep 6 '16 at 21:28
  • @user30184: You were absolutely correct, gdal is skipping closed ways which represent polygons in an osm-file.Thanks a lot! Furthermore, I found out why sometimes nodes of a way are "skipped": it only affects ways which are "cut off" because they partly lie outside of the geographic region described in my .osm file. (So if a way consists of 14 nodes, but it runs across the boundary of my region and f.ex. only 5 of the 14 nodes are inside my region, gdal considers only those 5 nodes, ) – Luigi_Papardelle Sep 7 '16 at 12:35
  • One more comment for everyone who might be interested: The osm driover of gdal seems to skip only closed ways which are explicitly tagged as polygons. To get only open ways, I had to check for each way individually if the first and last node id is different and only process those ones. – Luigi_Papardelle Sep 9 '16 at 12:02

Your Answer

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

Browse other questions tagged or ask your own question.