Skip to content
Permalink
Browse files
Fix potentially ambiguous sublayer names when querying sublayers in
an vsi archive file
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent 28e8b4d commit 5b921dcaa8d07fcc70026b55459fed1be37b5a83
@@ -3660,8 +3660,6 @@ QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const
dataset.reset( QgsGdalProviderBase::gdalOpen( gdalUri, GDAL_OF_READONLY ) );
if ( !dataset )
{
if ( CPLGetLastErrorNo() != CPLE_OpenFailed )
QgsDebugMsg( QStringLiteral( "Error querying sublayers: %1 " ).arg( QString::fromUtf8( CPLGetLastErrorMsg() ) ) );
return {};
}

@@ -3681,7 +3679,9 @@ QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const
const QVariantMap parts = decodeUri( uri );
if ( !parts.value( QStringLiteral( "vsiSuffix" ) ).toString().isEmpty() )
{
name = QgsProviderUtils::suggestLayerNameFromFilePath( parts.value( QStringLiteral( "vsiSuffix" ) ).toString() );
name = parts.value( QStringLiteral( "vsiSuffix" ) ).toString();
if ( name.startsWith( '/' ) )
name = name.mid( 1 );
}
else if ( parts.contains( QStringLiteral( "path" ) ) )
{
@@ -1187,7 +1187,8 @@ QList<QgsProviderSublayerDetails> QgsOgrProviderMetadata::querySublayers( const
// remove non-matching, unwanted layers
res.erase( std::remove_if( res.begin(), res.end(), [ = ]( const QgsProviderSublayerDetails & sublayer )
{
return sublayer.name() != originalUriLayerName;
const QVariantMap uriParts = decodeUri( sublayer.uri() );
return uriParts.value( QStringLiteral( "layerName" ) ).toString() != originalUriLayerName && sublayer.name() != originalUriLayerName;
} ), res.end() );
}

@@ -2376,7 +2376,14 @@ QList< QgsProviderSublayerDetails > QgsOgrProviderUtils::querySubLayerList( int

QgsProviderSublayerDetails details;
details.setLayerNumber( i );
details.setName( layerName );
if ( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().isEmpty() )
details.setName( layerName );
else
{
if ( parts.value( QStringLiteral( "layerName" ) ).toString().isEmpty() )
parts.insert( QStringLiteral( "layerName" ), layerName );
details.setName( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().mid( 1 ) );
}
details.setFeatureCount( layerFeatureCount );
details.setWkbType( QgsOgrUtils::ogrGeometryTypeToQgsWkbType( layerGeomType ) );
details.setGeometryColumnName( geometryColumnName );
@@ -2458,7 +2465,14 @@ QList< QgsProviderSublayerDetails > QgsOgrProviderUtils::querySubLayerList( int

QgsProviderSublayerDetails details;
details.setLayerNumber( i );
details.setName( layerName );
if ( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().isEmpty() )
details.setName( layerName );
else
{
if ( parts.value( QStringLiteral( "layerName" ) ).toString().isEmpty() )
parts.insert( QStringLiteral( "layerName" ), layerName );
details.setName( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().mid( 1 ) );
}
details.setFeatureCount( fCount.value( countIt.key() ) );
details.setWkbType( QgsOgrUtils::ogrGeometryTypeToQgsWkbType( ( bIs25D ) ? wkbSetZ( countIt.key() ) : countIt.key() ) );
details.setGeometryColumnName( geometryColumnName );
@@ -551,7 +551,7 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/zip/landsat_b1.zip" );
QCOMPARE( res.count(), 1 );
QCOMPARE( res.at( 0 ).layerNumber(), 1 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "landsat_b1" ) );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "landsat_b1.tif" ) );
QCOMPARE( res.at( 0 ).description(), QString() );
QCOMPARE( res.at( 0 ).uri(), QStringLiteral( "/vsizip/%1/zip/landsat_b1.zip/landsat_b1.tif" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "gdal" ) );
@@ -563,23 +563,23 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
res = gdalMetadata->querySublayers( QStringLiteral( TEST_DATA_DIR ) + "/zip/testtar.tgz" );
QCOMPARE( res.count(), 3 );
QCOMPARE( res.at( 0 ).layerNumber(), 1 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "landsat_b2" ) );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "folder/folder2/landsat_b2.tif" ) );
QCOMPARE( res.at( 0 ).description(), QString() );
QCOMPARE( res.at( 0 ).uri(), QStringLiteral( "/vsitar/%1/zip/testtar.tgz/folder/folder2/landsat_b2.tif" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "gdal" ) );
QCOMPARE( res.at( 0 ).type(), QgsMapLayerType::RasterLayer );
rl.reset( qgis::down_cast< QgsRasterLayer * >( res.at( 0 ).toLayer( options ) ) );
QVERIFY( rl->isValid() );
QCOMPARE( res.at( 1 ).layerNumber(), 1 );
QCOMPARE( res.at( 1 ).name(), QStringLiteral( "landsat_b1" ) );
QCOMPARE( res.at( 1 ).name(), QStringLiteral( "landsat_b1.tif" ) );
QCOMPARE( res.at( 1 ).description(), QString() );
QCOMPARE( res.at( 1 ).uri(), QStringLiteral( "/vsitar/%1/zip/testtar.tgz/landsat_b1.tif" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 1 ).providerKey(), QStringLiteral( "gdal" ) );
QCOMPARE( res.at( 1 ).type(), QgsMapLayerType::RasterLayer );
rl.reset( qgis::down_cast< QgsRasterLayer * >( res.at( 1 ).toLayer( options ) ) );
QVERIFY( rl->isValid() );
QCOMPARE( res.at( 2 ).layerNumber(), 1 );
QCOMPARE( res.at( 2 ).name(), QStringLiteral( "landsat_b1" ) );
QCOMPARE( res.at( 2 ).name(), QStringLiteral( "landsat_b1.vrt" ) );
QCOMPARE( res.at( 2 ).description(), QString() );
QCOMPARE( res.at( 2 ).uri(), QStringLiteral( "/vsitar/%1/zip/testtar.tgz/landsat_b1.vrt" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 2 ).providerKey(), QStringLiteral( "gdal" ) );
@@ -591,7 +591,7 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
res = gdalMetadata->querySublayers( QStringLiteral( "/vsitar/" ) + QStringLiteral( TEST_DATA_DIR ) + "/zip/testtar.tgz/folder/folder2/landsat_b2.tif" );
QCOMPARE( res.count(), 1 );
QCOMPARE( res.at( 0 ).layerNumber(), 1 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "landsat_b2" ) );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "folder/folder2/landsat_b2.tif" ) );
QCOMPARE( res.at( 0 ).description(), QString() );
QCOMPARE( res.at( 0 ).uri(), QStringLiteral( "/vsitar/%1/zip/testtar.tgz/folder/folder2/landsat_b2.tif" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "gdal" ) );
@@ -601,7 +601,7 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayers()
res = gdalMetadata->querySublayers( QStringLiteral( "/vsitar/" ) + QStringLiteral( TEST_DATA_DIR ) + "/zip/testtar.tgz/landsat_b1.tif" );
QCOMPARE( res.count(), 1 );
QCOMPARE( res.at( 0 ).layerNumber(), 1 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "landsat_b1" ) );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "landsat_b1.tif" ) );
QCOMPARE( res.at( 0 ).description(), QString() );
QCOMPARE( res.at( 0 ).uri(), QStringLiteral( "/vsitar/%1/zip/testtar.tgz/landsat_b1.tif" ).arg( QStringLiteral( TEST_DATA_DIR ) ) );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "gdal" ) );
@@ -1534,7 +1534,7 @@ def test_provider_sublayer_details(self):
res = metadata.querySublayers(os.path.join(TEST_DATA_DIR, 'zip', 'points2.zip'))
self.assertEqual(len(res), 1)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "points")
self.assertEqual(res[0].name(), "points.shp")
self.assertEqual(res[0].description(), '')
self.assertEqual(res[0].uri(), '/vsizip/' + TEST_DATA_DIR + "/zip/points2.zip/points.shp|layername=points")
self.assertEqual(res[0].providerKey(), "ogr")
@@ -1550,7 +1550,7 @@ def test_provider_sublayer_details(self):
res = metadata.querySublayers(os.path.join(TEST_DATA_DIR, 'zip', 'testtar.tgz'))
self.assertEqual(len(res), 2)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "points")
self.assertEqual(res[0].name(), "folder/points.geojson")
self.assertEqual(res[0].description(), '')
self.assertEqual(res[0].uri(), '/vsitar/' + TEST_DATA_DIR + "/zip/testtar.tgz/folder/points.geojson|layername=points")
self.assertEqual(res[0].providerKey(), "ogr")
@@ -1562,7 +1562,7 @@ def test_provider_sublayer_details(self):
self.assertTrue(vl.isValid())
self.assertEqual(vl.wkbType(), QgsWkbTypes.Point)
self.assertEqual(res[1].layerNumber(), 0)
self.assertEqual(res[1].name(), "points")
self.assertEqual(res[1].name(), "points.shp")
self.assertEqual(res[1].description(), '')
self.assertEqual(res[1].uri(), '/vsitar/' + TEST_DATA_DIR + "/zip/testtar.tgz/points.shp|layername=points")
self.assertEqual(res[1].providerKey(), "ogr")
@@ -1578,7 +1578,7 @@ def test_provider_sublayer_details(self):
res = metadata.querySublayers('/vsitar/' + os.path.join(TEST_DATA_DIR, 'zip', 'testtar.tgz') + '/folder/points.geojson')
self.assertEqual(len(res), 1)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "points")
self.assertEqual(res[0].name(), "folder/points.geojson")
self.assertEqual(res[0].description(), '')
self.assertEqual(res[0].uri(), '/vsitar/' + TEST_DATA_DIR + "/zip/testtar.tgz/folder/points.geojson|layername=points")
self.assertEqual(res[0].providerKey(), "ogr")
@@ -1594,7 +1594,7 @@ def test_provider_sublayer_details(self):
res = metadata.querySublayers('/vsitar/' + os.path.join(TEST_DATA_DIR, 'zip', 'testtar.tgz') + '/points.shp')
self.assertEqual(len(res), 1)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "points")
self.assertEqual(res[0].name(), "points.shp")
self.assertEqual(res[0].description(), '')
self.assertEqual(res[0].uri(), '/vsitar/' + TEST_DATA_DIR + "/zip/testtar.tgz/points.shp|layername=points")
self.assertEqual(res[0].providerKey(), "ogr")
@@ -1611,7 +1611,7 @@ def test_provider_sublayer_details(self):
res = metadata.querySublayers('/vsitar/' + os.path.join(TEST_DATA_DIR, 'zip', 'testtar.tgz') + '/points.shp|layername=points')
self.assertEqual(len(res), 1)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "points")
self.assertEqual(res[0].name(), "points.shp")
self.assertEqual(res[0].description(), '')
self.assertEqual(res[0].uri(), '/vsitar/' + TEST_DATA_DIR + "/zip/testtar.tgz/points.shp|layername=points")
self.assertEqual(res[0].providerKey(), "ogr")

0 comments on commit 5b921dc

Please sign in to comment.