QGIS API Documentation  2.6.0-Brighton
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33 
34 #ifndef _FEATURE_H
35 #define _FEATURE_H
36 
37 #include <iostream>
38 #include <fstream>
39 #include <cmath>
40 
41 #include <geos_c.h>
42 
43 #include <pal/palgeometry.h>
44 
45 #include "pointset.h"
46 #include "util.h"
47 
48 
49 namespace pal
50 {
52  class CORE_EXPORT LabelInfo
53  {
54  public:
55  typedef struct
56  {
57  unsigned short chr;
58  double width;
59  } CharacterInfo;
60 
61  LabelInfo( int num, double height, double maxinangle = 20.0, double maxoutangle = -20.0 )
62  {
63  max_char_angle_inside = maxinangle;
64  // outside angle should be negative
65  max_char_angle_outside = maxoutangle > 0 ? -maxoutangle : maxoutangle;
66  label_height = height;
67  char_num = num;
68  char_info = new CharacterInfo[num];
69  }
70  ~LabelInfo() { delete [] char_info; }
71 
74  double label_height;
75  int char_num;
77  };
78 
79  class LabelPosition;
80  class FeaturePart;
81 
82  class CORE_EXPORT Feature
83  {
84  friend class FeaturePart;
85 
86  public:
87  Feature( Layer* l, const char* id, PalGeometry* userG, double lx, double ly );
88  ~Feature();
89 
90  void setLabelInfo( LabelInfo* info ) { labelInfo = info; }
91  void setDistLabel( double dist ) { distlabel = dist; }
92  //Set label position of the feature to fixed x/y values
93  void setFixedPosition( double x, double y ) { fixedPos = true; fixedPosX = x; fixedPosY = y;}
94  void setQuadOffset( double x, double y ) { quadOffset = true; quadOffsetX = x; quadOffsetY = y;}
95  void setPosOffset( double x, double y ) { offsetPos = true; offsetPosX = x; offsetPosY = y;}
96  bool fixedPosition() const { return fixedPos; }
97  //Set label rotation to fixed value
98  void setFixedAngle( double a ) { fixedRotation = true; fixedAngle = a; }
99  void setRepeatDistance( double dist ) { repeatDist = dist; }
100  double repeatDistance() const { return repeatDist; }
101  void setAlwaysShow( bool bl ) { alwaysShow = bl; }
102 
103  protected:
106  double label_x;
107  double label_y;
108  double distlabel;
109  LabelInfo* labelInfo; // optional
110 
111  char *uid;
112 
113  bool fixedPos; //true in case of fixed position (only 1 candidate position with cost 0)
114  double fixedPosX;
115  double fixedPosY;
116  bool quadOffset; // true if a quadrant offset exists
117  double quadOffsetX;
118  double quadOffsetY;
119  bool offsetPos; //true if position is to be offset by set amount
120  double offsetPosX;
121  double offsetPosY;
122  //Fixed (e.g. data defined) angle only makes sense together with fixed position
124  double fixedAngle; //fixed angle value (in rad)
125  double repeatDist;
126 
127  bool alwaysShow; //true is label is to always be shown (but causes overlapping)
128 
129 
130  // array of parts - possibly not necessary
131  //int nPart;
132  //FeaturePart** parts;
133  };
134 
138  class CORE_EXPORT FeaturePart : public PointSet
139  {
140 
141  protected:
143 
144  int nbHoles;
146 
147  GEOSGeometry *the_geom;
148  bool ownsGeom;
149 
151  void extractCoords( const GEOSGeometry* geom );
152 
156  void removeDuplicatePoints();
157 
158  public:
159 
166  FeaturePart( Feature *feat, const GEOSGeometry* geom );
167 
171  virtual ~FeaturePart();
172 
184  int setPositionForPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle );
185 
189  int setPositionOverPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle );
190 
200  int setPositionForLine( double scale, LabelPosition ***lPos, PointSet *mapShape, double delta_width );
201 
202  LabelPosition* curvedPlacementAtOffset( PointSet* path_positions, double* path_distances,
203  int orientation, int index, double distance );
204 
208  int setPositionForLineCurved( LabelPosition ***lPos, PointSet* mapShape );
209 
219  int setPositionForPolygon( double scale, LabelPosition ***lPos, PointSet *mapShape, double delta_width );
220 
221 #if 0
222 
230  LinkedList<Feature*> *splitFeature( double bbox[4] );
231 
232 
237  int getId();
238 #endif
239 
244  Feature* getFeature() { return f; }
245 
250  const GEOSGeometry* getGeometry() const { return the_geom; }
251 
256  Layer * getLayer();
257 
258 #if 0
259 
264  void save( std::ofstream *file );
265 #endif
266 
278  int setPosition( double scale, LabelPosition ***lPos, double bbox_min[2], double bbox_max[2], PointSet *mapShape, RTree<LabelPosition*, double, 2, double>*candidates
279 #ifdef _EXPORT_MAP_
280  , std::ofstream &svgmap
281 #endif
282  );
283 
288  const char *getUID();
289 
290 
295  void print();
296 
297 
298  PalGeometry* getUserGeometry() { return f->userGeom; }
299 
300  void setLabelSize( double lx, double ly ) { f->label_x = lx; f->label_y = ly; }
301  double getLabelWidth() const { return f->label_x; }
302  double getLabelHeight() const { return f->label_y; }
303  void setLabelDistance( double dist ) { f->distlabel = dist; }
304  double getLabelDistance() const { return f->distlabel; }
305  void setLabelInfo( LabelInfo* info ) { f->labelInfo = info; }
306 
307  bool getFixedRotation() { return f->fixedRotation; }
308  double getLabelAngle() { return f->fixedAngle; }
309  bool getFixedPosition() { return f->fixedPos; }
310  bool getAlwaysShow() { return f->alwaysShow; }
311 
312  int getNumSelfObstacles() const { return nbHoles; }
313  PointSet* getSelfObstacle( int i ) { return holes[i]; }
314 
316  bool isConnected( FeaturePart* p2 );
317 
320  bool mergeWithFeaturePart( FeaturePart* other );
321 
322  void addSizePenalty( int nbp, LabelPosition** lPos, double bbx[4], double bby[4] );
323 
324  };
325 
326 } // end namespace pal
327 
328 #endif