QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsmetadataalgorithms.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmetadataalgorithms.cpp
3 ---------------------
4 begin : November 2024
5 copyright : (C) 2024 by Alexander Bruy
6 email : alexander dot bruy at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
21
22QString QgsCopyLayerMetadataAlgorithm::name() const
23{
24 return QStringLiteral( "copylayermetadata" );
25}
26
27QString QgsCopyLayerMetadataAlgorithm::displayName() const
28{
29 return QObject::tr( "Copy layer metadata" );
30}
31
32QStringList QgsCopyLayerMetadataAlgorithm::tags() const
33{
34 return QObject::tr( "change,layer,metadata,qmd" ).split( ',' );
35}
36
37QString QgsCopyLayerMetadataAlgorithm::group() const
38{
39 return QObject::tr( "Metadata tools" );
40}
41
42QString QgsCopyLayerMetadataAlgorithm::groupId() const
43{
44 return QStringLiteral( "metadatatools" );
45}
46
47QString QgsCopyLayerMetadataAlgorithm::shortHelpString() const
48{
49 return QObject::tr( "Copies metadata from an source layer to a target layer.\n\nAny existing metadata in the target layer will be replaced." );
50}
51
52QgsCopyLayerMetadataAlgorithm *QgsCopyLayerMetadataAlgorithm::createInstance() const
53{
54 return new QgsCopyLayerMetadataAlgorithm();
55}
56
57void QgsCopyLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
58{
59 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "SOURCE" ), QObject::tr( "Source layer" ) ) );
60 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "TARGET" ), QObject::tr( "Target layer" ) ) );
61 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DEFAULT" ), QObject::tr( "Save metadata as default" ), false ) );
62 addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated layer" ) ) );
63}
64
65bool QgsCopyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
66{
67 QgsMapLayer *sourceLayer = parameterAsLayer( parameters, QStringLiteral( "SOURCE" ), context );
68 QgsMapLayer *targetLayer = parameterAsLayer( parameters, QStringLiteral( "TARGET" ), context );
69 const bool saveAsDefault = parameterAsBool( parameters, QStringLiteral( "DEFAULT" ), context );
70
71 if ( !sourceLayer )
72 throw QgsProcessingException( QObject::tr( "Invalid source layer" ) );
73
74 if ( !targetLayer )
75 throw QgsProcessingException( QObject::tr( "Invalid target layer" ) );
76
77 mLayerId = targetLayer->id();
78
79 targetLayer->setMetadata( sourceLayer->metadata() );
80 if ( saveAsDefault )
81 {
82 bool ok;
83 targetLayer->saveDefaultMetadata( ok );
84 if ( !ok )
85 {
86 throw QgsProcessingException( QObject::tr( "Failed to save metadata as default metadata." ) );
87 }
88 }
89 return true;
90}
91
92QVariantMap QgsCopyLayerMetadataAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
93{
94 Q_UNUSED( parameters );
95 Q_UNUSED( context );
96
97 QVariantMap results;
98 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
99 return results;
100}
101
103
104QString QgsApplyLayerMetadataAlgorithm::name() const
105{
106 return QStringLiteral( "setlayermetadata" );
107}
108
109QString QgsApplyLayerMetadataAlgorithm::displayName() const
110{
111 return QObject::tr( "Set layer metadata" );
112}
113
114QStringList QgsApplyLayerMetadataAlgorithm::tags() const
115{
116 return QObject::tr( "change,layer,metadata,qmd" ).split( ',' );
117}
118
119QString QgsApplyLayerMetadataAlgorithm::group() const
120{
121 return QObject::tr( "Metadata tools" );
122}
123
124QString QgsApplyLayerMetadataAlgorithm::groupId() const
125{
126 return QStringLiteral( "metadatatools" );
127}
128
129QString QgsApplyLayerMetadataAlgorithm::shortHelpString() const
130{
131 return QObject::tr( "Applies the metadata to a layer. The metadata must be defined as QMD file.\n\nAny existing metadata in the layer will be replaced." );
132}
133
134QgsApplyLayerMetadataAlgorithm *QgsApplyLayerMetadataAlgorithm::createInstance() const
135{
136 return new QgsApplyLayerMetadataAlgorithm();
137}
138
139void QgsApplyLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
140{
141 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
142 addParameter( new QgsProcessingParameterFile( QStringLiteral( "METADATA" ), QObject::tr( "Metadata file" ), Qgis::ProcessingFileParameterBehavior::File, QStringLiteral( "qmd" ) ) );
143 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "DEFAULT" ), QObject::tr( "Save metadata as default" ), false ) );
144 addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated" ) ) );
145}
146
147bool QgsApplyLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
148{
149 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
150 const QString metadata = parameterAsFile( parameters, QStringLiteral( "METADATA" ), context );
151 const bool saveAsDefault = parameterAsBool( parameters, QStringLiteral( "DEFAULT" ), context );
152
153 if ( !layer )
154 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
155
156 mLayerId = layer->id();
157
158 bool ok = false;
159 const QString msg = layer->loadNamedMetadata( metadata, ok );
160 if ( !ok )
161 {
162 throw QgsProcessingException( QObject::tr( "Failed to apply metadata. Error: %1" ).arg( msg ) );
163 }
164
165 if ( saveAsDefault )
166 {
167 bool ok;
168 layer->saveDefaultMetadata( ok );
169 if ( !ok )
170 {
171 throw QgsProcessingException( QObject::tr( "Failed to save metadata as default metadata." ) );
172 }
173 }
174
175 return true;
176}
177
178QVariantMap QgsApplyLayerMetadataAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
179{
180 Q_UNUSED( parameters );
181 Q_UNUSED( context );
182
183 QVariantMap results;
184 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
185 return results;
186}
187
189
190QString QgsExportLayerMetadataAlgorithm::name() const
191{
192 return QStringLiteral( "exportlayermetadata" );
193}
194
195QString QgsExportLayerMetadataAlgorithm::displayName() const
196{
197 return QObject::tr( "Export layer metadata" );
198}
199
200QStringList QgsExportLayerMetadataAlgorithm::tags() const
201{
202 return QObject::tr( "export,layer,metadata,qmd" ).split( ',' );
203}
204
205QString QgsExportLayerMetadataAlgorithm::group() const
206{
207 return QObject::tr( "Metadata tools" );
208}
209
210QString QgsExportLayerMetadataAlgorithm::groupId() const
211{
212 return QStringLiteral( "metadatatools" );
213}
214
215QString QgsExportLayerMetadataAlgorithm::shortHelpString() const
216{
217 return QObject::tr( "Exports layer's metadata to a QMD file." );
218}
219
220QgsExportLayerMetadataAlgorithm *QgsExportLayerMetadataAlgorithm::createInstance() const
221{
222 return new QgsExportLayerMetadataAlgorithm();
223}
224
225void QgsExportLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
226{
227 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
228 addParameter( new QgsProcessingParameterFileDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Output" ), QObject::tr( "QGIS Metadata File" ) + QStringLiteral( " (*.qmd *.QMD)" ) ) );
229}
230
231QVariantMap QgsExportLayerMetadataAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
232{
233 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
234 const QString outputFile = parameterAsString( parameters, QStringLiteral( "OUTPUT" ), context );
235
236 if ( !layer )
237 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
238
239 bool ok = false;
240 const QString message = layer->saveNamedMetadata( outputFile, ok );
241 if ( !ok )
242 {
243 throw QgsProcessingException( QObject::tr( "Failed to save metadata. Error: %1" ).arg( message ) );
244 }
245
246 QVariantMap results;
247 results.insert( QStringLiteral( "OUTPUT" ), outputFile );
248 return results;
249}
250
252
253QString QgsAddHistoryMetadataAlgorithm::name() const
254{
255 return QStringLiteral( "addhistorymetadata" );
256}
257
258QString QgsAddHistoryMetadataAlgorithm::displayName() const
259{
260 return QObject::tr( "Add history metadata" );
261}
262
263QStringList QgsAddHistoryMetadataAlgorithm::tags() const
264{
265 return QObject::tr( "add,history,metadata" ).split( ',' );
266}
267
268QString QgsAddHistoryMetadataAlgorithm::group() const
269{
270 return QObject::tr( "Metadata tools" );
271}
272
273QString QgsAddHistoryMetadataAlgorithm::groupId() const
274{
275 return QStringLiteral( "metadatatools" );
276}
277
278QString QgsAddHistoryMetadataAlgorithm::shortHelpString() const
279{
280 return QObject::tr( "Adds a new history entry to the layer's metadata." );
281}
282
283QgsAddHistoryMetadataAlgorithm *QgsAddHistoryMetadataAlgorithm::createInstance() const
284{
285 return new QgsAddHistoryMetadataAlgorithm();
286}
287
288void QgsAddHistoryMetadataAlgorithm::initAlgorithm( const QVariantMap & )
289{
290 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
291 addParameter( new QgsProcessingParameterString( QStringLiteral( "HISTORY" ), QObject::tr( "History entry" ) ) );
292 addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated" ) ) );
293}
294
295bool QgsAddHistoryMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
296{
297 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
298 const QString history = parameterAsString( parameters, QStringLiteral( "HISTORY" ), context );
299
300 if ( !layer )
301 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
302
303 mLayerId = layer->id();
304
305 std::unique_ptr<QgsLayerMetadata> md( layer->metadata().clone() );
306 md->addHistoryItem( history );
307 layer->setMetadata( *md.get() );
308
309 return true;
310}
311
312QVariantMap QgsAddHistoryMetadataAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
313{
314 Q_UNUSED( parameters );
315 Q_UNUSED( context );
316
317 QVariantMap results;
318 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
319 return results;
320}
321
323
324QString QgsUpdateLayerMetadataAlgorithm::name() const
325{
326 return QStringLiteral( "updatelayermetadata" );
327}
328
329QString QgsUpdateLayerMetadataAlgorithm::displayName() const
330{
331 return QObject::tr( "Update layer metadata" );
332}
333
334QStringList QgsUpdateLayerMetadataAlgorithm::tags() const
335{
336 return QObject::tr( "change,update,layer,metadata,qmd" ).split( ',' );
337}
338
339QString QgsUpdateLayerMetadataAlgorithm::group() const
340{
341 return QObject::tr( "Metadata tools" );
342}
343
344QString QgsUpdateLayerMetadataAlgorithm::groupId() const
345{
346 return QStringLiteral( "metadatatools" );
347}
348
349QString QgsUpdateLayerMetadataAlgorithm::shortHelpString() const
350{
351 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." );
352}
353
354QgsUpdateLayerMetadataAlgorithm *QgsUpdateLayerMetadataAlgorithm::createInstance() const
355{
356 return new QgsUpdateLayerMetadataAlgorithm();
357}
358
359void QgsUpdateLayerMetadataAlgorithm::initAlgorithm( const QVariantMap & )
360{
361 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "SOURCE" ), QObject::tr( "Source layer" ) ) );
362 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "TARGET" ), QObject::tr( "Target layer" ) ) );
363 addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated layer" ) ) );
364}
365
366bool QgsUpdateLayerMetadataAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
367{
368 QgsMapLayer *sourceLayer = parameterAsLayer( parameters, QStringLiteral( "SOURCE" ), context );
369 QgsMapLayer *targetLayer = parameterAsLayer( parameters, QStringLiteral( "TARGET" ), context );
370
371 if ( !sourceLayer )
372 throw QgsProcessingException( QObject::tr( "Invalid source layer" ) );
373
374 if ( !targetLayer )
375 throw QgsProcessingException( QObject::tr( "Invalid target layer" ) );
376
377 mLayerId = targetLayer->id();
378
379 std::unique_ptr<QgsLayerMetadata> md( targetLayer->metadata().clone() );
380 md->combine( &sourceLayer->metadata() );
381 targetLayer->setMetadata( *md.get() );
382
383 return true;
384}
385
386QVariantMap QgsUpdateLayerMetadataAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
387{
388 Q_UNUSED( parameters );
389 Q_UNUSED( context );
390
391 QVariantMap results;
392 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
393 return results;
394}
395
397
398QString QgsSetMetadataFieldsAlgorithm::name() const
399{
400 return QStringLiteral( "setmetadatafields" );
401}
402
403QString QgsSetMetadataFieldsAlgorithm::displayName() const
404{
405 return QObject::tr( "Set metadata fields" );
406}
407
408QStringList QgsSetMetadataFieldsAlgorithm::tags() const
409{
410 return QObject::tr( "set,metadata,title,abstract,identifier" ).split( ',' );
411}
412
413QString QgsSetMetadataFieldsAlgorithm::group() const
414{
415 return QObject::tr( "Metadata tools" );
416}
417
418QString QgsSetMetadataFieldsAlgorithm::groupId() const
419{
420 return QStringLiteral( "metadatatools" );
421}
422
423QString QgsSetMetadataFieldsAlgorithm::shortHelpString() const
424{
425 return QObject::tr( "Sets various metadata fields for a layer." );
426}
427
428QgsSetMetadataFieldsAlgorithm *QgsSetMetadataFieldsAlgorithm::createInstance() const
429{
430 return new QgsSetMetadataFieldsAlgorithm();
431}
432
433void QgsSetMetadataFieldsAlgorithm::initAlgorithm( const QVariantMap & )
434{
435 addParameter( new QgsProcessingParameterMapLayer( QStringLiteral( "INPUT" ), QObject::tr( "Layer" ) ) );
436 addParameter( new QgsProcessingParameterString( QStringLiteral( "IDENTIFIER" ), QObject::tr( "Identifier" ), QVariant(), false, true ) );
437 addParameter( new QgsProcessingParameterString( QStringLiteral( "PARENT_IDENTIFIER" ), QObject::tr( "Parent identifier" ), QVariant(), false, true ) );
438 addParameter( new QgsProcessingParameterString( QStringLiteral( "TITLE" ), QObject::tr( "Title" ), QVariant(), false, true ) );
439 addParameter( new QgsProcessingParameterString( QStringLiteral( "TYPE" ), QObject::tr( "Type" ), QVariant(), false, true ) );
440 addParameter( new QgsProcessingParameterString( QStringLiteral( "LANGUAGE" ), QObject::tr( "Language" ), QVariant(), false, true ) );
441 addParameter( new QgsProcessingParameterString( QStringLiteral( "ENCODING" ), QObject::tr( "Encoding" ), QVariant(), false, true ) );
442 addParameter( new QgsProcessingParameterString( QStringLiteral( "ABSTRACT" ), QObject::tr( "Abstract" ), QVariant(), true, true ) );
443 addParameter( new QgsProcessingParameterCrs( QStringLiteral( "CRS" ), QObject::tr( "Coordinatem reference system" ), QVariant(), true ) );
444 addParameter( new QgsProcessingParameterString( QStringLiteral( "FEES" ), QObject::tr( "Fees" ), QVariant(), false, true ) );
445 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "IGNORE_EMPTY" ), QObject::tr( "Ignore empty fields" ), false ) );
446 addOutput( new QgsProcessingOutputMapLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Updated" ) ) );
447}
448
449bool QgsSetMetadataFieldsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
450{
451 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
452
453 if ( !layer )
454 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
455
456 mLayerId = layer->id();
457
458 const bool ignoreEmpty = parameterAsBool( parameters, QStringLiteral( "IGNORE_EMPTY" ), context );
459
460 std::unique_ptr<QgsLayerMetadata> md( layer->metadata().clone() );
461
462 if ( parameters.value( QStringLiteral( "IDENTIFIER" ) ).isValid() )
463 {
464 const QString identifier = parameterAsString( parameters, QStringLiteral( "IDENTIFIER" ), context );
465 if ( !identifier.isEmpty() || !ignoreEmpty )
466 {
467 md->setIdentifier( identifier );
468 }
469 }
470
471 if ( parameters.value( QStringLiteral( "PARENT_IDENTIFIER" ) ).isValid() )
472 {
473 const QString parentIdentifier = parameterAsString( parameters, QStringLiteral( "PARENT_IDENTIFIER" ), context );
474 if ( !parentIdentifier.isEmpty() || !ignoreEmpty )
475 {
476 md->setParentIdentifier( parentIdentifier );
477 }
478 }
479
480 if ( parameters.value( QStringLiteral( "TITLE" ) ).isValid() )
481 {
482 const QString title = parameterAsString( parameters, QStringLiteral( "TITLE" ), context );
483 if ( !title.isEmpty() || !ignoreEmpty )
484 {
485 md->setTitle( title );
486 }
487 }
488
489 if ( parameters.value( QStringLiteral( "TYPE" ) ).isValid() )
490 {
491 const QString type = parameterAsString( parameters, QStringLiteral( "TYPE" ), context );
492 if ( !type.isEmpty() || !ignoreEmpty )
493 {
494 md->setType( type );
495 }
496 }
497
498 if ( parameters.value( QStringLiteral( "LANGUAGE" ) ).isValid() )
499 {
500 const QString language = parameterAsString( parameters, QStringLiteral( "LANGUAGE" ), context );
501 if ( !language.isEmpty() || !ignoreEmpty )
502 {
503 md->setLanguage( language );
504 }
505 }
506
507 if ( parameters.value( QStringLiteral( "ENCODING" ) ).isValid() )
508 {
509 const QString encoding = parameterAsString( parameters, QStringLiteral( "ENCODING" ), context );
510 if ( !encoding.isEmpty() || !ignoreEmpty )
511 {
512 md->setEncoding( encoding );
513 }
514 }
515
516 if ( parameters.value( QStringLiteral( "ABSTRACT" ) ).isValid() )
517 {
518 const QString abstract = parameterAsString( parameters, QStringLiteral( "ABSTRACT" ), context );
519 if ( !abstract.isEmpty() || !ignoreEmpty )
520 {
521 md->setAbstract( abstract );
522 }
523 }
524
525 if ( parameters.value( QStringLiteral( "CRS" ) ).isValid() )
526 {
527 const QgsCoordinateReferenceSystem crs = parameterAsCrs( parameters, QStringLiteral( "CRS" ), context );
528 if ( crs.isValid() || !ignoreEmpty )
529 {
530 md->setCrs( crs );
531 }
532 }
533
534 if ( parameters.value( QStringLiteral( "FEES" ) ).isValid() )
535 {
536 const QString fees = parameterAsString( parameters, QStringLiteral( "FEES" ), context );
537 if ( !fees.isEmpty() || !ignoreEmpty )
538 {
539 md->setFees( fees );
540 }
541 }
542
543 layer->setMetadata( *md.get() );
544
545 return true;
546}
547
548QVariantMap QgsSetMetadataFieldsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
549{
550 Q_UNUSED( parameters );
551 Q_UNUSED( context );
552
553 QVariantMap results;
554 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
555 return results;
556}
557
@ File
Parameter is a single file.
This class represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
QgsLayerMetadata * clone() const override
Clones the metadata object.
Base class for all map layer types.
Definition qgsmaplayer.h:76
virtual QString loadNamedMetadata(const QString &uri, bool &resultFlag)
Retrieve a named metadata for this layer if one exists (either as a .qmd file on disk or as a record ...
QString id
Definition qgsmaplayer.h:79
QgsLayerMetadata metadata
Definition qgsmaplayer.h:82
virtual void setMetadata(const QgsLayerMetadata &metadata)
Sets the layer's metadata store.
virtual QString saveDefaultMetadata(bool &resultFlag)
Save the current metadata of this layer as the default metadata (either as a .qmd file on disk or as ...
QString saveNamedMetadata(const QString &uri, bool &resultFlag)
Save the current metadata of this layer as a named metadata (either as a .qmd file on disk or as a re...
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A map layer output for processing algorithms, where layers may be either vector or raster.
A boolean parameter for processing algorithms.
A coordinate reference system parameter for processing algorithms.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
An input file or folder parameter for processing algorithms.
A map layer parameter for processing algorithms.
A string parameter for processing algorithms.
const QgsCoordinateReferenceSystem & crs