QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
pal.cpp
Go to the documentation of this file.
1/*
2 * libpal - Automated Placement of Labels Library
3 *
4 * Copyright (C) 2008 Maxence Laurent, MIS-TIC, HEIG-VD
5 * University of Applied Sciences, Western Switzerland
6 * http://www.hes-so.ch
7 *
8 * Contact:
9 * maxence.laurent <at> heig-vd <dot> ch
10 * or
11 * eric.taillard <at> heig-vd <dot> ch
12 *
13 * This file is part of libpal.
14 *
15 * libpal is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * libpal is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with libpal. If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30#include "pal.h"
31
32#include "costcalculator.h"
33#include "feature.h"
34#include "geomfunction.h"
35#include "internalexception.h"
36#include "labelposition.h"
37#include "layer.h"
38#include "palrtree.h"
39#include "pointset.h"
40#include "problem.h"
41#include "qgsgeometry.h"
42#include "qgslabelingengine.h"
44#include "qgsrendercontext.h"
45#include "qgsruntimeprofiler.h"
47#include "util.h"
48
49#include <QString>
50
51using namespace Qt::StringLiterals;
52
53#if ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 10 )
54#include "qgsmessagelog.h"
55#endif
56#include <cfloat>
57#include <list>
58
59
60using namespace pal;
61
62const QgsSettingsEntryInteger *Pal::settingsRenderingLabelCandidatesLimitPoints = new QgsSettingsEntryInteger( u"label-candidates-limit-points"_s, sTreePal, 0 );
63const QgsSettingsEntryInteger *Pal::settingsRenderingLabelCandidatesLimitLines = new QgsSettingsEntryInteger( u"label-candidates-limit-lines"_s, sTreePal, 0 );
64const QgsSettingsEntryInteger *Pal::settingsRenderingLabelCandidatesLimitPolygons = new QgsSettingsEntryInteger( u"label-candidates-limit-polygons"_s, sTreePal, 0 );
65
66
68{
69 mGlobalCandidatesLimitPoint = Pal::settingsRenderingLabelCandidatesLimitPoints->value();
70 mGlobalCandidatesLimitLine = Pal::settingsRenderingLabelCandidatesLimitLines->value();
71 mGlobalCandidatesLimitPolygon = Pal::settingsRenderingLabelCandidatesLimitPolygons->value();
72}
73
74Pal::~Pal() = default;
75
76void Pal::removeLayer( Layer *layer )
77{
78 if ( !layer )
79 return;
80
81 mMutex.lock();
82
83 for ( auto it = mLayers.begin(); it != mLayers.end(); ++it )
84 {
85 if ( it->second.get() == layer )
86 {
87 mLayers.erase( it );
88 break;
89 }
90 }
91 mMutex.unlock();
92}
93
94Layer *Pal::addLayer( QgsAbstractLabelProvider *provider, const QString &layerName, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel )
95{
96 mMutex.lock();
97
98#ifdef QGISDEBUG
99 for ( const auto &it : mLayers )
100 {
101 Q_ASSERT( it.first != provider );
102 }
103#endif
104
105 auto layer = std::make_unique< Layer >( provider, layerName, arrangement, defaultPriority, active, toLabel, this );
106 Layer *res = layer.get();
107 mLayers.emplace_back( std::make_pair( provider, std::move( layer ) ) );
108 mMutex.unlock();
109
110 // cppcheck-suppress returnDanglingLifetime
111 return res;
112}
113
114std::unique_ptr<Problem> Pal::extractProblem( const QgsRectangle &extent, const QgsGeometry &mapBoundary, QgsRenderContext &context )
115{
116 QgsLabelingEngineFeedback *feedback = qobject_cast< QgsLabelingEngineFeedback * >( context.feedback() );
117 QgsLabelingEngineContext labelContext( context );
118 labelContext.setExtent( extent );
119 labelContext.setMapBoundaryGeometry( mapBoundary );
120
121 std::unique_ptr< QgsScopedRuntimeProfile > extractionProfile;
123 {
124 extractionProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Placing labels" ), u"rendering"_s );
125 }
126
127 // expand out the incoming buffer by 1000x -- that's the visible map extent, yet we may be getting features which exceed this extent
128 // (while 1000x may seem excessive here, this value is only used for scaling coordinates in the spatial indexes
129 // and the consequence of inserting coordinates outside this extent is worse than the consequence of setting this value too large.)
130 const QgsRectangle maxCoordinateExtentForSpatialIndices = extent.buffered( std::max( extent.width(), extent.height() ) * 1000 );
131
132 // to store obstacles
133 PalRtree< FeaturePart > obstacles( maxCoordinateExtentForSpatialIndices );
134 PalRtree< LabelPosition > allCandidatesFirstRound( maxCoordinateExtentForSpatialIndices );
135 std::vector< FeaturePart * > allObstacleParts;
136 auto prob = std::make_unique< Problem >( maxCoordinateExtentForSpatialIndices );
137
138 double bbx[4];
139 double bby[4];
140
141 bbx[0] = bbx[3] = prob->mMapExtentBounds[0] = extent.xMinimum();
142 bby[0] = bby[1] = prob->mMapExtentBounds[1] = extent.yMinimum();
143 bbx[1] = bbx[2] = prob->mMapExtentBounds[2] = extent.xMaximum();
144 bby[2] = bby[3] = prob->mMapExtentBounds[3] = extent.yMaximum();
145
146 prob->pal = this;
147
148 std::list< std::unique_ptr< Feats > > features;
149
150 // prepare map boundary
151 geos::unique_ptr mapBoundaryGeos( QgsGeos::asGeos( mapBoundary ) );
152 geos::prepared_unique_ptr mapBoundaryPrepared( GEOSPrepare_r( QgsGeosContext::get(), mapBoundaryGeos.get() ) );
153
154 int obstacleCount = 0;
155
156 // first step : extract features from layers
157
158 std::size_t previousFeatureCount = 0;
159 int previousObstacleCount = 0;
160
161 QStringList layersWithFeaturesInBBox;
162
163 QMutexLocker palLocker( &mMutex );
164
165 double step = !mLayers.empty() ? 100.0 / mLayers.size() : 1;
166 int index = -1;
167 std::unique_ptr< QgsScopedRuntimeProfile > candidateProfile;
169 {
170 candidateProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Generating label candidates" ), u"rendering"_s );
171 }
172
173 for ( auto it = mLayers.rbegin(); it != mLayers.rend(); ++it )
174 {
175 index++;
176 if ( feedback )
177 feedback->setProgress( index * step );
178
179 Layer *layer = it->second.get();
180 if ( !layer )
181 {
182 // invalid layer name
183 continue;
184 }
185
186 // only select those who are active
187 if ( !layer->active() )
188 continue;
189
190 if ( feedback )
191 feedback->emit candidateCreationAboutToBegin( it->first );
192
193 std::unique_ptr< QgsScopedRuntimeProfile > layerProfile;
195 {
196 layerProfile = std::make_unique< QgsScopedRuntimeProfile >( it->first->providerId(), u"rendering"_s );
197 }
198
199 // check for connected features with the same label text and join them
200 if ( layer->mergeConnectedLines() )
201 layer->joinConnectedFeatures();
202
203 if ( isCanceled() )
204 return nullptr;
205
207
208 if ( isCanceled() )
209 return nullptr;
210
211 QMutexLocker locker( &layer->mMutex );
212
213 const double featureStep = !layer->mFeatureParts.empty() ? step / layer->mFeatureParts.size() : 1;
214 std::size_t featureIndex = 0;
215 // generate candidates for all features
216 for ( const std::unique_ptr< FeaturePart > &featurePart : std::as_const( layer->mFeatureParts ) )
217 {
218 if ( feedback )
219 feedback->setProgress( index * step + featureIndex * featureStep );
220 featureIndex++;
221
222 if ( isCanceled() )
223 break;
224
225 // Holes of the feature are obstacles
226 for ( int i = 0; i < featurePart->getNumSelfObstacles(); i++ )
227 {
228 FeaturePart *selfObstacle = featurePart->getSelfObstacle( i );
229 obstacles.insert( selfObstacle, selfObstacle->boundingBox() );
230 allObstacleParts.emplace_back( selfObstacle );
231
232 if ( !featurePart->getSelfObstacle( i )->getHoleOf() )
233 {
234 //ERROR: SHOULD HAVE A PARENT!!!!!
235 }
236 }
237
238 // generate candidates for the feature part
239 std::vector< std::unique_ptr< LabelPosition > > candidates = featurePart->createCandidates( this );
240
241 if ( isCanceled() )
242 break;
243
244 // purge candidates that violate known constraints, eg
245 // - they are outside the bbox
246 // - they violate a labeling rule
247 candidates.erase(
248 std::remove_if(
249 candidates.begin(),
250 candidates.end(),
251 [&mapBoundaryPrepared, &labelContext, this]( std::unique_ptr< LabelPosition > &candidate ) {
252 if ( showPartialLabels() )
253 {
254 if ( !candidate->intersects( mapBoundaryPrepared.get() ) )
255 return true;
256 }
257 else
258 {
259 if ( !candidate->within( mapBoundaryPrepared.get() ) )
260 return true;
261 }
262
263 for ( QgsAbstractLabelingEngineRule *rule : std::as_const( mRules ) )
264 {
265 if ( rule->candidateIsIllegal( candidate.get(), labelContext ) )
266 {
267 return true;
268 }
269 }
270 return false;
271 }
272 ),
273 candidates.end()
274 );
275
276 if ( isCanceled() )
277 break;
278
279 if ( !candidates.empty() )
280 {
281 for ( std::unique_ptr< LabelPosition > &candidate : candidates )
282 {
283 candidate->insertIntoIndex( allCandidatesFirstRound, this );
284 candidate->setGlobalId( mNextCandidateId++ );
285 }
286
287 std::sort( candidates.begin(), candidates.end(), CostCalculator::candidateSortGrow );
288
289 // valid features are added to fFeats
290 auto ft = std::make_unique< Feats >();
291 ft->feature = featurePart.get();
292 ft->shape = nullptr;
293 ft->candidates = std::move( candidates );
294 ft->priority = featurePart->calculatePriority();
295 features.emplace_back( std::move( ft ) );
296 }
297 else
298 {
299 // no candidates, so generate a default "point on surface" one
300 std::unique_ptr< LabelPosition > unplacedPosition = featurePart->createCandidatePointOnSurface( featurePart.get() );
301 if ( !unplacedPosition )
302 continue;
303
304 if ( featurePart->feature()->allowDegradedPlacement() )
305 {
306 // if we are allowing degraded placements, we throw the default candidate in too
307 unplacedPosition->insertIntoIndex( allCandidatesFirstRound, this );
308 unplacedPosition->setGlobalId( mNextCandidateId++ );
309 candidates.emplace_back( std::move( unplacedPosition ) );
310
311 // valid features are added to fFeats
312 auto ft = std::make_unique< Feats >();
313 ft->feature = featurePart.get();
314 ft->shape = nullptr;
315 ft->candidates = std::move( candidates );
316 ft->priority = featurePart->calculatePriority();
317 features.emplace_back( std::move( ft ) );
318 }
319 else
320 {
321 // not displaying all labels for this layer, so it goes into the unlabeled feature list
322 prob->positionsWithNoCandidates()->emplace_back( std::move( unplacedPosition ) );
323 }
324 }
325 }
326 if ( isCanceled() )
327 return nullptr;
328
329 // collate all layer obstacles
330 for ( FeaturePart *obstaclePart : std::as_const( layer->mObstacleParts ) )
331 {
332 if ( isCanceled() )
333 break; // do not continue searching
334
335 // insert into obstacles
336 obstacles.insert( obstaclePart, obstaclePart->boundingBox() );
337 allObstacleParts.emplace_back( obstaclePart );
338 obstacleCount++;
339 }
340
341 if ( isCanceled() )
342 return nullptr;
343
344 locker.unlock();
345
346 if ( features.size() - previousFeatureCount > 0 || obstacleCount > previousObstacleCount )
347 {
348 layersWithFeaturesInBBox << layer->name();
349 }
350 previousFeatureCount = features.size();
351 previousObstacleCount = obstacleCount;
352
353 if ( feedback )
354 feedback->emit candidateCreationFinished( it->first );
355 }
356
357 candidateProfile.reset();
358
359 palLocker.unlock();
360
361 if ( isCanceled() )
362 return nullptr;
363
364 prob->mLayerCount = layersWithFeaturesInBBox.size();
365 prob->labelledLayersName = layersWithFeaturesInBBox;
366
367 prob->mFeatureCount = features.size();
368 prob->mTotalCandidates = 0;
369 prob->mCandidateCountForFeature.resize( prob->mFeatureCount );
370 prob->mFirstCandidateIndexForFeature.resize( prob->mFeatureCount );
371 prob->mUnlabeledCostForFeature.resize( prob->mFeatureCount );
372
373 if ( !features.empty() )
374 {
375 if ( feedback )
376 feedback->emit obstacleCostingAboutToBegin();
377
378 std::unique_ptr< QgsScopedRuntimeProfile > costingProfile;
379 if ( context.flags() & Qgis::RenderContextFlag::RecordProfile )
380 {
381 costingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Assigning label costs" ), u"rendering"_s );
382 }
383
384 // allow rules to alter candidate costs
385 for ( const auto &feature : features )
386 {
387 for ( auto &candidate : feature->candidates )
388 {
389 for ( QgsAbstractLabelingEngineRule *rule : std::as_const( mRules ) )
390 {
391 rule->alterCandidateCost( candidate.get(), labelContext );
392 }
393 }
394 }
395
396 // Filtering label positions against obstacles
397 index = -1;
398 step = !allObstacleParts.empty() ? 100.0 / allObstacleParts.size() : 1;
399
400 for ( FeaturePart *obstaclePart : allObstacleParts )
401 {
402 index++;
403 if ( feedback )
404 feedback->setProgress( step * index );
405
406 if ( isCanceled() )
407 break; // do not continue searching
408
409 allCandidatesFirstRound.intersects( obstaclePart->boundingBox(), [obstaclePart, this]( const LabelPosition *candidatePosition ) -> bool {
410 // test whether we should ignore this obstacle for the candidate. We do this if:
411 // 1. it's not a hole, and the obstacle belongs to the same label feature as the candidate (e.g.,
412 // features aren't obstacles for their own labels)
413 // 2. it IS a hole, and the hole belongs to a different label feature to the candidate (e.g., holes
414 // are ONLY obstacles for the labels of the feature they belong to)
415 // 3. The label is set to "Always Allow" overlap mode
416 if ( candidatePosition->getFeaturePart()->feature()->overlapHandling() == Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
417 || ( !obstaclePart->getHoleOf() && candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( obstaclePart ) )
418 || ( obstaclePart->getHoleOf() && !candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( dynamic_cast< FeaturePart * >( obstaclePart->getHoleOf() ) ) ) )
419 {
420 return true;
421 }
422
423 CostCalculator::addObstacleCostPenalty( const_cast< LabelPosition * >( candidatePosition ), obstaclePart, this );
424 return true;
425 } );
426 }
427
428 if ( feedback )
429 feedback->emit obstacleCostingFinished();
430 costingProfile.reset();
431
432 if ( isCanceled() )
433 {
434 return nullptr;
435 }
436
437 step = prob->mFeatureCount != 0 ? 100.0 / prob->mFeatureCount : 1;
438 if ( feedback )
439 feedback->emit calculatingConflictsAboutToBegin();
440
441 std::unique_ptr< QgsScopedRuntimeProfile > conflictProfile;
442 if ( context.flags() & Qgis::RenderContextFlag::RecordProfile )
443 {
444 conflictProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Calculating conflicts" ), u"rendering"_s );
445 }
446
447 int currentLabelPositionIndex = 0;
448 // loop through all the features registered in the problem
449 for ( std::size_t featureIndex = 0; featureIndex < prob->mFeatureCount; featureIndex++ )
450 {
451 if ( feedback )
452 feedback->setProgress( static_cast< double >( featureIndex ) * step );
453
454 std::unique_ptr< Feats > feat = std::move( features.front() );
455 features.pop_front();
456
457 prob->mFirstCandidateIndexForFeature[featureIndex] = currentLabelPositionIndex;
458 prob->mUnlabeledCostForFeature[featureIndex] = std::pow( 2, 10 - 10 * feat->priority );
459
460 std::size_t maxCandidates = 0;
461 switch ( feat->feature->getGeosType() )
462 {
463 case GEOS_POINT:
464 // this is usually 0, i.e. no maximum
465 maxCandidates = feat->feature->maximumPointCandidates();
466 break;
467
468 case GEOS_LINESTRING:
469 maxCandidates = feat->feature->maximumLineCandidates();
470 break;
471
472 case GEOS_POLYGON:
473 maxCandidates = std::max( static_cast< std::size_t >( 16 ), feat->feature->maximumPolygonCandidates() );
474 break;
475 }
476
477 if ( isCanceled() )
478 return nullptr;
479
480 auto pruneHardConflicts = [&] {
481 switch ( mPlacementVersion )
482 {
484 break;
485
487 {
488 // v2 placement rips out candidates where the candidate cost is too high when compared to
489 // their inactive cost
490
491 // note, we start this at the SECOND candidate (you'll see why after this loop)
492 feat->candidates.erase(
493 std::remove_if(
494 feat->candidates.begin() + 1,
495 feat->candidates.end(),
496 [&]( std::unique_ptr< LabelPosition > &candidate ) {
497 if ( candidate->hasHardObstacleConflict() )
498 {
499 return true;
500 }
501 return false;
502 }
503 ),
504 feat->candidates.end()
505 );
506
507 if ( feat->candidates.size() == 1 && feat->candidates[0]->hasHardObstacleConflict() )
508 {
509 switch ( feat->feature->feature()->overlapHandling() )
510 {
512 {
513 // we're going to end up removing ALL candidates for this label. Oh well, that's allowed. We just need to
514 // make sure we move this last candidate to the unplaced labels list
515 prob->positionsWithNoCandidates()->emplace_back( std::move( feat->candidates.front() ) );
516 feat->candidates.clear();
517 break;
518 }
519
522 // we can't avoid overlaps for this label, but in this mode we are allowing overlaps as a last resort.
523 // => don't discard this last remaining candidate.
524 break;
525 }
526 }
527 }
528 }
529 };
530
531 // if we're not showing all labels (including conflicts) for this layer, then we prune the candidates
532 // upfront to avoid extra work...
533 switch ( feat->feature->feature()->overlapHandling() )
534 {
536 pruneHardConflicts();
537 break;
538
541 break;
542 }
543
544 if ( feat->candidates.empty() )
545 continue;
546
547 // calculate final costs
548 CostCalculator::finalizeCandidatesCosts( feat.get(), bbx, bby );
549
550 // sort candidates list, best label to worst
551 std::sort( feat->candidates.begin(), feat->candidates.end(), CostCalculator::candidateSortGrow );
552
553 // but if we ARE showing all labels (including conflicts), let's go ahead and prune them now.
554 // Since we've calculated all their costs and sorted them, if we've hit the situation that ALL
555 // candidates have conflicts, then at least when we pick the first candidate to display it will be
556 // the lowest cost (i.e. best possible) overlapping candidate...
557 switch ( feat->feature->feature()->overlapHandling() )
558 {
560 break;
563 pruneHardConflicts();
564 break;
565 }
566
567 // only keep the 'maxCandidates' best candidates
568 if ( maxCandidates > 0 && feat->candidates.size() > maxCandidates )
569 {
570 feat->candidates.resize( maxCandidates );
571 }
572
573 if ( isCanceled() )
574 return nullptr;
575
576 // update problem's # candidate
577 prob->mCandidateCountForFeature[featureIndex] = static_cast< int >( feat->candidates.size() );
578 prob->mTotalCandidates += static_cast< int >( feat->candidates.size() );
579
580 // add all candidates into a rtree (to speed up conflicts searching)
581 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
582 {
583 candidate->insertIntoIndex( prob->allCandidatesIndex(), this );
584 candidate->setProblemIds( static_cast< int >( featureIndex ), currentLabelPositionIndex++ );
585 }
586 features.emplace_back( std::move( feat ) );
587 }
588
589 if ( feedback )
590 feedback->emit calculatingConflictsFinished();
591
592 conflictProfile.reset();
593
594 int nbOverlaps = 0;
595
596 if ( feedback )
597 feedback->emit finalizingCandidatesAboutToBegin();
598
599 std::unique_ptr< QgsScopedRuntimeProfile > finalizingProfile;
600 if ( context.flags() & Qgis::RenderContextFlag::RecordProfile )
601 {
602 finalizingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Finalizing labels" ), u"rendering"_s );
603 }
604
605 index = -1;
606 step = !features.empty() ? 100.0 / features.size() : 1;
607 while ( !features.empty() ) // for each feature
608 {
609 index++;
610 if ( feedback )
611 feedback->setProgress( step * index );
612
613 if ( isCanceled() )
614 return nullptr;
615
616 std::unique_ptr< Feats > feat = std::move( features.front() );
617 features.pop_front();
618
619 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
620 {
621 std::unique_ptr< LabelPosition > lp = std::move( candidate );
622
623 lp->resetNumOverlaps();
624
625 // make sure that candidate's cost is less than 1
626 lp->validateCost();
627
628 //prob->feat[idlp] = j;
629
630 // lookup for overlapping candidate
631 const QgsRectangle searchBounds = lp->boundingBoxForCandidateConflicts( this );
632 prob->allCandidatesIndex().intersects( searchBounds, [&lp, this]( const LabelPosition *lp2 ) -> bool {
633 if ( candidatesAreConflicting( lp.get(), lp2 ) )
634 {
635 lp->incrementNumOverlaps();
636 }
637
638 return true;
639 } );
640
641 nbOverlaps += lp->getNumOverlaps();
642
643 prob->addCandidatePosition( std::move( lp ) );
644
645 if ( isCanceled() )
646 return nullptr;
647 }
648 }
649
650 if ( feedback )
651 feedback->emit finalizingCandidatesFinished();
652
653 finalizingProfile.reset();
654
655 nbOverlaps /= 2;
656 prob->mAllNblp = prob->mTotalCandidates;
657 prob->mNbOverlap = nbOverlaps;
658 }
659
660 return prob;
661}
662
664{
665 fnIsCanceled = fnCanceled;
666 fnIsCanceledContext = context;
667}
668
669
670QList<LabelPosition *> Pal::solveProblem( Problem *prob, QgsRenderContext &context, bool displayAll, QList<LabelPosition *> *unlabeled )
671{
672 QgsLabelingEngineFeedback *feedback = qobject_cast< QgsLabelingEngineFeedback * >( context.feedback() );
673
674 if ( !prob )
675 return QList<LabelPosition *>();
676
677 std::unique_ptr< QgsScopedRuntimeProfile > calculatingProfile;
679 {
680 calculatingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Calculating optimal labeling" ), u"rendering"_s );
681 }
682
683 if ( feedback )
684 feedback->emit reductionAboutToBegin();
685
686 {
687 std::unique_ptr< QgsScopedRuntimeProfile > reductionProfile;
689 {
690 reductionProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Reducing labeling" ), u"rendering"_s );
691 }
692
693 prob->reduce();
694 }
695
696 if ( feedback )
697 feedback->emit reductionFinished();
698
699 if ( feedback )
700 feedback->emit solvingPlacementAboutToBegin();
701
702 {
703 std::unique_ptr< QgsScopedRuntimeProfile > solvingProfile;
705 {
706 solvingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr( "Solving labeling" ), u"rendering"_s );
707 }
708 try
709 {
710 prob->chainSearch( context );
711 }
712 catch ( InternalException::Empty & )
713 {
714 return QList<LabelPosition *>();
715 }
716 }
717
718 if ( feedback )
719 feedback->emit solvingPlacementFinished();
720
721 return prob->getSolution( displayAll, unlabeled );
722}
723
724void Pal::setMinIt( int min_it )
725{
726 if ( min_it >= 0 )
727 mTabuMinIt = min_it;
728}
729
730void Pal::setMaxIt( int max_it )
731{
732 if ( max_it > 0 )
733 mTabuMaxIt = max_it;
734}
735
736void Pal::setPopmusicR( int r )
737{
738 if ( r > 0 )
739 mPopmusicR = r;
740}
741
742void Pal::setEjChainDeg( int degree )
743{
744 this->mEjChainDeg = degree;
745}
746
747void Pal::setTenure( int tenure )
748{
749 this->mTenure = tenure;
750}
751
752void Pal::setCandListSize( double fact )
753{
754 this->mCandListSize = fact;
755}
756
758{
759 this->mShowPartialLabels = show;
760}
761
763{
764 return mPlacementVersion;
765}
766
771
773{
774 // we cache the value -- this can be costly to calculate, and we check this multiple times
775 // per candidate during the labeling problem solving
776
777 if ( lp1->getProblemFeatureId() == lp2->getProblemFeatureId() )
778 return false;
779
780 // conflicts are commutative - so we always store them in the cache using the smaller id as the first element of the key pair
781 auto key = qMakePair( std::min( lp1->globalId(), lp2->globalId() ), std::max( lp1->globalId(), lp2->globalId() ) );
782 auto it = mCandidateConflicts.constFind( key );
783 if ( it != mCandidateConflicts.constEnd() )
784 return *it;
785
786 bool res = false;
787
788 const double labelMarginDistance = std::max( lp1->getFeaturePart()->feature()->thinningSettings().labelMarginDistance(), lp2->getFeaturePart()->feature()->thinningSettings().labelMarginDistance() );
789
790 if ( labelMarginDistance > 0 )
791 {
792 GEOSContextHandle_t geosctxt = QgsGeosContext::get();
793 try
794 {
795#if GEOS_VERSION_MAJOR > 3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 10 )
796 if ( GEOSPreparedDistanceWithin_r( geosctxt, lp1->preparedMultiPartGeom(), lp2->multiPartGeom(), labelMarginDistance ) )
797 {
798 res = true;
799 }
800#else
801 QgsMessageLog::logMessage( u"label margin distance requires GEOS 3.10+"_s );
802#endif
803 }
804 catch ( QgsGeosException &e )
805 {
806 QgsDebugError( u"GEOS exception: %1"_s.arg( e.what() ) );
807 }
808 }
809
810 if ( !res )
811 {
812 for ( QgsAbstractLabelingEngineRule *rule : mRules )
813 {
814 if ( rule->candidatesAreConflicting( lp1, lp2 ) )
815 {
816 res = true;
817 break;
818 }
819 }
820 }
821
822 res |= lp1->isInConflict( lp2 );
823
824 mCandidateConflicts.insert( key, res );
825 return res;
826}
827
828void Pal::setRules( const QList<QgsAbstractLabelingEngineRule *> &rules )
829{
830 mRules = rules;
831}
832
833int Pal::getMinIt() const
834{
835 return mTabuMaxIt;
836}
837
838int Pal::getMaxIt() const
839{
840 return mTabuMinIt;
841}
842
844{
845 return mShowPartialLabels;
846}
A rtree spatial index for use in the pal labeling engine.
Definition palrtree.h:37
void insert(T *data, const QgsRectangle &bounds)
Inserts new data into the spatial index, with the specified bounds.
Definition palrtree.h:57
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1232
@ RecordProfile
Enable run-time profiling while rendering.
Definition qgis.h:2865
LabelPlacementEngineVersion
Labeling placement engine version.
Definition qgis.h:2973
@ Version2
Version 2 (default for new projects since QGIS 3.12).
Definition qgis.h:2975
@ Version1
Version 1, matches placement from QGIS <= 3.10.1.
Definition qgis.h:2974
@ AllowOverlapAtNoCost
Labels may freely overlap other labels, at no cost.
Definition qgis.h:1196
@ AllowOverlapIfRequired
Avoids overlapping labels when possible, but permit overlaps if labels for features cannot otherwise ...
Definition qgis.h:1195
@ PreventOverlap
Do not allow labels to overlap other labels.
Definition qgis.h:1194
An abstract interface class for label providers.
Abstract base class for labeling engine rules.
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:65
A geometry is the spatial representation of a feature.
static GEOSContextHandle_t get()
Returns a thread local instance of a GEOS context, safe for use in the current thread.
static geos::unique_ptr asGeos(const QgsGeometry &geometry, double precision=0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlags())
Returns a geos geometry - caller takes ownership of the object (should be deleted with GEOSGeom_destr...
Definition qgsgeos.cpp:254
double labelMarginDistance() const
Returns the minimum distance (in label units) between labels for this feature and other labels.
const QgsLabelFeatureThinningSettings & thinningSettings() const
Returns the thinning settings for this label.
Encapsulates the context for a labeling engine run.
void setMapBoundaryGeometry(const QgsGeometry &geometry)
Sets the map label boundary geometry, which defines the limits within which labels may be placed in t...
void setExtent(const QgsRectangle &extent)
Sets the map extent defining the limits for labeling.
QgsFeedback subclass for granular reporting of labeling engine progress.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
Adds a message to the log instance (and creates it if necessary).
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
QgsRectangle buffered(double width) const
Gets rectangle enlarged by buffer.
Contains information about the context of a rendering operation.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Qgis::RenderContextFlags flags() const
Returns combination of flags used for rendering.
An integer settings entry.
static void addObstacleCostPenalty(pal::LabelPosition *lp, pal::FeaturePart *obstacle, Pal *pal)
Increase candidate's cost according to its collision with passed feature.
static void finalizeCandidatesCosts(Feats *feat, double bbx[4], double bby[4])
Sort candidates by costs, skip the worse ones, evaluate polygon candidates.
static bool candidateSortGrow(const std::unique_ptr< pal::LabelPosition > &c1, const std::unique_ptr< pal::LabelPosition > &c2)
Sorts label candidates in ascending order of cost.
Represents a part of a label feature.
Definition feature.h:60
QgsLabelFeature * feature()
Returns the parent feature.
Definition feature.h:87
Thrown when trying to access an empty data set.
LabelPosition is a candidate feature label position.
bool isInConflict(const LabelPosition *ls) const
Check whether or not this overlap with another labelPosition.
const GEOSGeometry * multiPartGeom() const
Returns a GEOS representation of all label parts as a multipolygon.
unsigned int globalId() const
Returns the global ID for the candidate, which is unique for a single run of the pal labelling engine...
FeaturePart * getFeaturePart() const
Returns the feature corresponding to this labelposition.
const GEOSPreparedGeometry * preparedMultiPartGeom() const
Returns a prepared GEOS representation of all label parts as a multipolygon.
int getProblemFeatureId() const
QMutex mMutex
Definition layer.h:344
std::deque< std::unique_ptr< FeaturePart > > mFeatureParts
List of feature parts.
Definition layer.h:315
bool active() const
Returns whether the layer is currently active.
Definition layer.h:197
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition layer.h:255
void joinConnectedFeatures()
Join connected features with the same label text.
Definition layer.cpp:308
void chopFeaturesAtRepeatDistance()
Chop layer features at the repeat distance.
Definition layer.cpp:379
void setPlacementVersion(Qgis::LabelPlacementEngineVersion placementVersion)
Sets the placement engine version, which dictates how the label placement problem is solved.
Definition pal.cpp:767
void setShowPartialLabels(bool show)
Sets whether partial labels show be allowed.
Definition pal.cpp:757
std::unique_ptr< Problem > extractProblem(const QgsRectangle &extent, const QgsGeometry &mapBoundary, QgsRenderContext &context)
Extracts the labeling problem for the specified map extent - only features within this extent will be...
Definition pal.cpp:114
Qgis::LabelPlacementEngineVersion placementVersion() const
Returns the placement engine version, which dictates how the label placement problem is solved.
Definition pal.cpp:762
void setRules(const QList< QgsAbstractLabelingEngineRule * > &rules)
Sets rules which the labeling solution must satisfy.
Definition pal.cpp:828
void removeLayer(Layer *layer)
remove a layer
Definition pal.cpp:76
bool candidatesAreConflicting(const LabelPosition *lp1, const LabelPosition *lp2) const
Returns true if a labelling candidate lp1 conflicts with lp2.
Definition pal.cpp:772
friend class Layer
Definition pal.h:90
bool(* FnIsCanceled)(void *ctx)
Cancellation check callback function.
Definition pal.h:127
bool showPartialLabels() const
Returns whether partial labels should be allowed.
Definition pal.cpp:843
static const QgsSettingsEntryInteger * settingsRenderingLabelCandidatesLimitLines
Definition pal.h:96
static const QgsSettingsEntryInteger * settingsRenderingLabelCandidatesLimitPoints
Definition pal.h:95
static const QgsSettingsEntryInteger * settingsRenderingLabelCandidatesLimitPolygons
Definition pal.h:97
Pal()
Definition pal.cpp:67
friend class Problem
Definition pal.h:88
bool isCanceled()
Check whether the job has been canceled.
Definition pal.h:133
QList< LabelPosition * > solveProblem(Problem *prob, QgsRenderContext &context, bool displayAll, QList< pal::LabelPosition * > *unlabeled=nullptr)
Solves the labeling problem, selecting the best candidate locations for all labels and returns a list...
Definition pal.cpp:670
friend class FeaturePart
Definition pal.h:89
void registerCancellationCallback(FnIsCanceled fnCanceled, void *context)
Register a function that returns whether this job has been canceled - PAL calls it during the computa...
Definition pal.cpp:663
QList< QgsAbstractLabelingEngineRule * > rules() const
Returns the rules which the labeling solution must satisfy.
Definition pal.h:283
Layer * addLayer(QgsAbstractLabelProvider *provider, const QString &layerName, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel)
add a new layer
Definition pal.cpp:94
QgsRectangle boundingBox() const
Returns the point set bounding box.
Definition pointset.h:162
QList< LabelPosition * > getSolution(bool returnInactive, QList< LabelPosition * > *unlabeled=nullptr)
Solves the labeling problem, selecting the best candidate locations for all labels and returns a list...
Definition problem.cpp:609
void chainSearch(QgsRenderContext &context)
Test with very-large scale neighborhood.
Definition problem.cpp:535
void reduce()
Gets called AFTER extractProblem.
Definition problem.cpp:58
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition qgsgeos.h:112
std::unique_ptr< const GEOSPreparedGeometry, GeosDeleter > prepared_unique_ptr
Scoped GEOS prepared geometry pointer.
Definition qgsgeos.h:117
#define QgsDebugError(str)
Definition qgslogger.h:59