Skip to content
Permalink
Browse files
OGR querySublayers can scan inside vsi archives
  • Loading branch information
nyalldawson committed Jul 12, 2021
1 parent 5556848 commit 79ad5274e0293293a08fdf15f3f7378030d90f96
Showing with 74 additions and 1 deletion.
  1. +46 −0 src/core/providers/ogr/qgsogrprovidermetadata.cpp
  2. +28 −1 tests/src/python/test_provider_ogr.py
@@ -1049,6 +1049,52 @@ QList<QgsProviderSublayerDetails> QgsOgrProviderMetadata::querySublayers( const
}
}

if ( !uriParts.value( QStringLiteral( "vsiPrefix" ) ).toString().isEmpty()
&& uriParts.value( QStringLiteral( "vsiSuffix" ) ).toString().isEmpty() )
{
// get list of files inside archive file
QgsDebugMsgLevel( QStringLiteral( "Open file %1 with gdal vsi" ).arg( vsiPrefix + uriParts.value( QStringLiteral( "path" ) ).toString() ), 3 );
char **papszSiblingFiles = VSIReadDirRecursive( QString( vsiPrefix + uriParts.value( QStringLiteral( "path" ) ).toString() ).toLocal8Bit().constData() );
if ( papszSiblingFiles )
{
QList<QgsProviderSublayerDetails> res;

QStringList files;
for ( int i = 0; papszSiblingFiles[i]; i++ )
{
files << papszSiblingFiles[i];
}

for ( const QString &file : std::as_const( files ) )
{
if ( feedback && feedback->isCanceled() )
break;

// ugly hack to remove .dbf file if there is a .shp file
QFileInfo info( file );
if ( info.suffix().compare( QLatin1String( "dbf" ), Qt::CaseInsensitive ) == 0 )
{
if ( files.contains( file.left( file.size() - 4 ) + ".shp" ) )
continue;
}
if ( info.completeSuffix().compare( QLatin1String( "shp.xml" ), Qt::CaseInsensitive ) == 0
|| info.completeSuffix().compare( QLatin1String( "shx" ), Qt::CaseInsensitive ) == 0 )
{
continue;
}

// skip directories (files ending with /)
if ( file.right( 1 ) != QLatin1String( "/" ) )
{
uriParts.insert( QStringLiteral( "vsiSuffix" ), QStringLiteral( "/%1" ).arg( file ) );
res << querySublayers( encodeUri( uriParts ), flags, feedback );
}
}
CSLDestroy( papszSiblingFiles );
return res;
}
}

const QString originalUriLayerName = uriParts.value( QStringLiteral( "layerName" ) ).toString();
int layerId = 0;
bool originalUriLayerIdWasSpecified = false;
@@ -1536,7 +1536,7 @@ def test_provider_sublayer_details(self):
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "points")
self.assertEqual(res[0].description(), '')
self.assertEqual(res[0].uri(), '/vsizip/' + TEST_DATA_DIR + "/zip/points2.zip|layername=points")
self.assertEqual(res[0].uri(), '/vsizip/' + TEST_DATA_DIR + "/zip/points2.zip/points.shp|layername=points")
self.assertEqual(res[0].providerKey(), "ogr")
self.assertEqual(res[0].type(), QgsMapLayerType.VectorLayer)
self.assertEqual(res[0].wkbType(), QgsWkbTypes.Point)
@@ -1546,6 +1546,33 @@ def test_provider_sublayer_details(self):
self.assertTrue(vl.isValid())
self.assertEqual(vl.wkbType(), QgsWkbTypes.Point)

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].description(), '')
self.assertEqual(res[0].uri(), '/vsitar/' + TEST_DATA_DIR + "/zip/testtar.tgz/folder/points.geojson|layername=points")
self.assertEqual(res[0].providerKey(), "ogr")
self.assertEqual(res[0].type(), QgsMapLayerType.VectorLayer)
self.assertEqual(res[0].wkbType(), QgsWkbTypes.Point)
self.assertEqual(res[0].geometryColumnName(), '')
options = QgsProviderSublayerDetails.LayerOptions(QgsCoordinateTransformContext())
vl = res[0].toLayer(options)
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].description(), '')
self.assertEqual(res[1].uri(), '/vsitar/' + TEST_DATA_DIR + "/zip/testtar.tgz/points.shp|layername=points")
self.assertEqual(res[1].providerKey(), "ogr")
self.assertEqual(res[1].type(), QgsMapLayerType.VectorLayer)
self.assertEqual(res[1].wkbType(), QgsWkbTypes.Point)
self.assertEqual(res[1].geometryColumnName(), '')
options = QgsProviderSublayerDetails.LayerOptions(QgsCoordinateTransformContext())
vl = res[1].toLayer(options)
self.assertTrue(vl.isValid())
self.assertEqual(vl.wkbType(), QgsWkbTypes.Point)

# geometry collection sublayers -- requires a scan to resolve geometry type
res = metadata.querySublayers(os.path.join(TEST_DATA_DIR, 'multipatch.shp'))
self.assertEqual(len(res), 1)

0 comments on commit 79ad527

Please sign in to comment.