Wednesday, 13 November 2019

How to open OSM file in Python with GDAL


I am trying to open a *.osm file in Python with GDAL. But I doesn't work. What am I doing wrong?


import ogr
driver = ogr.GetDriverByName('OSM')
datasource = driver.Open('map.osm')
layer = datasource.GetLayer()


Error looks like this


>>> 

Traceback (most recent call last):
File ".../osm_reader.py", line 3, in
datasource = driver.Open('map.osm')
AttributeError: 'NoneType' object has no attribute 'Open'

I exported the data directly from the OSM Website. My Python Version is 2.7.5.


About my GDAL Version I am not sure. And this might be the problem.



Easy Install says


U:\>easy_install GDAL
Searching for GDAL
Best match: GDAL 1.10.1
Adding GDAL 1.10.1 to easy-install.pth file

Using c:\python27\lib\site-packages

GDAL Version says


>>> gdal.VersionInfo()

'1810'

Does 1810 mean GDAL 1.10.1? How do I get this to work?



Answer



>>> gdal.VersionInfo()
'1810'

This means indeed that the GDAL version Python is using is GDAL 1.8. So I re-installed GDAL for Python with this installation.


After that the GDAL version was up to date (1.10.1).


>>> gdal.VersionInfo()

'1100100'



Though now I got a new error:


>>> print ogr.Open('map.osm')
Warning 1: Cannot find osmconf.ini configuration file
ERROR 1: Could not parse configuration file for OSM import
None

After copying the osmconf.ini into my working directory it finally worked.



>>> print ogr.Open('map.osm')
0x02275518> >

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...