3

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'

  • Although I doubt it is the cause of your problem, is there any particular reason you are using GDAL 1.6 bindings? I think you may be better off with 1.7 or even 1.8. Just my 2 cents. – user890 Jun 16 '11 at 1:48
4

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

  • for Windows paths in Python, you can type this out as r'C:\myfiles\test.kml' otherwise the slashes are escaped fror something else. – Mike T Jun 16 '11 at 1:45
  • No it's not an error, it's a relative path... – Guilhain Jun 16 '11 at 6:42
  • I do that, it doesn't work : adresse = r'C:/Documents and Settings/Guilhain/Mes documents/My Dropbox/dev/seb_gare/point_station.kml' – Guilhain Jun 17 '11 at 9:29
  • Are you writing in an IDE that will allow you to step through the code? Take a step back and see if the output of 'datasource = driver.Open(adresse)' is what you want - it appears to be completing this step, but .GetLayer() is not set to operate on the correct input. – Radar Jun 17 '11 at 15:57
3

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

ogr.UseExceptions() 
  • It doesn't change – Guilhain Jun 16 '11 at 6:47
1

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.