QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
19 #include "qgsmapthemecollection.h"
21 #include "qgsnullpainterdevice.h"
22 #include "qgslabelsink.h"
23 #include "qgslayertree.h"
24 #include "qgsvectorlayer.h"
25 #include "qgsscalecalculator.h"
26 #include "qgstextlabelfeature.h"
27 #include "qgsnullsymbolrenderer.h"
28 #include "qgsprocessingfeedback.h"
29 
30 #include "pal/feature.h"
31 #include "pal/pointset.h"
32 #include "pal/labelposition.h"
33 
34 #include <QPainter>
35 
36 #include <cmath>
37 
39 
40 QString QgsExtractLabelsAlgorithm::name() const
41 {
42  return QStringLiteral( "extractlabels" );
43 }
44 
45 QString QgsExtractLabelsAlgorithm::displayName() const
46 {
47  return QObject::tr( "Extract labels" );
48 }
49 
50 QStringList QgsExtractLabelsAlgorithm::tags() const
51 {
52  return QObject::tr( "map themes,font,position" ).split( ',' );
53 }
54 
55 QgsProcessingAlgorithm::Flags QgsExtractLabelsAlgorithm::flags() const
56 {
57  return QgsProcessingAlgorithm::flags() | FlagRequiresProject;
58 }
59 
60 QString QgsExtractLabelsAlgorithm::group() const
61 {
62  return QObject::tr( "Cartography" );
63 }
64 
65 QString QgsExtractLabelsAlgorithm::groupId() const
66 {
67  return QStringLiteral( "cartography" );
68 }
69 
70 void QgsExtractLabelsAlgorithm::initAlgorithm( const QVariantMap & )
71 {
72  addParameter( new QgsProcessingParameterExtent(
73  QStringLiteral( "EXTENT" ),
74  QObject::tr( "Map extent" ) ) );
75 
76  addParameter( new QgsProcessingParameterScale(
77  QStringLiteral( "SCALE" ),
78  QObject::tr( "Map scale" ) ) );
79 
80  std::unique_ptr<QgsProcessingParameterMapTheme> mapThemeParameter = std::make_unique<QgsProcessingParameterMapTheme>(
81  QStringLiteral( "MAP_THEME" ),
82  QObject::tr( "Map theme" ),
83  QVariant(), true );
84  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." ) );
85  addParameter( mapThemeParameter.release() );
86 
87  addParameter( new QgsProcessingParameterBoolean(
88  QStringLiteral( "INCLUDE_UNPLACED" ),
89  QObject::tr( "Include unplaced labels" ),
90  QVariant( true ), true ) );
91 
92  std::unique_ptr<QgsProcessingParameterNumber> dpiParameter = std::make_unique<QgsProcessingParameterNumber>(
93  QStringLiteral( "DPI" ),
94  QObject::tr( "Map resolution (in DPI)" ),
96  QVariant( 96.0 ), true );
97  dpiParameter->setFlags( dpiParameter->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
98  addParameter( dpiParameter.release() );
99 
100  addParameter( new QgsProcessingParameterFeatureSink(
101  QStringLiteral( "OUTPUT" ),
102  QObject::tr( "Extracted labels" ),
104 }
105 
106 QString QgsExtractLabelsAlgorithm::shortDescription() const
107 {
108  return QObject::tr( "Converts map labels to a point layer with relevant details saved as attributes." );
109 }
110 
111 QString QgsExtractLabelsAlgorithm::shortHelpString() const
112 {
113  return QObject::tr( "This algorithm extracts label information from a rendered map at a given extent and scale.\n\n"
114  "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"
115  "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 details." );
116 }
117 
118 QgsExtractLabelsAlgorithm *QgsExtractLabelsAlgorithm::createInstance() const
119 {
120  return new QgsExtractLabelsAlgorithm();
121 }
122 
123 class ExtractLabelSink : public QgsLabelSink
124 {
125  public:
126  ExtractLabelSink( const QMap<QString, QString> &mapLayerNames, QgsProcessingFeedback *feedback )
127  : mMapLayerNames( mapLayerNames )
128  , mFeedback( feedback )
129  {
130  }
131 
132  void drawLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) override
133  {
134  processLabel( layerId, context, label, settings, false );
135  }
136 
137  void drawUnplacedLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings ) override
138  {
139  processLabel( layerId, context, label, settings, true );
140  }
141 
142  void processLabel( const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings, bool unplacedLabel )
143  {
144  if ( mFeedback->isCanceled() )
145  {
146  context.setRenderingStopped( true );
147  }
148 
149  const QgsFeatureId fid = label->getFeaturePart()->featureId();
150  if ( settings.placement == Qgis::LabelPlacement::Curved ||
152  {
153  if ( !mCurvedWarningPushed.contains( layerId ) )
154  {
155  mCurvedWarningPushed << layerId;
156  mFeedback->pushWarning( QObject::tr( "Curved placement not supported, skipping labels from layer %1" ).arg( mMapLayerNames.value( layerId ) ) );
157  }
158  return;
159  }
160 
161  QgsTextLabelFeature *labelFeature = dynamic_cast<QgsTextLabelFeature *>( label->getFeaturePart()->feature() );
162  if ( !labelFeature )
163  return;
164 
165  QgsPalLayerSettings labelSettings( settings );
166  const QMap< QgsPalLayerSettings::Property, QVariant > &dataDefinedValues = labelFeature->dataDefinedValues();
167 
168  if ( dataDefinedValues.contains( QgsPalLayerSettings::MultiLineWrapChar ) )
169  {
170  labelSettings.wrapChar = dataDefinedValues.value( QgsPalLayerSettings::MultiLineWrapChar ).toString();
171  }
172  if ( dataDefinedValues.contains( QgsPalLayerSettings::AutoWrapLength ) )
173  {
174  labelSettings.autoWrapLength = dataDefinedValues.value( QgsPalLayerSettings::AutoWrapLength ).toInt();
175  }
176  const QString labelText = QgsPalLabeling::splitToLines( labelFeature->text( -1 ),
177  labelSettings.wrapChar,
178  labelSettings.autoWrapLength,
179  labelSettings.useMaxLineLengthForAutoWrap ).join( '\n' );
180 
181  QString labelAlignment;
182  if ( dataDefinedValues.contains( QgsPalLayerSettings::MultiLineAlignment ) )
183  {
184  labelSettings.multilineAlign = static_cast< Qgis::LabelMultiLineAlignment >( dataDefinedValues.value( QgsPalLayerSettings::MultiLineAlignment ).toInt() );
185  }
186  switch ( labelSettings.multilineAlign )
187  {
188  case Qgis::LabelMultiLineAlignment::Right:
189  labelAlignment = QStringLiteral( "right" );
190  break;
191 
192  case Qgis::LabelMultiLineAlignment::Center:
193  labelAlignment = QStringLiteral( "center" );
194  break;
195 
196  case Qgis::LabelMultiLineAlignment::Left:
197  labelAlignment = QStringLiteral( "left" );
198  break;
199 
200  case Qgis::LabelMultiLineAlignment::Justify:
201  labelAlignment = QStringLiteral( "justify" );
202  break;
203 
204  case Qgis::LabelMultiLineAlignment::FollowPlacement:
205  switch ( label->getQuadrant() )
206  {
210  labelAlignment = QStringLiteral( "right" );
211  break;
212 
216  labelAlignment = QStringLiteral( "center" );
217  break;
218 
222  labelAlignment = QStringLiteral( "left" );
223  break;
224  }
225  break;
226  }
227 
228  const double labelRotation = !qgsDoubleNear( label->getAlpha(), 0.0 )
229  ? -( label->getAlpha() * 180 / M_PI ) + 360
230  : 0.0;
231 
232  const QFont font = labelFeature->definedFont();
233  const QString fontFamily = font.family();
234  const QString fontStyle = font.styleName();
235  const double fontSize = static_cast<double>( font.pixelSize() ) * 72 / context.painter()->device()->logicalDpiX();
236  const bool fontItalic = font.italic();
237  const bool fontBold = font.bold();
238  const bool fontUnderline = font.underline();
239  const double fontLetterSpacing = font.letterSpacing();
240  const double fontWordSpacing = font.wordSpacing();
241 
242  QgsTextFormat format = labelSettings.format();
243  if ( dataDefinedValues.contains( QgsPalLayerSettings::Size ) )
244  {
245  format.setSize( dataDefinedValues.value( QgsPalLayerSettings::Size ).toDouble() );
246  }
247  if ( dataDefinedValues.contains( QgsPalLayerSettings::Color ) )
248  {
249  format.setColor( dataDefinedValues.value( QgsPalLayerSettings::Color ).value<QColor>() );
250  }
251  if ( dataDefinedValues.contains( QgsPalLayerSettings::FontOpacity ) )
252  {
253  format.setOpacity( dataDefinedValues.value( QgsPalLayerSettings::FontOpacity ).toDouble() / 100.0 );
254  }
255  if ( dataDefinedValues.contains( QgsPalLayerSettings::MultiLineHeight ) )
256  {
257  format.setLineHeight( dataDefinedValues.value( QgsPalLayerSettings::MultiLineHeight ).toDouble() );
258  }
259 
260  const QString formatColor = format.color().name();
261  const double formatOpacity = format.opacity() * 100;
262  const double formatLineHeight = format.lineHeight();
263 
264  QgsTextBufferSettings buffer = format.buffer();
265  if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferDraw ) )
266  {
267  buffer.setEnabled( dataDefinedValues.value( QgsPalLayerSettings::BufferDraw ).toBool() );
268  }
269  const bool bufferDraw = buffer.enabled();
270  double bufferSize = 0.0;
271  QString bufferColor;
272  double bufferOpacity = 0.0;
273  if ( bufferDraw )
274  {
275  if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferSize ) )
276  {
277  buffer.setSize( dataDefinedValues.value( QgsPalLayerSettings::BufferSize ).toDouble() );
278  }
279  if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferColor ) )
280  {
281  buffer.setColor( dataDefinedValues.value( QgsPalLayerSettings::BufferColor ).value<QColor>() );
282  }
283  if ( dataDefinedValues.contains( QgsPalLayerSettings::BufferOpacity ) )
284  {
285  buffer.setOpacity( dataDefinedValues.value( QgsPalLayerSettings::BufferOpacity ).toDouble() / 100.0 );
286  }
287 
288  bufferSize = buffer.sizeUnit() == QgsUnitTypes::RenderPercentage
289  ? context.convertToPainterUnits( format.size(), format.sizeUnit(), format.sizeMapUnitScale() ) * buffer.size() / 100
290  : context.convertToPainterUnits( buffer.size(), buffer.sizeUnit(), buffer.sizeMapUnitScale() );
291  bufferSize = bufferSize * 72 / context.painter()->device()->logicalDpiX();
292  bufferColor = buffer.color().name();
293  bufferOpacity = buffer.opacity() * 100;
294  }
295 
296  QgsAttributes attributes;
297  attributes << mMapLayerNames.value( layerId ) << fid
298  << labelText << label->getWidth() << label->getHeight() << labelRotation << unplacedLabel
299  << fontFamily << fontSize << fontItalic << fontBold << fontUnderline << fontStyle << fontLetterSpacing << fontWordSpacing
300  << labelAlignment << formatLineHeight << formatColor << formatOpacity
301  << bufferDraw << bufferSize << bufferColor << bufferOpacity;
302 
303  double x = label->getX();
304  double y = label->getY();
305  QgsGeometry geometry( new QgsPoint( x, y ) );
306 
307  QgsFeature feature;
308  feature.setAttributes( attributes );
309  feature.setGeometry( geometry );
310  features << feature;
311  }
312 
313  QList<QgsFeature> features;
314 
315  private:
316 
317  QMap<QString, QString> mMapLayerNames;
318  QList<QString> mCurvedWarningPushed;
319 
320  QgsProcessingFeedback *mFeedback = nullptr;
321 };
322 
323 QVariantMap QgsExtractLabelsAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
324 {
325  const QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context );
326  const double scale = parameterAsDouble( parameters, QStringLiteral( "SCALE" ), context );
327  if ( qgsDoubleNear( scale, 0.0 ) )
328  {
329  throw QgsProcessingException( QObject::tr( "Invalid scale value, a number greater than 0 is required" ) );
330  }
331  double dpi = parameterAsDouble( parameters, QStringLiteral( "DPI" ), context );
332  if ( qgsDoubleNear( dpi, 0.0 ) )
333  {
334  dpi = 96.0;
335  }
336 
337  QgsScaleCalculator calculator;
338  calculator.setDpi( dpi );
339  calculator.setMapUnits( mCrs.mapUnits() );
340  const QSize imageSize = calculator.calculateImageSize( extent, scale ).toSize();
341 
342  QgsFields fields;
343  fields.append( QgsField( QStringLiteral( "Layer" ), QVariant::String, QString(), 0, 0 ) );
344  fields.append( QgsField( QStringLiteral( "FeatureID" ), QVariant::LongLong, QString(), 20 ) );
345  fields.append( QgsField( QStringLiteral( "LabelText" ), QVariant::String, QString(), 0, 0 ) );
346  fields.append( QgsField( QStringLiteral( "LabelWidth" ), QVariant::Double, QString(), 20, 8 ) );
347  fields.append( QgsField( QStringLiteral( "LabelHeight" ), QVariant::Double, QString(), 20, 8 ) );
348  fields.append( QgsField( QStringLiteral( "LabelRotation" ), QVariant::Double, QString(), 20, 2 ) );
349  fields.append( QgsField( QStringLiteral( "LabelUnplaced" ), QVariant::Bool, QString(), 1, 0 ) );
350  fields.append( QgsField( QStringLiteral( "Family" ), QVariant::String, QString(), 0, 0 ) );
351  fields.append( QgsField( QStringLiteral( "Size" ), QVariant::Double, QString(), 20, 4 ) );
352  fields.append( QgsField( QStringLiteral( "Italic" ), QVariant::Bool, QString(), 1, 0 ) );
353  fields.append( QgsField( QStringLiteral( "Bold" ), QVariant::Bool, QString(), 1, 0 ) );
354  fields.append( QgsField( QStringLiteral( "Underline" ), QVariant::Bool, QString(), 1, 0 ) );
355  fields.append( QgsField( QStringLiteral( "FontStyle" ), QVariant::String, QString(), 0, 0 ) );
356  fields.append( QgsField( QStringLiteral( "FontLetterSpacing" ), QVariant::Double, QString(), 20, 4 ) );
357  fields.append( QgsField( QStringLiteral( "FontWordSpacing" ), QVariant::Double, QString(), 20, 4 ) );
358  fields.append( QgsField( QStringLiteral( "MultiLineAlignment" ), QVariant::String, QString(), 0, 0 ) );
359  fields.append( QgsField( QStringLiteral( "MultiLineHeight" ), QVariant::Double, QString(), 20, 2 ) );
360  fields.append( QgsField( QStringLiteral( "Color" ), QVariant::String, QString(), 7, 0 ) );
361  fields.append( QgsField( QStringLiteral( "FontOpacity" ), QVariant::Double, QString(), 20, 1 ) );
362  fields.append( QgsField( QStringLiteral( "BufferDraw" ), QVariant::Bool, QString(), 1, 0 ) );
363  fields.append( QgsField( QStringLiteral( "BufferSize" ), QVariant::Double, QString(), 20, 4 ) );
364  fields.append( QgsField( QStringLiteral( "BufferColor" ), QVariant::String, QString(), 7, 0 ) );
365  fields.append( QgsField( QStringLiteral( "BufferOpacity" ), QVariant::Double, QString(), 20, 1 ) );
366 
367  QString dest;
368  std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Point, mCrs, QgsFeatureSink::RegeneratePrimaryKey ) );
369  if ( !sink )
370  throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) );
371 
372  QgsMapSettings mapSettings;
373  mapSettings.setDestinationCrs( mCrs );
374  mapSettings.setExtent( extent );
375  mapSettings.setOutputSize( imageSize );
376  mapSettings.setOutputDpi( dpi );
377  mapSettings.setFlag( Qgis::MapSettingsFlag::DrawLabeling, true );
380  mapSettings.setLayers( mMapLayers );
381  mapSettings.setLayerStyleOverrides( mMapThemeStyleOverrides );
382  mapSettings.setLabelingEngineSettings( mLabelSettings );
383 
384  //build the expression context
385  QgsExpressionContext expressionContext;
386  expressionContext << QgsExpressionContextUtils::globalScope()
389  mapSettings.setExpressionContext( expressionContext );
390 
391  QgsNullPaintDevice nullPaintDevice;
392  nullPaintDevice.setOutputSize( imageSize );
393  nullPaintDevice.setOutputDpi( static_cast< int >( std::round( dpi ) ) );
394  QPainter painter( &nullPaintDevice );
395 
396  QgsMapRendererCustomPainterJob renderJob( mapSettings, &painter );
397  ExtractLabelSink labelSink( mMapLayerNames, feedback );
398  renderJob.setLabelSink( &labelSink );
399 
400  feedback->pushInfo( QObject::tr( "Extracting labels" ) );
401 
402  QgsProcessingMultiStepFeedback multiStepFeedback( 10, feedback );
403  multiStepFeedback.setCurrentStep( 0 );
404 
405  QEventLoop loop;
406  QObject::connect( feedback, &QgsFeedback::canceled, &renderJob, &QgsMapRendererCustomPainterJob::cancel );
407  QObject::connect( &renderJob, &QgsMapRendererJob::renderingLayersFinished, feedback, [feedback]() { feedback->pushInfo( QObject::tr( "Calculating label placement" ) ); } );
408  int labelsCollectedFromLayers = 0;
409  QObject::connect( &renderJob, &QgsMapRendererJob::layerRenderingStarted, feedback, [this, &multiStepFeedback, &labelsCollectedFromLayers]( const QString & layerId )
410  {
411  multiStepFeedback.pushInfo( QObject::tr( "Collecting labelled features from %1" ).arg( mMapLayerNames.value( layerId ) ) );
412  multiStepFeedback.setProgress( 100.0 * static_cast< double >( labelsCollectedFromLayers ) / mMapLayers.size() );
413  labelsCollectedFromLayers++;
414  } );
415 
416  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::labelRegistrationAboutToBegin, &multiStepFeedback, [&multiStepFeedback]()
417  {
418  multiStepFeedback.setCurrentStep( 1 );
419  multiStepFeedback.pushInfo( QObject::tr( "Registering labels" ) );
420  } );
421 
422  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::providerRegistrationAboutToBegin, &multiStepFeedback, [this, &multiStepFeedback]( QgsAbstractLabelProvider * provider )
423  {
424  multiStepFeedback.setCurrentStep( 2 );
425  if ( !provider->layerId().isEmpty() )
426  {
427  multiStepFeedback.pushInfo( QObject::tr( "Adding labels from %1" ).arg( mMapLayerNames.value( provider->layerId() ) ) );
428  }
429  } );
430  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::candidateCreationAboutToBegin, &multiStepFeedback, [this, &multiStepFeedback]( QgsAbstractLabelProvider * provider )
431  {
432  multiStepFeedback.setCurrentStep( 3 );
433  if ( !provider->layerId().isEmpty() )
434  {
435  multiStepFeedback.pushInfo( QObject::tr( "Generating label placement candidates for %1" ).arg( mMapLayerNames.value( provider->layerId() ) ) );
436  }
437  } );
438  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::obstacleCostingAboutToBegin, &multiStepFeedback, [&multiStepFeedback]()
439  {
440  multiStepFeedback.setCurrentStep( 4 );
441  multiStepFeedback.setProgressText( QObject::tr( "Calculating obstacle costs" ) );
442  } );
443  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::calculatingConflictsAboutToBegin, &multiStepFeedback, [&multiStepFeedback]()
444  {
445  multiStepFeedback.setCurrentStep( 5 );
446  multiStepFeedback.setProgressText( QObject::tr( "Calculating label conflicts" ) );
447  } );
448  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::finalizingCandidatesAboutToBegin, &multiStepFeedback, [&multiStepFeedback]()
449  {
450  multiStepFeedback.setCurrentStep( 6 );
451  multiStepFeedback.setProgressText( QObject::tr( "Finalizing candidates" ) );
452  } );
453  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::reductionAboutToBegin, &multiStepFeedback, [&multiStepFeedback]()
454  {
455  multiStepFeedback.setCurrentStep( 7 );
456  multiStepFeedback.setProgressText( QObject::tr( "Reducing problem" ) );
457  } );
458  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::solvingPlacementAboutToBegin, &multiStepFeedback, [&multiStepFeedback]()
459  {
460  multiStepFeedback.setCurrentStep( 8 );
461  multiStepFeedback.setProgressText( QObject::tr( "Determining optimal label placements" ) );
462  } );
463  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::solvingPlacementFinished, &multiStepFeedback, [&multiStepFeedback]()
464  {
465  multiStepFeedback.setProgressText( QObject::tr( "Labeling complete" ) );
466  } );
467 
468  QObject::connect( renderJob.labelingEngineFeedback(), &QgsLabelingEngineFeedback::progressChanged, &multiStepFeedback, [&multiStepFeedback]( double progress )
469  {
470  multiStepFeedback.setProgress( progress );
471  } );
472 
473  QObject::connect( &renderJob, &QgsMapRendererJob::finished, &loop, [&loop]() { loop.exit(); } );
474  renderJob.start();
475  loop.exec();
476 
477  qDeleteAll( mMapLayers );
478  mMapLayers.clear();
479 
480  multiStepFeedback.setCurrentStep( 9 );
481  feedback->pushInfo( QObject::tr( "Writing %n label(s) to output layer", "", labelSink.features.count() ) );
482  const double step = !labelSink.features.empty() ? 100.0 / labelSink.features.count() : 1;
483  long long index = -1;
484  for ( QgsFeature &feature : labelSink.features )
485  {
486  index++;
487  multiStepFeedback.setProgress( step * index );
488  if ( feedback->isCanceled() )
489  break;
490 
491  if ( !sink->addFeature( feature, QgsFeatureSink::FastInsert ) )
492  throw QgsProcessingException( writeFeatureError( sink.get(), parameters, QStringLiteral( "OUTPUT" ) ) );
493  }
494  sink.reset();
495 
496  if ( QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( QgsProcessingUtils::mapLayerFromString( dest, context ) ) )
497  {
498  vl->setRenderer( new QgsNullSymbolRenderer() );
499  if ( vl->renderer() )
500  {
501  vl->renderer()->setReferenceScale( scale );
502 
503  QgsPalLayerSettings settings;
504  QgsPropertyCollection settingsProperties;
505 
506  settings.fieldName = QStringLiteral( "LabelText" );
507  settings.obstacleSettings().setIsObstacle( false );
509  settings.quadOffset = Qgis::LabelQuadrantPosition::AboveRight;
510  settings.placementSettings().setAllowDegradedPlacement( true );
512 
513  QgsTextFormat textFormat;
514  textFormat.setSize( 9 );
516  textFormat.setColor( QColor( 0, 0, 0 ) );
517 
518  QgsTextBufferSettings buffer = textFormat.buffer();
520 
521  textFormat.setBuffer( buffer );
522  settings.setFormat( textFormat );
523 
524  settingsProperties.setProperty( QgsPalLayerSettings::Color, QgsProperty::fromExpression( QStringLiteral( "if(\"LabelUnplaced\",'255,0,0',\"Color\")" ) ) );
525  settingsProperties.setProperty( QgsPalLayerSettings::FontOpacity, QgsProperty::fromField( QStringLiteral( "FontOpacity" ) ) );
526  settingsProperties.setProperty( QgsPalLayerSettings::Family, QgsProperty::fromField( QStringLiteral( "Family" ) ) );
527  settingsProperties.setProperty( QgsPalLayerSettings::Italic, QgsProperty::fromField( QStringLiteral( "Italic" ) ) );
528  settingsProperties.setProperty( QgsPalLayerSettings::Bold, QgsProperty::fromField( QStringLiteral( "Bold" ) ) );
529  settingsProperties.setProperty( QgsPalLayerSettings::Underline, QgsProperty::fromField( QStringLiteral( "Underline" ) ) );
530  settingsProperties.setProperty( QgsPalLayerSettings::Size, QgsProperty::fromField( QStringLiteral( "Size" ) ) );
531  settingsProperties.setProperty( QgsPalLayerSettings::FontLetterSpacing, QgsProperty::fromField( QStringLiteral( "FontLetterSpacing" ) ) );
532  settingsProperties.setProperty( QgsPalLayerSettings::FontWordSpacing, QgsProperty::fromField( QStringLiteral( "FontWordSpacing" ) ) );
533  settingsProperties.setProperty( QgsPalLayerSettings::MultiLineAlignment, QgsProperty::fromField( QStringLiteral( "MultiLineAlignment" ) ) );
534  settingsProperties.setProperty( QgsPalLayerSettings::MultiLineHeight, QgsProperty::fromField( QStringLiteral( "MultiLineHeight" ) ) );
535  settingsProperties.setProperty( QgsPalLayerSettings::LabelRotation, QgsProperty::fromField( QStringLiteral( "LabelRotation" ) ) );
536  settingsProperties.setProperty( QgsPalLayerSettings::BufferDraw, QgsProperty::fromField( QStringLiteral( "BufferDraw" ) ) );
537  settingsProperties.setProperty( QgsPalLayerSettings::BufferSize, QgsProperty::fromField( QStringLiteral( "BufferSize" ) ) );
538  settingsProperties.setProperty( QgsPalLayerSettings::BufferColor, QgsProperty::fromField( QStringLiteral( "BufferColor" ) ) );
539  settingsProperties.setProperty( QgsPalLayerSettings::BufferOpacity, QgsProperty::fromField( QStringLiteral( "BufferOpacity" ) ) );
540  settingsProperties.setProperty( QgsPalLayerSettings::Show, QgsProperty::fromExpression( QStringLiteral( "\"LabelUnplaced\"=false" ) ) );
541  settings.setDataDefinedProperties( settingsProperties );
542 
544  vl->setLabeling( labeling );
545  vl->setLabelsEnabled( true );
546 
547  QString errorMessage;
548  vl->saveStyleToDatabase( QString(), QString(), true, QString(), errorMessage );
549  }
550  }
551 
552  QVariantMap outputs;
553  outputs.insert( QStringLiteral( "OUTPUT" ), dest );
554  return outputs;
555 }
556 
557 
558 bool QgsExtractLabelsAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback * )
559 {
560  // Retrieve and clone layers
561  const QString mapTheme = parameterAsString( parameters, QStringLiteral( "MAP_THEME" ), context );
562  if ( !mapTheme.isEmpty() && context.project()->mapThemeCollection()->hasMapTheme( mapTheme ) )
563  {
564  const QList<QgsMapLayer *> constLayers = context.project()->mapThemeCollection()->mapThemeVisibleLayers( mapTheme );
565  for ( const QgsMapLayer *l : constLayers )
566  {
567  // only copy vector layers as other layer types aren't actors in the labeling process
568  if ( l->type() == QgsMapLayerType::VectorLayer )
569  mMapLayers.push_back( l->clone() );
570  }
571  mMapThemeStyleOverrides = context.project()->mapThemeCollection( )->mapThemeStyleOverrides( mapTheme );
572  }
573 
574  if ( mMapLayers.isEmpty() )
575  {
576  QList<QgsMapLayer *> layers;
577  QgsLayerTree *root = context.project()->layerTreeRoot();
578  const QList<QgsLayerTreeLayer *> layerTreeLayers = root->findLayers();
579  layers.reserve( layerTreeLayers.size() );
580  for ( QgsLayerTreeLayer *nodeLayer : layerTreeLayers )
581  {
582  QgsMapLayer *layer = nodeLayer->layer();
583  if ( nodeLayer->isVisible() && root->layerOrder().contains( layer ) )
584  layers << layer;
585  }
586 
587  for ( const QgsMapLayer *l : std::as_const( layers ) )
588  {
589  if ( l->type() == QgsMapLayerType::VectorLayer )
590  mMapLayers.push_back( l->clone() );
591  }
592  }
593 
594  for ( const QgsMapLayer *l : std::as_const( mMapLayers ) )
595  {
596  mMapLayerNames.insert( l->id(), l->name() );
597  }
598 
599  mCrs = parameterAsExtentCrs( parameters, QStringLiteral( "EXTENT" ), context );
600  if ( !mCrs.isValid() )
601  mCrs = context.project()->crs();
602 
603  bool includeUnplaced = parameterAsBoolean( parameters, QStringLiteral( "INCLUDE_UNPLACED" ), context );
604  mLabelSettings = context.project()->labelingEngineSettings();
605  mLabelSettings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, includeUnplaced );
606  mLabelSettings.setFlag( QgsLabelingEngineSettings::CollectUnplacedLabels, includeUnplaced );
607 
608  return true;
609 }
610 
611 
QgsTextBufferSettings::setSizeUnit
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units used for the buffer size.
Definition: qgstextbuffersettings.cpp:97
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsMapSettings::setDestinationCrs
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs (coordinate reference system) for the map render.
Definition: qgsmapsettings.cpp:350
QgsLayerTreeGroup::findLayers
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Definition: qgslayertreegroup.cpp:249
qgsexpressioncontextutils.h
QgsPalLayerSettings::placement
Qgis::LabelPlacement placement
Label placement mode.
Definition: qgspallabeling.h:434
QgsLabelingEngineSettings::CollectUnplacedLabels
@ CollectUnplacedLabels
Whether unplaced labels should be collected in the labeling results (regardless of whether they are b...
Definition: qgslabelingenginesettings.h:44
pal::LabelPosition::getY
double getY(int i=0) const
Returns the down-left y coordinate.
Definition: labelposition.cpp:343
QgsMapRendererJob::layerRenderingStarted
void layerRenderingStarted(const QString &layerId)
Emitted just before rendering starts for a particular layer.
QgsProperty::fromField
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
Definition: qgsproperty.cpp:245
QgsProcessingParameterNumber::Double
@ Double
Double/float values.
Definition: qgsprocessingparameters.h:2187
QgsTextBufferSettings::setOpacity
void setOpacity(double opacity)
Sets the buffer opacity.
Definition: qgstextbuffersettings.cpp:137
QgsTextLabelFeature::text
QString text(int partId) const
Returns the text component corresponding to a specified label part.
Definition: qgstextlabelfeature.cpp:34
Qgis::MapSettingsFlag::UseRenderingOptimization
@ UseRenderingOptimization
Enable vector simplification and other rendering optimizations.
QgsTextLabelFeature::dataDefinedValues
const QMap< QgsPalLayerSettings::Property, QVariant > & dataDefinedValues() const
Gets data-defined values.
Definition: qgstextlabelfeature.h:69
qgsnullsymbolrenderer.h
Qgis::MapSettingsFlag::SkipSymbolRendering
@ SkipSymbolRendering
Disable symbol rendering while still drawing labels if enabled (since QGIS 3.24)
QgsTextBufferSettings::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
Definition: qgstextbuffersettings.cpp:102
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:72
qgsscalecalculator.h
QgsScaleCalculator::setMapUnits
void setMapUnits(QgsUnitTypes::DistanceUnit mapUnits)
Set the map units.
Definition: qgsscalecalculator.cpp:38
QgsExpressionContextUtils::globalScope
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Definition: qgsexpressioncontextutils.cpp:40
QgsLabelingEngineFeedback::finalizingCandidatesAboutToBegin
void finalizingCandidatesAboutToBegin()
Emitted when the label candidates are about to be finalized.
QgsScaleCalculator::setDpi
void setDpi(double dpi)
Sets the dpi (dots per inch) for the output resolution, to be used in scale calculations.
Definition: qgsscalecalculator.cpp:29
QgsMapSettings::setFlag
void setFlag(Qgis::MapSettingsFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
Definition: qgsmapsettings.cpp:382
QgsProcessingContext::project
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Definition: qgsprocessingcontext.h:121
QgsMapLayerType::VectorLayer
@ VectorLayer
Vector layer.
qgsmapthemecollection.h
QgsPalLayerSettings::BufferSize
@ BufferSize
Definition: qgspallabeling.h:181
QgsLabelingEngineFeedback::candidateCreationAboutToBegin
void candidateCreationAboutToBegin(QgsAbstractLabelProvider *provider)
Emitted when the label candidate creation is about to begin for a provider.
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsPalLayerSettings::Color
@ Color
Text color.
Definition: qgspallabeling.h:151
QgsProcessingFeedback
Base class for providing feedback from a processing algorithm.
Definition: qgsprocessingfeedback.h:37
labelposition.h
QgsPalLayerSettings
Contains settings for how a map layer will be labeled.
Definition: qgspallabeling.h:86
QgsLabelingEngineFeedback::solvingPlacementFinished
void solvingPlacementFinished()
Emitted when the problem solving step is finished.
QgsMapRendererJob::renderingLayersFinished
void renderingLayersFinished()
Emitted when the layers are rendered.
QgsTextBufferSettings::setEnabled
void setEnabled(bool enabled)
Sets whether the text buffer will be drawn.
Definition: qgstextbuffersettings.cpp:77
QgsPalLayerSettings::FontOpacity
@ FontOpacity
Text opacity.
Definition: qgspallabeling.h:157
Qgis::MapSettingsFlag::DrawLabeling
@ DrawLabeling
Enable drawing of labels on top of the map.
QgsPalLayerSettings::MultiLineHeight
@ MultiLineHeight
Definition: qgspallabeling.h:167
QgsTextFormat::buffer
QgsTextBufferSettings & buffer()
Returns a reference to the text buffer settings.
Definition: qgstextformat.cpp:112
QgsFeedback::canceled
void canceled()
Internal routines can connect to this signal if they use event loop.
QgsProcessingFeedback::pushInfo
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
Definition: qgsprocessingfeedback.cpp:77
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
QgsLabelingEngineFeedback::providerRegistrationAboutToBegin
void providerRegistrationAboutToBegin(QgsAbstractLabelProvider *provider)
Emitted when the label registration is about to begin for a provider.
QgsLabelSink
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Definition: qgslabelsink.h:37
QgsUnitTypes::RenderPoints
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:173
QgsPalLayerSettings::BufferOpacity
@ BufferOpacity
Buffer opacity.
Definition: qgspallabeling.h:185
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
QgsPalLayerSettings::obstacleSettings
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the label obstacle settings.
Definition: qgspallabeling.h:905
pal::LabelPosition::QuadrantOver
@ QuadrantOver
Definition: labelposition.h:71
QgsTextFormat::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the size of rendered text.
Definition: qgstextformat.cpp:264
pal::LabelPosition::QuadrantAboveLeft
@ QuadrantAboveLeft
Definition: labelposition.h:67
QgsUnitTypes::RenderPercentage
@ RenderPercentage
Percentage of another measurement (e.g., canvas size, feature size)
Definition: qgsunittypes.h:172
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
QgsTextBufferSettings::opacity
double opacity() const
Returns the buffer opacity.
Definition: qgstextbuffersettings.cpp:132
QgsExpressionContextUtils::mapSettingsScope
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
Definition: qgsexpressioncontextutils.cpp:427
qgsmaprenderercustompainterjob.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsLabelPlacementSettings::setAllowDegradedPlacement
void setAllowDegradedPlacement(bool allow)
Sets whether labels can be placed in inferior fallback positions if they cannot otherwise be placed.
Definition: qgslabelplacementsettings.h:70
QgsProcessingParameterDefinition::FlagAdvanced
@ FlagAdvanced
Parameter is an advanced parameter which should be hidden from users by default.
Definition: qgsprocessingparameters.h:451
QgsPalLayerSettings::quadOffset
Qgis::LabelQuadrantPosition quadOffset
Sets the quadrant in which to offset labels from feature.
Definition: qgspallabeling.h:526
QgsLabelObstacleSettings::setIsObstacle
void setIsObstacle(bool isObstacle)
Sets whether features are obstacles to labels of other layers.
Definition: qgslabelobstaclesettings.h:71
pal::LabelPosition::getQuadrant
Quadrant getQuadrant() const
Definition: labelposition.h:270
QgsMapSettings::setOutputDpi
void setOutputDpi(double dpi)
Sets the dpi (dots per inch) used for conversion between real world units (e.g.
Definition: qgsmapsettings.cpp:272
QgsProperty::fromExpression
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
Definition: qgsproperty.cpp:237
QgsMapRendererCustomPainterJob::cancel
void cancel() override
Stop the rendering job - does not return until the job has terminated.
Definition: qgsmaprenderercustompainterjob.cpp:146
QgsFields::append
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false)
Definition: qgsfields.cpp:59
qgsalgorithmextractlabels.h
QgsTextLabelFeature
Class that adds extra information to QgsLabelFeature for text labels.
Definition: qgstextlabelfeature.h:34
QgsProcessing::TypeVectorPoint
@ TypeVectorPoint
Vector point layers.
Definition: qgsprocessing.h:49
QgsPalLayerSettings::MultiLineWrapChar
@ MultiLineWrapChar
Definition: qgspallabeling.h:165
Qgis::LabelOverlapHandling::AllowOverlapIfRequired
@ AllowOverlapIfRequired
Avoids overlapping labels when possible, but permit overlaps if labels for features cannot otherwise ...
QgsLabelingEngineFeedback::reductionAboutToBegin
void reductionAboutToBegin()
Emitted when the candidate reduction step is about to begin.
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsPalLayerSettings::Show
@ Show
Definition: qgspallabeling.h:281
pal::LabelPosition::getFeaturePart
FeaturePart * getFeaturePart() const
Returns the feature corresponding to this labelposition.
Definition: labelposition.cpp:361
QgsMapThemeCollection::mapThemeVisibleLayers
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
Definition: qgsmapthemecollection.cpp:331
QgsMapThemeCollection::mapThemeStyleOverrides
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
Definition: qgsmapthemecollection.cpp:385
QgsPalLayerSettings::placementSettings
const QgsLabelPlacementSettings & placementSettings() const
Returns the label placement settings.
Definition: qgspallabeling.h:949
QgsTextFormat::color
QColor color() const
Returns the color that text will be rendered in.
Definition: qgstextformat.cpp:297
QgsNullPaintDevice::setOutputDpi
void setOutputDpi(const int dpi)
Sets the dpi of the device.
Definition: qgsnullpainterdevice.h:99
QgsProcessingParameterFeatureSink
A feature sink output for processing algorithms.
Definition: qgsprocessingparameters.h:3219
QgsProject::labelingEngineSettings
const QgsLabelingEngineSettings & labelingEngineSettings() const
Returns project's global labeling engine settings.
Definition: qgsproject.cpp:2175
QgsPalLayerSettings::FontWordSpacing
@ FontWordSpacing
Word spacing.
Definition: qgspallabeling.h:160
pal::LabelPosition::QuadrantBelowRight
@ QuadrantBelowRight
Definition: labelposition.h:75
pal::LabelPosition::QuadrantBelowLeft
@ QuadrantBelowLeft
Definition: labelposition.h:73
Qgis::LabelMultiLineAlignment
LabelMultiLineAlignment
Text alignment for multi-line labels.
Definition: qgis.h:657
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsTextFormat::setColor
void setColor(const QColor &color)
Sets the color that text will be rendered in.
Definition: qgstextformat.cpp:302
QgsPalLayerSettings::MultiLineAlignment
@ MultiLineAlignment
Definition: qgspallabeling.h:168
feature.h
qgsnullpainterdevice.h
QgsProject::layerTreeRoot
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
Definition: qgsproject.cpp:3541
QgsExpressionContextUtils::projectScope
static QgsExpressionContextScope * projectScope(const QgsProject *project)
Creates a new scope which contains variables and functions relating to a QGIS project.
Definition: qgsexpressioncontextutils.cpp:291
QgsProcessingParameterScale
A double numeric parameter for map scale values.
Definition: qgsprocessingparameters.h:2408
QgsTextFormat::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the size.
Definition: qgstextformat.cpp:275
QgsLayerTree
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
QgsPalLayerSettings::FontLetterSpacing
@ FontLetterSpacing
Letter spacing.
Definition: qgspallabeling.h:159
QgsScaleCalculator
Calculates scale for a given combination of canvas size, map extent, and monitor dpi.
Definition: qgsscalecalculator.h:34
QgsPalLayerSettings::Bold
@ Bold
Use bold style.
Definition: qgspallabeling.h:148
QgsAbstractLabelProvider
The QgsAbstractLabelProvider class is an interface class. Implementations return list of labels and t...
Definition: qgslabelingengine.h:52
QgsProcessingMultiStepFeedback
Processing feedback object for multi-step operations.
Definition: qgsprocessingfeedback.h:166
QgsFeature::setGeometry
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
Definition: qgsfeature.cpp:170
QgsPalLayerSettings::setDataDefinedProperties
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the label's property collection, used for data defined overrides.
Definition: qgspallabeling.h:834
QgsProcessingContext
Contains information about the context in which a processing algorithm is executed.
Definition: qgsprocessingcontext.h:46
qgslabelsink.h
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2265
QgsPalLayerSettings::BufferColor
@ BufferColor
Definition: qgspallabeling.h:183
QgsLabelingEngineFeedback::labelRegistrationAboutToBegin
void labelRegistrationAboutToBegin()
Emitted when the label registration is about to begin.
QgsLayerTree::layerOrder
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
Definition: qgslayertree.cpp:79
QgsLayerTreeLayer
Layer tree node points to a map layer.
Definition: qgslayertreelayer.h:43
QgsRenderContext::setRenderingStopped
void setRenderingStopped(bool stopped)
Sets whether the rendering operation has been stopped and any ongoing rendering should be canceled im...
Definition: qgsrendercontext.h:457
QgsPalLayerSettings::setFormat
void setFormat(const QgsTextFormat &format)
Sets the label text formatting settings, e.g., font settings, buffer settings, etc.
Definition: qgspallabeling.h:849
QgsProcessingParameterExtent
A rectangular map extent parameter for processing algorithms.
Definition: qgsprocessingparameters.h:1772
QgsPalLayerSettings::LabelRotation
@ LabelRotation
Label rotation.
Definition: qgspallabeling.h:249
pointset.h
QgsPalLabeling::splitToLines
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).
Definition: qgspallabeling.cpp:3862
qgsprocessingfeedback.h
QgsTextFormat::lineHeight
double lineHeight() const
Returns the line height for text.
Definition: qgstextformat.cpp:341
Qgis::LabelPlacement::OverPoint
@ OverPoint
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point....
QgsNullPaintDevice
Null painter device that can be used for map renderer jobs which use custom painters.
Definition: qgsnullpainterdevice.h:67
QgsTextFormat::opacity
double opacity() const
Returns the text's opacity.
Definition: qgstextformat.cpp:308
QgsFeedback::progressChanged
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
QgsPalLayerSettings::AutoWrapLength
@ AutoWrapLength
Definition: qgspallabeling.h:166
QgsProject::mapThemeCollection
QgsMapThemeCollection mapThemeCollection
Definition: qgsproject.h:112
QgsNullSymbolRenderer
Null symbol renderer. Renderer which draws no symbols for features by default, but allows for labelin...
Definition: qgsnullsymbolrenderer.h:31
QgsTextBufferSettings
Container for settings relating to a text buffer.
Definition: qgstextbuffersettings.h:42
QgsPalLayerSettings::BufferDraw
@ BufferDraw
Definition: qgspallabeling.h:180
QgsTextBufferSettings::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
Definition: qgstextbuffersettings.cpp:92
qgslayertree.h
QgsFeatureSink::RegeneratePrimaryKey
@ RegeneratePrimaryKey
This flag indicates, that a primary key field cannot be guaranteed to be unique and the sink should i...
Definition: qgsfeaturesink.h:55
pal::LabelPosition::QuadrantAboveRight
@ QuadrantAboveRight
Definition: labelposition.h:69
QgsAbstractVectorLayerLabeling
Abstract base class - its implementations define different approaches to the labeling of a vector lay...
Definition: qgsvectorlayerlabeling.h:41
QgsMapSettings::setLabelingEngineSettings
void setLabelingEngineSettings(const QgsLabelingEngineSettings &settings)
Sets the global configuration of the labeling engine.
Definition: qgsmapsettings.h:654
QgsMapSettings::setLayers
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
Definition: qgsmapsettings.cpp:327
QgsTextBufferSettings::size
double size() const
Returns the size of the buffer.
Definition: qgstextbuffersettings.cpp:82
qgsvectorlayer.h
QgsRenderContext::convertToPainterUnits
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
Definition: qgsrendercontext.cpp:367
QgsTextFormat::size
double size() const
Returns the size for rendered text.
Definition: qgstextformat.cpp:286
QgsLabelingEngineFeedback::obstacleCostingAboutToBegin
void obstacleCostingAboutToBegin()
Emitted when the obstacle costing is about to begin.
QgsMapRendererCustomPainterJob
Job implementation that renders everything sequentially using a custom painter.
Definition: qgsmaprenderercustompainterjob.h:63
QgsLabelSink::drawLabel
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.
QgsScaleCalculator::calculateImageSize
QSizeF calculateImageSize(const QgsRectangle &mapExtent, double scale) const
Calculate the image size in pixel (physical) units.
Definition: qgsscalecalculator.cpp:67
QgsTextLabelFeature::definedFont
QFont definedFont() const
Font to be used for rendering.
Definition: qgstextlabelfeature.h:76
QgsPropertyCollection
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
Definition: qgspropertycollection.h:318
QgsLabelingEngineSettings::setFlag
void setFlag(Flag f, bool enabled=true)
Sets whether a particual flag is enabled.
Definition: qgslabelingenginesettings.h:86
qgsprocessingparameters.h
QgsPalLayerSettings::Size
@ Size
Label size.
Definition: qgspallabeling.h:147
QgsMapSettings::setExpressionContext
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Definition: qgsmapsettings.h:479
QgsTextBufferSettings::enabled
bool enabled() const
Returns whether the buffer is enabled.
Definition: qgstextbuffersettings.cpp:72
QgsProcessingParameterBoolean
A boolean parameter for processing algorithms.
Definition: qgsprocessingparameters.h:1709
pal::LabelPosition::QuadrantLeft
@ QuadrantLeft
Definition: labelposition.h:70
QgsTextFormat::setBuffer
void setBuffer(const QgsTextBufferSettings &bufferSettings)
Sets the text's buffer settings.
Definition: qgstextformat.cpp:118
QgsPalLayerSettings::fieldName
QString fieldName
Name of field (or an expression) to use for label text.
Definition: qgspallabeling.h:354
QgsVectorLayerSimpleLabeling
Basic implementation of the labeling interface.
Definition: qgsvectorlayerlabeling.h:163
QgsPalLayerSettings::Italic
@ Italic
Use italic style.
Definition: qgspallabeling.h:149
pal::LabelPosition::getHeight
double getHeight() const
Definition: labelposition.h:261
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
QgsTextFormat::setLineHeight
void setLineHeight(double height)
Sets the line height for text.
Definition: qgstextformat.cpp:346
QgsMapSettings::setLayerStyleOverrides
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 ...
Definition: qgsmapsettings.cpp:345
QgsVectorLayer
Represents a vector layer which manages a vector based data sets.
Definition: qgsvectorlayer.h:391
QgsPalLayerSettings::Family
@ Family
Font family.
Definition: qgspallabeling.h:153
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsTextFormat::setOpacity
void setOpacity(double opacity)
Sets the text's opacity.
Definition: qgstextformat.cpp:313
QgsNullPaintDevice::setOutputSize
void setOutputSize(const QSize &size)
Sets the size of the device in pixels.
Definition: qgsnullpainterdevice.h:94
pal::LabelPosition::getX
double getX(int i=0) const
Returns the down-left x coordinate.
Definition: labelposition.cpp:338
QgsTextBufferSettings::setSize
void setSize(double size)
Sets the size of the buffer.
Definition: qgstextbuffersettings.cpp:87
QgsTextBufferSettings::setColor
void setColor(const QColor &color)
Sets the color for the buffer.
Definition: qgstextbuffersettings.cpp:117
QgsTextFormat::setSizeUnit
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units for the size of rendered text.
Definition: qgstextformat.cpp:269
pal::LabelPosition::QuadrantRight
@ QuadrantRight
Definition: labelposition.h:72
QgsAttributes
A vector of attributes. Mostly equal to QVector<QVariant>.
Definition: qgsattributes.h:57
QgsProcessingUtils::mapLayerFromString
static QgsMapLayer * mapLayerFromString(const QString &string, QgsProcessingContext &context, bool allowLoadingNewLayers=true, QgsProcessingUtils::LayerHint typeHint=QgsProcessingUtils::LayerHint::UnknownType)
Interprets a string as a map layer within the supplied context.
Definition: qgsprocessingutils.cpp:376
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsPropertyCollection::setProperty
void setProperty(int key, const QgsProperty &property)
Adds a property to the collection and takes ownership of it.
Definition: qgspropertycollection.cpp:187
QgsMapThemeCollection::hasMapTheme
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
Definition: qgsmapthemecollection.cpp:257
Qgis::LabelPlacement::PerimeterCurved
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
QgsPalLayerSettings::Underline
@ Underline
Use underline.
Definition: qgspallabeling.h:150
QgsMapSettings::setOutputSize
void setOutputSize(QSize size)
Sets the size of the resulting map image, in pixels.
Definition: qgsmapsettings.cpp:244
pal::LabelPosition::getAlpha
double getAlpha() const
Returns the angle to rotate text (in rad).
Definition: labelposition.cpp:348
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:112
QgsFeature::setAttributes
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:160
QgsProcessingAlgorithm::flags
virtual Flags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Definition: qgsprocessingalgorithm.cpp:90
QgsTextFormat::setSize
void setSize(double size)
Sets the size for rendered text.
Definition: qgstextformat.cpp:291
pal::FeaturePart::featureId
QgsFeatureId featureId() const
Returns the unique ID of the feature.
Definition: feature.cpp:159
QgsTextBufferSettings::color
QColor color() const
Returns the color of the buffer.
Definition: qgstextbuffersettings.cpp:112
QgsLabelingEngineFeedback::calculatingConflictsAboutToBegin
void calculatingConflictsAboutToBegin()
Emitted when the conflict handling step is about to begin.
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
pal::FeaturePart::feature
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:94
pal::LabelPosition::QuadrantBelow
@ QuadrantBelow
Definition: labelposition.h:74
pal::LabelPosition::QuadrantAbove
@ QuadrantAbove
Definition: labelposition.h:68
QgsMapSettings::setExtent
void setExtent(const QgsRectangle &rect, bool magnified=true)
Sets the coordinates of the rectangle which should be rendered.
Definition: qgsmapsettings.cpp:80
qgstextlabelfeature.h
QgsLabelPlacementSettings::setOverlapHandling
void setOverlapHandling(Qgis::LabelOverlapHandling handling)
Sets the technique used to handle overlapping labels.
Definition: qgslabelplacementsettings.h:48
QgsProject::crs
QgsCoordinateReferenceSystem crs
Definition: qgsproject.h:109
QgsProcessingException
Custom exception class for processing related exceptions.
Definition: qgsexception.h:82
QgsLabelingEngineSettings::DrawUnplacedLabels
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
Definition: qgslabelingenginesettings.h:43
QgsFeatureSink::FastInsert
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
Definition: qgsfeaturesink.h:70
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
QgsLabelingEngineFeedback::solvingPlacementAboutToBegin
void solvingPlacementAboutToBegin()
Emitted when the problem solving step is about to begin.
QgsMapRendererJob::finished
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsLabelSink::drawUnplacedLabel
virtual void drawUnplacedLabel(const QString &layerId, QgsRenderContext &context, pal::LabelPosition *label, const QgsPalLayerSettings &settings)
The drawLabel method is called for each unplaced label.
Definition: qgslabelsink.h:57
pal::LabelPosition::getWidth
double getWidth() const
Definition: labelposition.h:260
QgsField
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:50
Qgis::LabelPlacement::Curved
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.