QGIS API Documentation 4.3.0-Master (7c7bd4d7018)
Loading...
Searching...
No Matches
qgsalgorithmextractlabels.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmextractlabels.cpp - QgsExtractLabelsAlgorithm
3 ---------------------
4 begin : 30.12.2021
5 copyright : (C) 2021 by Mathieu Pellerin
6 email : nirvn dot asia at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <cmath>
19
20#include "pal/feature.h"
21#include "pal/labelposition.h"
23#include "qgslabelsink.h"
24#include "qgslayertree.h"
31#include "qgsscalecalculator.h"
32#include "qgstextlabelfeature.h"
33#include "qgsvectorlayer.h"
34
35#include <QPainter>
36#include <QString>
37
38using namespace Qt::StringLiterals;
39
41
42QString QgsExtractLabelsAlgorithm::name() const
43{
44 return u"extractlabels"_s;
45}
46
47QString QgsExtractLabelsAlgorithm::displayName() const
48{
49 return QObject::tr( "Extract labels" );
50}
51
52QStringList QgsExtractLabelsAlgorithm::tags() const
53{
54 return QObject::tr( "map themes,font,position" ).split( ',' );
55}
56
57Qgis::ProcessingAlgorithmFlags QgsExtractLabelsAlgorithm::flags() const
58{
60}
61
62QString QgsExtractLabelsAlgorithm::group() const
63{
64 return QObject::tr( "Cartography" );
65}
66
67QString QgsExtractLabelsAlgorithm::groupId() const
68{
69 return u"cartography"_s;
70}
71
72void QgsExtractLabelsAlgorithm::initAlgorithm( const QVariantMap & )
73{
74 addParameter( new QgsProcessingParameterExtent( u"EXTENT"_s, QObject::tr( "Map extent" ) ) );
75
76 addParameter( new QgsProcessingParameterScale( u"SCALE"_s, QObject::tr( "Map scale" ) ) );
77
78 auto mapThemeParameter = std::make_unique<QgsProcessingParameterMapTheme>( u"MAP_THEME"_s, QObject::tr( "Map theme" ), QVariant(), true );
79 mapThemeParameter->setHelp( QObject::tr( "This parameter is optional. When left unset, the algorithm will fallback to extracting labels from all currently visible layers in the project." ) );
80 addParameter( mapThemeParameter.release() );
81
82 addParameter( new QgsProcessingParameterBoolean( u"INCLUDE_UNPLACED"_s, QObject::tr( "Include unplaced labels" ), QVariant( true ) ) );
83
84 auto dpiParameter = std::make_unique<QgsProcessingParameterNumber>( u"DPI"_s, QObject::tr( "Map resolution (in DPI)" ), Qgis::ProcessingNumberParameterType::Double, QVariant( 96.0 ), true );
85 dpiParameter->setFlags( dpiParameter->flags() | Qgis::ProcessingParameterFlag::Advanced );
86 addParameter( dpiParameter.release() );
87
88 addParameter( new QgsProcessingParameterFeatureSink( u"OUTPUT"_s, QObject::tr( "Extracted labels" ), Qgis::ProcessingSourceType::VectorPoint ) );
89}
90
91QString QgsExtractLabelsAlgorithm::shortDescription() const
92{
93 return QObject::tr( "Converts map labels to a point layer with relevant details saved as attributes." );
94}
95
96Qgis::ProcessingAlgorithmDocumentationFlags QgsExtractLabelsAlgorithm::documentationFlags() const
97{
99}
100
101QString QgsExtractLabelsAlgorithm::shortHelpString() const
102{
103 return QObject::tr(
104 "This algorithm extracts label information from a rendered map at a given extent and scale.\n\n"
105 "If a map theme is provided, the rendered map will match the visibility and symbology of that theme. If left blank, all visible layers from the project will be used.\n\n"
106 "Extracted label information include: position (served as point geometries), the associated layer name and feature ID, label text, rotation (in degree, clockwise), multiline alignment, and font "
107 "details."
108 );
109}
110
111QgsExtractLabelsAlgorithm *QgsExtractLabelsAlgorithm::createInstance() const
112{
113 return new QgsExtractLabelsAlgorithm();
114}
115
116class ExtractLabelSink : public QgsLabelSink
117{
118 public:
119 ExtractLabelSink( const QMap<QString, QString> &mapLayerNames, QgsProcessingFeedback *feedback )
120 : mMapLayerNames( mapLayerNames )
121 , mFeedback( feedback )
122 {}
123
124 void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) override
125 {
126 processLabel( layerId, context, label, settings, false );
127 }
128
129 void drawUnplacedLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) override
130 {
131 processLabel( layerId, context, label, settings, true );
132 }
133
134 void processLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings, bool unplacedLabel )
135 {
136 if ( mFeedback->isCanceled() )
137 {
138 context.setRenderingStopped( true );
139 }
140
141 const QgsFeatureId fid = label->getFeaturePart()->featureId();
142 switch ( settings.placement )
143 {
146 {
147 if ( !mCurvedWarningPushed.contains( layerId ) )
148 {
149 mCurvedWarningPushed << layerId;
150 mFeedback->pushWarning( QObject::tr( "Curved placement not supported, skipping labels from layer %1" ).arg( mMapLayerNames.value( layerId ) ) );
151 }
152 return;
153 }
154
162 break;
163 }
164
165 QgsTextLabelFeature *labelFeature = dynamic_cast<QgsTextLabelFeature *>( label->getFeaturePart()->feature() );
166 if ( !labelFeature )
167 return;
168
169 QgsPalLayerSettings labelSettings( settings );
170 const QMap<QgsPalLayerSettings::Property, QVariant> &dataDefinedValues = labelFeature->dataDefinedValues();
171
172 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::MultiLineWrapChar ) )
173 {
174 labelSettings.wrapChar = dataDefinedValues.value( QgsPalLayerSettings::Property::MultiLineWrapChar ).toString();
175 }
176 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::AutoWrapLength ) )
177 {
178 labelSettings.autoWrapLength = dataDefinedValues.value( QgsPalLayerSettings::Property::AutoWrapLength ).toInt();
179 }
180 const QString labelText = QgsPalLabeling::splitToLines( labelFeature->text( -1 ), labelSettings.wrapChar, labelSettings.autoWrapLength, labelSettings.useMaxLineLengthForAutoWrap ).join( '\n' );
181
182 QString labelAlignment;
183 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::MultiLineAlignment ) )
184 {
185 labelSettings.multilineAlign = static_cast<Qgis::LabelMultiLineAlignment>( dataDefinedValues.value( QgsPalLayerSettings::Property::MultiLineAlignment ).toInt() );
186 }
187 switch ( labelSettings.multilineAlign )
188 {
190 labelAlignment = u"right"_s;
191 break;
192
194 labelAlignment = u"center"_s;
195 break;
196
198 labelAlignment = u"left"_s;
199 break;
200
202 labelAlignment = u"justify"_s;
203 break;
204
206 switch ( label->quadrant() )
207 {
211 labelAlignment = u"right"_s;
212 break;
213
217 labelAlignment = u"center"_s;
218 break;
219
223 labelAlignment = u"left"_s;
224 break;
225 }
226 break;
227 }
228
229 const double labelRotation = !qgsDoubleNear( label->getAlpha(), 0.0 ) ? -( label->getAlpha() * 180 / M_PI ) + 360 : 0.0;
230
231 const QFont font = labelFeature->definedFont();
232 const QString fontFamily = font.family();
233 const QString fontStyle = font.styleName();
234 const double fontSize = static_cast<double>( font.pixelSize() ) * 72 / context.painter()->device()->logicalDpiX();
235 const bool fontItalic = font.italic();
236 const bool fontBold = font.bold();
237 const bool fontUnderline = font.underline();
238 const double fontLetterSpacing = font.letterSpacing();
239 const double fontWordSpacing = font.wordSpacing();
240
241 QgsTextFormat format = labelSettings.format();
242 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::Size ) )
243 {
244 format.setSize( dataDefinedValues.value( QgsPalLayerSettings::Property::Size ).toDouble() );
245 }
246 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::Color ) )
247 {
248 format.setColor( dataDefinedValues.value( QgsPalLayerSettings::Property::Color ).value<QColor>() );
249 }
250 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::FontOpacity ) )
251 {
252 format.setOpacity( dataDefinedValues.value( QgsPalLayerSettings::Property::FontOpacity ).toDouble() / 100.0 );
253 }
254 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::MultiLineHeight ) )
255 {
256 format.setLineHeight( dataDefinedValues.value( QgsPalLayerSettings::Property::MultiLineHeight ).toDouble() );
257 }
258
259 const QString formatColor = format.color().name();
260 const double formatOpacity = format.opacity() * 100;
261 const double formatLineHeight = format.lineHeight();
262
263 QgsTextBufferSettings buffer = format.buffer();
264 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::BufferDraw ) )
265 {
266 buffer.setEnabled( dataDefinedValues.value( QgsPalLayerSettings::Property::BufferDraw ).toBool() );
267 }
268 const bool bufferDraw = buffer.enabled();
269 double bufferSize = 0.0;
270 QString bufferColor;
271 double bufferOpacity = 0.0;
272 if ( bufferDraw )
273 {
274 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::BufferSize ) )
275 {
276 buffer.setSize( dataDefinedValues.value( QgsPalLayerSettings::Property::BufferSize ).toDouble() );
277 }
278 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::BufferColor ) )
279 {
280 buffer.setColor( dataDefinedValues.value( QgsPalLayerSettings::Property::BufferColor ).value<QColor>() );
281 }
282 if ( dataDefinedValues.contains( QgsPalLayerSettings::Property::BufferOpacity ) )
283 {
284 buffer.setOpacity( dataDefinedValues.value( QgsPalLayerSettings::Property::BufferOpacity ).toDouble() / 100.0 );
285 }
286
287 bufferSize = buffer.sizeUnit() == Qgis::RenderUnit::Percentage ? context.convertToPainterUnits( format.size(), format.sizeUnit(), format.sizeMapUnitScale() ) * buffer.size() / 100
288 : context.convertToPainterUnits( buffer.size(), buffer.sizeUnit(), buffer.sizeMapUnitScale() );
289 bufferSize = bufferSize * 72 / context.painter()->device()->logicalDpiX();
290 bufferColor = buffer.color().name();
291 bufferOpacity = buffer.opacity() * 100;
292 }
293
294 QgsAttributes attributes;
295 attributes
296 << mMapLayerNames.value( layerId )
297 << fid
298 << labelText
299 << label->getWidth()
300 << label->getHeight()
301 << labelRotation
302 << unplacedLabel
303 << fontFamily
304 << fontSize
305 << fontItalic
306 << fontBold
307 << fontUnderline
308 << fontStyle
309 << fontLetterSpacing
310 << fontWordSpacing
311 << labelAlignment
312 << formatLineHeight
313 << formatColor
314 << formatOpacity
315 << bufferDraw
316 << bufferSize
317 << bufferColor
318 << bufferOpacity;
319
320 double x = label->getX();
321 double y = label->getY();
322 QgsGeometry geometry( new QgsPoint( x, y ) );
323
324 QgsFeature feature;
325 feature.setAttributes( attributes );
326 feature.setGeometry( geometry );
327 features << feature;
328 }
329
330 QList<QgsFeature> features;
331
332 private:
333 QMap<QString, QString> mMapLayerNames;
334 QList<QString> mCurvedWarningPushed;
335
336 QgsProcessingFeedback *mFeedback = nullptr;
337};
338
339QVariantMap QgsExtractLabelsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
340{
341 const QgsRectangle extent = parameterAsExtent( parameters, u"EXTENT"_s, context );
342 const double scale = parameterAsDouble( parameters, u"SCALE"_s, context );
343 if ( qgsDoubleNear( scale, 0.0 ) )
344 {
345 throw QgsProcessingException( QObject::tr( "Invalid scale value, a number greater than 0 is required" ) );
346 }
347 double dpi = parameterAsDouble( parameters, u"DPI"_s, context );
348 if ( qgsDoubleNear( dpi, 0.0 ) )
349 {
350 dpi = 96.0;
351 }
352
353 QgsScaleCalculator calculator;
354 calculator.setDpi( dpi );
355 calculator.setMapUnits( mCrs.mapUnits() );
356 const QSize imageSize = calculator.calculateImageSize( extent, scale ).toSize();
357
358 QgsFields fields;
359 fields.append( QgsField( u"Layer"_s, QMetaType::Type::QString, QString(), 0, 0 ) );
360 fields.append( QgsField( u"FeatureID"_s, QMetaType::Type::LongLong, QString(), 20 ) );
361 fields.append( QgsField( u"LabelText"_s, QMetaType::Type::QString, QString(), 0, 0 ) );
362 fields.append( QgsField( u"LabelWidth"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
363 fields.append( QgsField( u"LabelHeight"_s, QMetaType::Type::Double, QString(), 20, 8 ) );
364 fields.append( QgsField( u"LabelRotation"_s, QMetaType::Type::Double, QString(), 20, 2 ) );
365 fields.append( QgsField( u"LabelUnplaced"_s, QMetaType::Type::Bool, QString(), 1, 0 ) );
366 fields.append( QgsField( u"Family"_s, QMetaType::Type::QString, QString(), 0, 0 ) );
367 fields.append( QgsField( u"Size"_s, QMetaType::Type::Double, QString(), 20, 4 ) );
368 fields.append( QgsField( u"Italic"_s, QMetaType::Type::Bool, QString(), 1, 0 ) );
369 fields.append( QgsField( u"Bold"_s, QMetaType::Type::Bool, QString(), 1, 0 ) );
370 fields.append( QgsField( u"Underline"_s, QMetaType::Type::Bool, QString(), 1, 0 ) );
371 fields.append( QgsField( u"FontStyle"_s, QMetaType::Type::QString, QString(), 0, 0 ) );
372 fields.append( QgsField( u"FontLetterSpacing"_s, QMetaType::Type::Double, QString(), 20, 4 ) );
373 fields.append( QgsField( u"FontWordSpacing"_s, QMetaType::Type::Double, QString(), 20, 4 ) );
374 fields.append( QgsField( u"MultiLineAlignment"_s, QMetaType::Type::QString, QString(), 0, 0 ) );
375 fields.append( QgsField( u"MultiLineHeight"_s, QMetaType::Type::Double, QString(), 20, 2 ) );
376 fields.append( QgsField( u"Color"_s, QMetaType::Type::QString, QString(), 7, 0 ) );
377 fields.append( QgsField( u"FontOpacity"_s, QMetaType::Type::Double, QString(), 20, 1 ) );
378 fields.append( QgsField( u"BufferDraw"_s, QMetaType::Type::Bool, QString(), 1, 0 ) );
379 fields.append( QgsField( u"BufferSize"_s, QMetaType::Type::Double, QString(), 20, 4 ) );
380 fields.append( QgsField( u"BufferColor"_s, QMetaType::Type::QString, QString(), 7, 0 ) );
381 fields.append( QgsField( u"BufferOpacity"_s, QMetaType::Type::Double, QString(), 20, 1 ) );
382
383 QString dest;
384 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u"OUTPUT"_s, context, dest, fields, Qgis::WkbType::Point, mCrs, QgsFeatureSink::RegeneratePrimaryKey ) );
385 if ( !sink )
386 throw QgsProcessingException( invalidSinkError( parameters, u"OUTPUT"_s ) );
387
388 QgsMapSettings mapSettings;
389 mapSettings.setDestinationCrs( mCrs );
390 mapSettings.setExtent( extent );
391 mapSettings.setOutputSize( imageSize );
392 mapSettings.setOutputDpi( dpi );
393 mapSettings.setFlag( Qgis::MapSettingsFlag::DrawLabeling, true );
396 mapSettings.setLayers( mMapLayers );
397 mapSettings.setLayerStyleOverrides( mMapThemeStyleOverrides );
398 mapSettings.setLabelingEngineSettings( mLabelSettings );
399 mapSettings.setScaleMethod( mScaleMethod );
400 mapSettings.setEllipsoid( context.ellipsoid() );
401
402 //build the expression context
403 QgsExpressionContext expressionContext;
405 mapSettings.setExpressionContext( expressionContext );
406
407 QgsNullPaintDevice nullPaintDevice;
408 nullPaintDevice.setOutputSize( imageSize );
409 nullPaintDevice.setOutputDpi( static_cast<int>( std::round( dpi ) ) );
410 QPainter painter( &nullPaintDevice );
411
412 QgsMapRendererCustomPainterJob renderJob( mapSettings, &painter );
413 ExtractLabelSink labelSink( mMapLayerNames, feedback );
414 renderJob.setLabelSink( &labelSink );
415
416 feedback->pushInfo( QObject::tr( "Extracting labels" ) );
417
418 QgsProcessingMultiStepFeedback multiStepFeedback( 10, feedback );
419 multiStepFeedback.setCurrentStep( 0 );
420
421 QEventLoop loop;
422 QObject::connect( feedback, &QgsFeedback::canceled, &renderJob, &QgsMapRendererCustomPainterJob::cancel );
423 QObject::connect( &renderJob, &QgsMapRendererJob::renderingLayersFinished, feedback, [feedback]() { feedback->pushInfo( QObject::tr( "Calculating label placement" ) ); } );
424 int labelsCollectedFromLayers = 0;
425 QObject::connect( &renderJob, &QgsMapRendererJob::layerRenderingStarted, feedback, [this, &multiStepFeedback, &labelsCollectedFromLayers]( const QString &layerId ) {
426 multiStepFeedback.pushInfo( QObject::tr( "Collecting labelled features from %1" ).arg( mMapLayerNames.value( layerId ) ) );
427 multiStepFeedback.setProgress( 100.0 * static_cast<double>( labelsCollectedFromLayers ) / mMapLayers.size() );
428 labelsCollectedFromLayers++;
429 } );
430
431 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::labelRegistrationAboutToBegin, &multiStepFeedback, [&multiStepFeedback]() {
432 multiStepFeedback.setCurrentStep( 1 );
433 multiStepFeedback.pushInfo( QObject::tr( "Registering labels" ) );
434 } );
435
436 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::providerRegistrationAboutToBegin, &multiStepFeedback, [this, &multiStepFeedback]( QgsAbstractLabelProvider *provider ) {
437 multiStepFeedback.setCurrentStep( 2 );
438 if ( !provider->layerId().isEmpty() )
439 {
440 multiStepFeedback.pushInfo( QObject::tr( "Adding labels from %1" ).arg( mMapLayerNames.value( provider->layerId() ) ) );
441 }
442 } );
443 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::candidateCreationAboutToBegin, &multiStepFeedback, [this, &multiStepFeedback]( QgsAbstractLabelProvider *provider ) {
444 multiStepFeedback.setCurrentStep( 3 );
445 if ( !provider->layerId().isEmpty() )
446 {
447 multiStepFeedback.pushInfo( QObject::tr( "Generating label placement candidates for %1" ).arg( mMapLayerNames.value( provider->layerId() ) ) );
448 }
449 } );
450 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::obstacleCostingAboutToBegin, &multiStepFeedback, [&multiStepFeedback]() {
451 multiStepFeedback.setCurrentStep( 4 );
452 multiStepFeedback.setProgressText( QObject::tr( "Calculating obstacle costs" ) );
453 } );
454 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::calculatingConflictsAboutToBegin, &multiStepFeedback, [&multiStepFeedback]() {
455 multiStepFeedback.setCurrentStep( 5 );
456 multiStepFeedback.setProgressText( QObject::tr( "Calculating label conflicts" ) );
457 } );
458 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::finalizingCandidatesAboutToBegin, &multiStepFeedback, [&multiStepFeedback]() {
459 multiStepFeedback.setCurrentStep( 6 );
460 multiStepFeedback.setProgressText( QObject::tr( "Finalizing candidates" ) );
461 } );
462 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::reductionAboutToBegin, &multiStepFeedback, [&multiStepFeedback]() {
463 multiStepFeedback.setCurrentStep( 7 );
464 multiStepFeedback.setProgressText( QObject::tr( "Reducing problem" ) );
465 } );
466 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::solvingPlacementAboutToBegin, &multiStepFeedback, [&multiStepFeedback]() {
467 multiStepFeedback.setCurrentStep( 8 );
468 multiStepFeedback.setProgressText( QObject::tr( "Determining optimal label placements" ) );
469 } );
470 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::solvingPlacementFinished, &multiStepFeedback, [&multiStepFeedback]() {
471 multiStepFeedback.setProgressText( QObject::tr( "Labeling complete" ) );
472 } );
473
474 QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::progressChanged, &multiStepFeedback, [&multiStepFeedback]( double progress ) {
475 multiStepFeedback.setProgress( progress );
476 } );
477
478 QObject::connect( &renderJob, &QgsMapRendererJob::finished, &loop, [&loop]() { loop.exit(); } );
479 renderJob.start();
480 loop.exec();
481
482 qDeleteAll( mMapLayers );
483 mMapLayers.clear();
484
485 multiStepFeedback.setCurrentStep( 9 );
486 feedback->pushInfo( QObject::tr( "Writing %n label(s) to output layer", "", labelSink.features.count() ) );
487 const double step = !labelSink.features.empty() ? 100.0 / labelSink.features.count() : 1;
488 long long index = -1;
489 for ( QgsFeature &feature : labelSink.features )
490 {
491 index++;
492 multiStepFeedback.setProgress( step * index );
493 if ( feedback->isCanceled() )
494 break;
495
496 if ( !sink->addFeature( feature, QgsFeatureSink::FastInsert ) )
497 throw QgsProcessingException( writeFeatureError( sink.get(), parameters, u"OUTPUT"_s ) );
498 else
499 feedback->featureAddedToSink( u"OUTPUT"_s );
500 }
501 sink->finalize();
502 feedback->featureSinkFinalized( u"OUTPUT"_s );
503 sink.reset();
504
505 if ( QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( QgsProcessingUtils::mapLayerFromString( dest, context ) ) )
506 {
507 vl->setRenderer( new QgsNullSymbolRenderer() );
508 if ( vl->renderer() )
509 {
510 vl->renderer()->setReferenceScale( scale );
511
512 QgsPalLayerSettings settings;
513 QgsPropertyCollection settingsProperties;
514
515 settings.fieldName = u"LabelText"_s;
516 settings.obstacleSettings().setIsObstacle( false );
521
522 QgsTextFormat textFormat;
523 textFormat.setSize( 9 );
525 textFormat.setColor( QColor( 0, 0, 0 ) );
526
527 QgsTextBufferSettings buffer = textFormat.buffer();
529
530 textFormat.setBuffer( buffer );
531 settings.setFormat( textFormat );
532
533 settingsProperties.setProperty( QgsPalLayerSettings::Property::Color, QgsProperty::fromExpression( u"if(\"LabelUnplaced\",'255,0,0',\"Color\")"_s ) );
534 settingsProperties.setProperty( QgsPalLayerSettings::Property::FontOpacity, QgsProperty::fromField( u"FontOpacity"_s ) );
538 settingsProperties.setProperty( QgsPalLayerSettings::Property::Underline, QgsProperty::fromField( u"Underline"_s ) );
540 settingsProperties.setProperty( QgsPalLayerSettings::Property::FontLetterSpacing, QgsProperty::fromField( u"FontLetterSpacing"_s ) );
541 settingsProperties.setProperty( QgsPalLayerSettings::Property::FontWordSpacing, QgsProperty::fromField( u"FontWordSpacing"_s ) );
542 settingsProperties.setProperty( QgsPalLayerSettings::Property::MultiLineAlignment, QgsProperty::fromField( u"MultiLineAlignment"_s ) );
543 settingsProperties.setProperty( QgsPalLayerSettings::Property::MultiLineHeight, QgsProperty::fromField( u"MultiLineHeight"_s ) );
544 settingsProperties.setProperty( QgsPalLayerSettings::Property::LabelRotation, QgsProperty::fromField( u"LabelRotation"_s ) );
545 settingsProperties.setProperty( QgsPalLayerSettings::Property::BufferDraw, QgsProperty::fromField( u"BufferDraw"_s ) );
546 settingsProperties.setProperty( QgsPalLayerSettings::Property::BufferSize, QgsProperty::fromField( u"BufferSize"_s ) );
547 settingsProperties.setProperty( QgsPalLayerSettings::Property::BufferColor, QgsProperty::fromField( u"BufferColor"_s ) );
548 settingsProperties.setProperty( QgsPalLayerSettings::Property::BufferOpacity, QgsProperty::fromField( u"BufferOpacity"_s ) );
549 settingsProperties.setProperty( QgsPalLayerSettings::Property::Show, QgsProperty::fromExpression( u"\"LabelUnplaced\"=false"_s ) );
550 settings.setDataDefinedProperties( settingsProperties );
551
553 vl->setLabeling( labeling );
554 vl->setLabelsEnabled( true );
555
556 QString errorMessage;
557 vl->saveStyleToDatabaseV2( QString(), QString(), true, QString(), errorMessage );
558 }
559 }
560
561 QVariantMap outputs;
562 outputs.insert( u"OUTPUT"_s, dest );
563 return outputs;
564}
565
566
567bool QgsExtractLabelsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
568{
569 // Retrieve and clone layers
570 const QString mapTheme = parameterAsString( parameters, u"MAP_THEME"_s, context );
571
572 QgsProject *project = context.project();
573 if ( project )
574 {
575 if ( !mapTheme.isEmpty() && project->mapThemeCollection()->hasMapTheme( mapTheme ) )
576 {
577 const QList<QgsMapLayer *> constLayers = project->mapThemeCollection()->mapThemeVisibleLayers( mapTheme );
578 for ( const QgsMapLayer *l : constLayers )
579 {
580 // only copy vector layers as other layer types aren't actors in the labeling process
581 if ( l->type() == Qgis::LayerType::Vector )
582 mMapLayers.push_back( l->clone() );
583 }
584 mMapThemeStyleOverrides = project->mapThemeCollection()->mapThemeStyleOverrides( mapTheme );
585 }
586 }
587
588 if ( project && mMapLayers.isEmpty() )
589 {
590 QList<QgsMapLayer *> layers;
591 QgsLayerTree *root = project->layerTreeRoot();
592 const QList<QgsLayerTreeLayer *> layerTreeLayers = root->findLayers();
593 layers.reserve( layerTreeLayers.size() );
594 for ( QgsLayerTreeLayer *nodeLayer : layerTreeLayers )
595 {
596 QgsMapLayer *layer = nodeLayer->layer();
597 if ( nodeLayer->isVisible() && root->layerOrder().contains( layer ) )
598 layers << layer;
599 }
600
601 for ( const QgsMapLayer *l : std::as_const( layers ) )
602 {
603 if ( l->type() == Qgis::LayerType::Vector )
604 mMapLayers.push_back( l->clone() );
605 }
606 }
607
608 for ( const QgsMapLayer *l : std::as_const( mMapLayers ) )
609 {
610 mMapLayerNames.insert( l->id(), l->name() );
611 }
612
613 mCrs = parameterAsExtentCrs( parameters, u"EXTENT"_s, context );
614 if ( project && !mCrs.isValid() )
615 mCrs = project->crs();
616
617 bool includeUnplaced = parameterAsBoolean( parameters, u"INCLUDE_UNPLACED"_s, context );
618 mLabelSettings = project ? project->labelingEngineSettings() : QgsLabelingEngineSettings();
619 mLabelSettings.setFlag( Qgis::LabelingFlag::DrawUnplacedLabels, includeUnplaced );
620 mLabelSettings.setFlag( Qgis::LabelingFlag::CollectUnplacedLabels, includeUnplaced );
621
622 if ( project )
623 {
624 mScaleMethod = project->scaleMethod();
625 }
626
627 return true;
628}
629
630
@ VectorPoint
Vector point layers.
Definition qgis.h:3716
@ OverPoint
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point....
Definition qgis.h:1276
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
Definition qgis.h:1278
@ AroundPoint
Arranges candidates in a circle around a point (or centroid of a polygon). Applies to point or polygo...
Definition qgis.h:1275
@ Line
Arranges candidates parallel to a generalised line representing the feature or parallel to a polygon'...
Definition qgis.h:1277
@ Free
Arranges candidates scattered throughout a polygon feature. Candidates are rotated to respect the pol...
Definition qgis.h:1280
@ OrderedPositionsAroundPoint
Candidates are placed in predefined positions around a point. Preference is given to positions with g...
Definition qgis.h:1281
@ Horizontal
Arranges horizontal candidates scattered throughout a polygon feature. Applies to polygon layers only...
Definition qgis.h:1279
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
Definition qgis.h:1282
@ OutsidePolygons
Candidates are placed outside of polygon boundaries. Applies to polygon layers only.
Definition qgis.h:1283
@ AboveRight
Above right.
Definition qgis.h:1365
@ BelowLeft
Below left.
Definition qgis.h:1369
@ Above
Above center.
Definition qgis.h:1364
@ BelowRight
Below right.
Definition qgis.h:1371
@ Right
Right middle.
Definition qgis.h:1368
@ AboveLeft
Above left.
Definition qgis.h:1363
@ Below
Below center.
Definition qgis.h:1370
@ Over
Center middle.
Definition qgis.h:1367
@ CollectUnplacedLabels
Whether unplaced labels should be collected in the labeling results (regardless of whether they are b...
Definition qgis.h:3018
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
Definition qgis.h:3017
@ RegeneratesPrimaryKey
Algorithm always drops any existing primary keys or FID values and regenerates them in outputs.
Definition qgis.h:3802
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3792
LabelMultiLineAlignment
Text alignment for multi-line labels.
Definition qgis.h:1445
@ Center
Center align.
Definition qgis.h:1447
@ FollowPlacement
Alignment follows placement of label, e.g., labels to the left of a feature will be drawn with right ...
Definition qgis.h:1449
@ Vector
Vector layer.
Definition qgis.h:207
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
Definition qgis.h:5611
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5612
QFlags< ProcessingAlgorithmDocumentationFlag > ProcessingAlgorithmDocumentationFlags
Flags describing algorithm behavior for documentation purposes.
Definition qgis.h:3813
@ Point
Point.
Definition qgis.h:296
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
Definition qgis.h:3779
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition qgis.h:3948
@ AllowOverlapIfRequired
Avoids overlapping labels when possible, but permit overlaps if labels for features cannot otherwise ...
Definition qgis.h:1237
@ Double
Double/float values.
Definition qgis.h:3989
@ UseRenderingOptimization
Enable vector simplification and other rendering optimizations.
Definition qgis.h:2885
@ DrawLabeling
Enable drawing of labels on top of the map.
Definition qgis.h:2884
@ SkipSymbolRendering
Disable symbol rendering while still drawing labels if enabled.
Definition qgis.h:2896
An abstract interface class for label providers.
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:60
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
void canceled()
Internal routines can connect to this signal if they use event loop.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
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:75
void setIsObstacle(bool isObstacle)
Sets whether features are obstacles to labels of other layers.
void setOverlapHandling(Qgis::LabelOverlapHandling handling)
Sets the technique used to handle overlapping labels.
void setAllowDegradedPlacement(bool allow)
Sets whether labels can be placed in inferior fallback positions if they cannot otherwise be placed.
void setQuadrant(Qgis::LabelQuadrantPosition quadrant)
Sets the quadrant in which to offset labels from the point.
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
virtual void drawUnplacedLabel(const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings)
The drawLabel method is called for each unplaced label.
virtual void drawLabel(const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings)=0
The drawLabel method is called for each label that is being drawn.
void obstacleCostingAboutToBegin()
Emitted when the obstacle costing is about to begin.
void solvingPlacementAboutToBegin()
Emitted when the problem solving step is about to begin.
void calculatingConflictsAboutToBegin()
Emitted when the conflict handling step is about to begin.
void reductionAboutToBegin()
Emitted when the candidate reduction step is about to begin.
void labelRegistrationAboutToBegin()
Emitted when the label registration is about to begin.
void solvingPlacementFinished()
Emitted when the problem solving step is finished.
void finalizingCandidatesAboutToBegin()
Emitted when the label candidates are about to be finalized.
void candidateCreationAboutToBegin(QgsAbstractLabelProvider *provider)
Emitted when the label candidate creation is about to begin for a provider.
void providerRegistrationAboutToBegin(QgsAbstractLabelProvider *provider)
Emitted when the label registration is about to begin for a provider.
Stores global configuration for labeling engine.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Layer tree node points to a map layer.
Namespace with helper functions for layer tree operations.
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
Base class for all map layer types.
Definition qgsmaplayer.h:83
Job implementation that renders everything sequentially using a custom painter.
void cancel() override
Stop the rendering job - does not return until the job has terminated.
void renderingLayersFinished()
Emitted when the layers are rendered.
void finished()
emitted when asynchronous rendering is finished (or canceled).
void layerRenderingStarted(const QString &layerId)
Emitted just before rendering starts for a particular layer.
Contains configuration for rendering maps.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
void setScaleMethod(Qgis::ScaleCalculationMethod method)
Sets the method to use for scale calculations for the map.
void setOutputDpi(double dpi)
Sets the dpi (dots per inch) used for conversion between real world units (e.g.
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Sets the map of map layer style overrides (key: layer ID, value: style name) where a different style ...
void setExtent(const QgsRectangle &rect, bool magnified=true)
Sets the coordinates of the rectangle which should be rendered.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setLabelingEngineSettings(const QgsLabelingEngineSettings &settings)
Sets the global configuration of the labeling engine.
void setOutputSize(QSize size)
Sets the size of the resulting map image, in pixels.
void setFlag(Qgis::MapSettingsFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected).
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs (coordinate reference system) for the map render.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
Null painter device that can be used for map renderer jobs which use custom painters.
void setOutputSize(const QSize &size)
Sets the size of the device in pixels.
void setOutputDpi(const int dpi)
Sets the dpi of the device.
Null symbol renderer, which draws no symbols for features by default, but allows for labeling and dia...
static QStringList splitToLines(const QString &text, const QString &wrapCharacter, int autoWrapLength=0, bool useMaxLineLengthWhenAutoWrapping=true)
Splits a text string to a list of separate lines, using a specified wrap character (wrapCharacter).
Contains settings for how a map layer will be labeled.
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the label obstacle settings.
const QgsLabelPlacementSettings & placementSettings() const
Returns the label placement settings.
void setFormat(const QgsTextFormat &format)
Sets the label text formatting settings, e.g., font settings, buffer settings, etc.
Qgis::LabelPlacement placement
Label placement mode.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the label's property collection, used for data defined overrides.
QString fieldName
Name of field (or an expression) to use for label text.
const QgsLabelPointSettings & pointSettings() const
Returns the label point settings, which contain settings related to how the label engine places and f...
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
void featureAddedToSink(const QString &output)
Reports that a feature was added to the the sink associated with the specified algorithm output.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
void featureSinkFinalized(const QString &output)
Reports that a feature sink has been finalized.
Processing feedback object for multi-step operations.
A boolean parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A feature sink output for processing algorithms.
A double numeric parameter for map scale values.
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType, QgsProcessing::LayerOptionsFlags flags=QgsProcessing::LayerOptionsFlags())
Interprets a string as a map layer within the supplied context.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:114
QgsMapThemeCollection * mapThemeCollection
Definition qgsproject.h:123
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
QgsCoordinateReferenceSystem crs
Definition qgsproject.h:120
const QgsLabelingEngineSettings & labelingEngineSettings() const
Returns project's global labeling engine settings.
Qgis::ScaleCalculationMethod scaleMethod
Definition qgsproject.h:136
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
A rectangle specified with double values.
void setRenderingStopped(bool stopped)
Sets whether the rendering operation has been stopped and any ongoing rendering should be canceled im...
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
Calculates scale for a given combination of canvas size, map extent, and monitor dpi.
void setDpi(double dpi)
Sets the dpi (dots per inch) for the output resolution, to be used in scale calculations.
void setMapUnits(Qgis::DistanceUnit mapUnits)
Set the map units.
QSizeF calculateImageSize(const QgsRectangle &mapExtent, double scale) const
Calculate the image size in pixel (physical) units.
Container for settings relating to a text buffer.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
double size() const
Returns the size of the buffer.
void setColor(const QColor &color)
Sets the color for the buffer.
void setOpacity(double opacity)
Sets the buffer opacity.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
bool enabled() const
Returns whether the buffer is enabled.
double opacity() const
Returns the buffer opacity.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units used for the buffer size.
void setEnabled(bool enabled)
Sets whether the text buffer will be drawn.
QColor color() const
Returns the color of the buffer.
void setSize(double size)
Sets the size of the buffer.
Container for all settings relating to text rendering.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the size.
double lineHeight() const
Returns the line height for text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void setOpacity(double opacity)
Sets the text's opacity.
void setBuffer(const QgsTextBufferSettings &bufferSettings)
Sets the text's buffer settings.
Qgis::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
double opacity() const
Returns the text's opacity.
double size() const
Returns the size for rendered text.
QColor color() const
Returns the color that text will be rendered in.
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
void setLineHeight(double height)
Sets the line height for text.
QFont definedFont() const
Font to be used for rendering.
const QMap< QgsPalLayerSettings::Property, QVariant > & dataDefinedValues() const
Gets data-defined values.
QString text(int partId) const
Returns the text component corresponding to a specified label part.
Basic implementation of the labeling interface.
Represents a vector layer which manages a vector based dataset.
QgsFeatureId featureId() const
Returns the unique ID of the feature.
Definition feature.cpp:168
double getAlpha() const
Returns the angle to rotate text (in radians).
double getHeight() const
double getWidth() const
Qgis::LabelQuadrantPosition quadrant() const
Returns the quadrant associated with this label position.
FeaturePart * getFeaturePart() const
Returns the feature corresponding to this labelposition.
double getX(int i=0) const
Returns the down-left x coordinate.
double getY(int i=0) const
Returns the down-left y coordinate.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:7340
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features