Skip to content
Permalink
Browse files
Better layer names for vsizip files in OGR/GDAL providers
  • Loading branch information
nyalldawson authored and github-actions committed Nov 2, 2021
1 parent 532315c commit d15655c18631f5c0b20850be35950782c69cf356
@@ -3694,7 +3694,9 @@ QList<QgsProviderSublayerDetails> QgsGdalProviderMetadata::querySublayers( const
details.setType( QgsMapLayerType::RasterLayer );
details.setProviderKey( QStringLiteral( "gdal" ) );
details.setUri( uri );
details.setName( QgsProviderUtils::suggestLayerNameFromFilePath( path ) );
details.setName( uriParts.value( QStringLiteral( "vsiSuffix" ) ).toString().isEmpty()
? QgsProviderUtils::suggestLayerNameFromFilePath( path )
: QFileInfo( uriParts.value( QStringLiteral( "vsiSuffix" ) ).toString() ).fileName() );
if ( QgsGdalUtils::multiLayerFileExtensions().contains( suffix ) )
{
// uri may contain sublayers, but query flags prevent us from examining them
@@ -1171,7 +1171,9 @@ QList<QgsProviderSublayerDetails> QgsOgrProviderMetadata::querySublayers( const
details.setType( QgsMapLayerType::VectorLayer );
details.setProviderKey( QStringLiteral( "ogr" ) );
details.setUri( uri );
details.setName( QgsProviderUtils::suggestLayerNameFromFilePath( path ) );
details.setName( uriParts.value( QStringLiteral( "vsiSuffix" ) ).toString().isEmpty()
? QgsProviderUtils::suggestLayerNameFromFilePath( path )
: QgsProviderUtils::suggestLayerNameFromFilePath( uriParts.value( QStringLiteral( "vsiSuffix" ) ).toString() ) );
if ( QgsGdalUtils::multiLayerFileExtensions().contains( suffix ) )
{
// uri may contain sublayers, but query flags prevent us from examining them
@@ -2420,7 +2420,14 @@ QList< QgsProviderSublayerDetails > QgsOgrProviderUtils::querySubLayerList( int
{
if ( parts.value( QStringLiteral( "layerName" ) ).toString().isEmpty() )
parts.insert( QStringLiteral( "layerName" ), layerName );
details.setName( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().mid( 1 ) );
if ( hasSingleLayerOnly )
{
details.setName( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().mid( 1 ) );
}
else
{
details.setName( layerName );
}
}
details.setFeatureCount( layerFeatureCount );
details.setWkbType( QgsOgrUtils::ogrGeometryTypeToQgsWkbType( layerGeomType ) );
@@ -2511,7 +2518,14 @@ QList< QgsProviderSublayerDetails > QgsOgrProviderUtils::querySubLayerList( int
{
if ( parts.value( QStringLiteral( "layerName" ) ).toString().isEmpty() )
parts.insert( QStringLiteral( "layerName" ), layerName );
details.setName( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().mid( 1 ) );
if ( hasSingleLayerOnly )
{
details.setName( parts.value( QStringLiteral( "vsiSuffix" ) ).toString().mid( 1 ) );
}
else
{
details.setName( layerName );
}
}
details.setFeatureCount( fCount.value( countIt.key() ) );
details.setWkbType( QgsOgrUtils::ogrGeometryTypeToQgsWkbType( ( bIs25D ) ? wkbSetZ( countIt.key() ) : countIt.key() ) );
@@ -693,20 +693,18 @@ void TestQgsGdalProvider::testGdalProviderQuerySublayersFastScan()
// multi-layer archive, but with specific suffix specified
res = gdalMetadata->querySublayers( QStringLiteral( "/vsitar/" ) + QStringLiteral( TEST_DATA_DIR ) + "/zip/testtar.tgz/folder/folder2/landsat_b2.tif", Qgis::SublayerQueryFlag::FastScan );
QCOMPARE( res.count(), 1 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "testtar" ) ); // this is wrong, should be landsat_b2.tif
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "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 );
res = gdalMetadata->querySublayers( QStringLiteral( "/vsitar/" ) + QStringLiteral( TEST_DATA_DIR ) + "/zip/testtar.tgz/landsat_b1.tif" );
res = gdalMetadata->querySublayers( QStringLiteral( "/vsitar/" ) + QStringLiteral( TEST_DATA_DIR ) + "/zip/testtar.tgz/landsat_b1.tif", Qgis::SublayerQueryFlag::FastScan );
QCOMPARE( res.count(), 1 );
QCOMPARE( res.at( 0 ).layerNumber(), 1 );
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" ) );
QCOMPARE( res.at( 0 ).type(), QgsMapLayerType::RasterLayer );
QCOMPARE( res.at( 0 ).driverName(), QStringLiteral( "GTiff" ) );

// multi-layer archive, format not supported by gdal
res = gdalMetadata->querySublayers( QStringLiteral( "/vsitar/" ) + QStringLiteral( TEST_DATA_DIR ) + "/zip/testtar.tgz/points.qml", Qgis::SublayerQueryFlag::FastScan );
@@ -2335,7 +2335,7 @@ def test_provider_sublayer_details_fast_scan(self):
res = metadata.querySublayers('/vsizip/' + TEST_DATA_DIR + '/zip/points2.zip/points.shp', Qgis.SublayerQueryFlag.FastScan)
self.assertEqual(len(res), 1)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "points2") # this is wrong
self.assertEqual(res[0].name(), "points")
self.assertEqual(res[0].description(), '')
self.assertEqual(res[0].uri(), '/vsizip/' + TEST_DATA_DIR + "/zip/points2.zip/points.shp")
self.assertEqual(res[0].providerKey(), "ogr")

0 comments on commit d15655c

Please sign in to comment.