[Qgis-developer] Python access to ogr provider datasource layer name

Dane Springmeyer blake at hailmail.net
Tue Jun 2 20:54:04 EDT 2009


So OGR has this notion of data files and then their layers. I'm trying  
to figure out in QGIS how to get at this information via python for  
data loaded into QGIS.

For example, for this sample gml file: http://svn.osgeo.org/gdal/trunk/autotest/ogr/data/ionic_wfs.gml

ogrinfo -so -al gives:

Had to open data source read-only.
INFO: Open of `/Users/spring/src/gdal/autotest/ogr/data/ionic_wfs.gml'
       using driver `GML' successful.

Layer name: GEM
Geometry: Unknown (any)
Feature Count: 1
Extent: (437.000000, 51131.000000) - (44137.000000, 511549.000000)
Layer SRS WKT:
(unknown)
Name: String (0.0)

What I am looking for is access to the name 'GEM'.

Via python I notice that 'subLayers()' are empty:

 >>> l = iface.mapCanvas().layer(0)
 >>> l.subLayers()
<PyQt4.QtCore.QStringList object at 0x13101618>
 >>> l.subLayers()[0]
exceptions.IndexError: sequence index out of range

While the provider subLayers have the name embedded in an odd Qstring:
 >>> l.dataProvider().subLayers()[0]
0:GEM:0:Unknown

Is parsing this string they best way to get at this information? What  
happens in QGIS when there are multiple layers?

Running this on a shapefile and a GeoJSON file I get this:

 >>> l.dataProvider().subLayers()[0]
0:statesp020:2895:Polygon

http://svn.osgeo.org/gdal/trunk/autotest/ogr/data/multipolygon.geojson
 >>> l.dataProvider().subLayers()[0]
0:OGRGeoJSON:1:Unknown WKB: 6

Can anyone explain what those numbers mean - Number of features?

Thanks,

Dane


More information about the Qgis-developer mailing list