Read KML file with GDAL/OGR for Python

by Guilhain   Last Updated July 12, 2019 01:22 AM

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\", line 6, in layer = datasource.GetLayer() AttributeError: 'NoneType' object has no attribute 'GetLayer'

Tags : python gdal kml ogr

Answers 3

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".

June 15, 2011 17:48 PM

Try adding this to your code to hopefully find out what might be going on with the datasource not being created:

Howard Butler
Howard Butler
June 15, 2011 20:43 PM

You must provide a name argument to the GetLayer() call, such as datasource.GetLayer("foo"). or use GetLayerByIndex(layer_number_starting_with_0)

Even Rouault
Even Rouault
June 25, 2011 18:32 PM

Related Questions

Updated February 21, 2019 10:22 AM

Updated March 03, 2018 02:22 AM

Updated February 28, 2019 14:22 PM

Updated July 28, 2017 20:22 PM

Updated August 24, 2018 15:22 PM