QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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"
39 #include "qgspallabeling.h"
41 #include <QList>
42 #include <iostream>
43 #include <ctime>
44 #include <QMutex>
45 #include <QStringList>
46 #include <unordered_map>
47 
48 // TODO ${MAJOR} ${MINOR} etc instead of 0.2
49 
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 
112  Layer *addLayer( QgsAbstractLabelProvider *provider, const QString &layerName, QgsPalLayerSettings::Placement arrangement, double defaultPriority, bool active, bool toLabel, bool displayAll = false );
113 
119  void removeLayer( Layer *layer );
120 
121  typedef bool ( *FnIsCanceled )( void *ctx );
122 
124  void registerCancellationCallback( FnIsCanceled fnCanceled, void *context );
125 
127  inline bool isCanceled() { return fnIsCanceled ? fnIsCanceled( fnIsCanceledContext ) : false; }
128 
136  std::unique_ptr< Problem > extractProblem( const QgsRectangle &extent, const QgsGeometry &mapBoundary );
137 
150  QList<LabelPosition *> solveProblem( Problem *prob, bool displayAll, QList<pal::LabelPosition *> *unlabeled = nullptr );
151 
157  void setShowPartialLabels( bool show );
158 
164  bool showPartialLabels() const;
165 
171  double maximumLineCandidatesPerMapUnit() const { return mMaxLineCandidatesPerMapUnit; }
172 
178  void setMaximumLineCandidatesPerMapUnit( double candidates ) { mMaxLineCandidatesPerMapUnit = candidates; }
179 
185  double maximumPolygonCandidatesPerMapUnitSquared() const { return mMaxPolygonCandidatesPerMapUnitSquared; }
186 
192  void setMaximumPolygonCandidatesPerMapUnitSquared( double candidates ) { mMaxPolygonCandidatesPerMapUnitSquared = candidates; }
193 
199  QgsLabelingEngineSettings::PlacementEngineVersion placementVersion() const;
200 
206  void setPlacementVersion( QgsLabelingEngineSettings::PlacementEngineVersion placementVersion );
207 
218  int globalCandidatesLimitPoint() const { return mGlobalCandidatesLimitPoint; }
219 
230  int globalCandidatesLimitLine() const { return mGlobalCandidatesLimitLine; }
231 
242  int globalCandidatesLimitPolygon() const { return mGlobalCandidatesLimitPolygon; }
243 
247  bool candidatesAreConflicting( const LabelPosition *lp1, const LabelPosition *lp2 ) const;
248 
249  private:
250 
251  std::unordered_map< QgsAbstractLabelProvider *, std::unique_ptr< Layer > > mLayers;
252 
253  QMutex mMutex;
254 
255  /*
256  * POPMUSIC Tuning
257  */
258  int mPopmusicR = 30;
259 
260  int mTabuMaxIt = 4;
261  int mTabuMinIt = 2;
262 
263  int mEjChainDeg = 50;
264  int mTenure = 10;
265  double mCandListSize = 0.2;
266 
267  unsigned int mNextCandidateId = 1;
268  mutable QHash< QPair< unsigned int, unsigned int >, bool > mCandidateConflicts;
269 
273  bool mShowPartialLabels = true;
274 
275  double mMaxLineCandidatesPerMapUnit = 0;
276  double mMaxPolygonCandidatesPerMapUnitSquared = 0;
277 
278  int mGlobalCandidatesLimitPoint = 0;
279  int mGlobalCandidatesLimitLine = 0;
280  int mGlobalCandidatesLimitPolygon = 0;
281 
283 
285  FnIsCanceled fnIsCanceled = nullptr;
287  void *fnIsCanceledContext = nullptr;
288 
294  std::unique_ptr< Problem > extract( const QgsRectangle &extent, const QgsGeometry &mapBoundary );
295 
300  void setPopmusicR( int r );
301 
306  void setMinIt( int min_it );
307 
312  void setMaxIt( int max_it );
313 
318  void setTenure( int tenure );
319 
324  void setEjChainDeg( int degree );
325 
330  void setCandListSize( double fact );
331 
332 
337  int getMinIt();
338 
343  int getMaxIt();
344 
345  };
346 
347 } // end namespace pal
348 
349 #endif
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)
Placement
Placement modes which determine how label candidates are generated for a feature.
A rectangle specified with double values.
Definition: qgsrectangle.h:42
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:62
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:171
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:218
double maximumPolygonCandidatesPerMapUnitSquared() const
Returns the maximum number of polygon label candidate positions per map unit squared.
Definition: pal.h:185
void setMaximumPolygonCandidatesPerMapUnitSquared(double candidates)
Sets the maximum number of polygon label candidates per map unit squared.
Definition: pal.h:192
int globalCandidatesLimitLine() const
Returns the global candidates limit for line features, or 0 if no global limit is in effect.
Definition: pal.h:230
void setMaximumLineCandidatesPerMapUnit(double candidates)
Sets the maximum number of line label candidates per map unit.
Definition: pal.h:178
bool isCanceled()
Check whether the job has been canceled.
Definition: pal.h:127
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:242
Representation of a labeling problem.
Definition: problem.h:72
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