[GIS] Read KML file with GDAL/OGR for Python

gdalkmlogrpython

I have file : test.kml and I try to open it with GDAL/OGR (1.6) librairie in Python (2.5).

But I don't succed can you help me ?

This is my code :

from osgeo import ogr

adresse = 'test.kml'
driver = ogr.GetDriverByName('KML')
datasource = driver.Open(adresse)
layer = datasource.GetLayer()
layerDefn = layer.GetLayerDefn()
featDefn = layer.GetLayerDefn()
feat = ogr.Feature(featDefn)
nbFeat = layer.GetFeatureCount()

print nbFeat

ERROR Traceback (most recent call
last): File "C:\Documents and
Settings\Guilhain\Mes documents\My
Dropbox\dev\test.py", line 6, in

layer = datasource.GetLayer() AttributeError: 'NoneType' object has
no attribute 'GetLayer'

Best Answer

The error you encountered shows that your value for "datasource" is not valid. Instead of using "adresse = 'test.kml'", try entering the full path to the xml file. For example "C:\myfiles\test.kml".