QGIS API Documentation  3.37.0-Master (a5b4d9743e8)
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 "qgstextrendererutils.h"
41 #include <iostream>
42 #include <fstream>
43 #include <cmath>
44 #include <QString>
45 
53 namespace pal
54 {
55  class LabelPosition;
56  class FeaturePart;
57 
64  class CORE_EXPORT FeaturePart : public PointSet
65  {
66 
67  public:
68 
71  {
74  NegativeOffset
75  };
76 
82  FeaturePart( QgsLabelFeature *lf, const GEOSGeometry *geom );
83 
84  FeaturePart( const FeaturePart &other );
85 
89  ~FeaturePart() override;
90 
94  QgsLabelFeature *feature() { return mLF; }
95 
99  Layer *layer();
100 
104  QgsFeatureId featureId() const;
105 
109  std::size_t maximumPointCandidates() const;
110 
114  std::size_t maximumLineCandidates() const;
115 
119  std::size_t maximumPolygonCandidates() const;
120 
124  std::vector<std::unique_ptr<LabelPosition> > createCandidates( Pal *pal );
125 
134  std::size_t createCandidatesAroundPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
135 
144  std::size_t createCandidatesOverPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
145 
154  std::size_t createCandidateCenteredOverPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
155 
162  std::unique_ptr< LabelPosition > createCandidatePointOnSurface( PointSet *mapShape );
163 
172  std::size_t createCandidatesAtOrderedPositionsOverPoint( double x, double y, std::vector<std::unique_ptr<LabelPosition> > &lPos, double angle );
173 
182  std::size_t createCandidatesAlongLine( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, bool allowOverrun, Pal *pal );
183 
191  std::size_t createHorizontalCandidatesAlongLine( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, Pal *pal );
192 
201  std::size_t createCandidatesAlongLineNearStraightSegments( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, Pal *pal );
202 
213  std::size_t createCandidatesAlongLineNearMidpoint( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, double initialCost = 0.0, Pal *pal = nullptr );
214 
226  std::unique_ptr< LabelPosition > curvedPlacementAtOffset( PointSet *mapShape, const std::vector<double> &pathDistances,
227  QgsTextRendererUtils::LabelLineDirection direction, double distance, bool &labeledLineSegmentIsRightToLeft, bool applyAngleConstraints,
229 
238  std::size_t createCurvedCandidatesAlongLine( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, bool allowOverrun, Pal *pal );
239 
247  std::size_t createCandidatesForPolygon( std::vector<std::unique_ptr<LabelPosition> > &lPos, PointSet *mapShape, Pal *pal );
248 
255  std::size_t createCandidatesOutsidePolygon( std::vector<std::unique_ptr<LabelPosition> > &lPos, Pal *pal );
256 
263  bool hasSameLabelFeatureAs( FeaturePart *part ) const;
264 
268  double getLabelWidth( double angle = 0.0 ) const { return mLF->size( angle ).width(); }
269 
273  double getLabelHeight( double angle = 0.0 ) const { return mLF->size( angle ).height(); }
274 
279  double getLabelDistance() const { return mLF->distLabel(); }
280 
282  bool hasFixedRotation() const { return mLF->hasFixedAngle(); }
283 
285  double fixedAngle() const { return mLF->fixedAngle(); }
286 
288  bool hasFixedPosition() const { return mLF->hasFixedPosition(); }
289 
294  bool alwaysShow() const { return mLF->alwaysShow(); }
295 
299  const QgsLabelObstacleSettings &obstacleSettings() const { return mLF->obstacleSettings(); }
300 
302  double repeatDistance() const { return mLF->repeatDistance(); }
303 
305  int getNumSelfObstacles() const { return mHoles.count(); }
307  FeaturePart *getSelfObstacle( int i ) { return mHoles.at( i ); }
308 
310  bool isConnected( FeaturePart *p2 );
311 
316  bool mergeWithFeaturePart( FeaturePart *other );
317 
323  void addSizePenalty( std::vector<std::unique_ptr<LabelPosition> > &lPos, double bbx[4], double bby[4] ) const;
324 
329  double calculatePriority() const;
330 
332  bool onlyShowUprightLabels() const;
333 
338  int totalRepeats() const;
339 
344  void setTotalRepeats( int repeats );
345 
346  protected:
347 
348  QgsLabelFeature *mLF = nullptr;
349  QList<FeaturePart *> mHoles;
350 
352  void extractCoords( const GEOSGeometry *geom );
353 
354  private:
355 
356  LabelPosition::Quadrant quadrantFromOffset() const;
357 
358  int mTotalRepeats = 0;
359 
360  mutable std::size_t mCachedMaxLineCandidates = 0;
361  mutable std::size_t mCachedMaxPolygonCandidates = 0;
362 
363  FeaturePart &operator= ( const FeaturePart & ) = delete;
364  };
365 
366 } // end namespace pal
367 
368 #endif
The QgsLabelFeature class describes a feature that should be used within the labeling engine.
Contains settings related to how the label engine treats features as obstacles.
LabelLineDirection
Controls behavior of curved text with respect to line directions.
QFlags< CurvedTextFlag > CurvedTextFlags
Flags controlling behavior of curved text generation.
Main class to handle feature.
Definition: feature.h:65
bool hasFixedRotation() const
Returns true if the feature's label has a fixed rotation.
Definition: feature.h:282
double getLabelHeight(double angle=0.0) const
Returns the height of the label, optionally taking an angle (in radians) into account.
Definition: feature.h:273
QList< FeaturePart * > mHoles
Definition: feature.h:349
double getLabelDistance() const
Returns the distance from the anchor point to the label.
Definition: feature.h:279
bool hasFixedPosition() const
Returns true if the feature's label has a fixed position.
Definition: feature.h:288
double fixedAngle() const
Returns the fixed angle for the feature's label.
Definition: feature.h:285
double repeatDistance() const
Returns the distance between repeating labels for this feature.
Definition: feature.h:302
int getNumSelfObstacles() const
Gets number of holes (inner rings) - they are considered as obstacles.
Definition: feature.h:305
const QgsLabelObstacleSettings & obstacleSettings() const
Returns the feature's obstacle settings.
Definition: feature.h:299
double getLabelWidth(double angle=0.0) const
Returns the width of the label, optionally taking an angle (in radians) into account.
Definition: feature.h:268
bool alwaysShow() const
Returns true if the feature's label should always been shown, even when it collides with other labels...
Definition: feature.h:294
PathOffset
Path offset variances used in curved placement.
Definition: feature.h:71
FeaturePart * getSelfObstacle(int i)
Gets hole (inner ring) - considered as obstacle.
Definition: feature.h:307
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:94
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:66
A set of features which influence the labeling process.
Definition: layer.h:63
Main Pal labeling class.
Definition: pal.h:83
The underlying raw pal geometry class.
Definition: pointset.h:77
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:716
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
struct GEOSGeom_t GEOSGeometry
Definition: util.h:41