Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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'

share|improve this question
    
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
add comment

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

share|improve this answer
    
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. –  Nick Ochoski Jun 17 '11 at 15:57
add comment

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

ogr.UseExceptions() 
share|improve this answer
    
It doesn't change –  Guilhain Jun 16 '11 at 6:47
add comment

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

share|improve this answer
1  
this isn't a problem for 1-layer data sources, as there is a default: GetLayer(self, iLayer=0) –  Mike T Jun 26 '11 at 6:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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