QVector<QgsDataItem *> QgsOgrDataCollectionItem::createChildren() { QVector<QgsDataItem *> children; QStringList skippedLayerNames; char **papszOptions = nullptr; papszOptions = CSLSetNameValue( papszOptions, "@LIST_ALL_TABLES", "YES" ); gdal::dataset_unique_ptr hDataSource( GDALOpenEx( mPath.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, papszOptions, nullptr ) ); CSLDestroy( papszOptions ); GDALDriverH hDriver = GDALGetDatasetDriver( hDataSource.get() ); QString driverName = QString::fromUtf8( GDALGetDriverShortName( hDriver ) ); if ( driverName == QStringLiteral( "SQLite" ) ) { skippedLayerNames = QgsSqliteUtils::systemTables(); } if ( !hDataSource ) return children; int numLayers = GDALDatasetGetLayerCount( hDataSource.get() ); // Check if layer names are unique, so we can use |layername= in URI QMap< QString, int > mapLayerNameToCount; QList< int > skippedLayers; bool uniqueNames = true; for ( int i = 0; i < numLayers; ++i ) { OGRLayerH hLayer = GDALDatasetGetLayer( hDataSource.get(), i ); OGRFeatureDefnH hDef = OGR_L_GetLayerDefn( hLayer ); QString layerName = QString::fromUtf8( OGR_FD_GetName( hDef ) ); ++mapLayerNameToCount[layerName]; if ( mapLayerNameToCount[layerName] > 1 ) { uniqueNames = false; break; } if ( ( driverName == QStringLiteral( "SQLite" ) && layerName.contains( QRegularExpression( QStringLiteral( "idx_.*_geometry($|_.*)" ) ) ) ) || skippedLayerNames.contains( layerName ) ) { skippedLayers << i; } } children.reserve( numLayers ); for ( int i = 0; i < numLayers; ++i ) { if ( !skippedLayers.contains( i ) ) { QgsOgrLayerItem *item = dataItemForLayer( this, QString(), mPath, hDataSource.get(), i, true, uniqueNames ); children.append( item ); } } return children; }
QVector<QgsDataItem *> QgsOgrDataCollectionItem::createChildren() { QVector<QgsDataItem *> children; gdal::dataset_unique_ptr hDataSource( GDALOpenEx( mPath.toUtf8().constData(), GDAL_OF_VECTOR, nullptr, nullptr, nullptr ) ); if ( !hDataSource ) return children; int numLayers = GDALDatasetGetLayerCount( hDataSource.get() ); children.reserve( numLayers ); for ( int i = 0; i < numLayers; ++i ) { QgsOgrLayerItem *item = dataItemForLayer( this, QString(), mPath, hDataSource.get(), i, true ); children.append( item ); } return children; }
QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem *parent, const QString &name, const QString &path, const QString &uri, LayerType layerType, bool isSubLayer ) : QgsLayerItem( parent, name, path, uri, layerType, QStringLiteral( "ogr" ) ) { mIsSubLayer = isSubLayer; mToolTip = uri; setState( Populated ); // children are not expected if ( mPath.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) ) { if ( OGRGetDriverCount() == 0 ) { OGRRegisterAll(); } gdal::dataset_unique_ptr hDataSource( GDALOpenEx( mPath.toUtf8().constData(), GDAL_OF_VECTOR | GDAL_OF_UPDATE, nullptr, nullptr, nullptr ) ); if ( hDataSource ) { mCapabilities |= SetCrs; } // It it is impossible to assign a crs to an existing layer // No OGR_L_SetSpatialRef : http://trac.osgeo.org/gdal/ticket/4032 } }