Skip to content
Permalink
Browse files
Implement querySublayers for mdal provider
  • Loading branch information
nyalldawson committed Jun 23, 2021
1 parent 69debff commit 04a735798ee336a51409315bc529f09b611aae2a
@@ -23,7 +23,9 @@
#include "qgsapplication.h"
#include "qgsmdaldataitems.h"
#include "qgsmeshdataprovidertemporalcapabilities.h"
#include "qgsprovidersublayerdetails.h"

#include <QFileInfo>

const QString QgsMdalProvider::MDAL_PROVIDER_KEY = QStringLiteral( "mdal" );
const QString QgsMdalProvider::MDAL_PROVIDER_DESCRIPTION = QStringLiteral( "MDAL provider" );
@@ -998,6 +1000,56 @@ QgsProviderMetadata::ProviderCapabilities QgsMdalProviderMetadata::providerCapab
return FileBasedUris;
}

QList<QgsProviderSublayerDetails> QgsMdalProviderMetadata::querySublayers( const QString &uri, Qgis::SublayerQueryFlags, QgsFeedback * ) const
{
if ( uri.isEmpty() )
return {};

// get suffix, removing .gz if present
const QFileInfo info( uri );
// allow only normal files
if ( !info.isFile() )
return {};

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

static QStringList sExtensions;
static std::once_flag initialized;
std::call_once( initialized, [ = ]( )
{
QStringList meshExtensions;
QStringList datasetsExtensions;
QgsMdalProvider::fileMeshExtensions( sExtensions, datasetsExtensions );
Q_UNUSED( datasetsExtensions )
} );

// Filter files by extension
if ( !sExtensions.contains( suffix ) )
return {};

const QStringList meshNames = QString( MDAL_MeshNames( uri.toUtf8() ) ).split( QStringLiteral( ";;" ) );

QList<QgsProviderSublayerDetails> res;
res.reserve( meshNames.size() );
int layerIndex = 0;
for ( const QString &layerUri : meshNames )
{
QgsProviderSublayerDetails details;
details.setUri( layerUri );
details.setProviderKey( QStringLiteral( "mdal" ) );
details.setType( QgsMapLayerType::MeshLayer );
details.setLayerNumber( layerIndex );

// strip the driver name and path from the MDAL uri to get the layer name
details.setName( layerUri.mid( layerUri.indexOf( uri ) + uri.length() + 2 ) );

res << details;

layerIndex++;
}
return res;
}

QString QgsMdalProviderMetadata::filters( FilterType type )
{
switch ( type )
@@ -146,6 +146,7 @@ class QgsMdalProviderMetadata: public QgsProviderMetadata
QVariantMap decodeUri( const QString &uri ) const override;
QString encodeUri( const QVariantMap &parts ) const override;
ProviderCapabilities providerCapabilities() const override;
QList< QgsProviderSublayerDetails > querySublayers( const QString &uri, Qgis::SublayerQueryFlags flags = Qgis::SublayerQueryFlags(), QgsFeedback *feedback = nullptr ) const override;
};

#endif //QGSMDALPROVIDER_H
@@ -30,6 +30,8 @@
#include "qgsmeshlayertemporalproperties.h"

#include "qgsmeshdataprovidertemporalcapabilities.h"
#include "qgsprovidermetadata.h"
#include "qgsprovidersublayerdetails.h"

/**
* \ingroup UnitTests
@@ -92,6 +94,8 @@ class TestQgsMeshLayer : public QObject
void test_memory_dataset_group_1d();

void test_setDataSource();

void testMdalProviderQuerySublayers();
};

QString TestQgsMeshLayer::readFile( const QString &fname ) const
@@ -1582,6 +1586,43 @@ void TestQgsMeshLayer::test_setDataSource()
QCOMPARE( QgsMeshDatasetValue( 2.0 ), layerWithBadDataSource.datasetValue( QgsMeshDatasetIndex( 4, 0 ), 1 ) );
}

void TestQgsMeshLayer::testMdalProviderQuerySublayers()
{
// test querying sub layers for a mesh layer
QgsProviderMetadata *mdalMetadata = QgsProviderRegistry::instance()->providerMetadata( QStringLiteral( "mdal" ) );

// invalid uri
QList< QgsProviderSublayerDetails >res = mdalMetadata->querySublayers( QString() );
QVERIFY( res.empty() );

// not a mesh
res = mdalMetadata->querySublayers( QString( TEST_DATA_DIR ) + "/lines.shp" );
QVERIFY( res.empty() );

// single layer mesh
res = mdalMetadata->querySublayers( mDataDir + "/quad_and_triangle.2dm" );
QCOMPARE( res.count(), 1 );
QCOMPARE( res.at( 0 ).layerNumber(), 0 );
QCOMPARE( res.at( 0 ).name(), QString( ) );
QCOMPARE( res.at( 0 ).uri(), QStringLiteral( "2DM:\"%1/quad_and_triangle.2dm\"" ).arg( mDataDir ) );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "mdal" ) );
QCOMPARE( res.at( 0 ).type(), QgsMapLayerType::MeshLayer );

// mesh with two layers
res = mdalMetadata->querySublayers( mDataDir + "/manzese_1d2d_small_map.nc" );
QCOMPARE( res.count(), 2 );
QCOMPARE( res.at( 0 ).layerNumber(), 0 );
QCOMPARE( res.at( 0 ).name(), QStringLiteral( "mesh1d" ) );
QCOMPARE( res.at( 0 ).uri(), QStringLiteral( "Ugrid:\"%1/manzese_1d2d_small_map.nc\":mesh1d" ).arg( mDataDir ) );
QCOMPARE( res.at( 0 ).providerKey(), QStringLiteral( "mdal" ) );
QCOMPARE( res.at( 0 ).type(), QgsMapLayerType::MeshLayer );
QCOMPARE( res.at( 1 ).layerNumber(), 1 );
QCOMPARE( res.at( 1 ).name(), QStringLiteral( "mesh2d" ) );
QCOMPARE( res.at( 1 ).uri(), QStringLiteral( "Ugrid:\"%1/manzese_1d2d_small_map.nc\":mesh2d" ).arg( mDataDir ) );
QCOMPARE( res.at( 1 ).providerKey(), QStringLiteral( "mdal" ) );
QCOMPARE( res.at( 1 ).type(), QgsMapLayerType::MeshLayer );
}

void TestQgsMeshLayer::test_temporal()
{
qint64 relativeTime_0 = -1000;
Binary file not shown.

0 comments on commit 04a7357

Please sign in to comment.