QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 #include "labelposition.h"
17 
19 
21 
22 void QgsLabelSearchTree::label( const QgsPointXY &point, QList<QgsLabelPosition *> &posList ) const
23 {
24  QgsPointXY p( point );
25 
26  QList<QgsLabelPosition *> searchResults;
27  mSpatialIndex.intersects( QgsRectangle( p.x() - 0.1, p.y() - 0.1, p.x() + 0.1, p.y() + 0.1 ), [&searchResults]( const QgsLabelPosition * pos ) -> bool
28  {
29  searchResults.push_back( const_cast< QgsLabelPosition * >( pos ) );
30  return true;
31  } );
32 
33  //tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
34  posList.clear();
35  QList<QgsLabelPosition *>::const_iterator resultIt = searchResults.constBegin();
36  for ( ; resultIt != searchResults.constEnd(); ++resultIt )
37  {
38  if ( ( *resultIt )->labelGeometry.contains( &p ) )
39  {
40  posList.push_back( *resultIt );
41  }
42  }
43 }
44 
45 void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
46 {
47  QList<QgsLabelPosition *> searchResults;
48  mSpatialIndex.intersects( r, [&searchResults]( const QgsLabelPosition * pos )->bool
49  {
50  searchResults.push_back( const_cast< QgsLabelPosition * >( pos ) );
51  return true;
52  } );
53 
54  posList.clear();
55  QList<QgsLabelPosition *>::const_iterator resultIt = searchResults.constBegin();
56  for ( ; resultIt != searchResults.constEnd(); ++resultIt )
57  {
58  if ( ( *resultIt )->labelGeometry.intersects( r ) )
59  {
60  posList.push_back( *resultIt );
61  }
62  }
63 }
64 
65 bool 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 )
66 {
67  if ( !labelPos )
68  {
69  return false;
70  }
71 
72  QVector<QgsPointXY> cornerPoints;
73  cornerPoints.reserve( 4 );
74  double xMin = std::numeric_limits< double >::max();
75  double yMin = std::numeric_limits< double >::max();
76  double xMax = std::numeric_limits< double >::lowest();
77  double yMax = std::numeric_limits< double >::lowest();
78  for ( int i = 0; i < 4; ++i )
79  {
80  // we have to transform the bounding box to convert pre-rotated label positions back to real world locations
81  QPointF res = mTransform.map( QPointF( labelPos->getX( i ), labelPos->getY( i ) ) );
82  cornerPoints.push_back( QgsPointXY( res ) );
83  xMin = std::min( xMin, res.x() );
84  xMax = std::max( xMax, res.x() );
85  yMin = std::min( yMin, res.y() );
86  yMax = std::max( yMax, res.y() );
87  }
88 
89  const QgsRectangle bounds( xMin, yMin, xMax, yMax );
90  QgsGeometry labelGeometry( QgsGeometry::fromPolygonXY( QVector<QgsPolylineXY>() << cornerPoints ) );
91  std::unique_ptr< QgsLabelPosition > newEntry = qgis::make_unique< QgsLabelPosition >( featureId, labelPos->getAlpha() + mMapSettings.rotation(), cornerPoints, bounds,
92  labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned, providerId, labelGeometry, isUnplaced );
93  mSpatialIndex.insert( newEntry.get(), bounds );
94  mOwnedPositions.emplace_back( std::move( newEntry ) );
95 
96  if ( pal::LabelPosition *next = labelPos->nextPart() )
97  {
98  return insertLabel( next, featureId, layerName, labeltext, labelfont, diagram, pinned, providerId, isUnplaced );
99  }
100  return true;
101 }
102 
104 {
105  mMapSettings = settings;
106 
107  if ( !qgsDoubleNear( mMapSettings.rotation(), 0.0 ) )
108  {
109  // build a transform to convert points from real world to pre-rotated label positions
110  const QgsPointXY center = mMapSettings.visibleExtent().center();
111  mTransform = QTransform::fromTranslate( center.x(), center.y() );
112  mTransform.rotate( mMapSettings.rotation() );
113  mTransform.translate( -center.x(), -center.y() );
114  }
115  else
116  {
117  mTransform = QTransform();
118  }
119 }
120 
121 
123 {
124 
125 }
pal::LabelPosition::getY
double getY(int i=0) const
Returns the down-left y coordinate.
Definition: labelposition.cpp:353
QgsPointXY::y
double y
Definition: qgspointxy.h:48
qgslabelsearchtree.h
labelposition.h
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
QgsLabelSearchTree::label
void label(const QgsPointXY &p, QList< QgsLabelPosition * > &posList) const
Returns label position(s) at a given point.
Definition: qgslabelsearchtree.cpp:22
pal::LabelPosition::nextPart
LabelPosition * nextPart() const
Returns the next part of this label position (i.e.
Definition: labelposition.h:276
QgsRectangle
Definition: qgsrectangle.h:41
QgsLabelSearchTree::labelsInRect
void labelsInRect(const QgsRectangle &r, QList< QgsLabelPosition * > &posList) const
Returns label position(s) in given rectangle.
Definition: qgslabelsearchtree.cpp:45
QgsLabelPosition
Definition: qgspallabeling.h:85
QgsLabelSearchTree::clear
Q_DECL_DEPRECATED void clear()
Removes and deletes all the entries.
Definition: qgslabelsearchtree.cpp:122
QgsMapSettings::rotation
double rotation() const
Returns the rotation of the resulting map image, in degrees clockwise.
Definition: qgsmapsettings.cpp:101
QgsLabelSearchTree::QgsLabelSearchTree
QgsLabelSearchTree()
Constructor for QgsLabelSearchTree.
QgsGenericSpatialIndex::intersects
bool intersects(const QgsRectangle &bounds, const std::function< bool(T *data)> &callback) const
Performs an intersection check against the index, for data intersecting the specified bounds.
Definition: qgsgenericspatialindex.h:123
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsGenericSpatialIndex::insert
bool insert(T *data, const QgsRectangle &bounds)
Inserts new data into the spatial index, with the specified bounds.
Definition: qgsgenericspatialindex.h:63
pal::LabelPosition::getUpsideDown
bool getUpsideDown() const
Definition: labelposition.h:267
QgsLabelSearchTree::setMapSettings
void setMapSettings(const QgsMapSettings &settings)
Sets the map settings associated with the labeling run.
Definition: qgslabelsearchtree.cpp:103
QgsPointXY
Definition: qgspointxy.h:43
QgsLabelSearchTree::~QgsLabelSearchTree
~QgsLabelSearchTree()
QgsGeometry::fromPolygonXY
static QgsGeometry fromPolygonXY(const QgsPolygonXY &polygon)
Creates a new geometry from a QgsPolygon.
Definition: qgsgeometry.cpp:189
QgsRectangle::center
QgsPointXY center() const
Returns the center point of the rectangle.
Definition: qgsrectangle.h:230
pal::LabelPosition::getHeight
double getHeight() const
Definition: labelposition.h:260
QgsGeometry
Definition: qgsgeometry.h:122
QgsPointXY::x
double x
Definition: qgspointxy.h:47
pal::LabelPosition::getX
double getX(int i=0) const
Returns the down-left x coordinate.
Definition: labelposition.cpp:348
QgsLabelSearchTree::insertLabel
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)
Inserts label position.
Definition: qgslabelsearchtree.cpp:65
pal::LabelPosition::getAlpha
double getAlpha() const
Returns the angle to rotate text (in rad).
Definition: labelposition.cpp:358
QgsMapSettings
Definition: qgsmapsettings.h:86
QgsMapSettings::visibleExtent
QgsRectangle visibleExtent() const
Returns the actual extent derived from requested extent that takes takes output image size into accou...
Definition: qgsmapsettings.cpp:370
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
pal::LabelPosition::getWidth
double getWidth() const
Definition: labelposition.h:259