QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 "qgsgeometry.h"
34 #include "palgeometry.h"
35 #include "pointset.h"
36 #include "util.h"
37 #include "labelposition.h"
38 #include <iostream>
39 #include <fstream>
40 #include <cmath>
41 #include <QString>
42 
43 namespace pal
44 {
46  class CORE_EXPORT LabelInfo
47  {
48  public:
49  typedef struct
50  {
51  unsigned short chr;
52  double width;
53  } CharacterInfo;
54 
55  LabelInfo( int num, double height, double maxinangle = 20.0, double maxoutangle = -20.0 )
56  {
57  max_char_angle_inside = maxinangle;
58  // outside angle should be negative
59  max_char_angle_outside = maxoutangle > 0 ? -maxoutangle : maxoutangle;
60  label_height = height;
61  char_num = num;
62  char_info = new CharacterInfo[num];
63  }
64  ~LabelInfo() { delete [] char_info; }
65 
68  double label_height;
69  int char_num;
71  };
72 
73  class LabelPosition;
74  class FeaturePart;
75 
76  class CORE_EXPORT Feature
77  {
78  friend class FeaturePart;
79 
80  public:
81  Feature( Layer* l, const QString& geom_id, PalGeometry* userG, double lx, double ly );
82  ~Feature();
83 
84  void setLabelInfo( LabelInfo* info ) { labelInfo = info; }
85  void setDistLabel( double dist ) { distlabel = dist; }
87  void setFixedPosition( double x, double y ) { fixedPos = true; fixedPosX = x; fixedPosY = y;}
88  void setQuadOffset( double x, double y ) { quadOffset = true; quadOffsetX = x; quadOffsetY = y;}
89 
94  void setFixedQuadrant( bool fixed ) { mFixedQuadrant = fixed; }
95 
99  bool fixedQuadrant() const { return mFixedQuadrant; }
100 
101  void setPosOffset( double x, double y ) { offsetPos = true; offsetPosX = x; offsetPosY = y;}
102  bool fixedPosition() const { return fixedPos; }
103 
106  void setFixedAngle( double a ) { fixedRotation = true; fixedAngle = a; }
107  void setRepeatDistance( double dist ) { repeatDist = dist; }
108  double repeatDistance() const { return repeatDist; }
109  void setAlwaysShow( bool bl ) { alwaysShow = bl; }
110 
115  void setIsObstacle( bool obstacle ) { mIsObstacle = obstacle; }
116 
121  double isObstacle() const { return mIsObstacle; }
122 
130  void setObstacleFactor( double factor ) { mObstacleFactor = factor; }
131 
136  double obstacleFactor() const { return mObstacleFactor; }
137 
145  void setPriority( double priority ) { mPriority = priority; }
146 
153  double priority() const { return mPriority; }
154 
160  double calculatePriority() const;
161 
162  protected:
165  double label_x;
166  double label_y;
167  double distlabel;
168  LabelInfo* labelInfo; // optional
169 
171 
172  bool fixedPos; //true in case of fixed position (only 1 candidate position with cost 0)
173  double fixedPosX;
174  double fixedPosY;
175  bool quadOffset; // true if a quadrant offset exists
176  double quadOffsetX;
177  double quadOffsetY;
178  bool offsetPos; //true if position is to be offset by set amount
179  double offsetPosX;
180  double offsetPosY;
181  //Fixed (e.g. data defined) angle only makes sense together with fixed position
183  double fixedAngle; //fixed angle value (in rad)
184  double repeatDist;
185 
186  bool alwaysShow; //true is label is to always be shown (but causes overlapping)
187 
188  private:
189 
190  bool mFixedQuadrant;
191  bool mIsObstacle;
192  double mObstacleFactor;
193 
194  //-1 if layer priority should be used
195  double mPriority;
196 
197 
198  };
199 
203  class CORE_EXPORT FeaturePart : public PointSet
204  {
205 
206  public:
207 
212  FeaturePart( Feature *feat, const GEOSGeometry* geom );
213 
216  virtual ~FeaturePart();
217 
226  int setPositionForPoint( double x, double y, LabelPosition ***lPos, double angle, PointSet *mapShape = 0 );
227 
236  int setPositionOverPoint( double x, double y, LabelPosition ***lPos, double angle, PointSet *mapShape = 0 );
237 
243  int setPositionForLine( LabelPosition ***lPos, PointSet *mapShape );
244 
245  LabelPosition* curvedPlacementAtOffset( PointSet* path_positions, double* path_distances,
246  int orientation, int index, double distance );
247 
253  int setPositionForLineCurved( LabelPosition ***lPos, PointSet* mapShape );
254 
260  int setPositionForPolygon( LabelPosition ***lPos, PointSet *mapShape );
261 
264  Feature* getFeature() { return mFeature; }
265 
268  Layer* layer();
269 
279  int setPosition( LabelPosition ***lPos, double bbox_min[2], double bbox_max[2], PointSet *mapShape, RTree<LabelPosition*, double, 2, double>*candidates );
280 
283  QString getUID() const;
284 
285 
286 #if 0
287 
291  void print();
292 #endif
293 
294  PalGeometry* getUserGeometry() { return mFeature->userGeom; }
295 
296  void setLabelSize( double lx, double ly ) { mFeature->label_x = lx; mFeature->label_y = ly; }
297  double getLabelWidth() const { return mFeature->label_x; }
298  double getLabelHeight() const { return mFeature->label_y; }
299  void setLabelDistance( double dist ) { mFeature->distlabel = dist; }
300  double getLabelDistance() const { return mFeature->distlabel; }
301  void setLabelInfo( LabelInfo* info ) { mFeature->labelInfo = info; }
302 
303  bool getFixedRotation() { return mFeature->fixedRotation; }
304  double getLabelAngle() { return mFeature->fixedAngle; }
305  bool getFixedPosition() { return mFeature->fixedPos; }
306  bool getAlwaysShow() { return mFeature->alwaysShow; }
307 
308  int getNumSelfObstacles() const { return mHoles.count(); }
309  FeaturePart* getSelfObstacle( int i ) { return mHoles.at( i ); }
310 
312  bool isConnected( FeaturePart* p2 );
313 
316  bool mergeWithFeaturePart( FeaturePart* other );
317 
318  void addSizePenalty( int nbp, LabelPosition** lPos, double bbx[4], double bby[4] );
319 
320  protected:
321 
324 
326  void extractCoords( const GEOSGeometry* geom );
327 
328  private:
329 
330  LabelPosition::Quadrant quadrantFromOffset() const;
331  };
332 
333 } // end namespace pal
334 
335 #endif
double obstacleFactor() const
Returns the obstacle factor for the feature.
Definition: feature.h:136
bool getAlwaysShow()
Definition: feature.h:306
double fixedAngle
Definition: feature.h:183
static unsigned index
double fixedPosY
Definition: feature.h:174
void setPriority(double priority)
Sets the priority for labeling the feature.
Definition: feature.h:145
void setIsObstacle(bool obstacle)
Sets whether the feature will act as an obstacle for labels.
Definition: feature.h:115
bool getFixedRotation()
Definition: feature.h:303
void setLabelInfo(LabelInfo *info)
Definition: feature.h:301
bool fixedRotation
Definition: feature.h:182
QList< FeaturePart * > mHoles
Definition: feature.h:323
double max_char_angle_outside
Definition: feature.h:67
Feature * getFeature()
Returns the parent feature.
Definition: feature.h:264
bool fixedPos
Definition: feature.h:172
double getLabelHeight() const
Definition: feature.h:298
int getNumSelfObstacles() const
Definition: feature.h:308
A layer of spacial entites.
Definition: layer.h:57
double offsetPosX
Definition: feature.h:179
void setFixedAngle(double a)
Set label rotation to fixed value.
Definition: feature.h:106
double quadOffsetY
Definition: feature.h:177
double getLabelWidth() const
Definition: feature.h:297
double repeatDist
Definition: feature.h:184
bool offsetPos
Definition: feature.h:178
LabelInfo(int num, double height, double maxinangle=20.0, double maxoutangle=-20.0)
Definition: feature.h:55
double distlabel
Definition: feature.h:167
PalGeometry * getUserGeometry()
Definition: feature.h:294
void setFixedQuadrant(bool fixed)
Sets whether the quadrant for the label must be respected.
Definition: feature.h:94
void setDistLabel(double dist)
Definition: feature.h:85
CharacterInfo * char_info
Definition: feature.h:70
void setPosOffset(double x, double y)
Definition: feature.h:101
Layer * layer
Definition: feature.h:163
void setQuadOffset(double x, double y)
Definition: feature.h:88
double isObstacle() const
Returns whether the feature will act as an obstacle for labels.
Definition: feature.h:121
QString uid
Definition: feature.h:170
double getLabelDistance() const
Definition: feature.h:300
bool getFixedPosition()
Definition: feature.h:305
void setAlwaysShow(bool bl)
Definition: feature.h:109
void setRepeatDistance(double dist)
Definition: feature.h:107
void setLabelInfo(LabelInfo *info)
Definition: feature.h:84
double quadOffsetX
Definition: feature.h:176
Optional additional info about label (for curved labels)
Definition: feature.h:46
double label_height
Definition: feature.h:68
double offsetPosY
Definition: feature.h:180
bool quadOffset
Definition: feature.h:175
Main class to handle feature.
Definition: feature.h:203
double getLabelAngle()
Definition: feature.h:304
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
void setFixedPosition(double x, double y)
Set label position of the feature to fixed x/y values.
Definition: feature.h:87
LabelInfo * labelInfo
Definition: feature.h:168
Feature * mFeature
Definition: feature.h:322
double label_y
Definition: feature.h:166
void setLabelSize(double lx, double ly)
Definition: feature.h:296
double max_char_angle_inside
Definition: feature.h:66
PalGeometry * userGeom
Definition: feature.h:164
double label_x
Definition: feature.h:165
FeaturePart * getSelfObstacle(int i)
Definition: feature.h:309
double priority() const
Returns the feature's labeling priority.
Definition: feature.h:153
void setLabelDistance(double dist)
Definition: feature.h:299
LabelPosition is a candidate feature label position.
Definition: labelposition.h:48
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:58
Interface that allows Pal to access user's geometries.
Definition: palgeometry.h:41
bool alwaysShow
Definition: feature.h:186
bool fixedQuadrant() const
Returns whether the quadrant for the label is fixed.
Definition: feature.h:99
double fixedPosX
Definition: feature.h:173
int char_num
Definition: feature.h:69
double repeatDistance() const
Definition: feature.h:108
bool fixedPosition() const
Definition: feature.h:102
void setObstacleFactor(double factor)
Sets the obstacle factor for the feature.
Definition: feature.h:130