113 std::unique_ptr< QgsScopedRuntimeProfile > extractionProfile;
116 extractionProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Placing labels" ), QStringLiteral(
"rendering" ) );
127 std::vector< FeaturePart * > allObstacleParts;
128 auto prob = std::make_unique< Problem >( maxCoordinateExtentForSpatialIndices );
133 bbx[0] = bbx[3] = prob->mMapExtentBounds[0] = extent.
xMinimum();
134 bby[0] = bby[1] = prob->mMapExtentBounds[1] = extent.
yMinimum();
135 bbx[1] = bbx[2] = prob->mMapExtentBounds[2] = extent.
xMaximum();
136 bby[2] = bby[3] = prob->mMapExtentBounds[3] = extent.
yMaximum();
140 std::list< std::unique_ptr< Feats > > features;
144 geos::prepared_unique_ptr mapBoundaryPrepared( GEOSPrepare_r(
QgsGeosContext::get(), mapBoundaryGeos.get() ) );
146 int obstacleCount = 0;
150 std::size_t previousFeatureCount = 0;
151 int previousObstacleCount = 0;
153 QStringList layersWithFeaturesInBBox;
155 QMutexLocker palLocker( &mMutex );
157 double step = !mLayers.empty() ? 100.0 / mLayers.size() : 1;
159 std::unique_ptr< QgsScopedRuntimeProfile > candidateProfile;
162 candidateProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Generating label candidates" ), QStringLiteral(
"rendering" ) );
165 for (
auto it = mLayers.rbegin(); it != mLayers.rend(); ++it )
171 Layer *layer = it->second.get();
183 feedback->emit candidateCreationAboutToBegin( it->first );
185 std::unique_ptr< QgsScopedRuntimeProfile > layerProfile;
188 layerProfile = std::make_unique< QgsScopedRuntimeProfile >( it->first->providerId(), QStringLiteral(
"rendering" ) );
203 QMutexLocker locker( &layer->
mMutex );
206 std::size_t featureIndex = 0;
208 for (
const std::unique_ptr< FeaturePart > &featurePart : std::as_const( layer->
mFeatureParts ) )
211 feedback->
setProgress( index * step + featureIndex * featureStep );
218 for (
int i = 0; i < featurePart->getNumSelfObstacles(); i++ )
222 allObstacleParts.emplace_back( selfObstacle );
224 if ( !featurePart->getSelfObstacle( i )->getHoleOf() )
231 std::vector< std::unique_ptr< LabelPosition > > candidates = featurePart->createCandidates(
this );
239 candidates.erase( std::remove_if( candidates.begin(), candidates.end(), [&mapBoundaryPrepared, &labelContext,
this]( std::unique_ptr< LabelPosition > &candidate )
241 if ( showPartialLabels() )
243 if ( !candidate->intersects( mapBoundaryPrepared.get() ) )
248 if ( !candidate->within( mapBoundaryPrepared.get() ) )
254 if ( rule->candidateIsIllegal( candidate.get(), labelContext ) )
260 } ), candidates.end() );
265 if ( !candidates.empty() )
267 for ( std::unique_ptr< LabelPosition > &candidate : candidates )
269 candidate->insertIntoIndex( allCandidatesFirstRound );
270 candidate->setGlobalId( mNextCandidateId++ );
276 auto ft = std::make_unique< Feats >();
277 ft->feature = featurePart.get();
279 ft->candidates = std::move( candidates );
280 ft->priority = featurePart->calculatePriority();
281 features.emplace_back( std::move( ft ) );
286 std::unique_ptr< LabelPosition > unplacedPosition = featurePart->createCandidatePointOnSurface( featurePart.get() );
287 if ( !unplacedPosition )
290 if ( featurePart->feature()->allowDegradedPlacement() )
293 unplacedPosition->insertIntoIndex( allCandidatesFirstRound );
294 unplacedPosition->setGlobalId( mNextCandidateId++ );
295 candidates.emplace_back( std::move( unplacedPosition ) );
298 auto ft = std::make_unique< Feats >();
299 ft->feature = featurePart.get();
301 ft->candidates = std::move( candidates );
302 ft->priority = featurePart->calculatePriority();
303 features.emplace_back( std::move( ft ) );
308 prob->positionsWithNoCandidates()->emplace_back( std::move( unplacedPosition ) );
316 for (
FeaturePart *obstaclePart : std::as_const( layer->mObstacleParts ) )
322 obstacles.
insert( obstaclePart, obstaclePart->boundingBox() );
323 allObstacleParts.emplace_back( obstaclePart );
332 if ( features.size() - previousFeatureCount > 0 || obstacleCount > previousObstacleCount )
334 layersWithFeaturesInBBox << layer->name();
336 previousFeatureCount = features.size();
337 previousObstacleCount = obstacleCount;
340 feedback->emit candidateCreationFinished( it->first );
343 candidateProfile.reset();
350 prob->mLayerCount = layersWithFeaturesInBBox.size();
351 prob->labelledLayersName = layersWithFeaturesInBBox;
353 prob->mFeatureCount = features.size();
354 prob->mTotalCandidates = 0;
355 prob->mCandidateCountForFeature.resize( prob->mFeatureCount );
356 prob->mFirstCandidateIndexForFeature.resize( prob->mFeatureCount );
357 prob->mUnlabeledCostForFeature.resize( prob->mFeatureCount );
359 if ( !features.empty() )
362 feedback->emit obstacleCostingAboutToBegin();
364 std::unique_ptr< QgsScopedRuntimeProfile > costingProfile;
367 costingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Assigning label costs" ), QStringLiteral(
"rendering" ) );
371 for (
const auto &feature : features )
373 for (
auto &candidate : feature->candidates )
377 rule->alterCandidateCost( candidate.get(), labelContext );
384 step = !allObstacleParts.empty() ? 100.0 / allObstacleParts.size() : 1;
386 for (
FeaturePart *obstaclePart : allObstacleParts )
390 feedback->setProgress( step * index );
395 allCandidatesFirstRound.intersects( obstaclePart->boundingBox(), [obstaclePart,
this](
const LabelPosition * candidatePosition ) ->
bool
403 if ( candidatePosition->getFeaturePart()->feature()->overlapHandling() == Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
404 || ( !obstaclePart->getHoleOf() && candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( obstaclePart ) )
405 || ( obstaclePart->getHoleOf() && !candidatePosition->getFeaturePart()->hasSameLabelFeatureAs( dynamic_cast< FeaturePart * >( obstaclePart->getHoleOf() ) ) ) )
416 feedback->emit obstacleCostingFinished();
417 costingProfile.reset();
424 step = prob->mFeatureCount != 0 ? 100.0 / prob->mFeatureCount : 1;
426 feedback->emit calculatingConflictsAboutToBegin();
428 std::unique_ptr< QgsScopedRuntimeProfile > conflictProfile;
431 conflictProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Calculating conflicts" ), QStringLiteral(
"rendering" ) );
434 int currentLabelPositionIndex = 0;
436 for ( std::size_t featureIndex = 0; featureIndex < prob->mFeatureCount; featureIndex++ )
439 feedback->setProgress(
static_cast< double >( featureIndex ) * step );
441 std::unique_ptr< Feats > feat = std::move( features.front() );
442 features.pop_front();
444 prob->mFirstCandidateIndexForFeature[featureIndex] = currentLabelPositionIndex;
445 prob->mUnlabeledCostForFeature[featureIndex] = std::pow( 2, 10 - 10 * feat->priority );
447 std::size_t maxCandidates = 0;
448 switch ( feat->feature->getGeosType() )
452 maxCandidates = feat->feature->maximumPointCandidates();
455 case GEOS_LINESTRING:
456 maxCandidates = feat->feature->maximumLineCandidates();
460 maxCandidates = std::max(
static_cast< std::size_t
>( 16 ), feat->feature->maximumPolygonCandidates() );
467 auto pruneHardConflicts = [&]
469 switch ( mPlacementVersion )
480 feat->candidates.erase( std::remove_if( feat->candidates.begin() + 1, feat->candidates.end(), [ & ]( std::unique_ptr< LabelPosition > &candidate )
482 if ( candidate->hasHardObstacleConflict() )
487 } ), feat->candidates.end() );
489 if ( feat->candidates.size() == 1 && feat->candidates[ 0 ]->hasHardObstacleConflict() )
491 switch ( feat->feature->feature()->overlapHandling() )
497 prob->positionsWithNoCandidates()->emplace_back( std::move( feat->candidates.front() ) );
498 feat->candidates.clear();
515 switch ( feat->feature->feature()->overlapHandling() )
518 pruneHardConflicts();
526 if ( feat->candidates.empty() )
539 switch ( feat->feature->feature()->overlapHandling() )
545 pruneHardConflicts();
550 if ( maxCandidates > 0 && feat->candidates.size() > maxCandidates )
552 feat->candidates.resize( maxCandidates );
559 prob->mCandidateCountForFeature[featureIndex] =
static_cast< int >( feat->candidates.size() );
560 prob->mTotalCandidates +=
static_cast< int >( feat->candidates.size() );
563 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
565 candidate->insertIntoIndex( prob->allCandidatesIndex() );
566 candidate->setProblemIds(
static_cast< int >( featureIndex ), currentLabelPositionIndex++ );
568 features.emplace_back( std::move( feat ) );
572 feedback->emit calculatingConflictsFinished();
574 conflictProfile.reset();
579 feedback->emit finalizingCandidatesAboutToBegin();
581 std::unique_ptr< QgsScopedRuntimeProfile > finalizingProfile;
584 finalizingProfile = std::make_unique< QgsScopedRuntimeProfile >( QObject::tr(
"Finalizing labels" ), QStringLiteral(
"rendering" ) );
588 step = !features.empty() ? 100.0 / features.size() : 1;
589 while ( !features.empty() )
593 feedback->setProgress( step * index );
598 std::unique_ptr< Feats > feat = std::move( features.front() );
599 features.pop_front();
601 for ( std::unique_ptr< LabelPosition > &candidate : feat->candidates )
603 std::unique_ptr< LabelPosition > lp = std::move( candidate );
605 lp->resetNumOverlaps();
613 const QgsRectangle searchBounds = lp->boundingBoxForCandidateConflicts(
this );
616 if ( candidatesAreConflicting( lp.get(), lp2 ) )
618 lp->incrementNumOverlaps();
625 nbOverlaps += lp->getNumOverlaps();
627 prob->addCandidatePosition( std::move( lp ) );
635 feedback->emit finalizingCandidatesFinished();
637 finalizingProfile.reset();
640 prob->mAllNblp = prob->mTotalCandidates;
641 prob->mNbOverlap = nbOverlaps;