121 std::unique_ptr< QgsScopedRuntimeProfile > extractionProfile;
124 extractionProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Placing labels" ), u
"rendering"_s );
135 std::vector< FeaturePart * > allObstacleParts;
136 auto prob = std::make_unique< Problem >( maxCoordinateExtentForSpatialIndices );
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();
148 std::list< std::unique_ptr< Feats > > features;
154 int obstacleCount = 0;
158 std::size_t previousFeatureCount = 0;
159 int previousObstacleCount = 0;
161 QStringList layersWithFeaturesInBBox;
163 QMutexLocker palLocker( &mMutex );
165 double step = !mLayers.empty() ? 100.0 / mLayers.size() : 1;
167 std::unique_ptr< QgsScopedRuntimeProfile > candidateProfile;
170 candidateProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Generating label candidates" ), u
"rendering"_s );
173 for (
auto it = mLayers.rbegin(); it != mLayers.rend(); ++it )
179 Layer *layer = it->second.get();
191 feedback->emit candidateCreationAboutToBegin( it->first );
193 std::unique_ptr< QgsScopedRuntimeProfile > layerProfile;
196 layerProfile = std::make_unique< QgsScopedRuntimeProfile >( it->first->providerId(), u
"rendering"_s );
211 QMutexLocker locker( &layer->
mMutex );
214 std::size_t featureIndex = 0;
216 for (
const std::unique_ptr< FeaturePart > &featurePart : std::as_const( layer->
mFeatureParts ) )
219 feedback->
setProgress( index * step + featureIndex * featureStep );
226 for (
int i = 0; i < featurePart->getNumSelfObstacles(); i++ )
228 FeaturePart *selfObstacle = featurePart->getSelfObstacle( i );
230 allObstacleParts.emplace_back( selfObstacle );
232 if ( !featurePart->getSelfObstacle( i )->getHoleOf() )
239 std::vector< std::unique_ptr< LabelPosition > > candidates = featurePart->createCandidates(
this );
251 [&mapBoundaryPrepared, &labelContext,
this]( std::unique_ptr< LabelPosition > &candidate ) {
252 if ( showPartialLabels() )
254 if ( !candidate->intersects( mapBoundaryPrepared.get() ) )
259 if ( !candidate->within( mapBoundaryPrepared.get() ) )
265 if ( rule->candidateIsIllegal( candidate.get(), labelContext ) )
279 if ( !candidates.empty() )
281 for ( std::unique_ptr< LabelPosition > &candidate : candidates )
283 candidate->insertIntoIndex( allCandidatesFirstRound,
this );
284 candidate->setGlobalId( mNextCandidateId++ );
290 auto ft = std::make_unique< Feats >();
291 ft->feature = featurePart.get();
293 ft->candidates = std::move( candidates );
294 ft->priority = featurePart->calculatePriority();
295 features.emplace_back( std::move( ft ) );
300 std::unique_ptr< LabelPosition > unplacedPosition = featurePart->createCandidatePointOnSurface( featurePart.get() );
301 if ( !unplacedPosition )
304 if ( featurePart->feature()->allowDegradedPlacement() )
307 unplacedPosition->insertIntoIndex( allCandidatesFirstRound,
this );
308 unplacedPosition->setGlobalId( mNextCandidateId++ );
309 candidates.emplace_back( std::move( unplacedPosition ) );
312 auto ft = std::make_unique< Feats >();
313 ft->feature = featurePart.get();
315 ft->candidates = std::move( candidates );
316 ft->priority = featurePart->calculatePriority();
317 features.emplace_back( std::move( ft ) );
322 prob->positionsWithNoCandidates()->emplace_back( std::move( unplacedPosition ) );
330 for (
FeaturePart *obstaclePart : std::as_const( layer->mObstacleParts ) )
336 obstacles.
insert( obstaclePart, obstaclePart->boundingBox() );
337 allObstacleParts.emplace_back( obstaclePart );
346 if ( features.size() - previousFeatureCount > 0 || obstacleCount > previousObstacleCount )
348 layersWithFeaturesInBBox << layer->name();
350 previousFeatureCount = features.size();
351 previousObstacleCount = obstacleCount;
354 feedback->emit candidateCreationFinished( it->first );
357 candidateProfile.reset();
364 prob->mLayerCount = layersWithFeaturesInBBox.size();
365 prob->labelledLayersName = layersWithFeaturesInBBox;
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 );
373 if ( !features.empty() )
376 feedback->emit obstacleCostingAboutToBegin();
378 std::unique_ptr< QgsScopedRuntimeProfile > costingProfile;
381 costingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Assigning label costs" ), u
"rendering"_s );
385 for (
const auto &feature : features )
387 for (
auto &candidate : feature->candidates )
391 rule->alterCandidateCost( candidate.get(), labelContext );
398 step = !allObstacleParts.empty() ? 100.0 / allObstacleParts.size() : 1;
400 for (
FeaturePart *obstaclePart : allObstacleParts )
404 feedback->setProgress( step * index );
409 allCandidatesFirstRound.intersects( obstaclePart->boundingBox(), [obstaclePart,
this](
const LabelPosition *candidatePosition ) ->
bool {
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() ) ) ) )
429 feedback->emit obstacleCostingFinished();
430 costingProfile.reset();
437 step = prob->mFeatureCount != 0 ? 100.0 / prob->mFeatureCount : 1;
439 feedback->emit calculatingConflictsAboutToBegin();
441 std::unique_ptr< QgsScopedRuntimeProfile > conflictProfile;
444 conflictProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Calculating conflicts" ), u
"rendering"_s );
447 int currentLabelPositionIndex = 0;
449 for ( std::size_t featureIndex = 0; featureIndex < prob->mFeatureCount; featureIndex++ )
452 feedback->setProgress(
static_cast< double >( featureIndex ) * step );
454 std::unique_ptr< Feats > feat = std::move( features.front() );
455 features.pop_front();
457 prob->mFirstCandidateIndexForFeature[featureIndex] = currentLabelPositionIndex;
458 prob->mUnlabeledCostForFeature[featureIndex] = std::pow( 2, 10 - 10 * feat->priority );
460 std::size_t maxCandidates = 0;
461 switch ( feat->feature->getGeosType() )
465 maxCandidates = feat->feature->maximumPointCandidates();
468 case GEOS_LINESTRING:
469 maxCandidates = feat->feature->maximumLineCandidates();
473 maxCandidates = std::max(
static_cast< std::size_t
>( 16 ), feat->feature->maximumPolygonCandidates() );
480 auto pruneHardConflicts = [&] {
481 switch ( mPlacementVersion )
492 feat->candidates.erase(
494 feat->candidates.begin() + 1,
495 feat->candidates.end(),
496 [&]( std::unique_ptr< LabelPosition > &candidate ) {
497 if ( candidate->hasHardObstacleConflict() )
504 feat->candidates.end()
507 if ( feat->candidates.size() == 1 && feat->candidates[0]->hasHardObstacleConflict() )
509 switch ( feat->feature->feature()->overlapHandling() )
515 prob->positionsWithNoCandidates()->emplace_back( std::move( feat->candidates.front() ) );
516 feat->candidates.clear();
533 switch ( feat->feature->feature()->overlapHandling() )
536 pruneHardConflicts();
544 if ( feat->candidates.empty() )
557 switch ( feat->feature->feature()->overlapHandling() )
563 pruneHardConflicts();
568 if ( maxCandidates > 0 && feat->candidates.size() > maxCandidates )
570 feat->candidates.resize( maxCandidates );
577 prob->mCandidateCountForFeature[featureIndex] =
static_cast< int >( feat->candidates.size() );
578 prob->mTotalCandidates +=
static_cast< int >( feat->candidates.size() );
581 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
583 candidate->insertIntoIndex( prob->allCandidatesIndex(),
this );
584 candidate->setProblemIds(
static_cast< int >( featureIndex ), currentLabelPositionIndex++ );
586 features.emplace_back( std::move( feat ) );
590 feedback->emit calculatingConflictsFinished();
592 conflictProfile.reset();
597 feedback->emit finalizingCandidatesAboutToBegin();
599 std::unique_ptr< QgsScopedRuntimeProfile > finalizingProfile;
602 finalizingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Finalizing labels" ), u
"rendering"_s );
606 step = !features.empty() ? 100.0 / features.size() : 1;
607 while ( !features.empty() )
611 feedback->setProgress( step * index );
616 std::unique_ptr< Feats > feat = std::move( features.front() );
617 features.pop_front();
619 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
621 std::unique_ptr< LabelPosition > lp = std::move( candidate );
623 lp->resetNumOverlaps();
631 const QgsRectangle searchBounds = lp->boundingBoxForCandidateConflicts(
this );
632 prob->allCandidatesIndex().intersects( searchBounds, [&lp,
this](
const LabelPosition *lp2 ) ->
bool {
633 if ( candidatesAreConflicting( lp.get(), lp2 ) )
635 lp->incrementNumOverlaps();
641 nbOverlaps += lp->getNumOverlaps();
643 prob->addCandidatePosition( std::move( lp ) );
651 feedback->emit finalizingCandidatesFinished();
653 finalizingProfile.reset();
656 prob->mAllNblp = prob->mTotalCandidates;
657 prob->mNbOverlap = nbOverlaps;