Skip to content

Commit 3e61891

Browse files
committed
[pal] Don't treat joined features as obstacles for each other
Eg, if the "merge connected features" option for a layer is checked then label candidates generated for the merged line should not be marked as in conflict with any part of the merged line. Previously they would be marked in conflict with all but one part of the merged line, resulting in false penalties being applied to these candidates and non-ideal final label placements.
1 parent e03f554 commit 3e61891

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/core/pal/feature.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,18 @@ namespace pal
170170
if ( !part )
171171
return false;
172172

173-
return mLF->id() == part->featureId() && mLF->layer()->name() == part->layer()->name();
173+
if ( mLF->layer()->name() != part->layer()->name() )
174+
return false;
175+
176+
if ( mLF->id() == part->featureId() )
177+
return true;
178+
179+
// any part of joined features are also treated as having the same label feature
180+
int connectedFeatureId = mLF->layer()->connectedFeatureId( mLF->id() );
181+
if ( connectedFeatureId >= 0 && connectedFeatureId == mLF->layer()->connectedFeatureId( part->featureId() ) )
182+
return true;
183+
184+
return false;
174185
}
175186

176187
LabelPosition::Quadrant FeaturePart::quadrantFromOffset() const

src/core/pal/layer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,14 @@ namespace pal
357357
void Layer::joinConnectedFeatures()
358358
{
359359
// go through all label texts
360+
int connectedFeaturesId = 0;
360361
Q_FOREACH ( const QString& labelText, mConnectedTexts )
361362
{
362363
if ( !mConnectedHashtable.contains( labelText ) )
363364
continue; // shouldn't happen
364365

366+
connectedFeaturesId++;
367+
365368
QLinkedList<FeaturePart*>* parts = mConnectedHashtable.value( labelText );
366369

367370
// go one-by-one part, try to merge
@@ -381,11 +384,13 @@ namespace pal
381384
mFeatureIndex->Remove( bmin, bmax, partCheck );
382385
mFeatureParts.removeOne( partCheck );
383386

387+
mConnectedFeaturesIds.insert( partCheck->featureId(), connectedFeaturesId );
384388
otherPart->getBoundingBox( bmin, bmax );
385389

386390
// merge points from partCheck to p->item
387391
if ( otherPart->mergeWithFeaturePart( partCheck ) )
388392
{
393+
mConnectedFeaturesIds.insert( otherPart->featureId(), connectedFeaturesId );
389394
// reinsert p->item to r-tree (probably not needed)
390395
mFeatureIndex->Remove( bmin, bmax, otherPart );
391396
otherPart->getBoundingBox( bmin, bmax );
@@ -409,6 +414,11 @@ namespace pal
409414
mConnectedTexts.clear();
410415
}
411416

417+
int Layer::connectedFeatureId( QgsFeatureId featureId ) const
418+
{
419+
return mConnectedFeaturesIds.value( featureId, -1 );
420+
}
421+
412422
void Layer::chopFeaturesAtRepeatDistance()
413423
{
414424
GEOSContextHandle_t geosctxt = geosContext();

src/core/pal/layer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ namespace pal
239239
/** Join connected features with the same label text */
240240
void joinConnectedFeatures();
241241

242+
/** Returns the connected feature ID for a label feature ID, which is unique for all features
243+
* which have been joined as a result of joinConnectedFeatures()
244+
* @returns connected feature ID, or -1 if feature was not joined
245+
*/
246+
int connectedFeatureId( QgsFeatureId featureId ) const;
247+
242248
/** Chop layer features at the repeat distance **/
243249
void chopFeaturesAtRepeatDistance();
244250

@@ -281,6 +287,7 @@ namespace pal
281287

282288
QHash< QString, QLinkedList<FeaturePart*>* > mConnectedHashtable;
283289
QStringList mConnectedTexts;
290+
QHash< QgsFeatureId, int > mConnectedFeaturesIds;
284291

285292
QMutex mMutex;
286293

0 commit comments

Comments
 (0)