25#if GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR < 10
42 mTargetLayerSource = std::make_unique< QgsVectorLayerFeatureSource >( mTargetLayer.
get() );
44 mDistanceMapUnits = context.
convertToMapUnits( mDistance, mDistanceUnit, mDistanceUnitScale );
50 element.setAttribute( QStringLiteral(
"distance" ), mDistance );
53 element.setAttribute( QStringLiteral(
"cost" ), mCost );
57 element.setAttribute( QStringLiteral(
"labeledLayer" ), mLabeledLayer.
layerId );
58 element.setAttribute( QStringLiteral(
"labeledLayerName" ), mLabeledLayer.
name );
59 element.setAttribute( QStringLiteral(
"labeledLayerSource" ), mLabeledLayer.
source );
60 element.setAttribute( QStringLiteral(
"labeledLayerProvider" ), mLabeledLayer.
provider );
64 element.setAttribute( QStringLiteral(
"targetLayer" ), mTargetLayer.
layerId );
65 element.setAttribute( QStringLiteral(
"targetLayerName" ), mTargetLayer.
name );
66 element.setAttribute( QStringLiteral(
"targetLayerSource" ), mTargetLayer.
source );
67 element.setAttribute( QStringLiteral(
"targetLayerProvider" ), mTargetLayer.
provider );
73 mDistance = element.attribute( QStringLiteral(
"distance" ), QStringLiteral(
"5" ) ).toDouble();
76 mCost = element.attribute( QStringLiteral(
"cost" ), QStringLiteral(
"10" ) ).toDouble();
79 const QString layerId = element.attribute( QStringLiteral(
"labeledLayer" ) );
80 const QString layerName = element.attribute( QStringLiteral(
"labeledLayerName" ) );
81 const QString layerSource = element.attribute( QStringLiteral(
"labeledLayerSource" ) );
82 const QString layerProvider = element.attribute( QStringLiteral(
"labeledLayerProvider" ) );
83 mLabeledLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
86 const QString layerId = element.attribute( QStringLiteral(
"targetLayer" ) );
87 const QString layerName = element.attribute( QStringLiteral(
"targetLayerName" ) );
88 const QString layerSource = element.attribute( QStringLiteral(
"targetLayerSource" ) );
89 const QString layerProvider = element.attribute( QStringLiteral(
"targetLayerProvider" ) );
90 mTargetLayer =
QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
96 mLabeledLayer.
resolve( project );
97 mTargetLayer.
resolve( project );
111 if ( !mTargetLayerSource )
114 return candidateExceedsTolerance( candidate, context );
128 if ( !mTargetLayerSource )
131 if ( candidateExceedsTolerance( candidate, context ) )
135 candidate->
setCost( candidate->
cost() + mCost / 1000 );
141#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
150 return mLabeledLayer.
get();
155 mLabeledLayer = layer;
160 return mTargetLayer.
get();
165 mTargetLayer = layer;
173 otherRule->mLabeledLayer = mLabeledLayer;
174 otherRule->mTargetLayer = mTargetLayer;
175 otherRule->mDistance = mDistance;
176 otherRule->mDistanceUnit = mDistanceUnit;
177 otherRule->mDistanceUnitScale = mDistanceUnitScale;
178 otherRule->mCost = mCost;
204 const QList<QgsFeatureId> overlapCandidates = mIndex->intersects( expandedBounds );
205 if ( overlapCandidates.empty() )
211 for (
const QgsFeatureId overlapCandidateId : overlapCandidates )
218 geos::unique_ptr featureCandidate =
QgsGeos::asGeos( mIndex->geometry( overlapCandidateId ).constGet() );
219#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
220 if ( GEOSPreparedDistanceWithin_r( geosctxt, candidateGeos, featureCandidate.get(), mDistanceMapUnits ) )
229 catch ( GEOSException &e )
231 QgsDebugError( QStringLiteral(
"GEOS exception: %1" ).arg( e.what() ) );
247 std::unique_ptr< QgsLabelingEngineRuleMinimumDistanceLabelToFeature> res = std::make_unique< QgsLabelingEngineRuleMinimumDistanceLabelToFeature >();
249 return res.release();
254 return QStringLiteral(
"minimumDistanceLabelToFeature" );
259 return QObject::tr(
"Push Labels Away from Features" );
264 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
267 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must be at least %2 %3 from features in <i>%4</i>" ).arg(
272 ) + QStringLiteral(
"</p>" );
291 std::unique_ptr< QgsLabelingEngineRuleMaximumDistanceLabelToFeature > res = std::make_unique< QgsLabelingEngineRuleMaximumDistanceLabelToFeature >();
293 return res.release();
298 return QStringLiteral(
"maximumDistanceLabelToFeature" );
303 return QObject::tr(
"Pull Labels Toward Features" );
308 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
311 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must be at most %2 %3 from features in <i>%4</i>" ).arg(
316 ) + QStringLiteral(
"</p>" );
331 std::unique_ptr< QgsLabelingEngineRuleMinimumDistanceLabelToLabel> res = std::make_unique< QgsLabelingEngineRuleMinimumDistanceLabelToLabel >();
333 res->mLabeledLayer = mLabeledLayer;
334 res->mTargetLayer = mTargetLayer;
335 res->mDistance = mDistance;
336 res->mDistanceUnit = mDistanceUnit;
337 res->mDistanceUnitScale = mDistanceUnitScale;
338 return res.release();
343 return QStringLiteral(
"minimumDistanceLabelToLabel" );
348 return QObject::tr(
"Push Labels Away from Other Labels" );
353 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
356 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must be at least %2 %3 from labels from <i>%4</i>" ).arg(
361 ) + QStringLiteral(
"</p>" );
368#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
377 element.setAttribute( QStringLiteral(
"distance" ), mDistance );
383 element.setAttribute( QStringLiteral(
"labeledLayer" ), mLabeledLayer.
layerId );
384 element.setAttribute( QStringLiteral(
"labeledLayerName" ), mLabeledLayer.
name );
385 element.setAttribute( QStringLiteral(
"labeledLayerSource" ), mLabeledLayer.
source );
386 element.setAttribute( QStringLiteral(
"labeledLayerProvider" ), mLabeledLayer.
provider );
390 element.setAttribute( QStringLiteral(
"targetLayer" ), mTargetLayer.
layerId );
391 element.setAttribute( QStringLiteral(
"targetLayerName" ), mTargetLayer.
name );
392 element.setAttribute( QStringLiteral(
"targetLayerSource" ), mTargetLayer.
source );
393 element.setAttribute( QStringLiteral(
"targetLayerProvider" ), mTargetLayer.
provider );
399 mDistance = element.attribute( QStringLiteral(
"distance" ), QStringLiteral(
"5" ) ).toDouble();
404 const QString layerId = element.attribute( QStringLiteral(
"labeledLayer" ) );
405 const QString layerName = element.attribute( QStringLiteral(
"labeledLayerName" ) );
406 const QString layerSource = element.attribute( QStringLiteral(
"labeledLayerSource" ) );
407 const QString layerProvider = element.attribute( QStringLiteral(
"labeledLayerProvider" ) );
408 mLabeledLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
411 const QString layerId = element.attribute( QStringLiteral(
"targetLayer" ) );
412 const QString layerName = element.attribute( QStringLiteral(
"targetLayerName" ) );
413 const QString layerSource = element.attribute( QStringLiteral(
"targetLayerSource" ) );
414 const QString layerProvider = element.attribute( QStringLiteral(
"targetLayerProvider" ) );
415 mTargetLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
421 mLabeledLayer.
resolve( project );
422 mTargetLayer.
resolve( project );
427 mDistanceMapUnits = context.
convertToMapUnits( mDistance, mDistanceUnit, mDistanceUnitScale );
433 return candidateBounds.
buffered( mDistanceMapUnits );
450#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
460 catch ( GEOSException &e )
462 QgsDebugError( QStringLiteral(
"GEOS exception: %1" ).arg( e.what() ) );
471 return mLabeledLayer.
get();
476 mLabeledLayer = layer;
481 return mTargetLayer.
get();
486 mTargetLayer = layer;
499 std::unique_ptr< QgsLabelingEngineRuleAvoidLabelOverlapWithFeature> res = std::make_unique< QgsLabelingEngineRuleAvoidLabelOverlapWithFeature >();
501 res->mLabeledLayer = mLabeledLayer;
502 res->mTargetLayer = mTargetLayer;
503 return res.release();
508 return QStringLiteral(
"avoidLabelOverlapWithFeature" );
513 return QObject::tr(
"Prevent Labels Overlapping Features" );
518 QString res = QStringLiteral(
"<b>%1</b>" ).arg(
name().isEmpty() ?
displayType() :
name() );
521 res += QStringLiteral(
"<p>" ) + QObject::tr(
"Labels from <i>%1</i> must not overlap features from <i>%2</i>" ).arg(
524 ) + QStringLiteral(
"</p>" );
535 mTargetLayerSource = std::make_unique< QgsVectorLayerFeatureSource >( mTargetLayer.
get() );
543 element.setAttribute( QStringLiteral(
"labeledLayer" ), mLabeledLayer.
layerId );
544 element.setAttribute( QStringLiteral(
"labeledLayerName" ), mLabeledLayer.
name );
545 element.setAttribute( QStringLiteral(
"labeledLayerSource" ), mLabeledLayer.
source );
546 element.setAttribute( QStringLiteral(
"labeledLayerProvider" ), mLabeledLayer.
provider );
550 element.setAttribute( QStringLiteral(
"targetLayer" ), mTargetLayer.
layerId );
551 element.setAttribute( QStringLiteral(
"targetLayerName" ), mTargetLayer.
name );
552 element.setAttribute( QStringLiteral(
"targetLayerSource" ), mTargetLayer.
source );
553 element.setAttribute( QStringLiteral(
"targetLayerProvider" ), mTargetLayer.
provider );
560 const QString layerId = element.attribute( QStringLiteral(
"labeledLayer" ) );
561 const QString layerName = element.attribute( QStringLiteral(
"labeledLayerName" ) );
562 const QString layerSource = element.attribute( QStringLiteral(
"labeledLayerSource" ) );
563 const QString layerProvider = element.attribute( QStringLiteral(
"labeledLayerProvider" ) );
564 mLabeledLayer =
QgsMapLayerRef( layerId, layerName, layerSource, layerProvider );
567 const QString layerId = element.attribute( QStringLiteral(
"targetLayer" ) );
568 const QString layerName = element.attribute( QStringLiteral(
"targetLayerName" ) );
569 const QString layerSource = element.attribute( QStringLiteral(
"targetLayerSource" ) );
570 const QString layerProvider = element.attribute( QStringLiteral(
"targetLayerProvider" ) );
571 mTargetLayer =
QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
577 mLabeledLayer.
resolve( project );
578 mTargetLayer.
resolve( project );
588 if ( !mTargetLayerSource )
594 const QList<QgsFeatureId> overlapCandidates = mIndex->intersects( candidate->
outerBoundingBox() );
595 if ( overlapCandidates.empty() )
601 for (
const QgsFeatureId overlapCandidateId : overlapCandidates )
608 geos::unique_ptr featureCandidate =
QgsGeos::asGeos( mIndex->geometry( overlapCandidateId ).constGet() );
609 if ( GEOSPreparedIntersects_r( geosctxt, candidateGeos, featureCandidate.get() ) == 1 )
612 catch ( GEOSException &e )
614 QgsDebugError( QStringLiteral(
"GEOS exception: %1" ).arg( e.what() ) );
623 return mLabeledLayer.
get();
628 mLabeledLayer = layer;
633 return mTargetLayer.
get();
638 mTargetLayer = layer;
QString layerId() const
Returns ID of associated layer, or empty string if no layer is associated with the provider.
Base class for labeling engine rules which prevents labels being placed too close or to far from feat...
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.
This class 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.
A labeling engine rule which prevents labels being placed overlapping features from a different layer...
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
A labeling engine rule which prevents labels being placed too far from features from a different laye...
~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()
A labeling engine rule which prevents labels being placed too close to features from a different laye...
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.
A labeling engine rule which prevents labels being placed too close to labels from a different layer.
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)
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,...
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
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 data sets.
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.
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
QString source
Weak reference to layer public source.
QString name
Weak reference to layer name.
TYPE * get() const
Returns a pointer to the layer, or nullptr if the reference has not yet been matched to a layer.
QString provider
Weak reference to layer provider.
TYPE * resolve(const QgsProject *project)
Resolves the map layer by attempting to find a layer with matching ID within a project.
QString layerId
Original layer ID.