QGIS API Documentation  3.6.0-Noosa (5873452)
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 
18 bool searchCallback( QgsLabelPosition *pos, void *context )
19 {
20  QList<QgsLabelPosition *> *list = static_cast< QList<QgsLabelPosition *>* >( context );
21  list->push_back( pos );
22  return true;
23 }
24 
26 {
27  clear();
28 }
29 
30 void QgsLabelSearchTree::label( const QgsPointXY &p, QList<QgsLabelPosition *> &posList ) const
31 {
32  double c_min[2];
33  c_min[0] = p.x() - 0.1;
34  c_min[1] = p.y() - 0.1;
35  double c_max[2];
36  c_max[0] = p.x() + 0.1;
37  c_max[1] = p.y() + 0.1;
38 
39  QList<QgsLabelPosition *> searchResults;
40  mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
41 
42  //tolerance +-0.1 could be high in case of degree crs, so check if p is really contained in the results
43  posList.clear();
44  QList<QgsLabelPosition *>::const_iterator resultIt = searchResults.constBegin();
45  for ( ; resultIt != searchResults.constEnd(); ++resultIt )
46  {
47  if ( ( *resultIt )->labelRect.contains( p ) )
48  {
49  posList.push_back( *resultIt );
50  }
51  }
52 }
53 
54 void QgsLabelSearchTree::labelsInRect( const QgsRectangle &r, QList<QgsLabelPosition *> &posList ) const
55 {
56  double c_min[2];
57  c_min[0] = r.xMinimum();
58  c_min[1] = r.yMinimum();
59  double c_max[2];
60  c_max[0] = r.xMaximum();
61  c_max[1] = r.yMaximum();
62 
63  QList<QgsLabelPosition *> searchResults;
64  mSpatialIndex.Search( c_min, c_max, searchCallback, &searchResults );
65 
66  posList.clear();
67  QList<QgsLabelPosition *>::const_iterator resultIt = searchResults.constBegin();
68  for ( ; resultIt != searchResults.constEnd(); ++resultIt )
69  {
70  posList.push_back( *resultIt );
71  }
72 }
73 
74 bool QgsLabelSearchTree::insertLabel( pal::LabelPosition *labelPos, int featureId, const QString &layerName, const QString &labeltext, const QFont &labelfont, bool diagram, bool pinned, const QString &providerId )
75 {
76  if ( !labelPos )
77  {
78  return false;
79  }
80 
81  double c_min[2];
82  double c_max[2];
83  labelPos->getBoundingBox( c_min, c_max );
84 
85  QVector<QgsPointXY> cornerPoints;
86  cornerPoints.reserve( 4 );
87  for ( int i = 0; i < 4; ++i )
88  {
89  cornerPoints.push_back( QgsPointXY( labelPos->getX( i ), labelPos->getY( i ) ) );
90  }
91  QgsLabelPosition *newEntry = new QgsLabelPosition( featureId, labelPos->getAlpha(), cornerPoints, QgsRectangle( c_min[0], c_min[1], c_max[0], c_max[1] ),
92  labelPos->getWidth(), labelPos->getHeight(), layerName, labeltext, labelfont, labelPos->getUpsideDown(), diagram, pinned, providerId );
93  mSpatialIndex.Insert( c_min, c_max, newEntry );
94  mOwnedPositions << newEntry;
95  return true;
96 }
97 
99 {
100  mSpatialIndex.RemoveAll();
101 
102  //PAL rtree iterator is buggy and doesn't iterate over all items, so we can't iterate through the tree to delete positions
103  qDeleteAll( mOwnedPositions );
104  mOwnedPositions.clear();
105 }
A rectangle specified with double values.
Definition: qgsrectangle.h:41
void getBoundingBox(double amin[2], double amax[2]) const
Returns bounding box - amin: xmin,ymin - amax: xmax,ymax.
double getY(int i=0) const
Returns the down-left y coordinate.
double y
Definition: qgspointxy.h:48
A class to represent a 2D point.
Definition: qgspointxy.h:43
bool insertLabel(pal::LabelPosition *labelPos, int featureId, const QString &layerName, const QString &labeltext, const QFont &labelfont, bool diagram=false, bool pinned=false, const QString &providerId=QString())
Inserts label position.
void labelsInRect(const QgsRectangle &r, QList< QgsLabelPosition *> &posList) const
Returns label position(s) in given rectangle.
double getHeight() const
bool searchCallback(QgsLabelPosition *pos, void *context)
double x
Definition: qgspointxy.h:47
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
double getAlpha() const
Returns the angle to rotate text (in rad).
double getWidth() const
double getX(int i=0) const
Returns the down-left x coordinate.
void clear()
Removes and deletes all the entries.
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
bool getUpsideDown() const
void label(const QgsPointXY &p, QList< QgsLabelPosition *> &posList) const
Returns label position(s) at a given point.