QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgslabelsearchtree.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslabelsearchtree.cpp
3 ---------------------
4 begin : November 2010
5 copyright : (C) 2010 by Marco Hugentobler
6 email : marco dot hugentobler at sourcepole dot ch
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#include "qgslabelsearchtree.h"
16
17#include "labelposition.h"
18
20
22
23void QgsLabelSearchTree::label( const QgsPointXY &point, QList<QgsLabelPosition *> &posList ) const
24{
25 const QgsPointXY p( point );
26
27 QList<QgsLabelPosition *> searchResults;
28 mSpatialIndex.intersects( QgsRectangle( p.x() - 0.1, p.y() - 0.1, p.x() + 0.1, p.y() + 0.1 ), [&searchResults]( const QgsLabelPosition * pos ) -> bool
29 {
30 searchResults.push_back( const_cast< QgsLabelPosition * >( pos ) );
31 return true;
32 } );
33
34 //tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
35 posList.clear();
36 QList<QgsLabelPosition *>::const_iterator resultIt = searchResults.constBegin();
37 for ( ; resultIt != searchResults.constEnd(); ++resultIt )
38 {
39 if ( ( *resultIt )->labelGeometry.contains( &p ) )
40 {
41 posList.push_back( *resultIt );
42 }
43 }
44}
45
46QList<QgsLabelPosition> QgsLabelSearchTree::allLabels() const
47{
48 QList<QgsLabelPosition> res;
49 res.reserve( mOwnedPositions.size() );
50 for ( const std::unique_ptr< QgsLabelPosition > &pos : mOwnedPositions )
51 {
52 res.append( * pos );
53 }
54 return res;
55}
56
57void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
58{
59 QList<QgsLabelPosition *> searchResults;
60 mSpatialIndex.intersects( r, [&searchResults]( const QgsLabelPosition * pos )->bool
61 {
62 searchResults.push_back( const_cast< QgsLabelPosition * >( pos ) );
63 return true;
64 } );
65
66 posList.clear();
67 QList<QgsLabelPosition *>::const_iterator resultIt = searchResults.constBegin();
68 for ( ; resultIt != searchResults.constEnd(); ++resultIt )
69 {
70 if ( ( *resultIt )->labelGeometry.intersects( r ) )
71 {
72 posList.push_back( *resultIt );
73 }
74 }
75}
76
77bool QgsLabelSearchTree::insertLabel( pal::LabelPosition *labelPos, QgsFeatureId featureId, const QString &layerName, const QString &labeltext, const QFont &labelfont, bool diagram, bool pinned, const QString &providerId, bool isUnplaced, long long linkedId )
78{
79 if ( !labelPos )
80 {
81 return false;
82 }
83
84 QVector<QgsPointXY> cornerPoints;
85 cornerPoints.reserve( 4 );
86 double xMin = std::numeric_limits< double >::max();
87 double yMin = std::numeric_limits< double >::max();
88 double xMax = std::numeric_limits< double >::lowest();
89 double yMax = std::numeric_limits< double >::lowest();
90 for ( int i = 0; i < 4; ++i )
91 {
92 // we have to transform the bounding box to convert pre-rotated label positions back to real world locations
93 const QPointF res = mTransform.map( QPointF( labelPos->getX( i ), labelPos->getY( i ) ) );
94 cornerPoints.push_back( QgsPointXY( res ) );
95 xMin = std::min( xMin, res.x() );
96 xMax = std::max( xMax, res.x() );
97 yMin = std::min( yMin, res.y() );
98 yMax = std::max( yMax, res.y() );
99 }
100
101 pal::LabelPosition *next = labelPos->nextPart();
102 long long uniqueLinkedId = 0;
103 if ( linkedId != 0 )
104 uniqueLinkedId = linkedId;
105 else if ( next )
106 uniqueLinkedId = mNextFeatureId++;
107
108 const QgsRectangle bounds( xMin, yMin, xMax, yMax );
109 const QgsGeometry labelGeometry( QgsGeometry::fromPolygonXY( QVector<QgsPolylineXY>() << cornerPoints ) );
110 auto newEntry = std::make_unique< QgsLabelPosition >( featureId, -labelPos->getAlpha() * 180 / M_PI + mMapSettings.rotation(), cornerPoints, bounds,
111 labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned, providerId, labelGeometry, isUnplaced );
112 newEntry->groupedLabelId = uniqueLinkedId;
113 mSpatialIndex.insert( newEntry.get(), bounds );
114
115 if ( uniqueLinkedId != 0 )
116 {
117 mLinkedLabelHash[ uniqueLinkedId ].append( newEntry.get() );
118 }
119
120 mOwnedPositions.emplace_back( std::move( newEntry ) );
121
122 if ( next )
123 {
124 return insertLabel( next, featureId, layerName, labeltext, labelfont, diagram, pinned, providerId, isUnplaced, uniqueLinkedId );
125 }
126 return true;
127}
128
130{
131 const QPointF origin = position.origin();
132 const QPointF destination = position.destination();
133
134 auto newEntry = std::make_unique< QgsCalloutPosition >( position );
135
136 mCalloutIndex.insert( newEntry.get(), QgsRectangle( origin.x(), origin.y(), origin.x(), origin.y() ) );
137 mCalloutIndex.insert( newEntry.get(), QgsRectangle( destination.x(), destination.y(), destination.x(), destination.y() ) );
138
139 mOwnedCalloutPositions.emplace_back( std::move( newEntry ) );
140
141 return true;
142}
143
144QList<const QgsCalloutPosition *> QgsLabelSearchTree::calloutsInRectangle( const QgsRectangle &rectangle ) const
145{
146 QList<const QgsCalloutPosition *> searchResults;
147 mCalloutIndex.intersects( rectangle, [&searchResults]( const QgsCalloutPosition * pos )->bool
148 {
149 searchResults.push_back( pos );
150 return true;
151 } );
152
153 std::sort( searchResults.begin(), searchResults.end() );
154 searchResults.erase( std::unique( searchResults.begin(), searchResults.end() ), searchResults.end() );
155
156 return searchResults;
157}
158
159QList<QgsLabelPosition *> QgsLabelSearchTree::groupedLabelPositions( long long groupId ) const
160{
161 return mLinkedLabelHash.value( groupId );
162}
163
165{
166 mMapSettings = settings;
167
168 if ( !qgsDoubleNear( mMapSettings.rotation(), 0.0 ) )
169 {
170 // build a transform to convert points from real world to pre-rotated label positions
171 const QgsPointXY center = mMapSettings.visibleExtent().center();
172 mTransform = QTransform::fromTranslate( center.x(), center.y() );
173 mTransform.rotate( mMapSettings.rotation() );
174 mTransform.translate( -center.x(), -center.y() );
175 }
176 else
177 {
178 mTransform = QTransform();
179 }
180}
181
182
184{
185
186}
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.
static QgsGeometry fromPolygonXY(const QgsPolygonXY &polygon)
Creates a new geometry from a QgsPolygonXY.
Represents the calculated placement of a map label.
QList< QgsLabelPosition * > groupedLabelPositions(long long groupId) const
Returns a list of all label positions sharing the same group ID (i.e.
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)
Inserts label position.
Q_DECL_DEPRECATED void clear()
Removes and deletes all the entries.
void label(const QgsPointXY &p, QList< QgsLabelPosition * > &posList) const
Returns label position(s) at a given point.
void labelsInRect(const QgsRectangle &r, QList< QgsLabelPosition * > &posList) const
Returns label position(s) in given rectangle.
QList< const QgsCalloutPosition * > calloutsInRectangle(const QgsRectangle &rectangle) const
Returns the list of callouts with origins or destinations inside the given rectangle.
bool insertCallout(const QgsCalloutPosition &position)
Inserts a rendered callout 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.
Contains configuration for rendering maps.
Represents a 2D point.
Definition qgspointxy.h:60
double y
Definition qgspointxy.h:64
double x
Definition qgspointxy.h:63
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 getHeight() const
double getWidth() const
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).
Definition qgis.h:6607
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features