27#if GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 10
44 mTargetLayerSource = std::make_unique< QgsVectorLayerFeatureSource >( mTargetLayer.get() );
46 mDistanceMapUnits = context.
convertToMapUnits( mDistance, mDistanceUnit, mDistanceUnitScale );
52 element.setAttribute( QStringLiteral(
"distance" ), mDistance );
55 element.setAttribute( QStringLiteral(
"cost" ), mCost );
59 element.setAttribute( QStringLiteral(
"labeledLayer" ), mLabeledLayer.layerId );
60 element.setAttribute( QStringLiteral(
"labeledLayerName" ), mLabeledLayer.name );
61 element.setAttribute( QStringLiteral(
"labeledLayerSource" ), mLabeledLayer.source );
62 element.setAttribute( QStringLiteral(
"labeledLayerProvider" ), mLabeledLayer.provider );
66 element.setAttribute( QStringLiteral(
"targetLayer" ), mTargetLayer.layerId );
67 element.setAttribute( QStringLiteral(
"targetLayerName" ), mTargetLayer.name );
68 element.setAttribute( QStringLiteral(
"targetLayerSource" ), mTargetLayer.source );
69 element.setAttribute( QStringLiteral(
"targetLayerProvider" ), mTargetLayer.provider );
75 mDistance = element.attribute( QStringLiteral(
"distance" ), QStringLiteral(
"5" ) ).toDouble();
78 mCost = element.attribute( QStringLiteral(
"cost" ), QStringLiteral(
"10" ) ).toDouble();
81 const QString layerId = element.attribute( QStringLiteral(
"labeledLayer" ) );
82 const QString layerName = element.attribute( QStringLiteral(
"labeledLayerName" ) );
83 const QString layerSource = element.attribute( QStringLiteral(
"labeledLayerSource" ) );
84 const QString layerProvider = element.attribute( QStringLiteral(
"labeledLayerProvider" ) );
85 mLabeledLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
88 const QString layerId = element.attribute( QStringLiteral(
"targetLayer" ) );
89 const QString layerName = element.attribute( QStringLiteral(
"targetLayerName" ) );
90 const QString layerSource = element.attribute( QStringLiteral(
"targetLayerSource" ) );
91 const QString layerProvider = element.attribute( QStringLiteral(
"targetLayerProvider" ) );
92 mTargetLayer =
QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
98 mLabeledLayer.resolve( project );
99 mTargetLayer.resolve( project );
113 if ( !mTargetLayerSource )
116 return candidateExceedsTolerance( candidate, context );
130 if ( !mTargetLayerSource )
133 if ( candidateExceedsTolerance( candidate, context ) )
137 candidate->
setCost( candidate->
cost() + mCost / 1000 );
143#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
152 return mLabeledLayer.get();
157 mLabeledLayer = layer;
162 return mTargetLayer.get();
167 mTargetLayer = layer;
175 otherRule->mLabeledLayer = mLabeledLayer;
176 otherRule->mTargetLayer = mTargetLayer;
177 otherRule->mDistance = mDistance;
178 otherRule->mDistanceUnit = mDistanceUnit;
179 otherRule->mDistanceUnitScale = mDistanceUnitScale;
180 otherRule->mCost = mCost;
204 const QgsRectangle expandedBounds = candidateBounds.
buffered( mDistanceMapUnits );
206 const QList<QgsFeatureId> overlapCandidates = mIndex->
intersects( expandedBounds );
207 if ( overlapCandidates.empty() )
213 for (
const QgsFeatureId overlapCandidateId : overlapCandidates )
221#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
222 if ( GEOSPreparedDistanceWithin_r( geosctxt, candidateGeos, featureCandidate.get(), mDistanceMapUnits ) )
231 catch ( QgsGeosException &e )
233 QgsDebugError( QStringLiteral(
"GEOS exception: %1" ).arg( e.what() ) );
249 auto res = std::make_unique< QgsLabelingEngineRuleMinimumDistanceLabelToFeature >();
251 return res.release();
256 return QStringLiteral(
"minimumDistanceLabelToFeature" );
261 return QObject::tr(
"Push Labels Away from Features" );
266 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
269 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must be at least %2 %3 from features in <i>%4</i>" ).arg(
274 ) + QStringLiteral(
"</p>" );
293 auto res = std::make_unique< QgsLabelingEngineRuleMaximumDistanceLabelToFeature >();
295 return res.release();
300 return QStringLiteral(
"maximumDistanceLabelToFeature" );
305 return QObject::tr(
"Pull Labels Toward Features" );
310 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
313 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must be at most %2 %3 from features in <i>%4</i>" ).arg(
318 ) + QStringLiteral(
"</p>" );
333 auto res = std::make_unique< QgsLabelingEngineRuleMinimumDistanceLabelToLabel >();
335 res->mLabeledLayer = mLabeledLayer;
336 res->mTargetLayer = mTargetLayer;
337 res->mDistance = mDistance;
338 res->mDistanceUnit = mDistanceUnit;
339 res->mDistanceUnitScale = mDistanceUnitScale;
340 return res.release();
345 return QStringLiteral(
"minimumDistanceLabelToLabel" );
350 return QObject::tr(
"Push Labels Away from Other Labels" );
355 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
358 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must be at least %2 %3 from labels from <i>%4</i>" ).arg(
363 ) + QStringLiteral(
"</p>" );
370#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
379 element.setAttribute( QStringLiteral(
"distance" ), mDistance );
385 element.setAttribute( QStringLiteral(
"labeledLayer" ), mLabeledLayer.layerId );
386 element.setAttribute( QStringLiteral(
"labeledLayerName" ), mLabeledLayer.name );
387 element.setAttribute( QStringLiteral(
"labeledLayerSource" ), mLabeledLayer.source );
388 element.setAttribute( QStringLiteral(
"labeledLayerProvider" ), mLabeledLayer.provider );
392 element.setAttribute( QStringLiteral(
"targetLayer" ), mTargetLayer.layerId );
393 element.setAttribute( QStringLiteral(
"targetLayerName" ), mTargetLayer.name );
394 element.setAttribute( QStringLiteral(
"targetLayerSource" ), mTargetLayer.source );
395 element.setAttribute( QStringLiteral(
"targetLayerProvider" ), mTargetLayer.provider );
401 mDistance = element.attribute( QStringLiteral(
"distance" ), QStringLiteral(
"5" ) ).toDouble();
406 const QString layerId = element.attribute( QStringLiteral(
"labeledLayer" ) );
407 const QString layerName = element.attribute( QStringLiteral(
"labeledLayerName" ) );
408 const QString layerSource = element.attribute( QStringLiteral(
"labeledLayerSource" ) );
409 const QString layerProvider = element.attribute( QStringLiteral(
"labeledLayerProvider" ) );
410 mLabeledLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
413 const QString layerId = element.attribute( QStringLiteral(
"targetLayer" ) );
414 const QString layerName = element.attribute( QStringLiteral(
"targetLayerName" ) );
415 const QString layerSource = element.attribute( QStringLiteral(
"targetLayerSource" ) );
416 const QString layerProvider = element.attribute( QStringLiteral(
"targetLayerProvider" ) );
417 mTargetLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
423 mLabeledLayer.resolve( project );
424 mTargetLayer.resolve( project );
429 mDistanceMapUnits = context.
convertToMapUnits( mDistance, mDistanceUnit, mDistanceUnitScale );
435 return candidateBounds.
buffered( mDistanceMapUnits );
452#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
462 catch ( QgsGeosException &e )
464 QgsDebugError( QStringLiteral(
"GEOS exception: %1" ).arg( e.what() ) );
473 return mLabeledLayer.get();
478 mLabeledLayer = layer;
483 return mTargetLayer.get();
488 mTargetLayer = layer;
501 auto res = std::make_unique< QgsLabelingEngineRuleAvoidLabelOverlapWithFeature >();
503 res->mLabeledLayer = mLabeledLayer;
504 res->mTargetLayer = mTargetLayer;
505 return res.release();
510 return QStringLiteral(
"avoidLabelOverlapWithFeature" );
515 return QObject::tr(
"Prevent Labels Overlapping Features" );
520 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
523 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must not overlap features from <i>%2</i>" ).arg(
526 ) + QStringLiteral(
"</p>" );
537 mTargetLayerSource = std::make_unique< QgsVectorLayerFeatureSource >( mTargetLayer.get() );
545 element.setAttribute( QStringLiteral(
"labeledLayer" ), mLabeledLayer.layerId );
546 element.setAttribute( QStringLiteral(
"labeledLayerName" ), mLabeledLayer.name );
547 element.setAttribute( QStringLiteral(
"labeledLayerSource" ), mLabeledLayer.source );
548 element.setAttribute( QStringLiteral(
"labeledLayerProvider" ), mLabeledLayer.provider );
552 element.setAttribute( QStringLiteral(
"targetLayer" ), mTargetLayer.layerId );
553 element.setAttribute( QStringLiteral(
"targetLayerName" ), mTargetLayer.name );
554 element.setAttribute( QStringLiteral(
"targetLayerSource" ), mTargetLayer.source );
555 element.setAttribute( QStringLiteral(
"targetLayerProvider" ), mTargetLayer.provider );
562 const QString layerId = element.attribute( QStringLiteral(
"labeledLayer" ) );
563 const QString layerName = element.attribute( QStringLiteral(
"labeledLayerName" ) );
564 const QString layerSource = element.attribute( QStringLiteral(
"labeledLayerSource" ) );
565 const QString layerProvider = element.attribute( QStringLiteral(
"labeledLayerProvider" ) );
566 mLabeledLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
569 const QString layerId = element.attribute( QStringLiteral(
"targetLayer" ) );
570 const QString layerName = element.attribute( QStringLiteral(
"targetLayerName" ) );
571 const QString layerSource = element.attribute( QStringLiteral(
"targetLayerSource" ) );
572 const QString layerProvider = element.attribute( QStringLiteral(
"targetLayerProvider" ) );
573 mTargetLayer =
QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
579 mLabeledLayer.resolve( project );
580 mTargetLayer.resolve( project );
590 if ( !mTargetLayerSource )
596 const QList<QgsFeatureId> overlapCandidates = mIndex->intersects( candidate->
outerBoundingBox() );
597 if ( overlapCandidates.empty() )
603 for (
const QgsFeatureId overlapCandidateId : overlapCandidates )
611 if ( GEOSPreparedIntersects_r( geosctxt, candidateGeos, featureCandidate.get() ) == 1 )
614 catch ( QgsGeosException &e )
616 QgsDebugError( QStringLiteral(
"GEOS exception: %1" ).arg( e.what() ) );
625 return mLabeledLayer.get();
630 mLabeledLayer = layer;
635 return mTargetLayer.get();
640 mTargetLayer = layer;
QString layerId() const
Returns ID of associated layer, or empty string if no layer is associated with the provider.
bool candidateIsIllegal(const pal::LabelPosition *candidate, QgsLabelingEngineContext &context) const override
Returns true if a labeling candidate violates the rule and should be eliminated.
bool prepare(QgsRenderContext &context) override
Prepares the rule.
~QgsAbstractLabelingEngineRuleDistanceFromFeature() override
void setTargetLayer(QgsVectorLayer *layer)
Sets the layer providing the features which labels must be distant from (or close to).
Qgis::RenderUnit distanceUnit() const
Returns the units for the distance between labels and the features from the targetLayer().
QgsAbstractLabelingEngineRuleDistanceFromFeature()
double distance() const
Returns the acceptable distance threshold between labels and the features from the targetLayer().
void alterCandidateCost(pal::LabelPosition *candidate, QgsLabelingEngineContext &context) const override
Provides an opportunity for the rule to alter the cost for a candidate.
bool isAvailable() const override
Returns true if the rule is available for use within the current QGIS environment.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const override
Writes the rule properties to an XML element.
void resolveReferences(const QgsProject *project) override
Resolves reference to layers from stored layer ID.
QgsVectorLayer * targetLayer() const
Returns the layer providing the features which labels must be distant from (or close to).
void setLabeledLayer(QgsMapLayer *layer)
Sets the layer providing the labels.
void copyCommonProperties(QgsAbstractLabelingEngineRule *other) const override
Copies common properties from this object to an other.
void readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the rule properties from an XML element.
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
bool mMustBeDistant
true if labels must be distant from features, false if they must be close
Abstract base class for labeling engine rules.
QString name() const
Returns the name for this instance of the rule.
virtual QString displayType() const =0
Returns a user-friendly, translated string representing the rule type.
virtual void copyCommonProperties(QgsAbstractLabelingEngineRule *other) const
Copies common properties from this object to an other.
Wrapper for iterator of features from vector data provider or vector layer.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature's geometries.
QgsFeatureRequest & setNoAttributes()
Set that no attributes will be fetched.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
bool isCanceled() const
Tells whether the operation has been canceled already.
static GEOSContextHandle_t get()
Returns a thread local instance of a GEOS context, safe for use in the current thread.
static geos::unique_ptr asGeos(const QgsGeometry &geometry, double precision=0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlags())
Returns a geos geometry - caller takes ownership of the object (should be deleted with GEOSGeom_destr...
QgsAbstractLabelProvider * provider() const
Returns provider of this instance.
Encapsulates the context for a labeling engine run.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
QgsRectangle extent() const
Returns the map extent defining the limits for labeling.
void readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the rule properties from an XML element.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const override
Writes the rule properties to an XML element.
QgsLabelingEngineRuleAvoidLabelOverlapWithFeature()
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
bool prepare(QgsRenderContext &context) override
Prepares the rule.
QString id() const override
Returns a string uniquely identifying the rule subclass.
void setTargetLayer(QgsVectorLayer *layer)
Sets the layer providing the features which labels must not overlap.
QgsLabelingEngineRuleAvoidLabelOverlapWithFeature * clone() const override
Creates a clone of this rule.
QString description() const override
Returns a user-friendly description of the rule.
void setLabeledLayer(QgsMapLayer *layer)
Sets the layer providing the labels.
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
void resolveReferences(const QgsProject *project) override
Resolves reference to layers from stored layer ID.
bool candidateIsIllegal(const pal::LabelPosition *candidate, QgsLabelingEngineContext &context) const override
Returns true if a labeling candidate violates the rule and should be eliminated.
QgsVectorLayer * targetLayer() const
Returns the layer providing the features which labels must not overlap.
~QgsLabelingEngineRuleAvoidLabelOverlapWithFeature() override
~QgsLabelingEngineRuleMaximumDistanceLabelToFeature() override
QString id() const override
Returns a string uniquely identifying the rule subclass.
QString description() const override
Returns a user-friendly description of the rule.
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
QgsLabelingEngineRuleMaximumDistanceLabelToFeature * clone() const override
Creates a clone of this rule.
QgsLabelingEngineRuleMaximumDistanceLabelToFeature()
QgsLabelingEngineRuleMinimumDistanceLabelToFeature()
QString description() const override
Returns a user-friendly description of the rule.
~QgsLabelingEngineRuleMinimumDistanceLabelToFeature() override
QgsLabelingEngineRuleMinimumDistanceLabelToFeature * clone() const override
Creates a clone of this rule.
QString id() const override
Returns a string uniquely identifying the rule subclass.
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
double distance() const
Returns the minimum permitted distance between labels from the labeledLayer() and the labels from the...
QgsMapLayer * labeledLayer() const
Returns the layer providing the labels.
QgsRectangle modifyCandidateConflictSearchBoundingBox(const QgsRectangle &candidateBounds) const override
Returns a (possibly expanded) bounding box to use when searching for conflicts for a candidate.
QString id() const override
Returns a string uniquely identifying the rule subclass.
void setLabeledLayer(QgsMapLayer *layer)
Sets the layer providing the labels.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const override
Writes the rule properties to an XML element.
void resolveReferences(const QgsProject *project) override
Resolves reference to layers from stored layer ID.
QgsMapLayer * targetLayer() const
Returns the layer providing the labels which labels must be distant from.
void readXml(const QDomElement &element, const QgsReadWriteContext &context) override
Reads the rule properties from an XML element.
QgsLabelingEngineRuleMinimumDistanceLabelToLabel * clone() const override
Creates a clone of this rule.
QString displayType() const override
Returns a user-friendly, translated string representing the rule type.
QString description() const override
Returns a user-friendly description of the rule.
void setTargetLayer(QgsMapLayer *layer)
Sets the layer providing the labels which labels must be distant from.
Qgis::RenderUnit distanceUnit() const
Returns the units for the distance between labels from the labeledLayer() and the labels from the tar...
QgsLabelingEngineRuleMinimumDistanceLabelToLabel()
~QgsLabelingEngineRuleMinimumDistanceLabelToLabel() override
bool prepare(QgsRenderContext &context) override
Prepares the rule.
bool isAvailable() const override
Returns true if the rule is available for use within the current QGIS environment.
bool candidatesAreConflicting(const pal::LabelPosition *lp1, const pal::LabelPosition *lp2) const override
Returns true if a labeling candidate lp1 conflicts with lp2 after applying the rule.
Base class for all map layer types.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
bool intersects(const QgsRectangle &rect) const
Returns true when rectangle intersects with other rectangle.
QgsRectangle buffered(double width) const
Gets rectangle enlarged by buffer.
Contains information about the context of a rendering operation.
double convertToMapUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to map units.
QgsCoordinateTransformContext transformContext() const
Returns the context's coordinate transform context, which stores various information regarding which ...
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static Q_INVOKABLE QString toAbbreviatedString(Qgis::DistanceUnit unit)
Returns a translated abbreviation representing a distance unit.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
Represents a vector layer which manages a vector based dataset.
QgsLabelFeature * feature()
Returns the parent feature.
LabelPosition is a candidate feature label position.
QgsRectangle outerBoundingBox() const
Returns bounding box.
void setCost(double newCost)
Sets the candidate label position's geographical cost.
const GEOSGeometry * multiPartGeom() const
Returns a GEOS representation of all label parts as a multipolygon.
double cost() const
Returns the candidate label position's geographical cost.
FeaturePart * getFeaturePart() const
Returns the feature corresponding to this labelposition.
const GEOSPreparedGeometry * preparedMultiPartGeom() const
Returns a prepared GEOS representation of all label parts as a multipolygon.
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugError(str)
_LayerRef< QgsMapLayer > QgsMapLayerRef
#define QGIS_CHECK_OTHER_QOBJECT_THREAD_ACCESS(other)
_LayerRef< QgsVectorLayer > QgsVectorLayerRef