QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 #define SIP_NO_FILE
34 
35 
36 #include "qgis_core.h"
37 #include "pointset.h"
38 #include "labelposition.h" // for LabelPosition enum
39 #include "qgslabelfeature.h"
40 #include "rtree.hpp"
41 #include <iostream>
42 #include <fstream>
43 #include <cmath>
44 #include <QString>
45 
52 namespace pal
53 {
55  class CORE_EXPORT LabelInfo
56  {
57  public:
58  typedef struct
59  {
60  double width;
61  } CharacterInfo;
62 
63  LabelInfo( int num, double height, double maxinangle = 20.0, double maxoutangle = -20.0 )
64  {
65  max_char_angle_inside = maxinangle;
66  // outside angle should be negative
67  max_char_angle_outside = maxoutangle > 0 ? -maxoutangle : maxoutangle;
68  label_height = height;
69  char_num = num;
70  char_info = new CharacterInfo[num];
71  }
72  ~LabelInfo() { delete [] char_info; }
73 
75  LabelInfo( const LabelInfo &rh ) = delete;
77  LabelInfo &operator=( const LabelInfo &rh ) = delete;
78 
81  double label_height;
82  int char_num;
83  CharacterInfo *char_info = nullptr;
84 
85  };
86 
87  class LabelPosition;
88  class FeaturePart;
89 
96  class CORE_EXPORT FeaturePart : public PointSet
97  {
98 
99  public:
100 
106  FeaturePart( QgsLabelFeature *lf, const GEOSGeometry *geom );
107 
108  FeaturePart( const FeaturePart &other );
109 
113  ~FeaturePart() override;
114 
118  QgsLabelFeature *feature() { return mLF; }
119 
123  Layer *layer();
124 
128  QgsFeatureId featureId() const;
129 
138  int createCandidates( QList<LabelPosition *> &lPos, const GEOSPreparedGeometry *mapBoundary, PointSet *mapShape, RTree<LabelPosition *, double, 2, double> *candidates );
139 
148  int createCandidatesAroundPoint( double x, double y, QList<LabelPosition *> &lPos, double angle );
149 
158  int createCandidatesOverPoint( double x, double y, QList<LabelPosition *> &lPos, double angle );
159 
168  int createCandidatesAtOrderedPositionsOverPoint( double x, double y, QList<LabelPosition *> &lPos, double angle );
169 
176  int createCandidatesAlongLine( QList<LabelPosition *> &lPos, PointSet *mapShape );
177 
185  int createCandidatesAlongLineNearStraightSegments( QList<LabelPosition *> &lPos, PointSet *mapShape );
186 
196  int createCandidatesAlongLineNearMidpoint( QList<LabelPosition *> &lPos, PointSet *mapShape, double initialCost = 0.0 );
197 
208  LabelPosition *curvedPlacementAtOffset( PointSet *path_positions, double *path_distances,
209  int &orientation, double distance, bool &reversed, bool &flip );
210 
217  int createCurvedCandidatesAlongLine( QList<LabelPosition *> &lPos, PointSet *mapShape );
218 
225  int createCandidatesForPolygon( QList<LabelPosition *> &lPos, PointSet *mapShape );
226 
233  bool hasSameLabelFeatureAs( FeaturePart *part ) const;
234 
235 #if 0
236 
241  void print();
242 #endif
243 
244  double getLabelWidth() const { return mLF->size().width(); }
245  double getLabelHeight() const { return mLF->size().height(); }
246  double getLabelDistance() const { return mLF->distLabel(); }
247 
249  bool hasFixedRotation() const { return mLF->hasFixedAngle(); }
250 
252  double fixedAngle() const { return mLF->fixedAngle(); }
253 
255  bool hasFixedPosition() const { return mLF->hasFixedPosition(); }
256 
261  bool alwaysShow() const { return mLF->alwaysShow(); }
262 
264  bool isObstacle() const { return mLF->isObstacle(); }
265 
270  double obstacleFactor() const { return mLF->obstacleFactor(); }
271 
273  double repeatDistance() const { return mLF->repeatDistance(); }
274 
276  int getNumSelfObstacles() const { return mHoles.count(); }
278  FeaturePart *getSelfObstacle( int i ) { return mHoles.at( i ); }
279 
281  bool isConnected( FeaturePart *p2 );
282 
286  bool mergeWithFeaturePart( FeaturePart *other );
287 
288  void addSizePenalty( int nbp, QList<LabelPosition *> &lPos, double bbx[4], double bby[4] );
289 
294  double calculatePriority() const;
295 
297  bool showUprightLabels() const;
298 
300  bool nextCharPosition( double charWidth, double segmentLength, PointSet *path_positions, int &index, double &currentDistanceAlongSegment,
301  double &characterStartX, double &characterStartY, double &characterEndX, double &characterEndY ) const;
302 
303  protected:
304 
305  QgsLabelFeature *mLF = nullptr;
306  QList<FeaturePart *> mHoles;
307 
309  void extractCoords( const GEOSGeometry *geom );
310 
311  private:
312 
313  LabelPosition::Quadrant quadrantFromOffset() const;
314  };
315 
316 } // end namespace pal
317 
318 #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:270
bool hasFixedPosition() const
Returns true if the feature&#39;s label has a fixed position.
Definition: feature.h:255
double max_char_angle_outside
Definition: feature.h:80
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:118
double getLabelHeight() const
Definition: feature.h:245
double fixedAngle() const
Returns the fixed angle for the feature&#39;s label.
Definition: feature.h:252
int getNumSelfObstacles() const
Gets number of holes (inner rings) - they are considered as obstacles.
Definition: feature.h:276
A set of features which influence the labeling process.
Definition: layer.h:63
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
double getLabelWidth() const
Definition: feature.h:244
LabelInfo(int num, double height, double maxinangle=20.0, double maxoutangle=-20.0)
Definition: feature.h:63
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.cpp:786
double getLabelDistance() const
Definition: feature.h:246
Optional additional info about label (for curved labels)
Definition: feature.h:55
bool hasFixedRotation() const
Returns true if the feature&#39;s label has a fixed rotation.
Definition: feature.h:249
double label_height
Definition: feature.h:81
Main class to handle feature.
Definition: feature.h:96
bool isObstacle() const
Returns true if the feature should act as an obstacle to labels.
Definition: feature.h:264
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:261
The QgsLabelFeature class describes a feature that should be used within the labeling engine...
QList< FeaturePart * > mHoles
Definition: feature.h:306
double max_char_angle_inside
Definition: feature.h:79
FeaturePart * getSelfObstacle(int i)
Gets hole (inner ring) - considered as obstacle.
Definition: feature.h:278
double repeatDistance() const
Returns the distance between repeating labels for this feature.
Definition: feature.h:273
LabelPosition is a candidate feature label position.
Definition: labelposition.h:55
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:65
int char_num
Definition: feature.h:82