Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Processing metadata tools #59913

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
307 changes: 307 additions & 0 deletions src/analysis/processing/qgsmetadataalgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,311 @@ QVariantMap QgsExportLayerMetadataAlgorithm::processAlgorithm( const QVariantMap
return results;
}

///

QString QgsAddHistoryMetadataAlgorithm::name() const
{
return QStringLiteral( "addhistorymetadata" );
}

QString QgsAddHistoryMetadataAlgorithm::displayName() const
{
return QObject::tr( "Add history metadata" );
}

QStringList QgsAddHistoryMetadataAlgorithm::tags() const
{
return QObject::tr( "add,history,metadata" ).split( ',' );
}

QString QgsAddHistoryMetadataAlgorithm::group() const
{
return QObject::tr( "Metadata tools" );
}

QString QgsAddHistoryMetadataAlgorithm::groupId() const
{
return QStringLiteral( "metadatatools" );
}

QString QgsAddHistoryMetadataAlgorithm::shortHelpString() const
{
return QObject::tr( "Adds a new history entry to the layer's metadata." );
}

QgsAddHistoryMetadataAlgorithm *QgsAddHistoryMetadataAlgorithm::createInstance() const
{
return new QgsAddHistoryMetadataAlgorithm();
}

void QgsAddHistoryMetadataAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "HISTORY" ), QObject::tr( "History entry" ) ) );
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated" ) ) );
}

bool QgsAddHistoryMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
const QString history = parameterAsString( parameters, QStringLiteral( "HISTORY" ), context );

if ( !layer )
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );

mLayerId = layer->id();

std::unique_ptr<QgsLayerMetadata> md( layer->metadata().clone() );
md->addHistoryItem( history );
layer->setMetadata( *md.get() );

return true;
}

QVariantMap QgsAddHistoryMetadataAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
Q_UNUSED( parameters );
Q_UNUSED( context );

QVariantMap results;
results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
return results;
}

///

QString QgsUpdateLayerMetadataAlgorithm::name() const
{
return QStringLiteral( "updatelayermetadata" );
}

QString QgsUpdateLayerMetadataAlgorithm::displayName() const
{
return QObject::tr( "Update layer metadata" );
}

QStringList QgsUpdateLayerMetadataAlgorithm::tags() const
{
return QObject::tr( "change,update,layer,metadata,qmd" ).split( ',' );
}

QString QgsUpdateLayerMetadataAlgorithm::group() const
{
return QObject::tr( "Metadata tools" );
}

QString QgsUpdateLayerMetadataAlgorithm::groupId() const
{
return QStringLiteral( "metadatatools" );
}

QString QgsUpdateLayerMetadataAlgorithm::shortHelpString() const
{
return QObject::tr( "Copies all non-empty metadata fields from an input layer to a target layer.\n\nLeaves empty input fields unchaged in the target." );
alexbruy marked this conversation as resolved.
Show resolved Hide resolved
}

QgsUpdateLayerMetadataAlgorithm *QgsUpdateLayerMetadataAlgorithm::createInstance() const
{
return new QgsUpdateLayerMetadataAlgorithm();
}

void QgsUpdateLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Source layer" ) ) );
alexbruy marked this conversation as resolved.
Show resolved Hide resolved
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "TARGET" ), QObject::tr( "Target layer" ) ) );
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated layer" ) ) );
}

bool QgsUpdateLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsMapLayer *inputLayer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
alexbruy marked this conversation as resolved.
Show resolved Hide resolved
QgsMapLayer *targetLayer = parameterAsLayer( parameters, QStringLiteral( "TARGET" ), context );

if ( !inputLayer )
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
alexbruy marked this conversation as resolved.
Show resolved Hide resolved

if ( !targetLayer )
throw QgsProcessingException( QObject::tr( "Invalid target layer" ) );

mLayerId = targetLayer->id();

std::unique_ptr<QgsLayerMetadata> md( targetLayer->metadata().clone() );
md->combine( &inputLayer->metadata() );
targetLayer->setMetadata( *md.get() );

return true;
}

QVariantMap QgsUpdateLayerMetadataAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
Q_UNUSED( parameters );
Q_UNUSED( context );

QVariantMap results;
results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
return results;
}

///

QString QgsSetMetadataFieldsAlgorithm::name() const
{
return QStringLiteral( "setmetadatafields" );
}

QString QgsSetMetadataFieldsAlgorithm::displayName() const
{
return QObject::tr( "Set metadata fields" );
}

QStringList QgsSetMetadataFieldsAlgorithm::tags() const
{
return QObject::tr( "set,metadata,title,abstract,identifier" ).split( ',' );
}

QString QgsSetMetadataFieldsAlgorithm::group() const
{
return QObject::tr( "Metadata tools" );
}

QString QgsSetMetadataFieldsAlgorithm::groupId() const
{
return QStringLiteral( "metadatatools" );
}

QString QgsSetMetadataFieldsAlgorithm::shortHelpString() const
{
return QObject::tr( "Sets various metadata fields for a layer." );
}

QgsSetMetadataFieldsAlgorithm *QgsSetMetadataFieldsAlgorithm::createInstance() const
{
return new QgsSetMetadataFieldsAlgorithm();
}

void QgsSetMetadataFieldsAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "IDENTIFIER" ), QObject::tr( "Identifier" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "PARENT_IDENTIFIER" ), QObject::tr( "Parent identifier" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "TITLE" ), QObject::tr( "Title" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "TYPE" ), QObject::tr( "Type" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "LANGUAGE" ), QObject::tr( "Language" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "ABSTRACT" ), QObject::tr( "Abstract" ), QVariant(), true, true ) );
addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Coordinatem reference system" ), QVariant(), true ) );
addParameter( new QgsProcessingParameterString( QStringLiteral( "FEES" ), QObject::tr( "Fees" ), QVariant(), false, true ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "IGNORE_EMPTY" ), QObject::tr( "Ignore empty fields" ), false ) );
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated" ) ) );
}

bool QgsSetMetadataFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );

if ( !layer )
throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );

mLayerId = layer->id();

const bool ignoreEmpty = parameterAsBool( parameters, QStringLiteral( "IGNORE_EMPTY" ), context );

std::unique_ptr<QgsLayerMetadata> md( layer->metadata().clone() );

if ( parameters.value( QStringLiteral( "IDENTIFIER" ) ).isValid() )
{
const QString identifier = parameterAsString( parameters, QStringLiteral( "IDENTIFIER" ), context );
if ( !identifier.isEmpty() || !ignoreEmpty )
{
md->setIdentifier( identifier );
}
}

if ( parameters.value( QStringLiteral( "PARENT_IDENTIFIER" ) ).isValid() )
{
const QString parentIdentifier = parameterAsString( parameters, QStringLiteral( "PARENT_IDENTIFIER" ), context );
if ( !parentIdentifier.isEmpty() || !ignoreEmpty )
{
md->setParentIdentifier( parentIdentifier );
}
}

if ( parameters.value( QStringLiteral( "TITLE" ) ).isValid() )
{
const QString title = parameterAsString( parameters, QStringLiteral( "TITLE" ), context );
if ( !title.isEmpty() || !ignoreEmpty )
{
md->setTitle( title );
}
}

if ( parameters.value( QStringLiteral( "TYPE" ) ).isValid() )
{
const QString type = parameterAsString( parameters, QStringLiteral( "TYPE" ), context );
if ( !type.isEmpty() || !ignoreEmpty )
{
md->setType( type );
}
}

if ( parameters.value( QStringLiteral( "LANGUAGE" ) ).isValid() )
{
const QString language = parameterAsString( parameters, QStringLiteral( "LANGUAGE" ), context );
if ( !language.isEmpty() || !ignoreEmpty )
{
md->setLanguage( language );
}
}

if ( parameters.value( QStringLiteral( "ENCODING" ) ).isValid() )
{
const QString encoding = parameterAsString( parameters, QStringLiteral( "ENCODING" ), context );
if ( !encoding.isEmpty() || !ignoreEmpty )
{
md->setEncoding( encoding );
}
}

if ( parameters.value( QStringLiteral( "ABSTRACT" ) ).isValid() )
{
const QString abstract = parameterAsString( parameters, QStringLiteral( "ABSTRACT" ), context );
if ( !abstract.isEmpty() || !ignoreEmpty )
{
md->setAbstract( abstract );
}
}

if ( parameters.value( QStringLiteral( "CRS" ) ).isValid() )
{
const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
if ( crs.isValid() || ( !crs.isValid() && !ignoreEmpty ) )
{
md->setCrs( crs );
}
}

if ( parameters.value( QStringLiteral( "FEES" ) ).isValid() )
{
const QString fees = parameterAsString( parameters, QStringLiteral( "FEES" ), context );
if ( !fees.isEmpty() || !ignoreEmpty )
{
md->setFees( fees );
}
}

layer->setMetadata( *md.get() );

return true;
}

QVariantMap QgsSetMetadataFieldsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
Q_UNUSED( parameters );
Q_UNUSED( context );

QVariantMap results;
results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
return results;
}

///@endcond
74 changes: 73 additions & 1 deletion src/analysis/processing/qgsmetadataalgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class QgsApplyLayerMetadataAlgorithm : public QgsProcessingAlgorithm
QString mLayerId;
};


/**
* Native export layer metadata algorithm.
*/
Expand All @@ -97,6 +96,79 @@ class QgsExportLayerMetadataAlgorithm : public QgsProcessingAlgorithm
QString mLayerId;
};

/**
* Native add history metadata algorithm.
*/
class QgsAddHistoryMetadataAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsAddHistoryMetadataAlgorithm() = 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;
QgsAddHistoryMetadataAlgorithm *createInstance() const override SIP_FACTORY;

protected:
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * ) override;

private:
QString mLayerId;
};

/**
* Native update layer metadata algorithm.
*/
class QgsUpdateLayerMetadataAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsUpdateLayerMetadataAlgorithm() = 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;
QgsUpdateLayerMetadataAlgorithm *createInstance() const override SIP_FACTORY;

protected:
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * ) override;

private:
QString mLayerId;
};


/**
* Native set metadata fields algorithm.
*/
class QgsSetMetadataFieldsAlgorithm : public QgsProcessingAlgorithm
{
public:
QgsSetMetadataFieldsAlgorithm() = 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;
QgsSetMetadataFieldsAlgorithm *createInstance() const override SIP_FACTORY;

protected:
bool prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) override;
QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * ) override;

private:
QString mLayerId;
};

///@endcond PRIVATE

#endif // QGSMETADATAALGORITHMS_H
Loading
Loading