QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 input 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( "INPUT" ), 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 *inputLayer = parameterAsLayer( parameters, QStringLiteral( "INPUT" ), context );
68 QgsMapLayer *targetLayer = parameterAsLayer( parameters, QStringLiteral( "TARGET" ), context );
69 const bool saveAsDefault = parameterAsBool( parameters, QStringLiteral( "DEFAULT" ), context );
70
71 if ( !inputLayer )
72 throw QgsProcessingException( QObject::tr( "Invalid input layer" ) );
73
74 if ( !targetLayer )
75 throw QgsProcessingException( QObject::tr( "Invalid target layer" ) );
76
77 mLayerId = targetLayer->id();
78
79 targetLayer->setMetadata( inputLayer->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
@ File
Parameter is a single file.
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 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.