QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
244  private:
245 
246  std::unordered_map< QgsAbstractLabelProvider *, std::unique_ptr< Layer > > mLayers;
247 
248  QMutex mMutex;
249 
250  /*
251  * POPMUSIC Tuning
252  */
253  int mPopmusicR = 30;
254 
255  int mTabuMaxIt = 4;
256  int mTabuMinIt = 2;
257 
258  int mEjChainDeg = 50;
259  int mTenure = 10;
260  double mCandListSize = 0.2;
261 
265  bool mShowPartialLabels = true;
266 
267  double mMaxLineCandidatesPerMapUnit = 0;
268  double mMaxPolygonCandidatesPerMapUnitSquared = 0;
269 
270  int mGlobalCandidatesLimitPoint = 0;
271  int mGlobalCandidatesLimitLine = 0;
272  int mGlobalCandidatesLimitPolygon = 0;
273 
275 
277  FnIsCanceled fnIsCanceled = nullptr;
279  void *fnIsCanceledContext = nullptr;
280 
286  std::unique_ptr< Problem > extract( const QgsRectangle &extent, const QgsGeometry &mapBoundary );
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();
330 
335  int getMaxIt();
336 
337  };
338 
339 } // end namespace pal
340 
341 #endif
pal::Pal::isCanceled
bool isCanceled()
Check whether the job has been canceled.
Definition: pal.h:127
pal::POPMUSIC_TABU_CHAIN
@ POPMUSIC_TABU_CHAIN
Is the best but slowest.
Definition: pal.h:64
qgspallabeling.h
pal::Pal::maximumPolygonCandidatesPerMapUnitSquared
double maximumPolygonCandidatesPerMapUnitSquared() const
Returns the maximum number of polygon label candidate positions per map unit squared.
Definition: pal.h:185
pal::Pal::setMaximumLineCandidatesPerMapUnit
void setMaximumLineCandidatesPerMapUnit(double candidates)
Sets the maximum number of line label candidates per map unit.
Definition: pal.h:178
qgslabelingenginesettings.h
pal::Pal::operator=
Pal & operator=(const Pal &other)=delete
Pal cannot be copied.
pal::Pal::~Pal
~Pal()
pal::Pal::globalCandidatesLimitLine
int globalCandidatesLimitLine() const
Returns the global candidates limit for line features, or 0 if no global limit is in effect.
Definition: pal.h:230
pal::Pal::maximumLineCandidatesPerMapUnit
double maximumLineCandidatesPerMapUnit() const
Returns the maximum number of line label candidate positions per map unit.
Definition: pal.h:171
pal::Problem
Representation of a labeling problem.
Definition: problem.h:70
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
pal
Definition: qgsdiagramrenderer.h:49
QgsLabelingEngineSettings::PlacementEngineVersion
PlacementEngineVersion
Placement engine version.
Definition: qgslabelingenginesettings.h:67
QgsPalLayerSettings::Placement
Placement
Placement modes which determine how label candidates are generated for a feature.
Definition: qgspallabeling.h:222
pal::SearchMethod
SearchMethod
Search method to use.
Definition: pal.h:62
QgsAbstractLabelProvider
The QgsAbstractLabelProvider class is an interface class.
Definition: qgslabelingengine.h:48
pal::CHAIN
@ CHAIN
Is the worst but fastest method.
Definition: pal.h:63
pal::Pal::setMaximumPolygonCandidatesPerMapUnitSquared
void setMaximumPolygonCandidatesPerMapUnitSquared(double candidates)
Sets the maximum number of polygon label candidates per map unit squared.
Definition: pal.h:192
pal::FeaturePart
Main class to handle feature.
Definition: feature.h:96
pal::FALP
@ FALP
Only initial solution.
Definition: pal.h:67
pal::POPMUSIC_CHAIN
@ POPMUSIC_CHAIN
Is slower and best than TABU, worse and faster than TABU_CHAIN.
Definition: pal.h:66
qgsgeometry.h
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
pal::Pal
Main Pal labeling class.
Definition: pal.h:80
pal::Pal::Pal
Pal(const Pal &other)=delete
Pal cannot be copied.
pal::Layer
A set of features which influence the labeling process.
Definition: layer.h:62
QgsLabelingEngineSettings::PlacementEngineVersion2
@ PlacementEngineVersion2
Version 2 (default for new projects since QGIS 3.12)
Definition: qgslabelingenginesettings.h:69
pal::POPMUSIC_TABU
@ POPMUSIC_TABU
Is a little bit better than CHAIN but slower.
Definition: pal.h:65
pal::Pal::globalCandidatesLimitPolygon
int globalCandidatesLimitPolygon() const
Returns the global candidates limit for polygon features, or 0 if no global limit is in effect.
Definition: pal.h:242
pal::Pal::globalCandidatesLimitPoint
int globalCandidatesLimitPoint() const
Returns the global candidates limit for point features, or 0 if no global limit is in effect.
Definition: pal.h:218
qgsgeos.h