26 : mLabelSearchTree( enableSearchTree ? std::make_unique<
QgsLabelSearchTree >() : nullptr )
33 return static_cast< bool >( mLabelSearchTree );
39 const QString &layerName,
40 const QString &labeltext,
41 const QFont &labelfont,
44 const QString &providerId,
47 long long linkedPositionIndex,
56 QVector<QgsPointXY> cornerPoints;
57 QVector< double > cornerPointsX;
58 QVector< double > cornerPointsY;
59 cornerPoints.resize( 4 );
60 cornerPointsX.resize( 5 );
61 cornerPointsY.resize( 5 );
62 double xMin = std::numeric_limits< double >::max();
63 double yMin = std::numeric_limits< double >::max();
64 double xMax = std::numeric_limits< double >::lowest();
65 double yMax = std::numeric_limits< double >::lowest();
66 for (
int i = 0; i < 4; ++i )
69 const QPointF res = mTransform.map( QPointF( labelPos->
getX( i ), labelPos->
getY( i ) ) );
71 cornerPointsX[i] = res.x();
72 cornerPointsY[i] = res.y();
73 xMin = std::min( xMin, res.x() );
74 xMax = std::max( xMax, res.x() );
75 yMin = std::min( yMin, res.y() );
76 yMax = std::max( yMax, res.y() );
78 cornerPointsX[4] = cornerPointsX[0];
79 cornerPointsY[4] = cornerPointsY[0];
82 long long uniqueLinkedId = 0;
84 uniqueLinkedId = linkedId;
86 uniqueLinkedId = mNextFeatureId++;
89 const QgsGeometry labelGeometry( std::make_unique< QgsPolygon >(
new QgsLineString( cornerPointsX, cornerPointsY ) ) );
90 auto newEntry = std::make_unique< QgsLabelPosition >(
92 -labelPos->
getAlpha() * 180 / M_PI + mMapSettings.rotation(),
107 newEntry->groupedLabelId = uniqueLinkedId;
108 newEntry->groupedPositionIndex = linkedPositionIndex;
109 newEntry->subPartId = subPartId;
111 if ( uniqueLinkedId != 0 )
113 mLinkedLabelHash[uniqueLinkedId].append( newEntry.get() );
116 if ( mLabelSearchTree )
118 mLabelSearchTree->mSpatialIndex.insert( newEntry.get(), bounds );
120 mOwnedPositions.emplace_back( std::move( newEntry ) );
124 return insertLabel( next, featureId, layerName, labeltext, labelfont, diagram, pinned, providerId, isUnplaced, uniqueLinkedId, linkedPositionIndex + 1, subPartId );
131 const QPointF origin = position.
origin();
132 const QPointF destination = position.
destination();
134 auto newEntry = std::make_unique< QgsCalloutPosition >( position );
136 if ( mLabelSearchTree )
138 mLabelSearchTree->mCalloutIndex.insert( newEntry.get(),
QgsRectangle( origin.x(), origin.y(), origin.x(), origin.y() ) );
139 mLabelSearchTree->mCalloutIndex.insert( newEntry.get(),
QgsRectangle( destination.x(), destination.y(), destination.x(), destination.y() ) );
142 mOwnedCalloutPositions.emplace_back( std::move( newEntry ) );
149 QList<QgsLabelPosition> res;
150 res.reserve(
static_cast< qsizetype
>( mOwnedPositions.size() ) );
151 for (
const std::unique_ptr< QgsLabelPosition > &pos : mOwnedPositions )
160 QList<QgsLabelPosition> positions;
162 QList<QgsLabelPosition *> positionPointers;
163 if ( mLabelSearchTree )
165 mLabelSearchTree->label( p, positionPointers );
166 QList<QgsLabelPosition *>::const_iterator pointerIt = positionPointers.constBegin();
167 for ( ; pointerIt != positionPointers.constEnd(); ++pointerIt )
178 QList<QgsLabelPosition> positions;
180 QList<QgsLabelPosition *> positionPointers;
181 if ( mLabelSearchTree )
183 mLabelSearchTree->labelsInRect( r, positionPointers );
184 QList<QgsLabelPosition *>::const_iterator pointerIt = positionPointers.constBegin();
185 for ( ; pointerIt != positionPointers.constEnd(); ++pointerIt )
196 QList<QgsLabelPosition> positions;
197 if ( mLabelSearchTree )
199 const QList<QgsLabelPosition *> positionPointers = mLinkedLabelHash.value( groupId );
200 positions.reserve( positionPointers.size() );
209 QList<QgsCalloutPosition> positions;
211 if ( mLabelSearchTree )
213 const QList<const QgsCalloutPosition *> positionPointers = mLabelSearchTree->calloutsInRectangle( rectangle );
225 mMapSettings = settings;
230 mTransform = QTransform::fromTranslate( center.
x(), center.
y() );
231 mTransform.rotate( mMapSettings.rotation() );
232 mTransform.translate( -center.
x(), -center.
y() );
236 mTransform = QTransform();
Represents the calculated placement of a map label callout line.
QPointF origin() const
Returns the origin of the callout line, in map coordinates.
QPointF destination() const
Returns the destination of the callout line, in map coordinates.
A geometry is the spatial representation of a feature.
Represents the calculated placement of a map label.
Queries the labeling structure at a given point.
bool insertCallout(const QgsCalloutPosition &position)
Inserts a rendered callout position.
bool hasSearchTree() const
Returns true if the results were constructed with search tree enabled.
QList< QgsCalloutPosition > calloutsWithinRectangle(const QgsRectangle &rectangle) const
Returns a list of callouts with origins or destinations inside the given rectangle.
QgsLabelingResults(bool enableSearchTree=true)
Constructor for QgsLabelingResults.
bool insertLabel(pal::LabelPosition *labelPos, QgsFeatureId featureId, const QString &layerName, const QString &labeltext, const QFont &labelfont, bool diagram=false, bool pinned=false, const QString &providerId=QString(), bool isUnplaced=false, long long linkedId=0, long long linkedPositionIndex=0, long long subPartId=0)
Inserts label position.
void setMapSettings(const QgsMapSettings &settings)
Sets the map settings associated with the labeling run.
QList< QgsLabelPosition > allLabels() const
Returns a list of all labels generated by the labeling run.
QList< QgsLabelPosition > labelsAtPosition(const QgsPointXY &p) const
Returns the details of any labels placed at the specified point (in map coordinates).
QList< QgsLabelPosition > groupedLabelPositions(long long groupId) const
Returns a list of all label positions sharing the same group ID (i.e.
QList< QgsLabelPosition > labelsWithinRect(const QgsRectangle &r) const
Returns the details of any labels placed within the specified rectangle (in map coordinates).
Line string geometry type, with support for z-dimension and m-values.
Contains configuration for rendering maps.
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes output image size into account.
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
A rectangle specified with double values.
LabelPosition is a candidate feature label position.
double getAlpha() const
Returns the angle to rotate text (in radians).
double getX(int i=0) const
Returns the down-left x coordinate.
double getY(int i=0) const
Returns the down-left y coordinate.
bool getUpsideDown() const
LabelPosition * nextPart() const
Returns the next part of this label position (i.e.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features