QGIS API Documentation  2.99.0-Master (9f5e33a)
feature.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 FEATURE_H
31 #define FEATURE_H
32 
33 #include "qgis_core.h"
34 #include "pointset.h"
35 #include "labelposition.h" // for LabelPosition enum
36 #include "qgslabelfeature.h"
37 #include "rtree.hpp"
38 #include <iostream>
39 #include <fstream>
40 #include <cmath>
41 #include <QString>
42 
48 namespace pal
49 {
51  class CORE_EXPORT LabelInfo
52  {
53  public:
54  typedef struct
55  {
56  double width;
57  } CharacterInfo;
58 
59  LabelInfo( int num, double height, double maxinangle = 20.0, double maxoutangle = -20.0 )
60  {
61  max_char_angle_inside = maxinangle;
62  // outside angle should be negative
63  max_char_angle_outside = maxoutangle > 0 ? -maxoutangle : maxoutangle;
64  label_height = height;
65  char_num = num;
66  char_info = new CharacterInfo[num];
67  }
68  ~LabelInfo() { delete [] char_info; }
69 
71  LabelInfo( const LabelInfo &rh ) = delete;
73  LabelInfo &operator=( const LabelInfo &rh ) = delete;
74 
77  double label_height;
78  int char_num;
79  CharacterInfo *char_info = nullptr;
80 
81  };
82 
83  class LabelPosition;
84  class FeaturePart;
85 
92  class CORE_EXPORT FeaturePart : public PointSet
93  {
94 
95  public:
96 
101  FeaturePart( QgsLabelFeature *lf, const GEOSGeometry *geom );
102 
103  FeaturePart( const FeaturePart &other );
104 
107  virtual ~FeaturePart();
108 
111  QgsLabelFeature *feature() { return mLF; }
112 
115  Layer *layer();
116 
119  QgsFeatureId featureId() const;
120 
129  int createCandidates( QList<LabelPosition *> &lPos, double bboxMin[2], double bboxMax[2], PointSet *mapShape, RTree<LabelPosition *, double, 2, double> *candidates );
130 
138  int createCandidatesAroundPoint( double x, double y, QList<LabelPosition *> &lPos, double angle );
139 
147  int createCandidatesOverPoint( double x, double y, QList<LabelPosition *> &lPos, double angle );
148 
156  int createCandidatesAtOrderedPositionsOverPoint( double x, double y, QList<LabelPosition *> &lPos, double angle );
157 
163  int createCandidatesAlongLine( QList<LabelPosition *> &lPos, PointSet *mapShape );
164 
171  int createCandidatesAlongLineNearStraightSegments( QList<LabelPosition *> &lPos, PointSet *mapShape );
172 
181  int createCandidatesAlongLineNearMidpoint( QList<LabelPosition *> &lPos, PointSet *mapShape, double initialCost = 0.0 );
182 
193  LabelPosition *curvedPlacementAtOffset( PointSet *path_positions, double *path_distances,
194  int &orientation, int index, double distance, bool &reversed, bool &flip );
195 
201  int createCurvedCandidatesAlongLine( QList<LabelPosition *> &lPos, PointSet *mapShape );
202 
208  int createCandidatesForPolygon( QList<LabelPosition *> &lPos, PointSet *mapShape );
209 
215  bool hasSameLabelFeatureAs( FeaturePart *part ) const;
216 
217 #if 0
218 
223  void print();
224 #endif
225 
226  double getLabelWidth() const { return mLF->size().width(); }
227  double getLabelHeight() const { return mLF->size().height(); }
228  double getLabelDistance() const { return mLF->distLabel(); }
229 
231  bool hasFixedRotation() const { return mLF->hasFixedAngle(); }
232 
234  double fixedAngle() const { return mLF->fixedAngle(); }
235 
237  bool hasFixedPosition() const { return mLF->hasFixedPosition(); }
238 
241  bool alwaysShow() const { return mLF->alwaysShow(); }
242 
244  bool isObstacle() const { return mLF->isObstacle(); }
245 
248  double obstacleFactor() const { return mLF->obstacleFactor(); }
249 
251  double repeatDistance() const { return mLF->repeatDistance(); }
252 
254  int getNumSelfObstacles() const { return mHoles.count(); }
256  FeaturePart *getSelfObstacle( int i ) { return mHoles.at( i ); }
257 
259  bool isConnected( FeaturePart *p2 );
260 
263  bool mergeWithFeaturePart( FeaturePart *other );
264 
265  void addSizePenalty( int nbp, QList<LabelPosition *> &lPos, double bbx[4], double bby[4] );
266 
272  double calculatePriority() const;
273 
275  bool showUprightLabels() const;
276 
278  bool nextCharPosition( double charWidth, double segment_length, PointSet *path_positions, int &index, double &distance,
279  double &start_x, double &start_y, double &end_x, double &end_y ) const;
280 
281  protected:
282 
283  QgsLabelFeature *mLF = nullptr;
284  QList<FeaturePart *> mHoles;
285 
287  void extractCoords( const GEOSGeometry *geom );
288 
289  private:
290 
291  LabelPosition::Quadrant quadrantFromOffset() const;
292  };
293 
294 } // end namespace pal
295 
296 #endif
double obstacleFactor() const
Returns the feature&#39;s obstacle factor, which represents the penalty incurred for a label to overlap t...
Definition: feature.h:248
double fixedAngle() const
Returns the fixed angle for the feature&#39;s label.
Definition: feature.h:234
double max_char_angle_outside
Definition: feature.h:76
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:111
bool hasFixedRotation() const
Returns true if the feature&#39;s label has a fixed rotation.
Definition: feature.h:231
A set of features which influence the labeling process.
Definition: layer.h:60
bool alwaysShow() const
Returns true if the feature&#39;s label should always been shown, even when it collides with other labels...
Definition: feature.h:241
int getNumSelfObstacles() const
Get number of holes (inner rings) - they are considered as obstacles.
Definition: feature.h:254
LabelInfo(int num, double height, double maxinangle=20.0, double maxoutangle=-20.0)
Definition: feature.h:59
double getLabelDistance() const
Definition: feature.h:228
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cc:857
double repeatDistance() const
Returns the distance between repeating labels for this feature.
Definition: feature.h:251
bool isObstacle() const
Returns true if the feature should act as an obstacle to labels.
Definition: feature.h:244
Optional additional info about label (for curved labels)
Definition: feature.h:51
double label_height
Definition: feature.h:77
double getLabelHeight() const
Definition: feature.h:227
Main class to handle feature.
Definition: feature.h:92
bool hasFixedPosition() const
Returns true if the feature&#39;s label has a fixed position.
Definition: feature.h:237
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
QList< FeaturePart * > mHoles
Definition: feature.h:284
double max_char_angle_inside
Definition: feature.h:75
FeaturePart * getSelfObstacle(int i)
Get hole (inner ring) - considered as obstacle.
Definition: feature.h:256
double getLabelWidth() const
Definition: feature.h:226
LabelPosition is a candidate feature label position.
Definition: labelposition.h:52
qint64 QgsFeatureId
Definition: qgsfeature.h:37
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:62
int char_num
Definition: feature.h:78