QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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"
40 #include <QMutex>
41 #include <QLinkedList>
42 #include <QHash>
43 #include <fstream>
44 
45 class QgsLabelFeature;
46 
47 namespace pal
48 {
49 
50  class FeaturePart;
51 
52  class Pal;
53  class LabelInfo;
54 
61  class CORE_EXPORT Layer
62  {
63  friend class Pal;
64  friend class FeaturePart;
65 
66  friend class Problem;
67 
68  friend class LabelPosition;
69 
70  public:
72  {
73  Upright, // upside-down labels (90 <= angle < 270) are shown upright
74  ShowDefined, // show upside down when rotation is layer- or data-defined
75  ShowAll // show upside down for all labels, including dynamic ones
76  };
77 
91  Layer( QgsAbstractLabelProvider *provider, const QString &name, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal, bool displayAll = false );
92 
93  virtual ~Layer();
94 
95  bool displayAll() const { return mDisplayAll; }
96 
100  int featureCount() { return mHashtable.size(); }
101 
106  std::size_t maximumPointLabelCandidates() const
107  {
108  // when an extreme number of features exist in the layer, we limit the number of candidates
109  // to avoid the engine processing endlessly...
110  const int size = mHashtable.size();
111  if ( size > 1000 )
112  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 4 ) : 4 );
113  else if ( size > 500 )
114  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 6 ) : 6 );
115  else if ( size > 200 )
116  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 8 ) : 8 );
117  else if ( size > 100 )
118  return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 12 ) : 12 );
119  else
120  return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPoint(), 0 ) );
121  }
122 
127  std::size_t maximumLineLabelCandidates() const
128  {
129  // when an extreme number of features exist in the layer, we limit the number of candidates
130  // to avoid the engine processing endlessly...
131  const int size = mHashtable.size();
132  if ( size > 1000 )
133  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 5 ) : 5 );
134  else if ( size > 500 )
135  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 10 ) : 10 );
136  else if ( size > 200 )
137  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 20 ) : 20 );
138  else if ( size > 100 )
139  return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 40 ) : 40 );
140  else
141  return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitLine(), 0 ) );
142  }
143 
148  std::size_t maximumPolygonLabelCandidates() const
149  {
150  // when an extreme number of features exist in the layer, we limit the number of candidates
151  // to avoid the engine processing endlessly...
152  const int size = mHashtable.size();
153  if ( size > 1000 )
154  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 5 ) : 5 );
155  else if ( size > 500 )
156  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 15 ) : 15 );
157  else if ( size > 200 )
158  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 20 ) : 20 );
159  else if ( size > 100 )
160  return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 25 ) : 25 );
161  else
162  return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPolygon(), 0 ) );
163  }
164 
166  QgsAbstractLabelProvider *provider() const { return mProvider; }
167 
171  QString name() const { return mName; }
172 
177  QgsPalLayerSettings::Placement arrangement() const { return mArrangement; }
178 
182  bool isCurved() const { return mArrangement == QgsPalLayerSettings::Curved || mArrangement == QgsPalLayerSettings::PerimeterCurved; }
183 
189  void setArrangement( QgsPalLayerSettings::Placement arrangement ) { mArrangement = arrangement; }
190 
201  void setActive( bool active ) { mActive = active; }
202 
207  bool active() const { return mActive; }
208 
216  void setLabelLayer( bool toLabel ) { mLabelLayer = toLabel; }
217 
222  bool labelLayer() const { return mLabelLayer; }
223 
229  QgsLabelObstacleSettings::ObstacleType obstacleType() const { return mObstacleType; }
230 
237  void setObstacleType( QgsLabelObstacleSettings::ObstacleType obstacleType ) { mObstacleType = obstacleType; }
238 
245  void setPriority( double priority );
246 
252  double priority() const { return mDefaultPriority; }
253 
259  void setMergeConnectedLines( bool merge ) { mMergeLines = merge; }
260 
265  bool mergeConnectedLines() const { return mMergeLines; }
266 
272  void setUpsidedownLabels( UpsideDownLabels ud ) { mUpsidedownLabels = ud; }
273 
278  UpsideDownLabels upsidedownLabels() const { return mUpsidedownLabels; }
279 
287  void setCentroidInside( bool forceInside ) { mCentroidInside = forceInside; }
288 
294  bool centroidInside() const { return mCentroidInside; }
295 
305  bool registerFeature( QgsLabelFeature *label );
306 
308  void joinConnectedFeatures();
309 
315  int connectedFeatureId( QgsFeatureId featureId ) const;
316 
318  void chopFeaturesAtRepeatDistance();
319 
320  protected:
322  QString mName;
323 
325  QLinkedList<FeaturePart *> mFeatureParts;
326 
328  QList<FeaturePart *> mObstacleParts;
329 
330  std::vector< geos::unique_ptr > mGeosObstacleGeometries;
331 
332  Pal *mPal = nullptr;
333 
335 
337  bool mActive;
341 
344 
346 
348 
350  QHash< QgsFeatureId, QgsLabelFeature *> mHashtable;
351 
352  QHash< QString, QVector<FeaturePart *> > mConnectedHashtable;
353  QHash< QgsFeatureId, int > mConnectedFeaturesIds;
354 
355  QMutex mMutex;
356 
358  void addFeaturePart( FeaturePart *fpart, const QString &labelText = QString() );
359 
361  void addObstaclePart( FeaturePart *fpart );
362 
363  };
364 
365 } // end namespace pal
366 
367 
368 #endif
pal::Layer::maximumPointLabelCandidates
std::size_t maximumPointLabelCandidates() const
Returns the maximum number of point label candidates to generate for features in this layer.
Definition: layer.h:106
pal::Layer::mMutex
QMutex mMutex
Definition: layer.h:355
pal::Layer::mObstacleParts
QList< FeaturePart * > mObstacleParts
List of obstacle parts.
Definition: layer.h:328
QgsPalLayerSettings::PerimeterCurved
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
Definition: qgspallabeling.h:229
pal::Layer::setUpsidedownLabels
void setUpsidedownLabels(UpsideDownLabels ud)
Sets how upside down labels will be handled within the layer.
Definition: layer.h:272
pal::Layer::isCurved
bool isCurved() const
Returns true if the layer has curved labels.
Definition: layer.h:182
pal::Layer::setCentroidInside
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:287
pal::Layer::centroidInside
bool centroidInside() const
Returns whether labels placed at the centroid of features within the layer are forced to be placed in...
Definition: layer.h:294
pal::LabelPosition
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
pal::Layer::arrangement
QgsPalLayerSettings::Placement arrangement() const
Returns the layer's arrangement policy.
Definition: layer.h:177
pal::Layer::provider
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition: layer.h:166
pal::Layer::mArrangement
QgsPalLayerSettings::Placement mArrangement
Optional flags used for some placement methods.
Definition: layer.h:343
pal::Layer::mGeosObstacleGeometries
std::vector< geos::unique_ptr > mGeosObstacleGeometries
Definition: layer.h:330
pal::Layer::mHashtable
QHash< QgsFeatureId, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them)
Definition: layer.h:350
pal::Layer::mergeConnectedLines
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition: layer.h:265
pal::Layer::active
bool active() const
Returns whether the layer is currently active.
Definition: layer.h:207
pal::Problem
Representation of a labeling problem.
Definition: problem.h:69
pal::Layer::featureCount
int featureCount()
Returns the number of features in layer.
Definition: layer.h:100
pal
Definition: qgsdiagramrenderer.h:49
pal::Layer::mDisplayAll
bool mDisplayAll
Definition: layer.h:339
pal::Layer::mProvider
QgsAbstractLabelProvider * mProvider
Definition: layer.h:321
QgsPalLayerSettings::Placement
Placement
Placement modes which determine how label candidates are generated for a feature.
Definition: qgspallabeling.h:220
QgsPalLayerSettings::Curved
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
Definition: qgspallabeling.h:225
pal::Layer::name
QString name() const
Returns the layer's name.
Definition: layer.h:171
QgsAbstractLabelProvider
The QgsAbstractLabelProvider class is an interface class. Implementations return list of labels and t...
Definition: qgslabelingengine.h:47
pal::Layer::mDefaultPriority
double mDefaultPriority
Definition: layer.h:334
pal::Layer::priority
double priority() const
Returns the layer's priority, between 0 and 1.
Definition: layer.h:252
pal::Layer::mConnectedHashtable
QHash< QString, QVector< FeaturePart * > > mConnectedHashtable
Definition: layer.h:352
pal::Layer::mConnectedFeaturesIds
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition: layer.h:353
pal::Layer::mFeatureParts
QLinkedList< FeaturePart * > mFeatureParts
List of feature parts.
Definition: layer.h:325
pal::Layer::setMergeConnectedLines
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
Definition: layer.h:259
pal::FeaturePart
Main class to handle feature.
Definition: feature.h:95
pal::Layer::maximumLineLabelCandidates
std::size_t maximumLineLabelCandidates() const
Returns the maximum number of line label candidates to generate for features in this layer.
Definition: layer.h:127
pal::Layer::displayAll
bool displayAll() const
Definition: layer.h:95
pal::Layer::mUpsidedownLabels
UpsideDownLabels mUpsidedownLabels
Definition: layer.h:347
pal::Layer::Upright
@ Upright
Definition: layer.h:73
pal::Layer::mActive
bool mActive
Definition: layer.h:337
pal::Layer::mMergeLines
bool mMergeLines
Definition: layer.h:345
pal::Layer::setObstacleType
void setObstacleType(QgsLabelObstacleSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition: layer.h:237
pal::Layer::labelLayer
bool labelLayer() const
Returns whether the layer will be labeled or not.
Definition: layer.h:222
QgsLabelObstacleSettings::PolygonBoundary
@ PolygonBoundary
Definition: qgslabelobstaclesettings.h:46
pal::Pal
Main Pal labeling class.
Definition: pal.h:79
pal::Layer
A set of features which influence the labeling process.
Definition: layer.h:61
pal::Layer::upsidedownLabels
UpsideDownLabels upsidedownLabels() const
Returns how upside down labels are handled within the layer.
Definition: layer.h:278
pal.h
QgsLabelObstacleSettings::ObstacleType
ObstacleType
Valid obstacle types, which affect how features within the layer will act as obstacles for labels.
Definition: qgslabelobstaclesettings.h:42
QgsLabelFeature
The QgsLabelFeature class describes a feature that should be used within the labeling engine....
Definition: qgslabelfeature.h:56
pal::Layer::mCentroidInside
bool mCentroidInside
Definition: layer.h:340
pal::Layer::mLabelLayer
bool mLabelLayer
Definition: layer.h:338
pal::Layer::setActive
void setActive(bool active)
Sets whether the layer is currently active.
Definition: layer.h:201
pal::Layer::maximumPolygonLabelCandidates
std::size_t maximumPolygonLabelCandidates() const
Returns the maximum number of polygon label candidates to generate for features in this layer.
Definition: layer.h:148
pal::Layer::setArrangement
void setArrangement(QgsPalLayerSettings::Placement arrangement)
Sets the layer's arrangement policy.
Definition: layer.h:189
pal::Layer::UpsideDownLabels
UpsideDownLabels
Definition: layer.h:71
pal::Layer::obstacleType
QgsLabelObstacleSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition: layer.h:229
pal::Layer::ShowDefined
@ ShowDefined
Definition: layer.h:74
qgsgeos.h
qgsgenericspatialindex.h
pal::Layer::mName
QString mName
Definition: layer.h:322
pal::Layer::setLabelLayer
void setLabelLayer(bool toLabel)
Sets whether the layer will be labeled.
Definition: layer.h:216
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25