110 std::unique_ptr< QgsScopedRuntimeProfile > extractionProfile;
113 extractionProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Placing labels" ), QStringLiteral(
"rendering" ) );
124 std::vector< FeaturePart * > allObstacleParts;
125 std::unique_ptr< Problem > prob = std::make_unique< Problem >( maxCoordinateExtentForSpatialIndices );
130 bbx[0] = bbx[3] = prob->mMapExtentBounds[0] = extent.
xMinimum();
131 bby[0] = bby[1] = prob->mMapExtentBounds[1] = extent.
yMinimum();
132 bbx[1] = bbx[2] = prob->mMapExtentBounds[2] = extent.
xMaximum();
133 bby[2] = bby[3] = prob->mMapExtentBounds[3] = extent.
yMaximum();
137 std::list< std::unique_ptr< Feats > > features;
141 geos::prepared_unique_ptr mapBoundaryPrepared( GEOSPrepare_r(
QgsGeosContext::get(), mapBoundaryGeos.get() ) );
143 int obstacleCount = 0;
147 std::size_t previousFeatureCount = 0;
148 int previousObstacleCount = 0;
150 QStringList layersWithFeaturesInBBox;
152 QMutexLocker palLocker( &mMutex );
154 double step = !mLayers.empty() ? 100.0 / mLayers.size() : 1;
156 std::unique_ptr< QgsScopedRuntimeProfile > candidateProfile;
159 candidateProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Generating label candidates" ), QStringLiteral(
"rendering" ) );
162 for (
const auto &it : mLayers )
168 Layer *layer = it.second.get();
180 feedback->emit candidateCreationAboutToBegin( it.first );
182 std::unique_ptr< QgsScopedRuntimeProfile > layerProfile;
185 layerProfile = std::make_unique< QgsScopedRuntimeProfile >( it.first->providerId(), QStringLiteral(
"rendering" ) );
200 QMutexLocker locker( &layer->
mMutex );
203 std::size_t featureIndex = 0;
205 for (
const std::unique_ptr< FeaturePart > &featurePart : std::as_const( layer->
mFeatureParts ) )
208 feedback->
setProgress( index * step + featureIndex * featureStep );
215 for (
int i = 0; i < featurePart->getNumSelfObstacles(); i++ )
219 allObstacleParts.emplace_back( selfObstacle );
221 if ( !featurePart->getSelfObstacle( i )->getHoleOf() )
228 std::vector< std::unique_ptr< LabelPosition > > candidates = featurePart->createCandidates(
this );
236 candidates.erase( std::remove_if( candidates.begin(), candidates.end(), [&mapBoundaryPrepared, &labelContext,
this]( std::unique_ptr< LabelPosition > &candidate )
238 if ( showPartialLabels() )
240 if ( !candidate->intersects( mapBoundaryPrepared.get() ) )
245 if ( !candidate->within( mapBoundaryPrepared.get() ) )
251 if ( rule->candidateIsIllegal( candidate.get(), labelContext ) )
257 } ), candidates.end() );
262 if ( !candidates.empty() )
264 for ( std::unique_ptr< LabelPosition > &candidate : candidates )
266 candidate->insertIntoIndex( allCandidatesFirstRound );
267 candidate->setGlobalId( mNextCandidateId++ );
273 std::unique_ptr< Feats > ft = std::make_unique< Feats >();
274 ft->feature = featurePart.get();
276 ft->candidates = std::move( candidates );
277 ft->priority = featurePart->calculatePriority();
278 features.emplace_back( std::move( ft ) );
283 std::unique_ptr< LabelPosition > unplacedPosition = featurePart->createCandidatePointOnSurface( featurePart.get() );
284 if ( !unplacedPosition )
287 if ( featurePart->feature()->allowDegradedPlacement() )
290 unplacedPosition->insertIntoIndex( allCandidatesFirstRound );
291 unplacedPosition->setGlobalId( mNextCandidateId++ );
292 candidates.emplace_back( std::move( unplacedPosition ) );
295 std::unique_ptr< Feats > ft = std::make_unique< Feats >();
296 ft->feature = featurePart.get();
298 ft->candidates = std::move( candidates );
299 ft->priority = featurePart->calculatePriority();
300 features.emplace_back( std::move( ft ) );
305 prob->positionsWithNoCandidates()->emplace_back( std::move( unplacedPosition ) );
313 for (
FeaturePart *obstaclePart : std::as_const( layer->mObstacleParts ) )
319 obstacles.
insert( obstaclePart, obstaclePart->boundingBox() );
320 allObstacleParts.emplace_back( obstaclePart );
329 if ( features.size() - previousFeatureCount > 0 || obstacleCount > previousObstacleCount )
331 layersWithFeaturesInBBox << layer->name();
333 previousFeatureCount = features.size();
334 previousObstacleCount = obstacleCount;
337 feedback->emit candidateCreationFinished( it.first );
340 candidateProfile.reset();
347 prob->mLayerCount = layersWithFeaturesInBBox.size();
348 prob->labelledLayersName = layersWithFeaturesInBBox;
350 prob->mFeatureCount = features.size();
351 prob->mTotalCandidates = 0;
352 prob->mCandidateCountForFeature.resize( prob->mFeatureCount );
353 prob->mFirstCandidateIndexForFeature.resize( prob->mFeatureCount );
354 prob->mUnlabeledCostForFeature.resize( prob->mFeatureCount );
356 if ( !features.empty() )
359 feedback->emit obstacleCostingAboutToBegin();
361 std::unique_ptr< QgsScopedRuntimeProfile > costingProfile;
364 costingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Assigning label costs" ), QStringLiteral(
"rendering" ) );
368 for (
const auto &feature : features )
370 for (
auto &candidate : feature->candidates )
374 rule->alterCandidateCost( candidate.get(), labelContext );
381 step = !allObstacleParts.empty() ? 100.0 / allObstacleParts.size() : 1;
383 for (
FeaturePart *obstaclePart : allObstacleParts )
387 feedback->setProgress( step * index );
392 allCandidatesFirstRound.intersects( obstaclePart->boundingBox(), [obstaclePart,
this](
const LabelPosition * candidatePosition ) ->
bool
400 if ( candidatePosition->getFeaturePart()->feature()->overlapHandling() == Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
401 || ( !obstaclePart->getHoleOf() && candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( obstaclePart ) )
402 || ( obstaclePart->getHoleOf() && !candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( dynamic_cast< FeaturePart * >( obstaclePart->getHoleOf() ) ) ) )
413 feedback->emit obstacleCostingFinished();
414 costingProfile.reset();
421 step = prob->mFeatureCount != 0 ? 100.0 / prob->mFeatureCount : 1;
423 feedback->emit calculatingConflictsAboutToBegin();
425 std::unique_ptr< QgsScopedRuntimeProfile > conflictProfile;
428 conflictProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Calculating conflicts" ), QStringLiteral(
"rendering" ) );
431 int currentLabelPositionIndex = 0;
433 for ( std::size_t featureIndex = 0; featureIndex < prob->mFeatureCount; featureIndex++ )
436 feedback->setProgress(
static_cast< double >( featureIndex ) * step );
438 std::unique_ptr< Feats > feat = std::move( features.front() );
439 features.pop_front();
441 prob->mFirstCandidateIndexForFeature[featureIndex] = currentLabelPositionIndex;
442 prob->mUnlabeledCostForFeature[featureIndex] = std::pow( 2, 10 - 10 * feat->priority );
444 std::size_t maxCandidates = 0;
445 switch ( feat->feature->getGeosType() )
449 maxCandidates = feat->feature->maximumPointCandidates();
452 case GEOS_LINESTRING:
453 maxCandidates = feat->feature->maximumLineCandidates();
457 maxCandidates = std::max(
static_cast< std::size_t
>( 16 ), feat->feature->maximumPolygonCandidates() );
464 auto pruneHardConflicts = [&]
466 switch ( mPlacementVersion )
477 feat->candidates.erase( std::remove_if( feat->candidates.begin() + 1, feat->candidates.end(), [ & ]( std::unique_ptr< LabelPosition > &candidate )
479 if ( candidate->hasHardObstacleConflict() )
484 } ), feat->candidates.end() );
486 if ( feat->candidates.size() == 1 && feat->candidates[ 0 ]->hasHardObstacleConflict() )
488 switch ( feat->feature->feature()->overlapHandling() )
494 prob->positionsWithNoCandidates()->emplace_back( std::move( feat->candidates.front() ) );
495 feat->candidates.clear();
512 switch ( feat->feature->feature()->overlapHandling() )
515 pruneHardConflicts();
523 if ( feat->candidates.empty() )
536 switch ( feat->feature->feature()->overlapHandling() )
542 pruneHardConflicts();
547 if ( maxCandidates > 0 && feat->candidates.size() > maxCandidates )
549 feat->candidates.resize( maxCandidates );
556 prob->mCandidateCountForFeature[featureIndex] =
static_cast< int >( feat->candidates.size() );
557 prob->mTotalCandidates +=
static_cast< int >( feat->candidates.size() );
560 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
562 candidate->insertIntoIndex( prob->allCandidatesIndex() );
563 candidate->setProblemIds(
static_cast< int >( featureIndex ), currentLabelPositionIndex++ );
565 features.emplace_back( std::move( feat ) );
569 feedback->emit calculatingConflictsFinished();
571 conflictProfile.reset();
576 feedback->emit finalizingCandidatesAboutToBegin();
578 std::unique_ptr< QgsScopedRuntimeProfile > finalizingProfile;
581 finalizingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Finalizing labels" ), QStringLiteral(
"rendering" ) );
585 step = !features.empty() ? 100.0 / features.size() : 1;
586 while ( !features.empty() )
590 feedback->setProgress( step * index );
595 std::unique_ptr< Feats > feat = std::move( features.front() );
596 features.pop_front();
598 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
600 std::unique_ptr< LabelPosition > lp = std::move( candidate );
602 lp->resetNumOverlaps();
610 const QgsRectangle searchBounds = lp->boundingBoxForCandidateConflicts(
this );
613 if ( candidatesAreConflicting( lp.get(), lp2 ) )
615 lp->incrementNumOverlaps();
622 nbOverlaps += lp->getNumOverlaps();
624 prob->addCandidatePosition( std::move( lp ) );
632 feedback->emit finalizingCandidatesFinished();
634 finalizingProfile.reset();
637 prob->mAllNblp = prob->mTotalCandidates;
638 prob->mNbOverlap = nbOverlaps;