Skip to content

Commit 29ac3f4

Browse files
committed
[browser] fix zip items unable to populate OGR layers
1 parent af0bae6 commit 29ac3f4

File tree

2 files changed

+21
-112
lines changed

2 files changed

+21
-112
lines changed

src/core/qgsdataitem.cpp

+21-110
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,7 @@ QVector<QgsDataItem *> QgsDirectoryItem::createChildren()
809809
continue;
810810
}
811811

812-
// vsizip support was added to GDAL/OGR 1.6 but GDAL_VERSION_NUM not available here
813-
// so we assume it's available anyway
812+
if ( fileInfo.suffix() == QLatin1String( "zip" ) )
814813
{
815814
QgsDataItem *item = QgsZipItem::itemFromPath( this, path, name, mPath + '/' + name );
816815
if ( item )
@@ -1262,7 +1261,6 @@ QVector<QgsDataItem *> QgsFavoritesItem::createChildren( const QString &favDir,
12621261

12631262
//-----------------------------------------------------------------------
12641263
QStringList QgsZipItem::sProviderNames = QStringList();
1265-
QVector<dataItem_t *> QgsZipItem::sDataItemPtr = QVector<dataItem_t *>();
12661264

12671265

12681266
QgsZipItem::QgsZipItem( QgsDataItem *parent, const QString &name, const QString &path )
@@ -1287,51 +1285,8 @@ void QgsZipItem::init()
12871285

12881286
if ( sProviderNames.isEmpty() )
12891287
{
1290-
// QStringList keys = QgsProviderRegistry::instance()->providerList();
1291-
// only use GDAL and OGR providers as we use the VSIFILE mechanism
1292-
QStringList keys;
1293-
// keys << "ogr" << "gdal";
1294-
keys << QStringLiteral( "gdal" ) << QStringLiteral( "ogr" );
1295-
1296-
for ( const auto &k : qgis::as_const( keys ) )
1297-
{
1298-
QgsDebugMsgLevel( "provider " + k, 3 );
1299-
// some providers hangs with empty uri (PostGIS) etc...
1300-
// -> using libraries directly
1301-
std::unique_ptr< QLibrary > library( QgsProviderRegistry::instance()->createProviderLibrary( k ) );
1302-
if ( library )
1303-
{
1304-
dataCapabilities_t *dataCapabilities = reinterpret_cast< dataCapabilities_t * >( cast_to_fptr( library->resolve( "dataCapabilities" ) ) );
1305-
if ( !dataCapabilities )
1306-
{
1307-
QgsDebugMsg( library->fileName() + " does not have dataCapabilities" );
1308-
continue;
1309-
}
1310-
if ( dataCapabilities() == QgsDataProvider::NoDataCapabilities )
1311-
{
1312-
QgsDebugMsg( library->fileName() + " has NoDataCapabilities" );
1313-
continue;
1314-
}
1315-
QgsDebugMsg( QString( "%1 dataCapabilities : %2" ).arg( library->fileName() ).arg( dataCapabilities() ) );
1316-
1317-
dataItem_t *dataItem = reinterpret_cast< dataItem_t * >( cast_to_fptr( library->resolve( "dataItem" ) ) );
1318-
if ( ! dataItem )
1319-
{
1320-
QgsDebugMsg( library->fileName() + " does not have dataItem" );
1321-
continue;
1322-
}
1323-
1324-
// mLibraries.append( library );
1325-
sDataItemPtr.append( dataItem );
1326-
sProviderNames.append( k );
1327-
}
1328-
else
1329-
{
1330-
//QgsDebugMsg ( "Cannot get provider " + k );
1331-
}
1332-
}
1288+
sProviderNames << QStringLiteral( "OGR" ) << QStringLiteral( "OGR" );
13331289
}
1334-
13351290
}
13361291

13371292
QVector<QgsDataItem *> QgsZipItem::createChildren()
@@ -1343,7 +1298,7 @@ QVector<QgsDataItem *> QgsZipItem::createChildren()
13431298

13441299
mZipFileList.clear();
13451300

1346-
QgsDebugMsgLevel( QString( "mFilePath = %1 path = %2 name= %3 scanZipSetting= %4 vsiPrefix= %5" ).arg( mFilePath, path(), name(), scanZipSetting, mVsiPrefix ), 2 );
1301+
QgsDebugMsgLevel( QString( "mFilePath = %1 path = %2 name= %3 scanZipSetting= %4 vsiPrefix= %5" ).arg( mFilePath, path(), name(), scanZipSetting, mVsiPrefix ), 3 );
13471302

13481303
// if scanZipBrowser == no: skip to the next file
13491304
if ( scanZipSetting == QLatin1String( "no" ) )
@@ -1354,18 +1309,22 @@ QVector<QgsDataItem *> QgsZipItem::createChildren()
13541309
// first get list of files
13551310
getZipFileList();
13561311

1312+
const QList<QgsDataItemProvider *> providers = QgsApplication::dataItemProviderRegistry()->providers();
1313+
13571314
// loop over files inside zip
13581315
Q_FOREACH ( const QString &fileName, mZipFileList )
13591316
{
13601317
QFileInfo info( fileName );
13611318
tmpPath = mVsiPrefix + mFilePath + '/' + fileName;
13621319
QgsDebugMsgLevel( "tmpPath = " + tmpPath, 3 );
13631320

1364-
// Q_FOREACH( dataItem_t *dataItem, mDataItemPtr )
1365-
for ( int i = 0; i < sProviderNames.size(); i++ )
1321+
for ( QgsDataItemProvider *provider : providers )
13661322
{
1323+
if ( !sProviderNames.contains( provider->name() ) )
1324+
continue;
1325+
13671326
// ugly hack to remove .dbf file if there is a .shp file
1368-
if ( sProviderNames[i] == QLatin1String( "ogr" ) )
1327+
if ( provider->name() == QStringLiteral( "OGR" ) )
13691328
{
13701329
if ( info.suffix().compare( QLatin1String( "dbf" ), Qt::CaseInsensitive ) == 0 )
13711330
{
@@ -1378,26 +1337,19 @@ QVector<QgsDataItem *> QgsZipItem::createChildren()
13781337
}
13791338
}
13801339

1381-
// try to get data item from provider
1382-
dataItem_t *dataItem = sDataItemPtr.at( i );
1383-
if ( dataItem )
1340+
QgsDebugMsgLevel( QString( "trying to load item %1 with %2" ).arg( tmpPath, provider->name() ), 3 );
1341+
QgsDataItem *item = provider->createDataItem( tmpPath, this );
1342+
if ( item )
13841343
{
1385-
QgsDebugMsgLevel( QString( "trying to load item %1 with %2" ).arg( tmpPath, sProviderNames.at( i ) ), 3 );
1386-
QgsDataItem *item = dataItem( tmpPath, this );
1387-
if ( item )
1388-
{
1389-
QgsDebugMsgLevel( "loaded item", 3 );
1390-
// the item comes with zipped file name, set the name to relative path within zip file
1391-
item->setName( fileName );
1392-
children.append( item );
1393-
}
1394-
else
1395-
{
1396-
QgsDebugMsgLevel( "not loaded item", 3 );
1397-
}
1344+
// the item comes with zipped file name, set the name to relative path within zip file
1345+
item->setName( fileName );
1346+
children.append( item );
1347+
}
1348+
else
1349+
{
1350+
QgsDebugMsgLevel( "not loaded item", 3 );
13981351
}
13991352
}
1400-
14011353
}
14021354

14031355
return children;
@@ -1412,7 +1364,6 @@ QgsDataItem *QgsZipItem::itemFromPath( QgsDataItem *parent, const QString &fileP
14121364
{
14131365
QgsSettings settings;
14141366
QString scanZipSetting = settings.value( QStringLiteral( "qgis/scanZipInBrowser2" ), "basic" ).toString();
1415-
int zipFileCount = 0;
14161367
QStringList zipFileList;
14171368
QString vsiPrefix = QgsZipItem::vsiPrefix( filePath );
14181369
QgsZipItem *zipItem = nullptr;
@@ -1462,48 +1413,8 @@ QgsDataItem *QgsZipItem::itemFromPath( QgsDataItem *parent, const QString &fileP
14621413
QgsDebugMsgLevel( "returning zipItem", 3 );
14631414
return zipItem;
14641415
}
1465-
// if 1 or 0 child found, create a single data item using the normal path or the full path given by QgsZipItem
1466-
else
1467-
{
1468-
QString vsiPath = vsiPrefix + filePath;
1469-
if ( zipItem )
1470-
{
1471-
QVector<QgsDataItem *> children = zipItem->children();
1472-
if ( children.size() == 1 )
1473-
{
1474-
// take the name of the only child so we can get a normal data item from it
1475-
QgsLayerItem *layerItem = qobject_cast<QgsLayerItem *>( children.first() );
1476-
if ( layerItem )
1477-
vsiPath = layerItem->uri();
1478-
}
1479-
zipFileCount = zipFileList.count();
1480-
delete zipItem;
1481-
}
1482-
1483-
QgsDebugMsgLevel( QString( "will try to create a normal dataItem from filePath= %2 or vsiPath = %3" ).arg( filePath, vsiPath ), 3 );
1484-
1485-
// try to open using registered providers (gdal and ogr)
1486-
for ( int i = 0; i < sProviderNames.size(); i++ )
1487-
{
1488-
dataItem_t *dataItem = sDataItemPtr.at( i );
1489-
if ( dataItem )
1490-
{
1491-
QgsDataItem *item = nullptr;
1492-
// try first with normal path (Passthru)
1493-
// this is to simplify .qml handling, and without this some tests will fail
1494-
// (e.g. testZipItemVectorTransparency(), second test)
1495-
if ( ( sProviderNames.at( i ) == QLatin1String( "ogr" ) ) ||
1496-
( sProviderNames.at( i ) == QLatin1String( "gdal" ) && zipFileCount == 1 ) )
1497-
item = dataItem( filePath, parent );
1498-
// try with /vsizip/
1499-
if ( ! item )
1500-
item = dataItem( vsiPath, parent );
1501-
if ( item )
1502-
return item;
1503-
}
1504-
}
1505-
}
15061416

1417+
// if 1 or 0 child found, let provider(s) create individual items
15071418
return nullptr;
15081419
}
15091420

src/providers/ogr/qgsogrdataitems.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ QGISEXTERN QStringList wildcards();
4444

4545
QGISEXTERN bool deleteLayer( const QString &uri, const QString &errCause );
4646

47-
4847
QgsOgrLayerItem::QgsOgrLayerItem( QgsDataItem *parent,
4948
const QString &name, const QString &path, const QString &uri, LayerType layerType, bool isSubLayer )
5049
: QgsLayerItem( parent, name, path, uri, layerType, QStringLiteral( "ogr" ) )
@@ -502,7 +501,6 @@ bool QgsOgrDataCollectionItem::createConnection( const QString &name, const QStr
502501

503502
// ---------------------------------------------------------------------------
504503

505-
506504
QgsDataItem *QgsOgrDataItemProvider::createDataItem( const QString &pathIn, QgsDataItem *parentItem )
507505
{
508506
QString path( pathIn );

0 commit comments

Comments
 (0)