[Gdal-dev] s-57 layer problem

Frank Warmerdam warmerdam at pobox.com
Wed Mar 31 12:45:34 EST 2004


Fulko van Westrenen wrote:
> Hello,
> 
> Until now I used a S-57 map-file containing all layers
> I needed. My real map consists of many files, and most
> of them do not contain all layers. The result looks like
> this:
> 
>   File "./test.gtk.py", line 593, in load_allmaps
>     load_map(files[i])
>   File "./test.gtk.py", line 574, in load_map
>     bouyslat=load_objects(dataset,'BOYLAT')
>   File "./test.gtk.py", line 513, in load_objects
>     objects=dataset.GetLayerByName( name )
>   File "/usr/lib/python2.3/site-packages/ogr.py", line 286, in GetLayerByName
>     raise IndexError, 'No layer %s on datasource' % name 
> IndexError: No layer BOYLAT on datasource
> 
> Can someone tell me how to test for the existence of a layer
> before loading it?

Fulko,

You can iterate through all the layers to build a name list like this if
you want:

import ogr

ds = ogr.Open( '/u/data/s57/NO410810.000' )

names = []
for i in range(ds.GetLayerCount()):
     names.append( ds.GetLayer(i).GetName())

print names
if ('BOYLAT' in names):
     print 'have BOYLAT'
else:
     print 'Dont have BOYLAT'


However, for your case it is likely just easier to use "try" to handle missing
layers.

eg.

try:
     objects = dataset.GetLayerByName( name )
except:
     return None

... use objects...

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list