@@ -73,14 +73,61 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
7373 // try to open for update, but disable error messages to avoid a
7474 // message if the file is read only, because we cope with that
7575 // ourselves.
76+
77+ // This part of the code parses the uri transmitted to the ogr provider to
78+ // get the options the client wants us to apply
79+
80+ QString mFilePath ;
81+ QString theLayerName;
82+ int theLayerIndex=0 ;
83+
84+ // If there is no & in the uri, then the uri is just the filename. The loaded
85+ // layer will be layer 0.
86+ if ( ! uri.contains (' &' , Qt::CaseSensitive))
87+ {
88+ mFilePath = uri;
89+ }
90+ else
91+ {
92+ // If we get here, there are some options added to the filename. We must parse
93+ // the different parts separated by &, and among each option, the name and the
94+ // value around the =.
95+ // A valid uri is of the form: filename&option1=value1&option2=value2,...
96+
97+ QStringList theURIParts = uri.split (" &" );
98+ mFilePath = theURIParts.at ( 0 );
99+
100+ for (int i = 1 ; i < theURIParts.size (); i++ )
101+ {
102+ QStringList theInstruction = theURIParts.at ( i ).split ( " =" );
103+ if ( theInstruction.at ( 0 ) == QString ( " layerid" ) )
104+ {
105+ bool ok;
106+ theLayerIndex = theInstruction.at ( 1 ).toInt ( &ok );
107+ if ( ! ok )
108+ {
109+ theLayerIndex = 0 ;
110+ }
111+ }
112+ if ( theInstruction.at ( 0 ) == QString ( " layername" ) )
113+ {
114+ theLayerName = theInstruction.at ( 1 );
115+ }
116+ }
117+ }
118+
119+ QgsDebugMsg (" mFilePath: " + mFilePath );
120+ QgsDebugMsg (" theLayerIndex: " +theLayerIndex);
121+ QgsDebugMsg (" theLayerName: " +theLayerName);
122+
76123 CPLPushErrorHandler ( CPLQuietErrorHandler );
77- ogrDataSource = OGROpen ( QFile::encodeName ( uri ).constData (), TRUE , &ogrDriver );
124+ ogrDataSource = OGROpen ( QFile::encodeName ( mFilePath ).constData (), TRUE , &ogrDriver );
78125 CPLPopErrorHandler ();
79126
80127 if ( ogrDataSource == NULL )
81128 {
82129 // try to open read-only
83- ogrDataSource = OGROpen ( QFile::encodeName ( uri ).constData (), FALSE , &ogrDriver );
130+ ogrDataSource = OGROpen ( QFile::encodeName ( mFilePath ).constData (), FALSE , &ogrDriver );
84131
85132 // TODO Need to set a flag or something to indicate that the layer
86133 // TODO is in read-only mode, otherwise edit ops will fail
@@ -95,8 +142,17 @@ QgsOgrProvider::QgsOgrProvider( QString const & uri )
95142 valid = true ;
96143
97144 ogrDriverName = OGR_Dr_GetName ( ogrDriver );
98-
99- ogrLayer = OGR_DS_GetLayer ( ogrDataSource, 0 );
145+
146+ // We get the layer which was requested by the uri. The layername
147+ // has precedence over the layerid if both are given.
148+ if ( theLayerName.isNull () )
149+ {
150+ ogrLayer = OGR_DS_GetLayer ( ogrDataSource, theLayerIndex );
151+ }
152+ else
153+ {
154+ ogrLayer = OGR_DS_GetLayerByName ( ogrDataSource, (char *)(theLayerName.toLocal8Bit ().data ()) );
155+ }
100156
101157 // get the extent_ (envelope) of the layer
102158
@@ -145,6 +201,44 @@ QgsOgrProvider::~QgsOgrProvider()
145201 }
146202}
147203
204+ QStringList QgsOgrProvider::subLayers () const
205+ {
206+ QStringList theList = QStringList ();
207+ if (! valid )
208+ {
209+ return theList;
210+ }
211+ for ( int i = 0 ; i < layerCount () ; i++ )
212+ {
213+ QString theLayerName = QString (OGR_FD_GetName (OGR_L_GetLayerDefn (OGR_DS_GetLayer ( ogrDataSource, i ))));
214+ OGRwkbGeometryType layerGeomType = OGR_FD_GetGeomType (OGR_L_GetLayerDefn (OGR_DS_GetLayer ( ogrDataSource, i )));
215+
216+ int theLayerFeatureCount=OGR_L_GetFeatureCount (OGR_DS_GetLayer ( ogrDataSource, i ),1 ) ;
217+
218+ QString geom;
219+ switch (layerGeomType)
220+ {
221+ case wkbUnknown: geom = " Unknown" ; break ;
222+ case wkbPoint: geom=" Point" ; break ;
223+ case wkbLineString: geom=" LineString" ; break ;
224+ case wkbPolygon: geom=" Polygon" ; break ;
225+ case wkbMultiPoint: geom=" MultiPoint" ; break ;
226+ case wkbMultiLineString: geom=" MultiLineString" ; break ;
227+ case wkbGeometryCollection: geom = " GeometryCollection" ; break ;
228+ case wkbNone: geom = " None" ; break ;
229+ case wkbPoint25D: geom=" Point25D" ; break ;
230+ case wkbLineString25D: geom=" LineString25D" ; break ;
231+ case wkbPolygon25D: geom=" Polygon25D" ; break ;
232+ case wkbMultiPoint25D: geom=" MultiPoint25D" ; break ;
233+ case wkbMultiLineString25D: geom=" MultiLineString25D" ; break ;
234+ case wkbMultiPolygon25D: geom=" MultiPolygon25D" ; break ;
235+ default : geom=" Unknown WKB: " + QString::number (layerGeomType);
236+ }
237+ theList.append (QString::number (i)+" :" + theLayerName+" :" +QString::number (theLayerFeatureCount)+" :" +geom);
238+ }
239+ return theList;
240+ }
241+
148242void QgsOgrProvider::setEncoding ( const QString& e )
149243{
150244 QgsVectorDataProvider::setEncoding ( e );
0 commit comments