QGIS API Documentation 4.1.0-Master (60fea48833c)
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
34#include <fstream>
35
36#include "pal.h"
37#include "qgis_core.h"
39#include "qgsgeos.h"
41
42#include <QHash>
43#include <QMutex>
44
45#define SIP_NO_FILE
46
47class QgsLabelFeature;
48
49namespace pal
50{
51
52 class FeaturePart;
53
54 class Pal;
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:
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
157
161 QString name() const { return mName; }
162
168
173
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
220
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
263
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< QPair< QgsFeatureId, int >, 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} // end namespace pal
354
355
356#endif
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition qgis.h:1232
@ Curved
Arranges candidates following the curvature of a line feature. Applies to line layers only.
Definition qgis.h:1236
@ PerimeterCurved
Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
Definition qgis.h:1240
UpsideDownLabelHandling
Handling techniques for upside down labels.
Definition qgis.h:1388
@ FlipUpsideDownLabels
Upside-down labels (90 <= angle < 270) are shown upright.
Definition qgis.h:1389
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: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
friend class LabelPosition
Definition layer.h:69
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
friend class Pal
Definition layer.h:64
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
Pal * mPal
Definition layer.h:322
QgsAbstractLabelProvider * mProvider
Definition layer.h:311
Qgis::LabelPlacement arrangement() const
Returns the layer's arrangement policy.
Definition layer.h:167
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:273
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
Qgis::UpsideDownLabelHandling mUpsidedownLabels
Definition layer.h:336
QHash< QgsFeatureId, int > mConnectedFeaturesIds
Definition layer.h:342
bool mMergeLines
Definition layer.h:334
bool mCentroidInside
Definition layer.h:329
Layer(QgsAbstractLabelProvider *provider, const QString &name, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel, Pal *pal)
Create a new layer.
Definition layer.cpp:49
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
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
void addObstaclePart(FeaturePart *fpart)
Add newly created obstacle part into r tree and to the list.
Definition layer.cpp:285
QHash< QPair< QgsFeatureId, int >, QgsLabelFeature * > mHashtable
Lookup table of label features (owned by the label feature provider that created them).
Definition layer.h:339
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
friend class Problem
Definition layer.h:67
QgsLabelObstacleSettings::ObstacleType mObstacleType
Definition layer.h:326
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
friend class FeaturePart
Definition layer.h:65
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
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features