mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-27 00:07:16 -05:00
add native set layer metadata algorithm
This commit is contained in:
parent
18fa9f6e5c
commit
65a8df1be6
@ -89,4 +89,78 @@ QVariantMap QgsCopyLayerMetadataAlgorithm::processAlgorithm( const QVariantMap &
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
|
||||
QString QgsApplyLayerMetadataAlgorithm::name() const
|
||||
{
|
||||
return QStringLiteral( "setlayermetadata" );
|
||||
}
|
||||
|
||||
QString QgsApplyLayerMetadataAlgorithm::displayName() const
|
||||
{
|
||||
return QObject::tr( "Set layer metadata" );
|
||||
}
|
||||
|
||||
QStringList QgsApplyLayerMetadataAlgorithm::tags() const
|
||||
{
|
||||
return QObject::tr( "change,layer,metadata,qmd" ).split( ',' );
|
||||
}
|
||||
|
||||
QString QgsApplyLayerMetadataAlgorithm::group() const
|
||||
{
|
||||
return QObject::tr( "Layer tools" );
|
||||
}
|
||||
|
||||
QString QgsApplyLayerMetadataAlgorithm::groupId() const
|
||||
{
|
||||
return QStringLiteral( "layertools" );
|
||||
}
|
||||
|
||||
QString QgsApplyLayerMetadataAlgorithm::shortHelpString() const
|
||||
{
|
||||
return QObject::tr( "Applies the metadata to a layer. The metadata must be defined as QMD file." );
|
||||
}
|
||||
|
||||
QgsApplyLayerMetadataAlgorithm *QgsApplyLayerMetadataAlgorithm::createInstance() const
|
||||
{
|
||||
return new QgsApplyLayerMetadataAlgorithm();
|
||||
}
|
||||
|
||||
void QgsApplyLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
|
||||
{
|
||||
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
|
||||
addParameter( new QgsProcessingParameterFile( QStringLiteral( "METADATA" ), QObject::tr( "Metadata file" ), Qgis::ProcessingFileParameterBehavior::File, QStringLiteral( "qmd" ) ) );
|
||||
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated" ) ) );
|
||||
}
|
||||
|
||||
bool QgsApplyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
|
||||
{
|
||||
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
|
||||
const QString metadata = parameterAsFile( parameters, QStringLiteral( "METADATA" ), context );
|
||||
|
||||
if ( !layer )
|
||||
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
|
||||
|
||||
mLayerId = layer->id();
|
||||
|
||||
bool ok = false;
|
||||
const QString msg = layer->loadNamedMetadata( metadata, ok );
|
||||
if ( !ok )
|
||||
{
|
||||
throw QgsProcessingException( QObject::tr( "Failed to apply metadata. Error: %1" ).arg( msg ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
QVariantMap QgsApplyLayerMetadataAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
|
||||
{
|
||||
Q_UNUSED( parameters );
|
||||
Q_UNUSED( context );
|
||||
|
||||
QVariantMap results;
|
||||
results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
|
||||
return results;
|
||||
}
|
||||
|
||||
///@endcond
|
||||
|
||||
@ -52,6 +52,33 @@ class QgsCopyLayerMetadataAlgorithm : public QgsProcessingAlgorithm
|
||||
QString mLayerId;
|
||||
};
|
||||
|
||||
/**
|
||||
* Native apply layer metadata algorithm.
|
||||
*/
|
||||
class QgsApplyLayerMetadataAlgorithm : public QgsProcessingAlgorithm
|
||||
{
|
||||
public:
|
||||
QgsApplyLayerMetadataAlgorithm() = default;
|
||||
QString name() const override;
|
||||
QString displayName() const override;
|
||||
QStringList tags() const override;
|
||||
QString group() const override;
|
||||
QString groupId() const override;
|
||||
QString shortHelpString() const override;
|
||||
void initAlgorithm( const QVariantMap &configuration = QVariantMap() ) override;
|
||||
QgsApplyLayerMetadataAlgorithm *createInstance() const override SIP_FACTORY;
|
||||
|
||||
protected:
|
||||
|
||||
bool prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
|
||||
QVariantMap processAlgorithm( const QVariantMap ¶meters,
|
||||
QgsProcessingContext &context, QgsProcessingFeedback * ) override;
|
||||
|
||||
private:
|
||||
|
||||
QString mLayerId;
|
||||
};
|
||||
|
||||
///@endcond PRIVATE
|
||||
|
||||
#endif // QGSMETADATAALGORITHMS_H
|
||||
|
||||
@ -300,6 +300,7 @@ void QgsNativeAlgorithms::loadAlgorithms()
|
||||
addAlgorithm( new QgsAlignRastersAlgorithm() );
|
||||
addAlgorithm( new QgsAlignSingleRasterAlgorithm() );
|
||||
addAlgorithm( new QgsAngleToNearestAlgorithm() );
|
||||
addAlgorithm( new QgsApplyLayerMetadataAlgorithm() );
|
||||
addAlgorithm( new QgsApplyLayerStyleAlgorithm() );
|
||||
addAlgorithm( new QgsArrayTranslatedFeaturesAlgorithm() );
|
||||
addAlgorithm( new QgsAspectAlgorithm() );
|
||||
|
||||
@ -106,6 +106,7 @@ class TestQgsProcessingAlgsPt2: public QgsTest
|
||||
void generateElevationProfileImage();
|
||||
|
||||
void copyMetadata();
|
||||
void applyMetadata();
|
||||
|
||||
private:
|
||||
|
||||
@ -2028,6 +2029,34 @@ void TestQgsProcessingAlgsPt2::copyMetadata()
|
||||
QCOMPARE( targetMetadata.abstract(), QStringLiteral( "Abstract" ) );
|
||||
}
|
||||
|
||||
void TestQgsProcessingAlgsPt2::applyMetadata()
|
||||
{
|
||||
std::unique_ptr< QgsVectorLayer > layer = std::make_unique< QgsVectorLayer >( QStringLiteral( "Point?crs=epsg:4326&field=pk:int&field=col1:string" ), QStringLiteral( "input" ), QStringLiteral( "memory" ) );
|
||||
QVERIFY( layer->isValid() );
|
||||
|
||||
std::unique_ptr< QgsProcessingAlgorithm > alg( QgsApplication::processingRegistry()->createAlgorithmById( QStringLiteral( "native:setlayermetadata" ) ) );
|
||||
QVERIFY( alg != nullptr );
|
||||
|
||||
const QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
|
||||
const QString metadataFileName = dataDir + "/simple_metadata.qmd";
|
||||
|
||||
QVariantMap parameters;
|
||||
parameters.insert( QStringLiteral( "INPUT" ), QVariant::fromValue( layer.get() ) );
|
||||
parameters.insert( QStringLiteral( "METADATA" ), metadataFileName );
|
||||
|
||||
bool ok = false;
|
||||
std::unique_ptr< QgsProcessingContext > context = std::make_unique< QgsProcessingContext >();
|
||||
QgsProcessingFeedback feedback;
|
||||
QVariantMap results;
|
||||
results = alg->run( parameters, *context, &feedback, &ok );
|
||||
QVERIFY( ok );
|
||||
|
||||
QCOMPARE( results.value( QStringLiteral( "OUTPUT" ) ), layer->id() );
|
||||
|
||||
QgsLayerMetadata targetMetadata = layer->metadata();
|
||||
QCOMPARE( targetMetadata.title(), QStringLiteral( "Title" ) );
|
||||
QCOMPARE( targetMetadata.abstract(), QStringLiteral( "Abstract" ) );
|
||||
}
|
||||
|
||||
QGSTEST_MAIN( TestQgsProcessingAlgsPt2 )
|
||||
#include "testqgsprocessingalgspt2.moc"
|
||||
|
||||
44
tests/testdata/simple_metadata.qmd
vendored
Normal file
44
tests/testdata/simple_metadata.qmd
vendored
Normal file
@ -0,0 +1,44 @@
|
||||
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
|
||||
<qgis version="3.40.0-Bratislava">
|
||||
<identifier></identifier>
|
||||
<parentidentifier></parentidentifier>
|
||||
<language></language>
|
||||
<type></type>
|
||||
<title>Title</title>
|
||||
<abstract>Abstract</abstract>
|
||||
<contact>
|
||||
<name></name>
|
||||
<organization></organization>
|
||||
<position></position>
|
||||
<voice></voice>
|
||||
<fax></fax>
|
||||
<email></email>
|
||||
<role></role>
|
||||
</contact>
|
||||
<links/>
|
||||
<dates/>
|
||||
<fees></fees>
|
||||
<encoding></encoding>
|
||||
<crs>
|
||||
<spatialrefsys nativeFormat="Wkt">
|
||||
<wkt></wkt>
|
||||
<proj4></proj4>
|
||||
<srsid>0</srsid>
|
||||
<srid>0</srid>
|
||||
<authid></authid>
|
||||
<description></description>
|
||||
<projectionacronym></projectionacronym>
|
||||
<ellipsoidacronym></ellipsoidacronym>
|
||||
<geographicflag>false</geographicflag>
|
||||
</spatialrefsys>
|
||||
</crs>
|
||||
<extent>
|
||||
<spatial miny="179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368" maxz="0" crs="" maxy="-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368" maxx="-179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368" minx="179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368" minz="0" dimensions="2"/>
|
||||
<temporal>
|
||||
<period>
|
||||
<start></start>
|
||||
<end></end>
|
||||
</period>
|
||||
</temporal>
|
||||
</extent>
|
||||
</qgis>
|
||||
Loading…
x
Reference in New Issue
Block a user