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 all 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
319 changes: 313 additions & 6 deletions src/analysis/processing/qgsmetadataalgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ QString QgsCopyLayerMetadataAlgorithm::groupId() const

QString QgsCopyLayerMetadataAlgorithm::shortHelpString() const
{
return QObject::tr( "Copies metadata from an input layer to a target layer.\n\nAny existing metadata in the target layer will be replaced." );
return QObject::tr( "Copies metadata from an source layer to a target layer.\n\nAny existing metadata in the target layer will be replaced." );
}

QgsCopyLayerMetadataAlgorithm *QgsCopyLayerMetadataAlgorithm::createInstance() const
Expand All @@ -56,27 +56,27 @@ QgsCopyLayerMetadataAlgorithm *QgsCopyLayerMetadataAlgorithm::createInstance() c

void QgsCopyLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Source layer" ) ) );
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "SOURCE" ), QObject::tr( "Source layer" ) ) );
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "TARGET" ), QObject::tr( "Target layer" ) ) );
addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DEFAULT" ), QObject::tr( "Save metadata as default" ), false ) );
addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated layer" ) ) );
}

bool QgsCopyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
{
QgsMapLayer *inputLayer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
QgsMapLayer *sourceLayer = parameterAsLayer( parameters, QStringLiteral( "SOURCE" ), context );
QgsMapLayer *targetLayer = parameterAsLayer( parameters, QStringLiteral( "TARGET" ), context );
const bool saveAsDefault = parameterAsBool( parameters, QStringLiteral( "DEFAULT" ), context );

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

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

mLayerId = targetLayer->id();

targetLayer->setMetadata( inputLayer->metadata() );
targetLayer->setMetadata( sourceLayer->metadata() );
if ( saveAsDefault )
{
bool ok;
Expand Down 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 source layer to a target layer.\n\nLeaves empty input fields unchanged in the target." );
}

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

void QgsUpdateLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "SOURCE" ), QObject::tr( "Source layer" ) ) );
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 *sourceLayer = parameterAsLayer( parameters, QStringLiteral( "SOURCE" ), context );
QgsMapLayer *targetLayer = parameterAsLayer( parameters, QStringLiteral( "TARGET" ), context );

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

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

mLayerId = targetLayer->id();

std::unique_ptr<QgsLayerMetadata> md( targetLayer->metadata().clone() );
md->combine( &sourceLayer->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() || !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
Loading
Loading