QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
layer.h
Go to the documentation of this file.
1 /*
2  * libpal - Automated Placement of Labels Library
3  *
4  * Copyright (C) 2008 Maxence Laurent, MIS-TIC, HEIG-VD
5  * University of Applied Sciences, Western Switzerland
6  * http://www.hes-so.ch
7  *
8  * Contact:
9  * maxence.laurent <at> heig-vd <dot> ch
10  * or
11  * eric.taillard <at> heig-vd <dot> ch
12  *
13  * This file is part of libpal.
14  *
15  * libpal is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * libpal is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with libpal. If not, see <http://www.gnu.org/licenses/>.
27  *
28  */
29 
30 #ifndef PAL_LAYER_H_
31 #define PAL_LAYER_H_
32 
33 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include "pal.h" // for LineArrangementFlags enum
38 #include "rtree.hpp"
39 #include <QMutex>
40 #include <QLinkedList>
41 #include <QHash>
42 #include <fstream>
43 
44 class QgsLabelFeature;
45 
46 namespace pal
47 {
48 
50  template<class DATATYPE, class ELEMTYPE, int NUMDIMS, class ELEMTYPEREAL, int TMAXNODES, int TMINNODES> class RTree;
52 
53  class FeaturePart;
54  class Pal;
55  class LabelInfo;
56 
63  class CORE_EXPORT Layer
64  {
65  friend class Pal;
66  friend class FeaturePart;
67 
68  friend class Problem;
69 
70  friend class LabelPosition;
71 
72  public:
74  {
75  Upright, // upside-down labels (90 <= angle < 270) are shown upright
76  ShowDefined, // show upside down when rotation is layer- or data-defined
77  ShowAll // show upside down for all labels, including dynamic ones
78  };
79 
80  virtual ~Layer();
81 
82  bool displayAll() const { return mDisplayAll; }
83 
87  int featureCount() { return mHashtable.size(); }
88 
94  {
95  // when an extreme number of features exist in the layer, we limit the number of candidates
96  // to avoid the engine processing endlessly...
97  const int size = mHashtable.size();
98  if ( size > 1000 )
99  return std::min( pal->point_p, 4 );
100  else if ( size > 500 )
101  return std::min( pal->point_p, 6 );
102  else if ( size > 200 )
103  return std::min( pal->point_p, 8 );
104  else if ( size > 100 )
105  return std::min( pal->point_p, 12 );
106  else
107  return pal->point_p;
108  }
109 
115  {
116  // when an extreme number of features exist in the layer, we limit the number of candidates
117  // to avoid the engine processing endlessly...
118  const int size = mHashtable.size();
119  if ( size > 1000 )
120  return std::min( pal->line_p, 5 );
121  else if ( size > 500 )
122  return std::min( pal->line_p, 10 );
123  else if ( size > 200 )
124  return std::min( pal->line_p, 20 );
125  else if ( size > 100 )
126  return std::min( pal->line_p, 40 );
127  else
128  return pal->line_p;
129  }
130 
136  {
137  // when an extreme number of features exist in the layer, we limit the number of candidates
138  // to avoid the engine processing endlessly...
139  const int size = mHashtable.size();
140  if ( size > 1000 )
141  return std::min( pal->poly_p, 5 );
142  else if ( size > 500 )
143  return std::min( pal->poly_p, 15 );
144  else if ( size > 200 )
145  return std::min( pal->poly_p, 20 );
146  else if ( size > 100 )
147  return std::min( pal->poly_p, 25 );
148  else
149  return pal->poly_p;
150  }
151 
153  QgsAbstractLabelProvider *provider() const { return mProvider; }
154 
158  QString name() const { return mName; }
159 
164  QgsPalLayerSettings::Placement arrangement() const { return mArrangement; }
165 
169  bool isCurved() const { return mArrangement == QgsPalLayerSettings::Curved || mArrangement == QgsPalLayerSettings::PerimeterCurved; }
170 
176  void setArrangement( QgsPalLayerSettings::Placement arrangement ) { mArrangement = arrangement; }
177 
188  void setActive( bool active ) { mActive = active; }
189 
194  bool active() const { return mActive; }
195 
203  void setLabelLayer( bool toLabel ) { mLabelLayer = toLabel; }
204 
209  bool labelLayer() const { return mLabelLayer; }
210 
216  QgsPalLayerSettings::ObstacleType obstacleType() const { return mObstacleType; }
217 
224  void setObstacleType( QgsPalLayerSettings::ObstacleType obstacleType ) { mObstacleType = obstacleType; }
225 
232  void setPriority( double priority );
233 
239  double priority() const { return mDefaultPriority; }
240 
246  void setMergeConnectedLines( bool merge ) { mMergeLines = merge; }
247 
252  bool mergeConnectedLines() const { return mMergeLines; }
253 
259  void setUpsidedownLabels( UpsideDownLabels ud ) { mUpsidedownLabels = ud; }
260 
265  UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
266 
274  void setCentroidInside( bool forceInside ) { mCentroidInside = forceInside; }
275 
281  bool centroidInside() const { return mCentroidInside; }
282 
292  bool registerFeature( QgsLabelFeature *label );
293 
295  void joinConnectedFeatures();
296 
302  int connectedFeatureId( QgsFeatureId featureId ) const;
303 
305  void chopFeaturesAtRepeatDistance();
306 
307  protected:
309  QString mName;
310 
312  QLinkedList<FeaturePart *> mFeatureParts;
313 
315  QList<FeaturePart *> mObstacleParts;
316 
317  Pal *pal = nullptr;
318 
320 
322  bool mActive;
326 
329 
331 
333 
334  // indexes (spatial and id)
335  RTree<FeaturePart *, double, 2, double, 8, 4> *mFeatureIndex;
337  QHash< QgsFeatureId, QgsLabelFeature *> mHashtable;
338 
339  //obstacle r-tree
340  RTree<FeaturePart *, double, 2, double, 8, 4> *mObstacleIndex;
341 
342  QHash< QString, QVector<FeaturePart *> > mConnectedHashtable;
343  QHash< QgsFeatureId, int > mConnectedFeaturesIds;
344 
345  QMutex mMutex;
346 
360  Layer( QgsAbstractLabelProvider *provider, const QString &name, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll = false );
361 
363  void addFeaturePart( FeaturePart *fpart, const QString &labelText = QString() );
364 
366  void addObstaclePart( FeaturePart *fpart );
367 
368  };
369 
370 } // end namespace pal
371 
372 #endif
bool labelLayer() const
Returns whether the layer will be labeled or not.
Definition: layer.h:209
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition: layer.h:153
bool centroidInside() const
Returns whether labels placed at the centroid of features within the layer are forced to be placed in...
Definition: layer.h:281
QList< FeaturePart * > mObstacleParts
List of obstacle parts.
Definition: layer.h:315
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition: layer.h:343
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition: layer.h:252
bool mMergeLines
Definition: layer.h:330
bool mActive
Definition: layer.h:322
bool mLabelLayer
Definition: layer.h:323
QHash< QgsFeatureId, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them) ...
Definition: layer.h:337
Arranges candidates following the curvature of a line feature. Applies to line layers only...
A set of features which influence the labeling process.
Definition: layer.h:63
double mDefaultPriority
Definition: layer.h:319
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
Main Pal labeling class.
Definition: pal.h:87
UpsideDownLabels
Definition: layer.h:73
void setUpsidedownLabels(UpsideDownLabels ud)
Sets how upside down labels will be handled within the layer.
Definition: layer.h:259
int maximumPointLabelCandidates() const
Returns the maximum number of point label candidates to generate for features in this layer...
Definition: layer.h:93
UpsideDownLabels upsidedownLabels() const
Returns how upside down labels are handled within the layer.
Definition: layer.h:265
void setObstacleType(QgsPalLayerSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels...
Definition: layer.h:224
void setCentroidInside(bool forceInside)
Sets whether labels placed at the centroid of features within the layer are forced to be placed insid...
Definition: layer.h:274
QgsPalLayerSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels...
Definition: layer.h:216
QString mName
Definition: layer.h:309
bool displayAll() const
Definition: layer.h:82
QHash< QString, QVector< FeaturePart * > > mConnectedHashtable
Definition: layer.h:342
double priority() const
Returns the layer&#39;s priority, between 0 and 1.
Definition: layer.h:239
RTree< FeaturePart *, double, 2, double, 8, 4 > * mFeatureIndex
Definition: layer.h:335
void setArrangement(QgsPalLayerSettings::Placement arrangement)
Sets the layer&#39;s arrangement policy.
Definition: layer.h:176
QgsPalLayerSettings::Placement arrangement() const
Returns the layer&#39;s arrangement policy.
Definition: layer.h:164
int featureCount()
Returns the number of features in layer.
Definition: layer.h:87
bool mCentroidInside
Definition: layer.h:325
QgsPalLayerSettings::ObstacleType mObstacleType
Definition: layer.h:321
Main class to handle feature.
Definition: feature.h:96
The QgsAbstractLabelProvider class is an interface class.
Placement
Placement modes which determine how label candidates are generated for a feature. ...
bool mDisplayAll
Definition: layer.h:324
void setActive(bool active)
Sets whether the layer is currently active.
Definition: layer.h:188
void setLabelLayer(bool toLabel)
Sets whether the layer will be labeled.
Definition: layer.h:203
QgsAbstractLabelProvider * mProvider
Definition: layer.h:308
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
int maximumPolygonLabelCandidates() const
Returns the maximum number of polygon label candidates to generate for features in this layer...
Definition: layer.h:135
int maximumLineLabelCandidates() const
Returns the maximum number of line label candidates to generate for features in this layer...
Definition: layer.h:114
QLinkedList< FeaturePart * > mFeatureParts
List of feature parts.
Definition: layer.h:312
RTree< FeaturePart *, double, 2, double, 8, 4 > * mObstacleIndex
Definition: layer.h:340
QMutex mMutex
Definition: layer.h:345
QString name() const
Returns the layer&#39;s name.
Definition: layer.h:158
bool active() const
Returns whether the layer is currently active.
Definition: layer.h:194
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
Representation of a labeling problem.
Definition: problem.h:73
QgsPalLayerSettings::Placement mArrangement
Optional flags used for some placement methods.
Definition: layer.h:328
Arranges candidates following the curvature of a polygon&#39;s boundary. Applies to polygon layers only...
bool isCurved() const
Returns true if the layer has curved labels.
Definition: layer.h:169
UpsideDownLabels mUpsidedownLabels
Definition: layer.h:332
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
Definition: layer.h:246
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels...