QGIS API Documentation  3.27.0-Master (0e23467727)
pal.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_H
31 #define PAL_H
32 
33 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include "qgsgeometry.h"
38 #include "qgsgeos.h"
40 #include <QList>
41 #include <iostream>
42 #include <ctime>
43 #include <QMutex>
44 #include <QStringList>
45 #include <unordered_map>
46 
47 // TODO ${MAJOR} ${MINOR} etc instead of 0.2
48 
50 class QgsRenderContext;
51 
52 namespace pal
53 {
54  class Layer;
55  class LabelPosition;
56  class PalStat;
57  class Problem;
58  class PointSet;
59 
62  {
63  CHAIN = 0,
67  FALP = 4
68  };
69 
79  class CORE_EXPORT Pal
80  {
81  friend class Problem;
82  friend class FeaturePart;
83  friend class Layer;
84 
85  public:
86 
90  Pal();
91 
92  ~Pal();
93 
95  Pal( const Pal &other ) = delete;
97  Pal &operator=( const Pal &other ) = delete;
98 
111  Layer *addLayer( QgsAbstractLabelProvider *provider, const QString &layerName, Qgis::LabelPlacement arrangement, double defaultPriority, bool active, bool toLabel );
112 
118  void removeLayer( Layer *layer );
119 
120  typedef bool ( *FnIsCanceled )( void *ctx );
121 
123  void registerCancellationCallback( FnIsCanceled fnCanceled, void *context );
124 
126  inline bool isCanceled() { return fnIsCanceled ? fnIsCanceled( fnIsCanceledContext ) : false; }
127 
135  std::unique_ptr< Problem > extractProblem( const QgsRectangle &extent, const QgsGeometry &mapBoundary, QgsRenderContext &context );
136 
149  QList<LabelPosition *> solveProblem( Problem *prob, QgsRenderContext &context, bool displayAll, QList<pal::LabelPosition *> *unlabeled = nullptr );
150 
156  void setShowPartialLabels( bool show );
157 
163  bool showPartialLabels() const;
164 
170  double maximumLineCandidatesPerMapUnit() const { return mMaxLineCandidatesPerMapUnit; }
171 
177  void setMaximumLineCandidatesPerMapUnit( double candidates ) { mMaxLineCandidatesPerMapUnit = candidates; }
178 
184  double maximumPolygonCandidatesPerMapUnitSquared() const { return mMaxPolygonCandidatesPerMapUnitSquared; }
185 
191  void setMaximumPolygonCandidatesPerMapUnitSquared( double candidates ) { mMaxPolygonCandidatesPerMapUnitSquared = candidates; }
192 
198  QgsLabelingEngineSettings::PlacementEngineVersion placementVersion() const;
199 
205  void setPlacementVersion( QgsLabelingEngineSettings::PlacementEngineVersion placementVersion );
206 
217  int globalCandidatesLimitPoint() const { return mGlobalCandidatesLimitPoint; }
218 
229  int globalCandidatesLimitLine() const { return mGlobalCandidatesLimitLine; }
230 
241  int globalCandidatesLimitPolygon() const { return mGlobalCandidatesLimitPolygon; }
242 
246  bool candidatesAreConflicting( const LabelPosition *lp1, const LabelPosition *lp2 ) const;
247 
248  private:
249 
250  std::unordered_map< QgsAbstractLabelProvider *, std::unique_ptr< Layer > > mLayers;
251 
252  QMutex mMutex;
253 
254  /*
255  * POPMUSIC Tuning
256  */
257  int mPopmusicR = 30;
258 
259  int mTabuMaxIt = 4;
260  int mTabuMinIt = 2;
261 
262  int mEjChainDeg = 50;
263  int mTenure = 10;
264  double mCandListSize = 0.2;
265 
266  unsigned int mNextCandidateId = 1;
267  mutable QHash< QPair< unsigned int, unsigned int >, bool > mCandidateConflicts;
268 
272  bool mShowPartialLabels = true;
273 
274  double mMaxLineCandidatesPerMapUnit = 0;
275  double mMaxPolygonCandidatesPerMapUnitSquared = 0;
276 
277  int mGlobalCandidatesLimitPoint = 0;
278  int mGlobalCandidatesLimitLine = 0;
279  int mGlobalCandidatesLimitPolygon = 0;
280 
282 
284  FnIsCanceled fnIsCanceled = nullptr;
286  void *fnIsCanceledContext = nullptr;
287 
292  void setPopmusicR( int r );
293 
298  void setMinIt( int min_it );
299 
304  void setMaxIt( int max_it );
305 
310  void setTenure( int tenure );
311 
316  void setEjChainDeg( int degree );
317 
322  void setCandListSize( double fact );
323 
324 
329  int getMinIt() const;
330 
335  int getMaxIt() const;
336 
337  };
338 
339 } // end namespace pal
340 
341 #endif
LabelPlacement
Placement modes which determine how label candidates are generated for a feature.
Definition: qgis.h:561
The QgsAbstractLabelProvider class is an interface class.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
PlacementEngineVersion
Placement engine version.
@ PlacementEngineVersion2
Version 2 (default for new projects since QGIS 3.12)
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
Main class to handle feature.
Definition: feature.h:65
LabelPosition is a candidate feature label position.
Definition: labelposition.h:56
A set of features which influence the labeling process.
Definition: layer.h:63
Main Pal labeling class.
Definition: pal.h:80
double maximumLineCandidatesPerMapUnit() const
Returns the maximum number of line label candidate positions per map unit.
Definition: pal.h:170
Pal & operator=(const Pal &other)=delete
Pal cannot be copied.
int globalCandidatesLimitPoint() const
Returns the global candidates limit for point features, or 0 if no global limit is in effect.
Definition: pal.h:217
double maximumPolygonCandidatesPerMapUnitSquared() const
Returns the maximum number of polygon label candidate positions per map unit squared.
Definition: pal.h:184
void setMaximumPolygonCandidatesPerMapUnitSquared(double candidates)
Sets the maximum number of polygon label candidates per map unit squared.
Definition: pal.h:191
int globalCandidatesLimitLine() const
Returns the global candidates limit for line features, or 0 if no global limit is in effect.
Definition: pal.h:229
void setMaximumLineCandidatesPerMapUnit(double candidates)
Sets the maximum number of line label candidates per map unit.
Definition: pal.h:177
bool isCanceled()
Check whether the job has been canceled.
Definition: pal.h:126
Pal(const Pal &other)=delete
Pal cannot be copied.
int globalCandidatesLimitPolygon() const
Returns the global candidates limit for polygon features, or 0 if no global limit is in effect.
Definition: pal.h:241
Representation of a labeling problem.
Definition: problem.h:73
SearchMethod
Search method to use.
Definition: pal.h:62
@ FALP
Only initial solution.
Definition: pal.h:67
@ POPMUSIC_TABU
Is a little bit better than CHAIN but slower.
Definition: pal.h:65
@ POPMUSIC_CHAIN
Is slower and best than TABU, worse and faster than TABU_CHAIN.
Definition: pal.h:66
@ CHAIN
Is the worst but fastest method.
Definition: pal.h:63
@ POPMUSIC_TABU_CHAIN
Is the best but slowest.
Definition: pal.h:64