Skip to content

Commit

Permalink
[ogr] Add special handling for querying sublayers for an ArcInfo
Browse files Browse the repository at this point in the history
coverage directory
  • Loading branch information
nyalldawson committed Oct 6, 2021
1 parent 92707dc commit 0ddb4b3
Show file tree
Hide file tree
Showing 25 changed files with 50 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/core/providers/ogr/qgsogrprovidermetadata.cpp
Expand Up @@ -37,6 +37,7 @@ email : nyall dot dawson at gmail dot com
#include <QDir>
#include <QMessageBox>
#include <QRegularExpression>
#include <QDirIterator>

///@cond PRIVATE

Expand Down Expand Up @@ -1102,18 +1103,26 @@ QList<QgsProviderSublayerDetails> QgsOgrProviderMetadata::querySublayers( const
}
}

const QStringList dirExtensions = QgsOgrProviderUtils::directoryExtensions();

const QString path = uriParts.value( QStringLiteral( "path" ) ).toString();
const QFileInfo pathInfo( path );
if ( ( flags & Qgis::SublayerQueryFlag::FastScan ) && ( pathInfo.isFile() || pathInfo.isDir() ) )
const QString suffix = pathInfo.suffix().toLower();
bool isOgrSupportedDirectory = pathInfo.isDir() && dirExtensions.contains( suffix );

bool forceDeepScanDir = false;
if ( pathInfo.isDir() && !isOgrSupportedDirectory )
{
QDirIterator it( path, { QStringLiteral( "*.adf" ), QStringLiteral( "*.ADF" ) }, QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot );
forceDeepScanDir = it.hasNext();
}

if ( ( flags & Qgis::SublayerQueryFlag::FastScan ) && ( pathInfo.isFile() || pathInfo.isDir() ) && !forceDeepScanDir )
{
// fast scan, so we don't actually try to open the dataset and instead just check the extension alone
const QStringList fileExtensions = QgsOgrProviderUtils::fileExtensions();
const QStringList dirExtensions = QgsOgrProviderUtils::directoryExtensions();

const QString suffix = pathInfo.suffix().toLower();

// allow only normal files or supported directories to continue
const bool isOgrSupportedDirectory = pathInfo.isDir() && dirExtensions.contains( suffix );
if ( !isOgrSupportedDirectory && !pathInfo.isFile() )
return {};

Expand Down
23 changes: 23 additions & 0 deletions tests/src/python/test_provider_ogr.py
Expand Up @@ -2093,6 +2093,17 @@ def test_provider_sublayer_details(self):
os.path.join(TEST_DATA_DIR, 'esri_tin', 'metadata.xml'))
self.assertFalse(res)

# ESRI Arcinfo file
res = metadata.querySublayers(os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc'))
self.assertEqual(len(res), 4)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "ARC")
self.assertEqual(res[0].description(), "")
self.assertEqual(res[0].uri(), '{}|layername=ARC'.format(os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc'), "vector_vrt.vrt"))
self.assertEqual(res[0].providerKey(), "ogr")
self.assertEqual(res[0].type(), QgsMapLayerType.VectorLayer)
self.assertFalse(res[0].skippedContainerScan())

@unittest.skipIf(int(gdal.VersionInfo('VERSION_NUM')) < GDAL_COMPUTE_VERSION(3, 4, 0), "GDAL 3.4 required")
def test_provider_sublayer_details_hierarchy(self):
"""
Expand Down Expand Up @@ -2234,6 +2245,18 @@ def test_provider_sublayer_details_fast_scan(self):
os.path.join(TEST_DATA_DIR, 'esri_tin', 'metadata.xml'), Qgis.SublayerQueryFlag.FastScan)
self.assertFalse(res)

# ESRI Arcinfo file
res = metadata.querySublayers(
os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc'), Qgis.SublayerQueryFlag.FastScan)
self.assertEqual(len(res), 4)
self.assertEqual(res[0].layerNumber(), 0)
self.assertEqual(res[0].name(), "ARC")
self.assertEqual(res[0].description(), "")
self.assertEqual(res[0].uri(), '{}|layername=ARC'.format(os.path.join(TEST_DATA_DIR, 'esri_coverage', 'testpolyavc'), "vector_vrt.vrt"))
self.assertEqual(res[0].providerKey(), "ogr")
self.assertEqual(res[0].type(), QgsMapLayerType.VectorLayer)
self.assertFalse(res[0].skippedContainerScan())

def test_provider_sidecar_files_for_uri(self):
"""
Test retrieving sidecar files for uris
Expand Down
Binary file added tests/testdata/esri_coverage/info/arc.dir
Binary file not shown.
Binary file added tests/testdata/esri_coverage/info/arc0000.dat
Binary file not shown.
Binary file added tests/testdata/esri_coverage/info/arc0000.nit
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/esri_coverage/info/arc0001.dat
@@ -0,0 +1 @@
../testpolyavc/bnd.adf
Binary file added tests/testdata/esri_coverage/info/arc0001.nit
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/esri_coverage/info/arc0002.dat
@@ -0,0 +1 @@
../testpolyavc/pat.adf
Binary file added tests/testdata/esri_coverage/info/arc0002.nit
Binary file not shown.
Binary file added tests/testdata/esri_coverage/info/arc0003.dat
Binary file not shown.
Binary file added tests/testdata/esri_coverage/info/arc0003.nit
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/esri_coverage/info/arc0004.dat
@@ -0,0 +1 @@
../testpolyavc/tic.adf
Binary file added tests/testdata/esri_coverage/info/arc0004.nit
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/arc.adf
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/arx.adf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/testdata/esri_coverage/testpolyavc/bnd.adf
@@ -0,0 +1 @@
H�|Jz>�H�t�JzD�
Binary file added tests/testdata/esri_coverage/testpolyavc/cnt.adf
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/cnx.adf
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/lab.adf
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/pal.adf
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/pat.adf
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/pax.adf
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/testdata/esri_coverage/testpolyavc/prj.adf
@@ -0,0 +1,9 @@
Projection UTM
Zone 13
Datum NAD27
Zunits NO
Units METERS
Spheroid CLARKE1866
Xshift 0.0000000000
Yshift 0.0000000000
Parameters
Binary file added tests/testdata/esri_coverage/testpolyavc/tic.adf
Binary file not shown.
Binary file added tests/testdata/esri_coverage/testpolyavc/tol.adf
Binary file not shown.

0 comments on commit 0ddb4b3

Please sign in to comment.