QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 "qgsgeos.h"
39 #include "qgsgenericspatialindex.h"
41 #include <QMutex>
42 #include <QLinkedList>
43 #include <QHash>
44 #include <fstream>
45 
46 class QgsLabelFeature;
47 
48 namespace pal
49 {
50 
51  class FeaturePart;
52 
53  class Pal;
54  class LabelInfo;
55 
62  class CORE_EXPORT Layer
63  {
64  friend class Pal;
65  friend class FeaturePart;
66 
67  friend class Problem;
68 
69  friend class LabelPosition;
70 
71  public:
72 
84  Layer( QgsAbstractLabelProvider *provider, const QString &name, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal );
85 
86  virtual ~Layer();
87 
91  int featureCount() { return mHashtable.size(); }
92 
97  std::size_t maximumPointLabelCandidates() const
98  {
99  // when an extreme number of features exist in the layer, we limit the number of candidates
100  // to avoid the engine processing endlessly...
101  const int size = mHashtable.size();
102  if ( size > 1000 )
103  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 4 ) : 4 );
104  else if ( size > 500 )
105  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 6 ) : 6 );
106  else if ( size > 200 )
107  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 8 ) : 8 );
108  else if ( size > 100 )
109  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 12 ) : 12 );
110  else
111  return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPoint(), 0 ) );
112  }
113 
118  std::size_t maximumLineLabelCandidates() const
119  {
120  // when an extreme number of features exist in the layer, we limit the number of candidates
121  // to avoid the engine processing endlessly...
122  const int size = mHashtable.size();
123  if ( size > 1000 )
124  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 5 ) : 5 );
125  else if ( size > 500 )
126  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 10 ) : 10 );
127  else if ( size > 200 )
128  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 20 ) : 20 );
129  else if ( size > 100 )
130  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 40 ) : 40 );
131  else
132  return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitLine(), 0 ) );
133  }
134 
139  std::size_t maximumPolygonLabelCandidates() const
140  {
141  // when an extreme number of features exist in the layer, we limit the number of candidates
142  // to avoid the engine processing endlessly...
143  const int size = mHashtable.size();
144  if ( size > 1000 )
145  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 5 ) : 5 );
146  else if ( size > 500 )
147  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 15 ) : 15 );
148  else if ( size > 200 )
149  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 20 ) : 20 );
150  else if ( size > 100 )
151  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 25 ) : 25 );
152  else
153  return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPolygon(), 0 ) );
154  }
155 
157  QgsAbstractLabelProvider *provider() const { return mProvider; }
158 
162  QString name() const { return mName; }
163 
168  Qgis::LabelPlacement arrangement() const { return mArrangement; }
169 
173  bool isCurved() const { return mArrangement == Qgis::LabelPlacement::Curved || mArrangement == Qgis::LabelPlacement::PerimeterCurved; }
174 
180  void setArrangement( Qgis::LabelPlacement arrangement ) { mArrangement = arrangement; }
181 
192  void setActive( bool active ) { mActive = active; }
193 
198  bool active() const { return mActive; }
199 
207  void setLabelLayer( bool toLabel ) { mLabelLayer = toLabel; }
208 
213  bool labelLayer() const { return mLabelLayer; }
214 
220  QgsLabelObstacleSettings::ObstacleType obstacleType() const { return mObstacleType; }
221 
228  void setObstacleType( QgsLabelObstacleSettings::ObstacleType obstacleType ) { mObstacleType = obstacleType; }
229 
236  void setPriority( double priority );
237 
243  double priority() const { return mDefaultPriority; }
244 
250  void setMergeConnectedLines( bool merge ) { mMergeLines = merge; }
251 
256  bool mergeConnectedLines() const { return mMergeLines; }
257 
263  void setUpsidedownLabels( Qgis::UpsideDownLabelHandling ud ) { mUpsidedownLabels = ud; }
264 
269  Qgis::UpsideDownLabelHandling upsidedownLabels() const { return mUpsidedownLabels; }
270 
278  void setCentroidInside( bool forceInside ) { mCentroidInside = forceInside; }
279 
285  bool centroidInside() const { return mCentroidInside; }
286 
296  bool registerFeature( QgsLabelFeature *label );
297 
299  void joinConnectedFeatures();
300 
306  int connectedFeatureId( QgsFeatureId featureId ) const;
307 
309  void chopFeaturesAtRepeatDistance();
310 
311  protected:
313  QString mName;
314 
316  std::deque< std::unique_ptr< FeaturePart > > mFeatureParts;
317 
319  QList<FeaturePart *> mObstacleParts;
320 
321  std::vector< geos::unique_ptr > mGeosObstacleGeometries;
322 
323  Pal *mPal = nullptr;
324 
326 
328  bool mActive;
330  bool mCentroidInside = false;
331 
334 
335  bool mMergeLines = false;
336 
338 
340  QHash< QgsFeatureId, QgsLabelFeature *> mHashtable;
341 
342  QHash< QString, QVector<FeaturePart *> > mConnectedHashtable;
343  QHash< QgsFeatureId, int > mConnectedFeaturesIds;
344 
345  QMutex mMutex;
346 
348  void addFeaturePart( std::unique_ptr< FeaturePart > fpart, const QString &labelText = QString() );
349 
351  void addObstaclePart( FeaturePart *fpart );
352 
353  };
354 
355 } // end namespace pal
356 
357 
358 #endif
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition: qgis.h:914
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition: qgis.h:1042
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
The QgsAbstractLabelProvider class is an interface class.
The QgsLabelFeature class describes a feature that should be used within the labeling engine.
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels.
@ PolygonBoundary
Avoid placing labels over boundary of polygon (prefer placing outside or completely inside polygon)
Main class to handle feature.
Definition: feature.h:65
pal labeling engine
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
A set of features which influence the labeling process.
Definition: layer.h:63
QMutex mMutex
Definition: layer.h:345
void setUpsidedownLabels(Qgis::UpsideDownLabelHandling ud)
Sets how upside down labels will be handled within the layer.
Definition: layer.h:263
QHash< QString, QVector< FeaturePart * > > mConnectedHashtable
Definition: layer.h:342
std::deque< std::unique_ptr< FeaturePart > > mFeatureParts
List of feature parts.
Definition: layer.h:316
QList< FeaturePart * > mObstacleParts
List of obstacle parts.
Definition: layer.h:319
double mDefaultPriority
Definition: layer.h:325
QString name() const
Returns the layer's name.
Definition: layer.h:162
bool mLabelLayer
Definition: layer.h:329
std::size_t maximumPolygonLabelCandidates() const
Returns the maximum number of polygon label candidates to generate for features in this layer.
Definition: layer.h:139
bool active() const
Returns whether the layer is currently active.
Definition: layer.h:198
void setObstacleType(QgsLabelObstacleSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition: layer.h:228
QgsAbstractLabelProvider * mProvider
Definition: layer.h:312
Qgis::LabelPlacement arrangement() const
Returns the layer's arrangement policy.
Definition: layer.h:168
std::size_t maximumPointLabelCandidates() const
Returns the maximum number of point label candidates to generate for features in this layer.
Definition: layer.h:97
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition: layer.h:256
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
Definition: layer.h:250
void setActive(bool active)
Sets whether the layer is currently active.
Definition: layer.h:192
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition: layer.h:343
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition: layer.h:157
Qgis::UpsideDownLabelHandling upsidedownLabels() const
Returns how upside down labels are handled within the layer.
Definition: layer.h:269
void setArrangement(Qgis::LabelPlacement arrangement)
Sets the layer's arrangement policy.
Definition: layer.h:180
QHash< QgsFeatureId, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them)
Definition: layer.h:340
void setLabelLayer(bool toLabel)
Sets whether the layer will be labeled.
Definition: layer.h:207
std::vector< geos::unique_ptr > mGeosObstacleGeometries
Definition: layer.h:321
int featureCount()
Returns the number of features in layer.
Definition: layer.h:91
QString mName
Definition: layer.h:313
Qgis::LabelPlacement mArrangement
Optional flags used for some placement methods.
Definition: layer.h:333
bool labelLayer() const
Returns whether the layer will be labeled or not.
Definition: layer.h:213
bool centroidInside() const
Returns whether labels placed at the centroid of features within the layer are forced to be placed in...
Definition: layer.h:285
bool isCurved() const
Returns true if the layer has curved labels.
Definition: layer.h:173
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:278
QgsLabelObstacleSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition: layer.h:220
double priority() const
Returns the layer's priority, between 0 and 1.
Definition: layer.h:243
bool mActive
Definition: layer.h:328
std::size_t maximumLineLabelCandidates() const
Returns the maximum number of line label candidates to generate for features in this layer.
Definition: layer.h:118
Main Pal labeling class.
Definition: pal.h:83
Representation of a labeling problem.
Definition: problem.h:73
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28