123 std::unique_ptr< QgsScopedRuntimeProfile > extractionProfile;
126 extractionProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Placing labels" ), u
"rendering"_s );
137 std::vector< FeaturePart * > allObstacleParts;
138 auto prob = std::make_unique< Problem >( maxCoordinateExtentForSpatialIndices );
143 bbx[0] = bbx[3] = prob->mMapExtentBounds[0] = extent.
xMinimum();
144 bby[0] = bby[1] = prob->mMapExtentBounds[1] = extent.
yMinimum();
145 bbx[1] = bbx[2] = prob->mMapExtentBounds[2] = extent.
xMaximum();
146 bby[2] = bby[3] = prob->mMapExtentBounds[3] = extent.
yMaximum();
150 std::list< std::unique_ptr< Feats > > features;
156 int obstacleCount = 0;
160 std::size_t previousFeatureCount = 0;
161 int previousObstacleCount = 0;
163 QStringList layersWithFeaturesInBBox;
165 QMutexLocker palLocker( &mMutex );
167 double step = !mLayers.empty() ? 100.0 / mLayers.size() : 1;
169 std::unique_ptr< QgsScopedRuntimeProfile > candidateProfile;
172 candidateProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Generating label candidates" ), u
"rendering"_s );
175 for (
auto it = mLayers.rbegin(); it != mLayers.rend(); ++it )
181 Layer *layer = it->second.get();
193 feedback->emit candidateCreationAboutToBegin( it->first );
195 std::unique_ptr< QgsScopedRuntimeProfile > layerProfile;
198 layerProfile = std::make_unique< QgsScopedRuntimeProfile >( it->first->providerId(), u
"rendering"_s );
213 QMutexLocker locker( &layer->
mMutex );
216 std::size_t featureIndex = 0;
218 for (
const std::unique_ptr< FeaturePart > &featurePart : std::as_const( layer->
mFeatureParts ) )
221 feedback->
setProgress( index * step + featureIndex * featureStep );
228 for (
int i = 0; i < featurePart->getNumSelfObstacles(); i++ )
230 FeaturePart *selfObstacle = featurePart->getSelfObstacle( i );
232 allObstacleParts.emplace_back( selfObstacle );
234 if ( !featurePart->getSelfObstacle( i )->getHoleOf() )
241 std::vector< std::unique_ptr< LabelPosition > > candidates = featurePart->createCandidates(
this );
253 [&mapBoundaryPrepared, &labelContext,
this]( std::unique_ptr< LabelPosition > &candidate ) {
254 if ( showPartialLabels() )
256 if ( !candidate->intersects( mapBoundaryPrepared.get() ) )
261 if ( !candidate->within( mapBoundaryPrepared.get() ) )
267 if ( rule->candidateIsIllegal( candidate.get(), labelContext ) )
281 if ( !candidates.empty() )
283 for ( std::unique_ptr< LabelPosition > &candidate : candidates )
285 candidate->insertIntoIndex( allCandidatesFirstRound,
this );
286 candidate->setGlobalId( mNextCandidateId++ );
292 auto ft = std::make_unique< Feats >();
293 ft->feature = featurePart.get();
295 ft->candidates = std::move( candidates );
296 ft->priority = featurePart->calculatePriority();
297 features.emplace_back( std::move( ft ) );
302 std::unique_ptr< LabelPosition > unplacedPosition = featurePart->createCandidatePointOnSurface( featurePart.get() );
303 if ( !unplacedPosition )
306 if ( featurePart->feature()->allowDegradedPlacement() )
309 unplacedPosition->insertIntoIndex( allCandidatesFirstRound,
this );
310 unplacedPosition->setGlobalId( mNextCandidateId++ );
311 candidates.emplace_back( std::move( unplacedPosition ) );
314 auto ft = std::make_unique< Feats >();
315 ft->feature = featurePart.get();
317 ft->candidates = std::move( candidates );
318 ft->priority = featurePart->calculatePriority();
319 features.emplace_back( std::move( ft ) );
324 prob->positionsWithNoCandidates()->emplace_back( std::move( unplacedPosition ) );
334 for (
FeaturePart *obstaclePart : std::as_const( layer->mObstacleParts ) )
340 obstacles.
insert( obstaclePart, obstaclePart->boundingBox() );
341 allObstacleParts.emplace_back( obstaclePart );
351 if ( features.size() - previousFeatureCount > 0 || obstacleCount > previousObstacleCount )
353 layersWithFeaturesInBBox << layer->name();
355 previousFeatureCount = features.size();
356 previousObstacleCount = obstacleCount;
359 feedback->emit candidateCreationFinished( it->first );
362 candidateProfile.reset();
369 prob->mLayerCount = layersWithFeaturesInBBox.size();
370 prob->labelledLayersName = layersWithFeaturesInBBox;
372 prob->mFeatureCount = features.size();
373 prob->mTotalCandidates = 0;
374 prob->mCandidateCountForFeature.resize( prob->mFeatureCount );
375 prob->mFirstCandidateIndexForFeature.resize( prob->mFeatureCount );
376 prob->mUnlabeledCostForFeature.resize( prob->mFeatureCount );
378 if ( !features.empty() )
383 feedback->emit obstacleCostingAboutToBegin();
385 std::unique_ptr< QgsScopedRuntimeProfile > costingProfile;
388 costingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Assigning label costs" ), u
"rendering"_s );
392 for (
const auto &feature : features )
394 for (
auto &candidate : feature->candidates )
398 rule->alterCandidateCost( candidate.get(), labelContext );
405 step = !allObstacleParts.empty() ? 100.0 / allObstacleParts.size() : 1;
407 for (
FeaturePart *obstaclePart : allObstacleParts )
411 feedback->setProgress( step * index );
416 allCandidatesFirstRound.intersects( obstaclePart->boundingBox(), [obstaclePart,
this](
const LabelPosition *candidatePosition ) ->
bool {
423 if ( candidatePosition->getFeaturePart()->feature()->overlapHandling() == Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
424 || ( !obstaclePart->getHoleOf() && candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( obstaclePart ) )
425 || ( obstaclePart->getHoleOf() && !candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( dynamic_cast< FeaturePart * >( obstaclePart->getHoleOf() ) ) ) )
436 feedback->emit obstacleCostingFinished();
444 step = prob->mFeatureCount != 0 ? 100.0 / prob->mFeatureCount : 1;
446 feedback->emit calculatingConflictsAboutToBegin();
448 std::unique_ptr< QgsScopedRuntimeProfile > conflictProfile;
451 conflictProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Calculating conflicts" ), u
"rendering"_s );
454 int currentLabelPositionIndex = 0;
456 for ( std::size_t featureIndex = 0; featureIndex < prob->mFeatureCount; featureIndex++ )
459 feedback->setProgress(
static_cast< double >( featureIndex ) * step );
461 std::unique_ptr< Feats > feat = std::move( features.front() );
462 features.pop_front();
464 prob->mFirstCandidateIndexForFeature[featureIndex] = currentLabelPositionIndex;
465 prob->mUnlabeledCostForFeature[featureIndex] = std::pow( 2, 10 - 10 * feat->priority );
467 std::size_t maxCandidates = 0;
468 switch ( feat->feature->getGeosType() )
472 maxCandidates = feat->feature->maximumPointCandidates();
475 case GEOS_LINESTRING:
476 maxCandidates = feat->feature->maximumLineCandidates();
480 maxCandidates = std::max(
static_cast< std::size_t
>( 16 ), feat->feature->maximumPolygonCandidates() );
487 auto pruneHardConflicts = [&] {
488 switch ( mPlacementVersion )
499 feat->candidates.erase(
501 feat->candidates.begin() + 1,
502 feat->candidates.end(),
503 [&]( std::unique_ptr< LabelPosition > &candidate ) {
504 if ( candidate->hasHardObstacleConflict() )
511 feat->candidates.end()
514 if ( feat->candidates.size() == 1 && feat->candidates[0]->hasHardObstacleConflict() )
516 switch ( feat->feature->feature()->overlapHandling() )
522 prob->positionsWithNoCandidates()->emplace_back( std::move( feat->candidates.front() ) );
523 feat->candidates.clear();
540 switch ( feat->feature->feature()->overlapHandling() )
545 pruneHardConflicts();
554 if ( feat->candidates.empty() )
567 switch ( feat->feature->feature()->overlapHandling() )
575 pruneHardConflicts();
581 if ( maxCandidates > 0 && feat->candidates.size() > maxCandidates )
583 feat->candidates.resize( maxCandidates );
590 prob->mCandidateCountForFeature[featureIndex] =
static_cast< int >( feat->candidates.size() );
591 prob->mTotalCandidates +=
static_cast< int >( feat->candidates.size() );
594 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
596 candidate->insertIntoIndex( prob->allCandidatesIndex(),
this );
597 candidate->setProblemIds(
static_cast< int >( featureIndex ), currentLabelPositionIndex++ );
599 features.emplace_back( std::move( feat ) );
603 feedback->emit calculatingConflictsFinished();
605 conflictProfile.reset();
610 feedback->emit finalizingCandidatesAboutToBegin();
612 std::unique_ptr< QgsScopedRuntimeProfile > finalizingProfile;
615 finalizingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Finalizing labels" ), u
"rendering"_s );
619 step = !features.empty() ? 100.0 / features.size() : 1;
620 while ( !features.empty() )
624 feedback->setProgress( step * index );
629 std::unique_ptr< Feats > feat = std::move( features.front() );
630 features.pop_front();
632 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
634 std::unique_ptr< LabelPosition > lp = std::move( candidate );
636 lp->resetNumOverlaps();
646 const QgsRectangle searchBounds = lp->boundingBoxForCandidateConflicts(
this );
647 prob->allCandidatesIndex().intersects( searchBounds, [&lp,
this](
const LabelPosition *lp2 ) ->
bool {
648 if ( candidatesAreConflicting( lp.get(), lp2 ) )
650 lp->incrementNumOverlaps();
656 nbOverlaps += lp->getNumOverlaps();
659 prob->addCandidatePosition( std::move( lp ) );
667 feedback->emit finalizingCandidatesFinished();
669 finalizingProfile.reset();
672 prob->mAllNblp = prob->mTotalCandidates;
673 prob->mNbOverlap = nbOverlaps;