QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsalgorithmcategorizeusingstyle.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmcategorizeusingstyle.cpp
3 ---------------------
4 begin : August 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson 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
22#include "qgsstyle.h"
23#include "qgsvectorlayer.h"
24
26
27QgsCategorizeUsingStyleAlgorithm::QgsCategorizeUsingStyleAlgorithm() = default;
28
29QgsCategorizeUsingStyleAlgorithm::~QgsCategorizeUsingStyleAlgorithm() = default;
30
31void QgsCategorizeUsingStyleAlgorithm::initAlgorithm( const QVariantMap & )
32{
33 addParameter( new QgsProcessingParameterVectorLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ), QList<int>() << static_cast<int>( Qgis::ProcessingSourceType::Vector ) ) );
34 addParameter( new QgsProcessingParameterExpression( QStringLiteral( "FIELD" ), QObject::tr( "Categorize using expression" ), QVariant(), QStringLiteral( "INPUT" ) ) );
35
36 addParameter( new QgsProcessingParameterFile( QStringLiteral( "STYLE" ), QObject::tr( "Style database (leave blank to use saved symbols)" ), Qgis::ProcessingFileParameterBehavior::File, QStringLiteral( "xml" ), QVariant(), true ) );
37 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "CASE_SENSITIVE" ), QObject::tr( "Use case-sensitive match to symbol names" ), false ) );
38 addParameter( new QgsProcessingParameterBoolean( QStringLiteral( "TOLERANT" ), QObject::tr( "Ignore non-alphanumeric characters while matching" ), false ) );
39
40 addOutput( new QgsProcessingOutputVectorLayer( QStringLiteral( "OUTPUT" ), QObject::tr( "Categorized layer" ) ) );
41
42 auto failCategories = std::make_unique<QgsProcessingParameterFeatureSink>( QStringLiteral( "NON_MATCHING_CATEGORIES" ), QObject::tr( "Non-matching categories" ), Qgis::ProcessingSourceType::Vector, QVariant(), true, false );
43 // not supported for outputs yet!
44 //failCategories->setFlags( failCategories->flags() | Qgis::ProcessingParameterFlag::Advanced );
45 addParameter( failCategories.release() );
46
47 auto failSymbols = std::make_unique<QgsProcessingParameterFeatureSink>( QStringLiteral( "NON_MATCHING_SYMBOLS" ), QObject::tr( "Non-matching symbol names" ), Qgis::ProcessingSourceType::Vector, QVariant(), true, false );
48 //failSymbols->setFlags( failSymbols->flags() | Qgis::ProcessingParameterFlag::Advanced );
49 addParameter( failSymbols.release() );
50}
51
52Qgis::ProcessingAlgorithmFlags QgsCategorizeUsingStyleAlgorithm::flags() const
53{
56 return f;
57}
58
59QString QgsCategorizeUsingStyleAlgorithm::name() const
60{
61 return QStringLiteral( "categorizeusingstyle" );
62}
63
64QString QgsCategorizeUsingStyleAlgorithm::displayName() const
65{
66 return QObject::tr( "Create categorized renderer from styles" );
67}
68
69QStringList QgsCategorizeUsingStyleAlgorithm::tags() const
70{
71 return QObject::tr( "file,database,symbols,names,category,categories" ).split( ',' );
72}
73
74QString QgsCategorizeUsingStyleAlgorithm::group() const
75{
76 return QObject::tr( "Cartography" );
77}
78
79QString QgsCategorizeUsingStyleAlgorithm::groupId() const
80{
81 return QStringLiteral( "cartography" );
82}
83
84QString QgsCategorizeUsingStyleAlgorithm::shortHelpString() const
85{
86 return QObject::tr( "This algorithm sets a vector layer's renderer to a categorized renderer using matching symbols from a style database. If no "
87 "style file is specified, symbols from the user's current style library are used instead.\n\n"
88 "The specified expression (or field name) is used to create categories for the renderer. A category will be "
89 "created for each unique value within the layer.\n\n"
90 "Each category is individually matched to the symbols which exist within the specified QGIS XML style database. Whenever "
91 "a matching symbol name is found, the category's symbol will be set to this matched symbol.\n\n"
92 "The matching is case-insensitive by default, but can be made case-sensitive if required.\n\n"
93 "Optionally, non-alphanumeric characters in both the category value and symbol name can be ignored "
94 "while performing the match. This allows for greater tolerance when matching categories to symbols.\n\n"
95 "If desired, tables can also be output containing lists of the categories which could not be matched "
96 "to symbols, and symbols which were not matched to categories."
97 );
98}
99
100QString QgsCategorizeUsingStyleAlgorithm::shortDescription() const
101{
102 return QObject::tr( "Sets a vector layer's renderer to a categorized renderer using symbols from a style database." );
103}
104
105QgsCategorizeUsingStyleAlgorithm *QgsCategorizeUsingStyleAlgorithm::createInstance() const
106{
107 return new QgsCategorizeUsingStyleAlgorithm();
108}
109
110class SetCategorizedRendererPostProcessor : public QgsProcessingLayerPostProcessorInterface
111{
112 public:
113 SetCategorizedRendererPostProcessor( std::unique_ptr<QgsCategorizedSymbolRenderer> renderer )
114 : mRenderer( std::move( renderer ) )
115 {}
116
117 void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &, QgsProcessingFeedback * ) override
118 {
119 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer ) )
120 {
121 vl->setRenderer( mRenderer.release() );
122 vl->triggerRepaint();
123 }
124 }
125
126 private:
127 std::unique_ptr<QgsCategorizedSymbolRenderer> mRenderer;
128};
129
130// Do most of the heavy lifting in a background thread, but save the thread-sensitive stuff for main thread execution!
131
132bool QgsCategorizeUsingStyleAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
133{
134 QgsVectorLayer *layer = parameterAsVectorLayer( parameters, QStringLiteral( "INPUT" ), context );
135 if ( !layer )
136 throw QgsProcessingException( invalidSourceError( parameters, QStringLiteral( "INPUT" ) ) );
137
138 mField = parameterAsString( parameters, QStringLiteral( "FIELD" ), context );
139
140 mLayerId = layer->id();
141 mLayerName = layer->name();
142 mLayerGeometryType = layer->geometryType();
143 mLayerFields = layer->fields();
144
145 mExpressionContext << QgsExpressionContextUtils::globalScope()
148
149 mExpression = QgsExpression( mField );
150 mExpression.prepare( &mExpressionContext );
151
153 req.setSubsetOfAttributes( mExpression.referencedColumns(), mLayerFields );
154 if ( !mExpression.needsGeometry() )
156
157 mIterator = layer->getFeatures( req );
158
159 return true;
160}
161
162QVariantMap QgsCategorizeUsingStyleAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
163{
164 const QString styleFile = parameterAsFile( parameters, QStringLiteral( "STYLE" ), context );
165 const bool caseSensitive = parameterAsBoolean( parameters, QStringLiteral( "CASE_SENSITIVE" ), context );
166 const bool tolerant = parameterAsBoolean( parameters, QStringLiteral( "TOLERANT" ), context );
167
168 QgsStyle *style = nullptr;
169 std::unique_ptr<QgsStyle> importedStyle;
170 if ( !styleFile.isEmpty() )
171 {
172 importedStyle = std::make_unique<QgsStyle>();
173 if ( !importedStyle->importXml( styleFile ) )
174 {
175 throw QgsProcessingException( QObject::tr( "An error occurred while reading style file: %1" ).arg( importedStyle->errorString() ) );
176 }
177 style = importedStyle.get();
178 }
179 else
180 {
181 style = QgsStyle::defaultStyle();
182 }
183
184 QgsFields nonMatchingCategoryFields;
185 nonMatchingCategoryFields.append( QgsField( QStringLiteral( "category" ), QMetaType::Type::QString ) );
186 QString nonMatchingCategoriesDest;
187 std::unique_ptr<QgsFeatureSink> nonMatchingCategoriesSink( parameterAsSink( parameters, QStringLiteral( "NON_MATCHING_CATEGORIES" ), context, nonMatchingCategoriesDest, nonMatchingCategoryFields, Qgis::WkbType::NoGeometry ) );
188 if ( !nonMatchingCategoriesSink && parameters.contains( QStringLiteral( "NON_MATCHING_CATEGORIES" ) ) && parameters.value( QStringLiteral( "NON_MATCHING_CATEGORIES" ) ).isValid() )
189 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "NON_MATCHING_CATEGORIES" ) ) );
190
191 QgsFields nonMatchingSymbolFields;
192 nonMatchingSymbolFields.append( QgsField( QStringLiteral( "name" ), QMetaType::Type::QString ) );
193 QString nonMatchingSymbolsDest;
194 std::unique_ptr<QgsFeatureSink> nonMatchingSymbolsSink( parameterAsSink( parameters, QStringLiteral( "NON_MATCHING_SYMBOLS" ), context, nonMatchingSymbolsDest, nonMatchingSymbolFields, Qgis::WkbType::NoGeometry ) );
195 if ( !nonMatchingSymbolsSink && parameters.contains( QStringLiteral( "NON_MATCHING_SYMBOLS" ) ) && parameters.value( QStringLiteral( "NON_MATCHING_SYMBOLS" ) ).isValid() )
196 throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "NON_MATCHING_SYMBOLS" ) ) );
197
198 QSet<QVariant> uniqueVals;
199 QgsFeature feature;
200 while ( mIterator.nextFeature( feature ) )
201 {
202 mExpressionContext.setFeature( feature );
203 QVariant value = mExpression.evaluate( &mExpressionContext );
204 if ( uniqueVals.contains( value ) )
205 continue;
206 uniqueVals << value;
207 }
208
209 QVariantList sortedUniqueVals = qgis::setToList( uniqueVals );
210 std::sort( sortedUniqueVals.begin(), sortedUniqueVals.end() );
211
212 QgsCategoryList cats;
213 cats.reserve( uniqueVals.count() );
214 std::unique_ptr<QgsSymbol> defaultSymbol( QgsSymbol::defaultSymbol( mLayerGeometryType ) );
215 for ( const QVariant &val : std::as_const( sortedUniqueVals ) )
216 {
217 cats.append( QgsRendererCategory( val, defaultSymbol->clone(), val.toString() ) );
218 }
219
220 mRenderer = std::make_unique<QgsCategorizedSymbolRenderer>( mField, cats );
221
222 const Qgis::SymbolType type = mLayerGeometryType == Qgis::GeometryType::Point ? Qgis::SymbolType::Marker
223 : mLayerGeometryType == Qgis::GeometryType::Line ? Qgis::SymbolType::Line
225
226 QVariantList unmatchedCategories;
227 QStringList unmatchedSymbols;
228 const int matched = mRenderer->matchToSymbols( style, type, unmatchedCategories, unmatchedSymbols, caseSensitive, tolerant );
229
230 if ( matched > 0 )
231 {
232 feedback->pushInfo( QObject::tr( "Matched %n categories to symbols from file.", nullptr, matched ) );
233 }
234 else
235 {
236 feedback->reportError( QObject::tr( "No categories could be matched to symbols in file." ) );
237 }
238
239 if ( !unmatchedCategories.empty() )
240 {
241 feedback->pushInfo( QObject::tr( "\n%n categories could not be matched:", nullptr, unmatchedCategories.count() ) );
242 std::sort( unmatchedCategories.begin(), unmatchedCategories.end() );
243 for ( const QVariant &cat : std::as_const( unmatchedCategories ) )
244 {
245 feedback->pushInfo( QStringLiteral( "∙ “%1”" ).arg( cat.toString() ) );
246 if ( nonMatchingCategoriesSink )
247 {
248 QgsFeature f;
249 f.setAttributes( QgsAttributes() << cat.toString() );
250 if ( !nonMatchingCategoriesSink->addFeature( f, QgsFeatureSink::FastInsert ) )
251 throw QgsProcessingException( writeFeatureError( nonMatchingCategoriesSink.get(), parameters, QStringLiteral( "NON_MATCHING_CATEGORIES" ) ) );
252 }
253 }
254 }
255
256 if ( !unmatchedSymbols.empty() )
257 {
258 feedback->pushInfo( QObject::tr( "\n%n symbol(s) in style were not matched:", nullptr, unmatchedSymbols.count() ) );
259 std::sort( unmatchedSymbols.begin(), unmatchedSymbols.end() );
260 for ( const QString &name : std::as_const( unmatchedSymbols ) )
261 {
262 feedback->pushInfo( QStringLiteral( "∙ “%1”" ).arg( name ) );
263 if ( nonMatchingSymbolsSink )
264 {
265 QgsFeature f;
266 f.setAttributes( QgsAttributes() << name );
267 if ( !nonMatchingSymbolsSink->addFeature( f, QgsFeatureSink::FastInsert ) )
268 throw QgsProcessingException( writeFeatureError( nonMatchingSymbolsSink.get(), parameters, QStringLiteral( "NON_MATCHING_SYMBOLS" ) ) );
269 }
270 }
271 }
272
273 context.addLayerToLoadOnCompletion( mLayerId, QgsProcessingContext::LayerDetails( mLayerName, context.project(), mLayerName ) );
274 context.layerToLoadOnCompletionDetails( mLayerId ).setPostProcessor( new SetCategorizedRendererPostProcessor( std::move( mRenderer ) ) );
275
276 if ( nonMatchingCategoriesSink )
277 nonMatchingCategoriesSink->finalize();
278 if ( nonMatchingSymbolsSink )
279 nonMatchingSymbolsSink->finalize();
280
281 QVariantMap results;
282 results.insert( QStringLiteral( "OUTPUT" ), mLayerId );
283 if ( nonMatchingCategoriesSink )
284 results.insert( QStringLiteral( "NON_MATCHING_CATEGORIES" ), nonMatchingCategoriesDest );
285 if ( nonMatchingSymbolsSink )
286 results.insert( QStringLiteral( "NON_MATCHING_SYMBOLS" ), nonMatchingSymbolsDest );
287 return results;
288}
289
@ Vector
Tables (i.e. vector layers with or without geometry). When used for a sink this indicates the sink ha...
Definition qgis.h:3539
@ File
Parameter is a single file.
Definition qgis.h:3789
@ NoGeometry
Geometry is not required. It may still be returned if e.g. required for a filter condition.
Definition qgis.h:2196
@ Point
Points.
Definition qgis.h:359
@ Line
Lines.
Definition qgis.h:360
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3609
SymbolType
Symbol types.
Definition qgis.h:610
@ Marker
Marker symbol.
Definition qgis.h:611
@ Line
Line symbol.
Definition qgis.h:612
@ Fill
Fill symbol.
Definition qgis.h:613
@ NoGeometry
No geometry.
Definition qgis.h:294
@ NotAvailableInStandaloneTool
Algorithm should not be available from the standalone "qgis_process" tool. Used to flag algorithms wh...
Definition qgis.h:3595
A vector of attributes.
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * layerScope(const QgsMapLayer *layer)
Creates a new scope which contains variables and functions relating to a QgsMapLayer.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Handles parsing and evaluation of expressions (formerly called "search strings").
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setFlags(Qgis::FeatureRequestFlags flags)
Sets flags that affect how features will be fetched.
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
Container of fields for a vector layer.
Definition qgsfields.h:46
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:73
QString name
Definition qgsmaplayer.h:84
QString id
Definition qgsmaplayer.h:83
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Details for layers to load into projects.
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingContext::LayerDetails & layerToLoadOnCompletionDetails(const QString &layer)
Returns a reference to the details for a given layer which is loaded on completion of the algorithm o...
void addLayerToLoadOnCompletion(const QString &layer, const QgsProcessingContext::LayerDetails &details)
Adds a layer to load (by ID or datasource) into the canvas upon completion of the algorithm or model.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
An interface for layer post-processing handlers for execution following a processing algorithm operat...
virtual void postProcessLayer(QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback)=0
Post-processes the specified layer, following successful execution of a processing algorithm.
A vector layer output for processing algorithms.
A boolean parameter for processing algorithms.
An expression parameter for processing algorithms.
An input file or folder parameter for processing algorithms.
A vector layer (with or without geometry) parameter for processing algorithms.
Represents an individual category (class) from a QgsCategorizedSymbolRenderer.
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:88
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:147
static QgsSymbol * defaultSymbol(Qgis::GeometryType geomType)
Returns a new default symbol for the specified geometry type.
Represents a vector layer which manages a vector based dataset.
QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest()) const final
Queries the layer for features specified in request.
Q_INVOKABLE Qgis::GeometryType geometryType() const
Returns point, line or polygon.
QList< QgsRendererCategory > QgsCategoryList