Read KML file with GDAL/OGR for Python

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

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

python GDAL translate to convert and coloring

Updated March 03, 2018 02:22 AM

WMS Tutorial on Python Gdal

Updated February 28, 2019 14:22 PM

Module has no attribute Warp?

Updated July 28, 2017 20:22 PM

gdal.Warp workingType option default

Updated August 24, 2018 15:22 PM