[gdal-dev] Multiple layers in OGRDataSource

Mateusz Loskot mateusz at loskot.net
Wed Apr 9 20:14:52 EDT 2008


Olivier Tournaire wrote:
> I read the OGR documentation OGR API Tutorial, and I found this sentence :
> 
> " An OGRDataSource <http://www.gdal.org/ogr/classOGRDataSource.html> can
> potentially have many layers associated with it"
> 
> However, I cannot find the way to add multiple layers in one instance of
> OGRDataSource.

There are two situations possible:

1. Open multi-layer datasource, ie. a directory with number of 
shapefiles. After you instantiate OGRDataSource object and open the 
directory, you will have access to N number of layers.

2. Create new layer what means that new shapefile or new PostGIS table 
is physically created, see:

http://gdal.org/ogr/classOGRDataSource.html#39cfc6e0ee790506d7638b0dce03c7da

There is no way to add a layer to OGRDataSource object in term of 
attaching it to the datasource, like:

OGRDataSource* ds = ...
ds->GetLayerCount()  // let's say 5 is returned

OGRLayer* someLayer = ...
ds->AddLayer(someLayer)

ds->GetLayerCount()  // 6 is returned



> What I would like to do is to read a set of shapefiles,
> create a layer for each of them and store them in an OGRDataSource object.

You do not need to open/read every shapefile separately.
In order to read a set of shapefiles as a set of layers, just open the 
directory consisting of those shapefiles all of them will be accessible
through the OGRDataSource object, automagicallly.

> Note I also want to be able to create an OGRDataSource object and add layers
> (from shapefiles) in a second time. Could you please give me the way to do
> such a task ?

You can't, see above.

Greetings
-- 
Mateusz Loskot
http://mateusz.loskot.net


More information about the gdal-dev mailing list