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