QGIS API Documentation 3.43.0-Master (b60ef06885e)
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"
41#include <QMutex>
42#include <QLinkedList>
43#include <QHash>
44#include <fstream>
45
46class QgsLabelFeature;
47
48namespace pal
49{
50
51 class FeaturePart;
52
53 class Pal;
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:
71
83 Layer( QgsAbstractLabelProvider *provider, const QString &name, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal );
84
85 virtual ~Layer();
86
90 int featureCount() const { return mHashtable.size(); }
91
96 std::size_t maximumPointLabelCandidates() const
97 {
98 // when an extreme number of features exist in the layer, we limit the number of candidates
99 // to avoid the engine processing endlessly...
100 const int size = mHashtable.size();
101 if ( size > 1000 )
102 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 4 ) : 4 );
103 else if ( size > 500 )
104 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 6 ) : 6 );
105 else if ( size > 200 )
106 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 8 ) : 8 );
107 else if ( size > 100 )
108 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 12 ) : 12 );
109 else
110 return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPoint(), 0 ) );
111 }
112
117 std::size_t maximumLineLabelCandidates() const
118 {
119 // when an extreme number of features exist in the layer, we limit the number of candidates
120 // to avoid the engine processing endlessly...
121 const int size = mHashtable.size();
122 if ( size > 1000 )
123 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 5 ) : 5 );
124 else if ( size > 500 )
125 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 10 ) : 10 );
126 else if ( size > 200 )
127 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 20 ) : 20 );
128 else if ( size > 100 )
129 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 40 ) : 40 );
130 else
131 return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitLine(), 0 ) );
132 }
133
139 {
140 // when an extreme number of features exist in the layer, we limit the number of candidates
141 // to avoid the engine processing endlessly...
142 const int size = mHashtable.size();
143 if ( size > 1000 )
144 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 5 ) : 5 );
145 else if ( size > 500 )
146 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 15 ) : 15 );
147 else if ( size > 200 )
148 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 20 ) : 20 );
149 else if ( size > 100 )
150 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 25 ) : 25 );
151 else
152 return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPolygon(), 0 ) );
153 }
154
156 QgsAbstractLabelProvider *provider() const { return mProvider; }
157
161 QString name() const { return mName; }
162
167 Qgis::LabelPlacement arrangement() const { return mArrangement; }
168
172 bool isCurved() const { return mArrangement == Qgis::LabelPlacement::Curved || mArrangement == Qgis::LabelPlacement::PerimeterCurved; }
173
179 void setArrangement( Qgis::LabelPlacement arrangement ) { mArrangement = arrangement; }
180
191 void setActive( bool active ) { mActive = active; }
192
197 bool active() const { return mActive; }
198
206 void setLabelLayer( bool toLabel ) { mLabelLayer = toLabel; }
207
212 bool labelLayer() const { return mLabelLayer; }
213
219 QgsLabelObstacleSettings::ObstacleType obstacleType() const { return mObstacleType; }
220
227 void setObstacleType( QgsLabelObstacleSettings::ObstacleType obstacleType ) { mObstacleType = obstacleType; }
228
235 void setPriority( double priority );
236
242 double priority() const { return mDefaultPriority; }
243
249 void setMergeConnectedLines( bool merge ) { mMergeLines = merge; }
250
255 bool mergeConnectedLines() const { return mMergeLines; }
256
262 void setUpsidedownLabels( Qgis::UpsideDownLabelHandling ud ) { mUpsidedownLabels = ud; }
263
268 Qgis::UpsideDownLabelHandling upsidedownLabels() const { return mUpsidedownLabels; }
269
277 void setCentroidInside( bool forceInside ) { mCentroidInside = forceInside; }
278
284 bool centroidInside() const { return mCentroidInside; }
285
295 bool registerFeature( QgsLabelFeature *label );
296
298 void joinConnectedFeatures();
299
305 int connectedFeatureId( QgsFeatureId featureId ) const;
306
308 void chopFeaturesAtRepeatDistance();
309
310 protected:
312 QString mName;
313
315 std::deque< std::unique_ptr< FeaturePart > > mFeatureParts;
316
318 QList<FeaturePart *> mObstacleParts;
319
320 std::vector< geos::unique_ptr > mGeosObstacleGeometries;
321
322 Pal *mPal = nullptr;
323
325
329 bool mCentroidInside = false;
330
333
334 bool mMergeLines = false;
335
337
339 QHash< QgsFeatureId, QgsLabelFeature *> mHashtable;
340
341 QHash< QString, QVector<FeaturePart *> > mConnectedHashtable;
342 QHash< QgsFeatureId, int > mConnectedFeaturesIds;
343
344 QMutex mMutex;
345
347 void addFeaturePart( std::unique_ptr< FeaturePart > fpart, const QString &labelText = QString() );
348
350 void addObstaclePart( FeaturePart *fpart );
351
352 };
353
354} // end namespace pal
355
356
357#endif
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1158
@ 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:1287
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
An abstract interface class for label providers.
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)
Represents a part of a label feature.
Definition feature.h:58
LabelPosition is a candidate feature label position.
A set of features which influence the labeling process.
Definition layer.h:62
QMutex mMutex
Definition layer.h:344
void setUpsidedownLabels(Qgis::UpsideDownLabelHandling ud)
Sets how upside down labels will be handled within the layer.
Definition layer.h:262
QHash< QString, QVector< FeaturePart * > > mConnectedHashtable
Definition layer.h:341
std::deque< std::unique_ptr< FeaturePart > > mFeatureParts
List of feature parts.
Definition layer.h:315
QList< FeaturePart * > mObstacleParts
List of obstacle parts.
Definition layer.h:318
double mDefaultPriority
Definition layer.h:324
QString name() const
Returns the layer's name.
Definition layer.h:161
bool mLabelLayer
Definition layer.h:328
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition layer.h:156
std::size_t maximumPolygonLabelCandidates() const
Returns the maximum number of polygon label candidates to generate for features in this layer.
Definition layer.h:138
bool active() const
Returns whether the layer is currently active.
Definition layer.h:197
void setObstacleType(QgsLabelObstacleSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition layer.h:227
QgsAbstractLabelProvider * mProvider
Definition layer.h:311
Qgis::LabelPlacement arrangement() const
Returns the layer's arrangement policy.
Definition layer.h:167
std::size_t maximumPointLabelCandidates() const
Returns the maximum number of point label candidates to generate for features in this layer.
Definition layer.h:96
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition layer.h:255
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
Definition layer.h:249
void setActive(bool active)
Sets whether the layer is currently active.
Definition layer.h:191
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition layer.h:342
Qgis::UpsideDownLabelHandling upsidedownLabels() const
Returns how upside down labels are handled within the layer.
Definition layer.h:268
void setArrangement(Qgis::LabelPlacement arrangement)
Sets the layer's arrangement policy.
Definition layer.h:179
QHash< QgsFeatureId, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them)
Definition layer.h:339
void setLabelLayer(bool toLabel)
Sets whether the layer will be labeled.
Definition layer.h:206
std::vector< geos::unique_ptr > mGeosObstacleGeometries
Definition layer.h:320
QString mName
Definition layer.h:312
Qgis::LabelPlacement mArrangement
Optional flags used for some placement methods.
Definition layer.h:332
bool labelLayer() const
Returns whether the layer will be labeled or not.
Definition layer.h:212
bool centroidInside() const
Returns whether labels placed at the centroid of features within the layer are forced to be placed in...
Definition layer.h:284
bool isCurved() const
Returns true if the layer has curved labels.
Definition layer.h:172
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:277
int featureCount() const
Returns the number of features in layer.
Definition layer.h:90
QgsLabelObstacleSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition layer.h:219
double priority() const
Returns the layer's priority, between 0 and 1.
Definition layer.h:242
bool mActive
Definition layer.h:327
std::size_t maximumLineLabelCandidates() const
Returns the maximum number of line label candidates to generate for features in this layer.
Definition layer.h:117
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