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 );
247 candidates.erase( std::remove_if( candidates.begin(), candidates.end(), [&mapBoundaryPrepared, &labelContext,
this]( std::unique_ptr< LabelPosition > &candidate )
249 if ( showPartialLabels() )
251 if ( !candidate->intersects( mapBoundaryPrepared.get() ) )
256 if ( !candidate->within( mapBoundaryPrepared.get() ) )
262 if ( rule->candidateIsIllegal( candidate.get(), labelContext ) )
268 } ), candidates.end() );
273 if ( !candidates.empty() )
275 for ( std::unique_ptr< LabelPosition > &candidate : candidates )
277 candidate->insertIntoIndex( allCandidatesFirstRound,
this );
278 candidate->setGlobalId( mNextCandidateId++ );
284 auto ft = std::make_unique< Feats >();
285 ft->feature = featurePart.get();
287 ft->candidates = std::move( candidates );
288 ft->priority = featurePart->calculatePriority();
289 features.emplace_back( std::move( ft ) );
294 std::unique_ptr< LabelPosition > unplacedPosition = featurePart->createCandidatePointOnSurface( featurePart.get() );
295 if ( !unplacedPosition )
298 if ( featurePart->feature()->allowDegradedPlacement() )
301 unplacedPosition->insertIntoIndex( allCandidatesFirstRound,
this );
302 unplacedPosition->setGlobalId( mNextCandidateId++ );
303 candidates.emplace_back( std::move( unplacedPosition ) );
306 auto ft = std::make_unique< Feats >();
307 ft->feature = featurePart.get();
309 ft->candidates = std::move( candidates );
310 ft->priority = featurePart->calculatePriority();
311 features.emplace_back( std::move( ft ) );
316 prob->positionsWithNoCandidates()->emplace_back( std::move( unplacedPosition ) );
324 for (
FeaturePart *obstaclePart : std::as_const( layer->mObstacleParts ) )
330 obstacles.
insert( obstaclePart, obstaclePart->boundingBox() );
331 allObstacleParts.emplace_back( obstaclePart );
340 if ( features.size() - previousFeatureCount > 0 || obstacleCount > previousObstacleCount )
342 layersWithFeaturesInBBox << layer->name();
344 previousFeatureCount = features.size();
345 previousObstacleCount = obstacleCount;
348 feedback->emit candidateCreationFinished( it->first );
351 candidateProfile.reset();
358 prob->mLayerCount = layersWithFeaturesInBBox.size();
359 prob->labelledLayersName = layersWithFeaturesInBBox;
361 prob->mFeatureCount = features.size();
362 prob->mTotalCandidates = 0;
363 prob->mCandidateCountForFeature.resize( prob->mFeatureCount );
364 prob->mFirstCandidateIndexForFeature.resize( prob->mFeatureCount );
365 prob->mUnlabeledCostForFeature.resize( prob->mFeatureCount );
367 if ( !features.empty() )
370 feedback->emit obstacleCostingAboutToBegin();
372 std::unique_ptr< QgsScopedRuntimeProfile > costingProfile;
375 costingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Assigning label costs" ), u
"rendering"_s );
379 for (
const auto &feature : features )
381 for (
auto &candidate : feature->candidates )
385 rule->alterCandidateCost( candidate.get(), labelContext );
392 step = !allObstacleParts.empty() ? 100.0 / allObstacleParts.size() : 1;
394 for (
FeaturePart *obstaclePart : allObstacleParts )
398 feedback->setProgress( step * index );
403 allCandidatesFirstRound.intersects( obstaclePart->boundingBox(), [obstaclePart,
this](
const LabelPosition * candidatePosition ) ->
bool
411 if ( candidatePosition->getFeaturePart()->feature()->overlapHandling() == Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
412 || ( !obstaclePart->getHoleOf() && candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( obstaclePart ) )
413 || ( obstaclePart->getHoleOf() && !candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( dynamic_cast< FeaturePart * >( obstaclePart->getHoleOf() ) ) ) )
424 feedback->emit obstacleCostingFinished();
425 costingProfile.reset();
432 step = prob->mFeatureCount != 0 ? 100.0 / prob->mFeatureCount : 1;
434 feedback->emit calculatingConflictsAboutToBegin();
436 std::unique_ptr< QgsScopedRuntimeProfile > conflictProfile;
439 conflictProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Calculating conflicts" ), u
"rendering"_s );
442 int currentLabelPositionIndex = 0;
444 for ( std::size_t featureIndex = 0; featureIndex < prob->mFeatureCount; featureIndex++ )
447 feedback->setProgress(
static_cast< double >( featureIndex ) * step );
449 std::unique_ptr< Feats > feat = std::move( features.front() );
450 features.pop_front();
452 prob->mFirstCandidateIndexForFeature[featureIndex] = currentLabelPositionIndex;
453 prob->mUnlabeledCostForFeature[featureIndex] = std::pow( 2, 10 - 10 * feat->priority );
455 std::size_t maxCandidates = 0;
456 switch ( feat->feature->getGeosType() )
460 maxCandidates = feat->feature->maximumPointCandidates();
463 case GEOS_LINESTRING:
464 maxCandidates = feat->feature->maximumLineCandidates();
468 maxCandidates = std::max(
static_cast< std::size_t
>( 16 ), feat->feature->maximumPolygonCandidates() );
475 auto pruneHardConflicts = [&]
477 switch ( mPlacementVersion )
488 feat->candidates.erase( std::remove_if( feat->candidates.begin() + 1, feat->candidates.end(), [ & ]( std::unique_ptr< LabelPosition > &candidate )
490 if ( candidate->hasHardObstacleConflict() )
495 } ), feat->candidates.end() );
497 if ( feat->candidates.size() == 1 && feat->candidates[ 0 ]->hasHardObstacleConflict() )
499 switch ( feat->feature->feature()->overlapHandling() )
505 prob->positionsWithNoCandidates()->emplace_back( std::move( feat->candidates.front() ) );
506 feat->candidates.clear();
523 switch ( feat->feature->feature()->overlapHandling() )
526 pruneHardConflicts();
534 if ( feat->candidates.empty() )
547 switch ( feat->feature->feature()->overlapHandling() )
553 pruneHardConflicts();
558 if ( maxCandidates > 0 && feat->candidates.size() > maxCandidates )
560 feat->candidates.resize( maxCandidates );
567 prob->mCandidateCountForFeature[featureIndex] =
static_cast< int >( feat->candidates.size() );
568 prob->mTotalCandidates +=
static_cast< int >( feat->candidates.size() );
571 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
573 candidate->insertIntoIndex( prob->allCandidatesIndex(),
this );
574 candidate->setProblemIds(
static_cast< int >( featureIndex ), currentLabelPositionIndex++ );
576 features.emplace_back( std::move( feat ) );
580 feedback->emit calculatingConflictsFinished();
582 conflictProfile.reset();
587 feedback->emit finalizingCandidatesAboutToBegin();
589 std::unique_ptr< QgsScopedRuntimeProfile > finalizingProfile;
592 finalizingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Finalizing labels" ), u
"rendering"_s );
596 step = !features.empty() ? 100.0 / features.size() : 1;
597 while ( !features.empty() )
601 feedback->setProgress( step * index );
606 std::unique_ptr< Feats > feat = std::move( features.front() );
607 features.pop_front();
609 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
611 std::unique_ptr< LabelPosition > lp = std::move( candidate );
613 lp->resetNumOverlaps();
621 const QgsRectangle searchBounds = lp->boundingBoxForCandidateConflicts(
this );
622 prob->allCandidatesIndex().intersects( searchBounds, [&lp,
this](
const LabelPosition * lp2 )->
bool
624 if ( candidatesAreConflicting( lp.get(), lp2 ) )
626 lp->incrementNumOverlaps();
633 nbOverlaps += lp->getNumOverlaps();
635 prob->addCandidatePosition( std::move( lp ) );
643 feedback->emit finalizingCandidatesFinished();
645 finalizingProfile.reset();
648 prob->mAllNblp = prob->mTotalCandidates;
649 prob->mNbOverlap = nbOverlaps;