QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
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 <fstream>
37
38#include "pal.h"
39#include "qgis_core.h"
41#include "qgsgeos.h"
43
44#include <QHash>
45#include <QLinkedList>
46#include <QMutex>
47
48class QgsLabelFeature;
49
50namespace pal
51{
52
53 class FeaturePart;
54
55 class Pal;
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:
73
85 Layer( QgsAbstractLabelProvider *provider, const QString &name, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal );
86
87 virtual ~Layer();
88
92 int featureCount() const { return mHashtable.size(); }
93
98 std::size_t maximumPointLabelCandidates() const
99 {
100 // when an extreme number of features exist in the layer, we limit the number of candidates
101 // to avoid the engine processing endlessly...
102 const int size = mHashtable.size();
103 if ( size > 1000 )
104 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 4 ) : 4 );
105 else if ( size > 500 )
106 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 6 ) : 6 );
107 else if ( size > 200 )
108 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 8 ) : 8 );
109 else if ( size > 100 )
110 return static_cast< std::size_t >( mPal->globalCandidatesLimitPoint() > 0 ? std::min( mPal->globalCandidatesLimitPoint(), 12 ) : 12 );
111 else
112 return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPoint(), 0 ) );
113 }
114
119 std::size_t maximumLineLabelCandidates() const
120 {
121 // when an extreme number of features exist in the layer, we limit the number of candidates
122 // to avoid the engine processing endlessly...
123 const int size = mHashtable.size();
124 if ( size > 1000 )
125 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 5 ) : 5 );
126 else if ( size > 500 )
127 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 10 ) : 10 );
128 else if ( size > 200 )
129 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 20 ) : 20 );
130 else if ( size > 100 )
131 return static_cast< std::size_t >( mPal->globalCandidatesLimitLine() > 0 ? std::min( mPal->globalCandidatesLimitLine(), 40 ) : 40 );
132 else
133 return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitLine(), 0 ) );
134 }
135
141 {
142 // when an extreme number of features exist in the layer, we limit the number of candidates
143 // to avoid the engine processing endlessly...
144 const int size = mHashtable.size();
145 if ( size > 1000 )
146 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 5 ) : 5 );
147 else if ( size > 500 )
148 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 15 ) : 15 );
149 else if ( size > 200 )
150 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 20 ) : 20 );
151 else if ( size > 100 )
152 return static_cast< std::size_t >( mPal->globalCandidatesLimitPolygon() > 0 ? std::min( mPal->globalCandidatesLimitPolygon(), 25 ) : 25 );
153 else
154 return static_cast< std::size_t >( std::max( mPal->globalCandidatesLimitPolygon(), 0 ) );
155 }
156
159
163 QString name() const { return mName; }
164
170
175
182
193 void setActive( bool active ) { mActive = active; }
194
199 bool active() const { return mActive; }
200
208 void setLabelLayer( bool toLabel ) { mLabelLayer = toLabel; }
209
214 bool labelLayer() const { return mLabelLayer; }
215
222
230
237 void setPriority( double priority );
238
244 double priority() const { return mDefaultPriority; }
245
251 void setMergeConnectedLines( bool merge ) { mMergeLines = merge; }
252
257 bool mergeConnectedLines() const { return mMergeLines; }
258
265
271
279 void setCentroidInside( bool forceInside ) { mCentroidInside = forceInside; }
280
286 bool centroidInside() const { return mCentroidInside; }
287
297 bool registerFeature( QgsLabelFeature *label );
298
300 void joinConnectedFeatures();
301
307 int connectedFeatureId( QgsFeatureId featureId ) const;
308
310 void chopFeaturesAtRepeatDistance();
311
312 protected:
314 QString mName;
315
317 std::deque< std::unique_ptr< FeaturePart > > mFeatureParts;
318
320 QList<FeaturePart *> mObstacleParts;
321
322 std::vector< geos::unique_ptr > mGeosObstacleGeometries;
323
324 Pal *mPal = nullptr;
325
327
331 bool mCentroidInside = false;
332
335
336 bool mMergeLines = false;
337
339
341 QHash< QgsFeatureId, QgsLabelFeature *> mHashtable;
342
343 QHash< QString, QVector<FeaturePart *> > mConnectedHashtable;
344 QHash< QgsFeatureId, int > mConnectedFeaturesIds;
345
346 QMutex mMutex;
347
349 void addFeaturePart( std::unique_ptr< FeaturePart > fpart, const QString &labelText = QString() );
350
352 void addObstaclePart( FeaturePart *fpart );
353
354 };
355
356} // end namespace pal
357
358
359#endif
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1194
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
Definition qgis.h:1198
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
Definition qgis.h:1202
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition qgis.h:1323
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
Definition qgis.h:1324
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).
QMutex mMutex
Definition layer.h:346
void setUpsidedownLabels(Qgis::UpsideDownLabelHandling ud)
Sets how upside down labels will be handled within the layer.
Definition layer.h:264
QHash< QString, QVector< FeaturePart * > > mConnectedHashtable
Definition layer.h:343
friend class LabelPosition
Definition layer.h:70
std::deque< std::unique_ptr< FeaturePart > > mFeatureParts
List of feature parts.
Definition layer.h:317
QList< FeaturePart * > mObstacleParts
List of obstacle parts.
Definition layer.h:320
double mDefaultPriority
Definition layer.h:326
friend class Pal
Definition layer.h:65
QString name() const
Returns the layer's name.
Definition layer.h:163
bool mLabelLayer
Definition layer.h:330
QgsAbstractLabelProvider * provider() const
Returns pointer to the associated provider.
Definition layer.h:158
std::size_t maximumPolygonLabelCandidates() const
Returns the maximum number of polygon label candidates to generate for features in this layer.
Definition layer.h:140
bool active() const
Returns whether the layer is currently active.
Definition layer.h:199
void setObstacleType(QgsLabelObstacleSettings::ObstacleType obstacleType)
Sets the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition layer.h:229
Pal * mPal
Definition layer.h:324
QgsAbstractLabelProvider * mProvider
Definition layer.h:313
Qgis::LabelPlacement arrangement() const
Returns the layer's arrangement policy.
Definition layer.h:169
void addFeaturePart(std::unique_ptr< FeaturePart > fpart, const QString &labelText=QString())
Add newly created feature part into r tree and to the list.
Definition layer.cpp:263
std::size_t maximumPointLabelCandidates() const
Returns the maximum number of point label candidates to generate for features in this layer.
Definition layer.h:98
bool mergeConnectedLines() const
Returns whether connected lines will be merged before labeling.
Definition layer.h:257
void setMergeConnectedLines(bool merge)
Sets whether connected lines should be merged before labeling.
Definition layer.h:251
void setActive(bool active)
Sets whether the layer is currently active.
Definition layer.h:193
Qgis::UpsideDownLabelHandling mUpsidedownLabels
Definition layer.h:338
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition layer.h:344
bool mMergeLines
Definition layer.h:336
bool mCentroidInside
Definition layer.h:331
Layer(QgsAbstractLabelProvider *provider, const QString &name, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal)
Create a new layer.
Definition layer.cpp:45
Qgis::UpsideDownLabelHandling upsidedownLabels() const
Returns how upside down labels are handled within the layer.
Definition layer.h:270
void setArrangement(Qgis::LabelPlacement arrangement)
Sets the layer's arrangement policy.
Definition layer.h:181
QHash< QgsFeatureId, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them).
Definition layer.h:341
void setLabelLayer(bool toLabel)
Sets whether the layer will be labeled.
Definition layer.h:208
std::vector< geos::unique_ptr > mGeosObstacleGeometries
Definition layer.h:322
void addObstaclePart(FeaturePart *fpart)
Add newly created obstacle part into r tree and to the list.
Definition layer.cpp:275
QString mName
Definition layer.h:314
Qgis::LabelPlacement mArrangement
Optional flags used for some placement methods.
Definition layer.h:334
bool labelLayer() const
Returns whether the layer will be labeled or not.
Definition layer.h:214
friend class Problem
Definition layer.h:68
QgsLabelObstacleSettings::ObstacleType mObstacleType
Definition layer.h:328
bool centroidInside() const
Returns whether labels placed at the centroid of features within the layer are forced to be placed in...
Definition layer.h:286
friend class FeaturePart
Definition layer.h:66
bool isCurved() const
Returns true if the layer has curved labels.
Definition layer.h:174
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:279
int featureCount() const
Returns the number of features in layer.
Definition layer.h:92
QgsLabelObstacleSettings::ObstacleType obstacleType() const
Returns the obstacle type, which controls how features within the layer act as obstacles for labels.
Definition layer.h:221
double priority() const
Returns the layer's priority, between 0 and 1.
Definition layer.h:244
bool mActive
Definition layer.h:329
std::size_t maximumLineLabelCandidates() const
Returns the maximum number of line label candidates to generate for features in this layer.
Definition layer.h:119
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features