QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
feature.cpp
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 #define _CRT_SECURE_NO_DEPRECATE
35 
36 
37 #if defined(_VERBOSE_) || (_DEBUG_)
38 #include <iostream>
39 #endif
40 
41 #include <qglobal.h>
42 
43 #include <cmath>
44 #include <cstring>
45 #include <cfloat>
46 
47 #include <pal/pal.h>
48 #include <pal/layer.h>
49 
50 #include "linkedlist.hpp"
51 #include "feature.h"
52 #include "geomfunction.h"
53 #include "labelposition.h"
54 #include "pointset.h"
55 #include "simplemutex.h"
56 #include "util.h"
57 
58 #ifndef M_PI
59 #define M_PI 3.14159265358979323846
60 #endif
61 
62 namespace pal
63 {
64  Feature::Feature( Layer* l, const char* geom_id, PalGeometry* userG, double lx, double ly )
65  : layer( l )
66  , userGeom( userG )
67  , label_x( lx )
68  , label_y( ly )
69  , distlabel( 0 )
70  , labelInfo( NULL )
71  , fixedPos( false )
72  , fixedPosX( 0.0 )
73  , fixedPosY( 0.0 )
74  , quadOffset( false )
75  , quadOffsetX( 0.0 )
76  , quadOffsetY( 0.0 )
77  , offsetPos( false )
78  , offsetPosX( 0.0 )
79  , offsetPosY( 0.0 )
80  , fixedRotation( false )
81  , fixedAngle( 0.0 )
82  , repeatDist( 0.0 )
83  , alwaysShow( false )
84  {
85  assert( finite( lx ) && finite( ly ) );
86 
87  uid = new char[strlen( geom_id ) +1];
88  strcpy( uid, geom_id );
89  }
90 
92  {
93  delete[] uid;
94  }
95 
97 
98  FeaturePart::FeaturePart( Feature *feat, const GEOSGeometry* geom )
99  : f( feat ), nbHoles( 0 ), holes( NULL )
100  {
101  // we'll remove const, but we won't modify that geometry
102  the_geom = const_cast<GEOSGeometry*>( geom );
103  ownsGeom = false; // geometry is owned by Feature class
104 
105  extractCoords( geom );
106 
107  holeOf = NULL;
108  for ( int i = 0; i < nbHoles; i++ )
109  {
110  holes[i]->holeOf = this;
111  }
112  }
113 
114 
116  {
117  // X and Y are deleted in PointSet
118 
119  if ( holes )
120  {
121  for ( int i = 0; i < nbHoles; i++ )
122  delete holes[i];
123  delete [] holes;
124  holes = NULL;
125  }
126 
127  if ( ownsGeom )
128  {
129  GEOSGeom_destroy_r( geosContext(), the_geom );
130  the_geom = NULL;
131  }
132  }
133 
134 
135  /*
136  * \brief read coordinates from a GEOS geom
137  */
138  void FeaturePart::extractCoords( const GEOSGeometry* geom )
139  {
140  int i, j;
141 
142  const GEOSCoordSequence *coordSeq;
143  GEOSContextHandle_t geosctxt = geosContext();
144 
145  type = GEOSGeomTypeId_r( geosctxt, geom );
146 
147  if ( type == GEOS_POLYGON )
148  {
149  if ( GEOSGetNumInteriorRings_r( geosctxt, geom ) > 0 )
150  {
151  // set nbHoles, holes member variables
152  nbHoles = GEOSGetNumInteriorRings_r( geosctxt, geom );
153  holes = new PointSet*[nbHoles];
154 
155  for ( i = 0; i < nbHoles; i++ )
156  {
157  holes[i] = new PointSet();
158  holes[i]->holeOf = NULL;
159 
160  const GEOSGeometry* interior = GEOSGetInteriorRingN_r( geosctxt, geom, i );
161  holes[i]->nbPoints = GEOSGetNumCoordinates_r( geosctxt, interior );
162  holes[i]->x = new double[holes[i]->nbPoints];
163  holes[i]->y = new double[holes[i]->nbPoints];
164 
165  holes[i]->xmin = holes[i]->ymin = DBL_MAX;
166  holes[i]->xmax = holes[i]->ymax = -DBL_MAX;
167 
168  coordSeq = GEOSGeom_getCoordSeq_r( geosctxt, interior );
169 
170  for ( j = 0; j < holes[i]->nbPoints; j++ )
171  {
172  GEOSCoordSeq_getX_r( geosctxt, coordSeq, j, &holes[i]->x[j] );
173  GEOSCoordSeq_getY_r( geosctxt, coordSeq, j, &holes[i]->y[j] );
174 
175  holes[i]->xmax = holes[i]->x[j] > holes[i]->xmax ? holes[i]->x[j] : holes[i]->xmax;
176  holes[i]->xmin = holes[i]->x[j] < holes[i]->xmin ? holes[i]->x[j] : holes[i]->xmin;
177 
178  holes[i]->ymax = holes[i]->y[j] > holes[i]->ymax ? holes[i]->y[j] : holes[i]->ymax;
179  holes[i]->ymin = holes[i]->y[j] < holes[i]->ymin ? holes[i]->y[j] : holes[i]->ymin;
180  }
181 
182  reorderPolygon( holes[i]->nbPoints, holes[i]->x, holes[i]->y );
183  }
184  }
185 
186  // use exterior ring for the extraction of coordinates that follows
187  geom = GEOSGetExteriorRing_r( geosctxt, geom );
188  }
189  else
190  {
191  nbHoles = 0;
192  holes = NULL;
193  }
194 
195  // find out number of points
196  nbPoints = GEOSGetNumCoordinates_r( geosctxt, geom );
197  coordSeq = GEOSGeom_getCoordSeq_r( geosctxt, geom );
198 
199  // initialize bounding box
200  xmin = ymin = DBL_MAX;
201  xmax = ymax = -DBL_MAX;
202 
203  // initialize coordinate arrays
204  x = new double[nbPoints];
205  y = new double[nbPoints];
206 
207  for ( i = 0; i < nbPoints; i++ )
208  {
209  GEOSCoordSeq_getX_r( geosctxt, coordSeq, i, &x[i] );
210  GEOSCoordSeq_getY_r( geosctxt, coordSeq, i, &y[i] );
211 
212  xmax = x[i] > xmax ? x[i] : xmax;
213  xmin = x[i] < xmin ? x[i] : xmin;
214 
215  ymax = y[i] > ymax ? y[i] : ymax;
216  ymin = y[i] < ymin ? y[i] : ymin;
217  }
218  }
219 
221  {
222  // TODO add simplify() process
223  int new_nbPoints = nbPoints;
224  bool *ok = new bool[new_nbPoints];
225  int i, j;
226 
227  for ( i = 0; i < nbPoints; i++ )
228  {
229  ok[i] = true;
230  j = ( i + 1 ) % nbPoints;
231  if ( i == j )
232  break;
233  if ( vabs( x[i] - x[j] ) < 0.0000001 && vabs( y[i] - y[j] ) < 0.0000001 )
234  {
235  new_nbPoints--;
236  ok[i] = false;
237  }
238  }
239 
240  if ( new_nbPoints < nbPoints )
241  {
242  double *new_x = new double[new_nbPoints];
243  double *new_y = new double[new_nbPoints];
244  for ( i = 0, j = 0; i < nbPoints; i++ )
245  {
246  if ( ok[i] )
247  {
248  new_x[j] = x[i];
249  new_y[j] = y[i];
250  j++;
251  }
252  }
253  delete[] x;
254  delete[] y;
255  // interchange the point arrays
256  x = new_x;
257  y = new_y;
258  nbPoints = new_nbPoints;
259  }
260 
261  delete[] ok;
262  }
263 
264 
265 
266 
268  {
269  return f->layer;
270  }
271 
272 
273  const char * FeaturePart::getUID()
274  {
275  return f->uid;
276  }
277 
278  LabelPosition::Quadrant FeaturePart::quadrantFromOffset() const
279  {
280  if ( f->quadOffsetX < 0 )
281  {
282  if ( f->quadOffsetY < 0 )
283  {
285  }
286  else if ( f->quadOffsetY > 0 )
287  {
289  }
290  else
291  {
293  }
294  }
295  else if ( f->quadOffsetX > 0 )
296  {
297  if ( f->quadOffsetY < 0 )
298  {
300  }
301  else if ( f->quadOffsetY > 0 )
302  {
304  }
305  else
306  {
308  }
309  }
310  else
311  {
312  if ( f->quadOffsetY < 0 )
313  {
315  }
316  else if ( f->quadOffsetY > 0 )
317  {
319  }
320  else
321  {
323  }
324  }
325  }
326 
327  int FeaturePart::setPositionOverPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle )
328  {
329  Q_UNUSED( scale );
330  Q_UNUSED( delta_width );
331  int nbp = 1;
332  *lPos = new LabelPosition *[nbp];
333 
334  // get from feature
335  double labelW = f->label_x;
336  double labelH = f->label_y;
337 
338  double cost = 0.0001;
339  int id = 0;
340 
341  double xdiff = -labelW / 2.0;
342  double ydiff = -labelH / 2.0;
343 
344  if ( f->quadOffset )
345  {
346  if ( f->quadOffsetX != 0 )
347  {
348  xdiff += labelW / 2.0 * f->quadOffsetX;
349  }
350  if ( f->quadOffsetY != 0 )
351  {
352  ydiff += labelH / 2.0 * f->quadOffsetY;
353  }
354  }
355 
356  if ( ! f->fixedPosition() )
357  {
358  if ( angle != 0 )
359  {
360  double xd = xdiff * cos( angle ) - ydiff * sin( angle );
361  double yd = xdiff * sin( angle ) + ydiff * cos( angle );
362  xdiff = xd;
363  ydiff = yd;
364  }
365  }
366 
367  if ( f->offsetPos )
368  {
369  if ( f->offsetPosX != 0 )
370  {
371  xdiff += f->offsetPosX;
372  }
373  if ( f->offsetPosY != 0 )
374  {
375  ydiff += f->offsetPosY;
376  }
377  }
378 
379  double lx = x + xdiff;
380  double ly = y + ydiff;
381 
382  ( *lPos )[0] = new LabelPosition( id, lx, ly, labelW, labelH, angle, cost, this );
383  return nbp;
384  }
385 
386  int FeaturePart::setPositionForPoint( double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle )
387  {
388 
389 #ifdef _DEBUG_
390  std::cout << "SetPosition (point) : " << layer->name << "/" << uid << std::endl;
391 #endif
392 
393  int dpi = f->layer->pal->dpi;
394 
395 
396  double xrm;
397  double yrm;
398  double distlabel = f->distlabel;
399 
400  xrm = unit_convert( f->label_x,
401  f->layer->label_unit,
402  f->layer->pal->map_unit,
403  dpi, scale, delta_width );
404 
405  yrm = unit_convert( f->label_y,
406  f->layer->label_unit,
407  f->layer->pal->map_unit,
408  dpi, scale, delta_width );
409 
410  int nbp = f->layer->pal->point_p;
411 
412  //std::cout << "Nbp : " << nbp << std::endl;
413 
414  int i;
415  int icost = 0;
416  int inc = 2;
417 
418  double alpha;
419  double beta = 2 * M_PI / nbp; /* angle bw 2 pos */
420 
421  // uncomment for Wolff 2 position model test on RailwayStation
422  //if (nbp==2)
423  // beta = M_PI/2;
424 
425 #if 0
426  double distlabel = unit_convert( this->distlabel,
427  pal::PIXEL,
428  layer->pal->map_unit,
429  dpi, scale, delta_width );
430 #endif
431 
432  double lx, ly; /* label pos */
433 
434  /* various alpha */
435  double a90 = M_PI / 2;
436  double a180 = M_PI;
437  double a270 = a180 + a90;
438  double a360 = 2 * M_PI;
439 
440 
441  double gamma1, gamma2;
442 
443  if ( distlabel > 0 )
444  {
445  gamma1 = atan2( yrm / 2, distlabel + xrm / 2 );
446  gamma2 = atan2( xrm / 2, distlabel + yrm / 2 );
447  }
448  else
449  {
450  gamma1 = gamma2 = a90 / 3.0;
451  }
452 
453 
454  if ( gamma1 > a90 / 3.0 )
455  gamma1 = a90 / 3.0;
456 
457  if ( gamma2 > a90 / 3.0 )
458  gamma2 = a90 / 3.0;
459 
460 
461  if ( gamma1 == 0 || gamma2 == 0 )
462  {
463  std::cout << "Oups... label size error..." << std::endl;
464  }
465 
466  *lPos = new LabelPosition *[nbp];
467 
468  for ( i = 0, alpha = M_PI / 4; i < nbp; i++, alpha += beta )
469  {
470  lx = x;
471  ly = y;
472 
473  if ( alpha > a360 )
474  alpha -= a360;
475 
477 
478  if ( alpha < gamma1 || alpha > a360 - gamma1 ) // on the right
479  {
480  lx += distlabel;
481  double iota = ( alpha + gamma1 );
482  if ( iota > a360 - gamma1 )
483  iota -= a360;
484 
485  //ly += -yrm/2.0 + tan(alpha)*(distlabel + xrm/2);
486  ly += -yrm + yrm * iota / ( 2 * gamma1 );
487 
488  quadrant = LabelPosition::QuadrantRight;
489  }
490  else if ( alpha < a90 - gamma2 ) // top-right
491  {
492  lx += distlabel * cos( alpha );
493  ly += distlabel * sin( alpha );
495  }
496  else if ( alpha < a90 + gamma2 ) // top
497  {
498  //lx += -xrm/2.0 - tan(alpha+a90)*(distlabel + yrm/2);
499  lx += -xrm * ( alpha - a90 + gamma2 ) / ( 2 * gamma2 );
500  ly += distlabel;
501  quadrant = LabelPosition::QuadrantAbove;
502  }
503  else if ( alpha < a180 - gamma1 ) // top left
504  {
505  lx += distlabel * cos( alpha ) - xrm;
506  ly += distlabel * sin( alpha );
508  }
509  else if ( alpha < a180 + gamma1 ) // left
510  {
511  lx += -distlabel - xrm;
512  //ly += -yrm/2.0 - tan(alpha)*(distlabel + xrm/2);
513  ly += - ( alpha - a180 + gamma1 ) * yrm / ( 2 * gamma1 );
514  quadrant = LabelPosition::QuadrantLeft;
515  }
516  else if ( alpha < a270 - gamma2 ) // down - left
517  {
518  lx += distlabel * cos( alpha ) - xrm;
519  ly += distlabel * sin( alpha ) - yrm;
521  }
522  else if ( alpha < a270 + gamma2 ) // down
523  {
524  ly += -distlabel - yrm;
525  //lx += -xrm/2.0 + tan(alpha+a90)*(distlabel + yrm/2);
526  lx += -xrm + ( alpha - a270 + gamma2 ) * xrm / ( 2 * gamma2 );
527  quadrant = LabelPosition::QuadrantBelow;
528  }
529  else if ( alpha < a360 ) // down - right
530  {
531  lx += distlabel * cos( alpha );
532  ly += distlabel * sin( alpha ) - yrm;
534  }
535 
536  double cost;
537 
538  if ( nbp == 1 )
539  cost = 0.0001;
540  else
541  cost = 0.0001 + 0.0020 * double( icost ) / double( nbp - 1 );
542 
543  ( *lPos )[i] = new LabelPosition( i, lx, ly, xrm, yrm, angle, cost, this, false, quadrant );
544 
545  icost += inc;
546 
547  if ( icost == nbp )
548  {
549  icost = nbp - 1;
550  inc = -2;
551  }
552  else if ( icost > nbp )
553  {
554  icost = nbp - 2;
555  inc = -2;
556  }
557 
558  }
559 
560  return nbp;
561  }
562 
563 // TODO work with squared distance by remonving call to sqrt or dist_euc2d
564  int FeaturePart::setPositionForLine( double scale, LabelPosition ***lPos, PointSet *mapShape, double delta_width )
565  {
566 #ifdef _DEBUG_
567  std::cout << "SetPosition (line) : " << layer->name << "/" << uid << std::endl;
568 #endif
569  int i;
570  int dpi = f->layer->pal->dpi;
571  double xrm, yrm;
572  double distlabel = f->distlabel;
573 
574  xrm = unit_convert( f->label_x,
575  f->layer->label_unit,
576  f->layer->pal->map_unit,
577  dpi, scale, delta_width );
578 
579  yrm = unit_convert( f->label_y,
580  f->layer->label_unit,
581  f->layer->pal->map_unit,
582  dpi, scale, delta_width );
583 
584 
585 #if 0
586  double distlabel = unit_convert( this->distlabel,
587  pal::PIXEL,
588  layer->pal->map_unit,
589  dpi, scale, delta_width );
590 #endif
591 
592 
593  double *d; // segments lengths distance bw pt[i] && pt[i+1]
594  double *ad; // absolute distance bw pt[0] and pt[i] along the line
595  double ll; // line length
596  double dist;
597  double bx, by, ex, ey;
598  int nbls;
599  double alpha;
600  double cost;
601 
602  unsigned long flags = f->layer->getArrangementFlags();
603  if ( flags == 0 )
604  flags = FLAG_ON_LINE; // default flag
605 
606  //LinkedList<PointSet*> *shapes_final;
607 
608  //shapes_final = new LinkedList<PointSet*>(ptrPSetCompare);
609 
611 
612  int nbPoints;
613  double *x;
614  double *y;
615 
616  PointSet * line = mapShape;
617 #ifdef _DEBUG_FULL_
618  std::cout << "New line of " << line->nbPoints << " points with label " << xrm << "x" << yrm << std::endl;
619 #endif
620 
621  nbPoints = line->nbPoints;
622  x = line->x;
623  y = line->y;
624 
625  d = new double[nbPoints-1];
626  ad = new double[nbPoints];
627 
628  ll = 0.0; // line length
629  for ( i = 0; i < line->nbPoints - 1; i++ )
630  {
631  if ( i == 0 )
632  ad[i] = 0;
633  else
634  ad[i] = ad[i-1] + d[i-1];
635 
636  d[i] = dist_euc2d( x[i], y[i], x[i+1], y[i+1] );
637  ll += d[i];
638  }
639 
640  ad[line->nbPoints-1] = ll;
641 
642 
643  nbls = ( int )( ll / xrm ); // ratio bw line length and label width
644 
645 #ifdef _DEBUG_FULL_
646  std::cout << "line length :" << ll << std::endl;
647  std::cout << "nblp :" << nbls << std::endl;
648 #endif
649 
650  dist = ( ll - xrm );
651 
652  double l;
653 
654  if ( nbls > 0 )
655  {
656  //dist /= nbls;
657  l = 0;
658  dist = min( yrm, xrm );
659  }
660  else // line length < label with => centering label position
661  {
662  l = - ( xrm - ll ) / 2.0;
663  dist = xrm;
664  ll = xrm;
665  }
666 
667  double birdfly;
668  double beta;
669  i = 0;
670  //for (i=0;i<nbp;i++){
671 #ifdef _DEBUG_FULL_
672  std::cout << l << " / " << ll - xrm << std::endl;
673 #endif
674  while ( l < ll - xrm )
675  {
676  // => bx, by
677  line->getPoint( d, ad, l, &bx, &by );
678  // same but l = l+xrm
679  line->getPoint( d, ad, l + xrm, &ex, &ey );
680 
681  // Label is bigger than line ...
682  if ( l < 0 )
683  birdfly = sqrt(( x[nbPoints-1] - x[0] ) * ( x[nbPoints-1] - x[0] )
684  + ( y[nbPoints-1] - y[0] ) * ( y[nbPoints-1] - y[0] ) );
685  else
686  birdfly = sqrt(( ex - bx ) * ( ex - bx ) + ( ey - by ) * ( ey - by ) );
687 
688  cost = birdfly / xrm;
689  if ( cost > 0.98 )
690  cost = 0.0001;
691  else
692  cost = ( 1 - cost ) / 100; // < 0.0001, 0.01 > (but 0.005 is already pretty much)
693 
694  // penalize positions which are further from the line's midpoint
695  double costCenter = vabs( ll / 2 - ( l + xrm / 2 ) ) / ll; // <0, 0.5>
696  cost += costCenter / 1000; // < 0, 0.0005 >
697 
698  if (( vabs( ey - by ) < EPSILON ) && ( vabs( ex - bx ) < EPSILON ) )
699  {
700  std::cout << "EPSILON " << EPSILON << std::endl;
701  std::cout << "b: " << bx << ";" << by << std::endl;
702  std::cout << "e: " << ex << ";" << ey << std::endl;
703  alpha = 0.0;
704  }
705  else
706  alpha = atan2( ey - by, ex - bx );
707 
708  beta = alpha + M_PI / 2;
709 
710 #ifdef _DEBUG_FULL_
711  std::cout << " Create new label" << std::endl;
712 #endif
713  if ( f->layer->arrangement == P_LINE )
714  {
715  // find out whether the line direction for this candidate is from right to left
716  bool isRightToLeft = ( alpha > M_PI / 2 || alpha <= -M_PI / 2 );
717  // meaning of above/below may be reversed if using line position dependent orientation
718  // and the line has right-to-left direction
719  bool reversed = (( flags & FLAG_MAP_ORIENTATION ) ? isRightToLeft : false );
720  bool aboveLine = ( !reversed && ( flags & FLAG_ABOVE_LINE ) ) || ( reversed && ( flags & FLAG_BELOW_LINE ) );
721  bool belowLine = ( !reversed && ( flags & FLAG_BELOW_LINE ) ) || ( reversed && ( flags & FLAG_ABOVE_LINE ) );
722 
723  if ( aboveLine )
724  positions->push_back( new LabelPosition( i, bx + cos( beta ) *distlabel, by + sin( beta ) *distlabel, xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
725  if ( belowLine )
726  positions->push_back( new LabelPosition( i, bx - cos( beta ) *( distlabel + yrm ), by - sin( beta ) *( distlabel + yrm ), xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
727  if ( flags & FLAG_ON_LINE )
728  positions->push_back( new LabelPosition( i, bx - yrm*cos( beta ) / 2, by - yrm*sin( beta ) / 2, xrm, yrm, alpha, cost, this, isRightToLeft ) ); // Line
729  }
730  else if ( f->layer->arrangement == P_HORIZ )
731  {
732  positions->push_back( new LabelPosition( i, bx - xrm / 2, by - yrm / 2, xrm, yrm, 0, cost, this ) ); // Line
733  //positions->push_back( new LabelPosition(i, bx -yrm/2, by - yrm*sin(beta)/2, xrm, yrm, alpha, cost, this, line)); // Line
734  }
735  else
736  {
737  // an invalid arrangement?
738  }
739 
740  l += dist;
741 
742  i++;
743 
744  if ( nbls == 0 )
745  break;
746  }
747 
748  //delete line;
749 
750  delete[] d;
751  delete[] ad;
752 
753  int nbp = positions->size();
754  *lPos = new LabelPosition *[nbp];
755  i = 0;
756  while ( positions->size() > 0 )
757  {
758  ( *lPos )[i] = positions->pop_front();
759  i++;
760  }
761 
762  delete positions;
763 
764  return nbp;
765  }
766 
767 
768  LabelPosition* FeaturePart::curvedPlacementAtOffset( PointSet* path_positions, double* path_distances, int orientation, int index, double distance )
769  {
770  // Check that the given distance is on the given index and find the correct index and distance if not
771  while ( distance < 0 && index > 1 )
772  {
773  index--;
774  distance += path_distances[index];
775  }
776 
777  if ( index <= 1 && distance < 0 ) // We've gone off the start, fail out
778  {
779  return NULL;
780  }
781 
782  // Same thing, checking if we go off the end
783  while ( index < path_positions->nbPoints && distance > path_distances[index] )
784  {
785  distance -= path_distances[index];
786  index += 1;
787  }
788  if ( index >= path_positions->nbPoints )
789  {
790  return NULL;
791  }
792 
793  // Keep track of the initial index,distance incase we need to re-call get_placement_offset
794  int initial_index = index;
795  double initial_distance = distance;
796 
797  double string_height = f->labelInfo->label_height;
798  double old_x = path_positions->x[index-1];
799  double old_y = path_positions->y[index-1];
800 
801  double new_x = path_positions->x[index];
802  double new_y = path_positions->y[index];
803 
804  double dx = new_x - old_x;
805  double dy = new_y - old_y;
806 
807  double segment_length = path_distances[index];
808  if ( segment_length == 0 )
809  {
810  // Not allowed to place across on 0 length segments or discontinuities
811  return NULL;
812  }
813 
814  LabelPosition* slp = NULL;
815  LabelPosition* slp_tmp = NULL;
816  // current_placement = placement_result()
817  double angle = atan2( -dy, dx );
818 
819  bool orientation_forced = ( orientation != 0 ); // Whether the orientation was set by the caller
820  if ( !orientation_forced )
821  orientation = ( angle > 0.55 * M_PI || angle < -0.45 * M_PI ? -1 : 1 );
822 
823  int upside_down_char_count = 0; // Count of characters that are placed upside down.
824 
825  for ( int i = 0; i < f->labelInfo->char_num; i++ )
826  {
827  double last_character_angle = angle;
828 
829  // grab the next character according to the orientation
830  LabelInfo::CharacterInfo& ci = ( orientation > 0 ? f->labelInfo->char_info[i] : f->labelInfo->char_info[f->labelInfo->char_num-i-1] );
831 
832  // Coordinates this character will start at
833  if ( segment_length == 0 )
834  {
835  // Not allowed to place across on 0 length segments or discontinuities
836  delete slp;
837  return NULL;
838  }
839 
840  double start_x = old_x + dx * distance / segment_length;
841  double start_y = old_y + dy * distance / segment_length;
842  // Coordinates this character ends at, calculated below
843  double end_x = 0;
844  double end_y = 0;
845 
846  //std::cerr << "segment len " << segment_length << " distance " << distance << std::endl;
847  if ( segment_length - distance >= ci.width )
848  {
849  // if the distance remaining in this segment is enough, we just go further along the segment
850  distance += ci.width;
851  end_x = old_x + dx * distance / segment_length;
852  end_y = old_y + dy * distance / segment_length;
853  }
854  else
855  {
856  // If there isn't enough distance left on this segment
857  // then we need to search until we find the line segment that ends further than ci.width away
858  do
859  {
860  old_x = new_x;
861  old_y = new_y;
862  index++;
863  if ( index >= path_positions->nbPoints ) // Bail out if we run off the end of the shape
864  {
865  delete slp;
866  return NULL;
867  }
868  new_x = path_positions->x[index];
869  new_y = path_positions->y[index];
870  dx = new_x - old_x;
871  dy = new_y - old_y;
872  segment_length = path_distances[index];
873 
874  //std::cerr << "-> " << sqrt(pow(start_x - new_x,2) + pow(start_y - new_y,2)) << " vs " << ci.width << std::endl;
875 
876  }
877  while ( sqrt( pow( start_x - new_x, 2 ) + pow( start_y - new_y, 2 ) ) < ci.width ); // Distance from start_ to new_
878 
879  // Calculate the position to place the end of the character on
880  findLineCircleIntersection( start_x, start_y, ci.width, old_x, old_y, new_x, new_y, end_x, end_y );
881 
882  // Need to calculate distance on the new segment
883  distance = sqrt( pow( old_x - end_x, 2 ) + pow( old_y - end_y, 2 ) );
884  }
885 
886  // Calculate angle from the start of the character to the end based on start_/end_ position
887  angle = atan2( start_y - end_y, end_x - start_x );
888  //angle = atan2(end_y-start_y, end_x-start_x);
889 
890  // Test last_character_angle vs angle
891  // since our rendering angle has changed then check against our
892  // max allowable angle change.
893  double angle_delta = last_character_angle - angle;
894  // normalise between -180 and 180
895  while ( angle_delta > M_PI ) angle_delta -= 2 * M_PI;
896  while ( angle_delta < -M_PI ) angle_delta += 2 * M_PI;
897  if (( f->labelInfo->max_char_angle_inside > 0 && angle_delta > 0
898  && angle_delta > f->labelInfo->max_char_angle_inside*( M_PI / 180 ) )
899  || ( f->labelInfo->max_char_angle_outside < 0 && angle_delta < 0
900  && angle_delta < f->labelInfo->max_char_angle_outside*( M_PI / 180 ) ) )
901  {
902  delete slp;
903  return NULL;
904  }
905 
906  double render_angle = angle;
907 
908  double render_x = start_x;
909  double render_y = start_y;
910 
911  // Center the text on the line
912  //render_x -= ((string_height/2.0) - 1.0)*math.cos(render_angle+math.pi/2)
913  //render_y += ((string_height/2.0) - 1.0)*math.sin(render_angle+math.pi/2)
914 
915  if ( orientation < 0 )
916  {
917  // rotate in place
918  render_x += ci.width * cos( render_angle ); //- (string_height-2)*sin(render_angle);
919  render_y -= ci.width * sin( render_angle ); //+ (string_height-2)*cos(render_angle);
920  render_angle += M_PI;
921  }
922 
923  //std::cerr << "adding part: " << render_x << " " << render_y << std::endl;
924  LabelPosition* tmp = new LabelPosition( 0, render_x /*- xBase*/, render_y /*- yBase*/, ci.width, string_height, -render_angle, 0.0001, this );
925  tmp->setPartId( orientation > 0 ? i : f->labelInfo->char_num - i - 1 );
926  if ( slp == NULL )
927  slp = tmp;
928  else
929  slp_tmp->setNextPart( tmp );
930  slp_tmp = tmp;
931 
932  //current_placement.add_node(ci.character,render_x, -render_y, render_angle);
933  //current_placement.add_node(ci.character,render_x - current_placement.starting_x, render_y - current_placement.starting_y, render_angle)
934 
935  // Normalise to 0 <= angle < 2PI
936  while ( render_angle >= 2*M_PI ) render_angle -= 2 * M_PI;
937  while ( render_angle < 0 ) render_angle += 2 * M_PI;
938 
939  if ( render_angle > M_PI / 2 && render_angle < 1.5*M_PI )
940  upside_down_char_count++;
941  }
942  // END FOR
943 
944  // If we placed too many characters upside down
945  if ( upside_down_char_count >= f->labelInfo->char_num / 2.0 )
946  {
947  // if we auto-detected the orientation then retry with the opposite orientation
948  if ( !orientation_forced )
949  {
950  orientation = -orientation;
951  delete slp;
952  slp = curvedPlacementAtOffset( path_positions, path_distances, orientation, initial_index, initial_distance );
953  }
954  else
955  {
956  // Otherwise we have failed to find a placement
957  delete slp;
958  return NULL;
959  }
960  }
961 
962  return slp;
963  }
964 
965  static LabelPosition* _createCurvedCandidate( LabelPosition* lp, double angle, double dist )
966  {
967  LabelPosition* newLp = new LabelPosition( *lp );
968  newLp->offsetPosition( dist*cos( angle + M_PI / 2 ), dist*sin( angle + M_PI / 2 ) );
969  return newLp;
970  }
971 
973  {
974  // label info must be present
975  if ( f->labelInfo == NULL || f->labelInfo->char_num == 0 )
976  return 0;
977 
978  // distance calculation
979  double* path_distances = new double[mapShape->nbPoints];
980  double total_distance = 0;
981  double old_x = -1.0, old_y = -1.0;
982  for ( int i = 0; i < mapShape->nbPoints; i++ )
983  {
984  if ( i == 0 )
985  path_distances[i] = 0;
986  else
987  path_distances[i] = sqrt( pow( old_x - mapShape->x[i], 2 ) + pow( old_y - mapShape->y[i], 2 ) );
988  old_x = mapShape->x[i];
989  old_y = mapShape->y[i];
990 
991  total_distance += path_distances[i];
992  }
993 
994  if ( total_distance == 0 )
995  {
996  delete[] path_distances;
997  return 0;
998  }
999 
1001  double delta = max( f->labelInfo->label_height, total_distance / 10.0 );
1002 
1003  unsigned long flags = f->layer->getArrangementFlags();
1004  if ( flags == 0 )
1005  flags = FLAG_ON_LINE; // default flag
1006 
1007  // generate curved labels
1008  for ( int i = 0; i*delta < total_distance; i++ )
1009  {
1010  LabelPosition* slp = curvedPlacementAtOffset( mapShape, path_distances, 0, 1, i * delta );
1011 
1012  if ( slp )
1013  {
1014  // evaluate cost
1015  double angle_diff = 0.0, angle_last = 0.0, diff;
1016  LabelPosition* tmp = slp;
1017  double sin_avg = 0, cos_avg = 0;
1018  while ( tmp )
1019  {
1020  if ( tmp != slp ) // not first?
1021  {
1022  diff = fabs( tmp->getAlpha() - angle_last );
1023  if ( diff > 2*M_PI ) diff -= 2 * M_PI;
1024  diff = min( diff, 2 * M_PI - diff ); // difference 350 deg is actually just 10 deg...
1025  angle_diff += diff;
1026  }
1027 
1028  sin_avg += sin( tmp->getAlpha() );
1029  cos_avg += cos( tmp->getAlpha() );
1030  angle_last = tmp->getAlpha();
1031  tmp = tmp->getNextPart();
1032  }
1033 
1034  double angle_diff_avg = f->labelInfo->char_num > 1 ? ( angle_diff / ( f->labelInfo->char_num - 1 ) ) : 0; // <0, pi> but pi/8 is much already
1035  double cost = angle_diff_avg / 100; // <0, 0.031 > but usually <0, 0.003 >
1036  if ( cost < 0.0001 ) cost = 0.0001;
1037 
1038  // penalize positions which are further from the line's midpoint
1039  double labelCenter = ( i * delta ) + f->label_x / 2;
1040  double costCenter = vabs( total_distance / 2 - labelCenter ) / total_distance; // <0, 0.5>
1041  cost += costCenter / 1000; // < 0, 0.0005 >
1042  //std::cerr << "cost " << angle_diff << " vs " << costCenter << std::endl;
1043  slp->setCost( cost );
1044 
1045 
1046  // average angle is calculated with respect to periodicity of angles
1047  double angle_avg = atan2( sin_avg / f->labelInfo->char_num, cos_avg / f->labelInfo->char_num );
1048  // displacement
1049  if ( flags & FLAG_ABOVE_LINE )
1050  positions->push_back( _createCurvedCandidate( slp, angle_avg, f->distlabel ) );
1051  if ( flags & FLAG_ON_LINE )
1052  positions->push_back( _createCurvedCandidate( slp, angle_avg, -f->labelInfo->label_height / 2 ) );
1053  if ( flags & FLAG_BELOW_LINE )
1054  positions->push_back( _createCurvedCandidate( slp, angle_avg, -f->labelInfo->label_height - f->distlabel ) );
1055 
1056  // delete original candidate
1057  delete slp;
1058  }
1059  }
1060 
1061 
1062  int nbp = positions->size();
1063  ( *lPos ) = new LabelPosition*[nbp];
1064  for ( int i = 0; i < nbp; i++ )
1065  {
1066  ( *lPos )[i] = positions->pop_front();
1067  }
1068  delete positions;
1069  delete[] path_distances;
1070 
1071  return nbp;
1072  }
1073 
1074 
1075 
1076 
1077  /*
1078  * seg 2
1079  * pt3 ____________pt2
1080  * ¦ ¦
1081  * ¦ ¦
1082  * seg 3 ¦ BBOX ¦ seg 1
1083  * ¦ ¦
1084  * ¦____________¦
1085  * pt0 seg 0 pt1
1086  *
1087  */
1088 
1089  int FeaturePart::setPositionForPolygon( double scale, LabelPosition ***lPos, PointSet *mapShape, double delta_width )
1090  {
1091 
1092 #ifdef _DEBUG_
1093  std::cout << "SetPosition (polygon) : " << layer->name << "/" << uid << std::endl;
1094 #endif
1095 
1096  int i;
1097  int j;
1098 
1099  double xrm;
1100  double yrm;
1101 
1102  xrm = unit_convert( f->label_x,
1103  f->layer->label_unit,
1104  f->layer->pal->map_unit,
1105  f->layer->pal->dpi, scale, delta_width );
1106 
1107  yrm = unit_convert( f->label_y,
1108  f->layer->label_unit,
1109  f->layer->pal->map_unit,
1110  f->layer->pal->dpi, scale, delta_width );
1111 
1112  //print();
1113 
1114  //LinkedList<PointSet*> *shapes_toCut;
1115  LinkedList<PointSet*> *shapes_toProcess;
1116  LinkedList<PointSet*> *shapes_final;
1117 
1118  //shapes_toCut = new LinkedList<PointSet*>(ptrPSetCompare);
1119  shapes_toProcess = new LinkedList<PointSet*> ( ptrPSetCompare );
1120  shapes_final = new LinkedList<PointSet*> ( ptrPSetCompare );
1121 
1122  mapShape->parent = NULL;
1123 
1124  shapes_toProcess->push_back( mapShape );
1125 
1126  splitPolygons( shapes_toProcess, shapes_final, xrm, yrm, f->uid );
1127 
1128 
1129  delete shapes_toProcess;
1130 
1131  int nbp;
1132 
1133  if ( shapes_final->size() > 0 )
1134  {
1136 
1137  int id = 0; // ids for candidates
1138  double dlx, dly; // delta from label center and bottom-left corner
1139  double alpha = 0.0; // rotation for the label
1140  double px, py;
1141  double dx;
1142  double dy;
1143  int bbid;
1144  double beta;
1145  double diago = sqrt( xrm * xrm / 4.0 + yrm * yrm / 4 );
1146  double rx, ry;
1147  CHullBox **boxes = new CHullBox*[shapes_final->size()];
1148  j = 0;
1149 
1150  // Compute bounding box foreach finalShape
1151  while ( shapes_final->size() > 0 )
1152  {
1153  PointSet *shape = shapes_final->pop_front();
1154  boxes[j] = shape->compute_chull_bbox();
1155 
1156  if ( shape->parent )
1157  delete shape;
1158 
1159  j++;
1160  }
1161 
1162  //dx = dy = min( yrm, xrm ) / 2;
1163  dx = xrm / 2.0;
1164  dy = yrm / 2.0;
1165 
1166 
1167  int num_try = 0;
1168  int max_try = 10;
1169  do
1170  {
1171  for ( bbid = 0; bbid < j; bbid++ )
1172  {
1173  CHullBox *box = boxes[bbid];
1174 
1175  if (( box->length * box->width ) > ( xmax - xmin ) *( ymax - ymin ) *5 )
1176  {
1177  std::cout << "Very Large BBOX (should never occur : bug-report please)" << std::endl;
1178  std::cout << " Box size: " << box->length << "/" << box->width << std::endl;
1179  std::cout << " Alpha: " << alpha << " " << alpha * 180 / M_PI << std::endl;
1180  std::cout << " Dx;Dy: " << dx << " " << dy << std::endl;
1181  std::cout << " LabelSizerm: " << xrm << " " << yrm << std::endl;
1182  std::cout << " LabelSizeUn: " << f->label_x << " " << f->label_y << std::endl;
1183  continue;
1184  }
1185 
1186 #ifdef _DEBUG_FULL_
1187  std::cout << "New BBox : " << bbid << std::endl;
1188  for ( i = 0; i < 4; i++ )
1189  {
1190  std::cout << box->x[i] << "\t" << box->y[i] << std::endl;
1191  }
1192 #endif
1193 
1194  bool enoughPlace = false;
1195  if ( f->layer->getArrangement() == P_FREE )
1196  {
1197  enoughPlace = true;
1198  px = ( box->x[0] + box->x[2] ) / 2 - xrm;
1199  py = ( box->y[0] + box->y[2] ) / 2 - yrm;
1200  int i, j;
1201 
1202  // Virtual label: center on bbox center, label size = 2x original size
1203  // alpha = 0.
1204  // If all corner are in bbox then place candidates horizontaly
1205  for ( rx = px, i = 0; i < 2; rx = rx + 2 * xrm, i++ )
1206  {
1207  for ( ry = py, j = 0; j < 2; ry = ry + 2 * yrm, j++ )
1208  {
1209  // TODO should test with the polyone insteand of the bbox
1210  if ( !isPointInPolygon( 4, box->x, box->y, rx, ry ) )
1211  {
1212  enoughPlace = false;
1213  break;
1214  }
1215  }
1216  if ( !enoughPlace )
1217  {
1218  break;
1219  }
1220  }
1221 
1222  } // arrangement== FREE ?
1223 
1224  if ( f->layer->getArrangement() == P_HORIZ || enoughPlace )
1225  {
1226  alpha = 0.0; // HORIZ
1227  }
1228  else if ( box->length > 1.5*xrm && box->width > 1.5*xrm )
1229  {
1230  if ( box->alpha <= M_PI / 4 )
1231  {
1232  alpha = box->alpha;
1233  }
1234  else
1235  {
1236  alpha = box->alpha - M_PI / 2;
1237  }
1238  }
1239  else if ( box->length > box->width )
1240  {
1241  alpha = box->alpha - M_PI / 2;
1242  }
1243  else
1244  {
1245  alpha = box->alpha;
1246  }
1247 
1248  beta = atan2( yrm, xrm ) + alpha;
1249 
1250 
1251  //alpha = box->alpha;
1252 
1253  // delta from label center and down-left corner
1254  dlx = cos( beta ) * diago;
1255  dly = sin( beta ) * diago;
1256 
1257 
1258  double px0, py0;
1259 
1260  px0 = box->width / 2.0;
1261  py0 = box->length / 2.0;
1262 
1263  px0 -= ceil( px0 / dx ) * dx;
1264  py0 -= ceil( py0 / dy ) * dy;
1265 
1266  for ( px = px0; px <= box->width; px += dx )
1267  {
1268  for ( py = py0; py <= box->length; py += dy )
1269  {
1270 
1271  rx = cos( box->alpha ) * px + cos( box->alpha - M_PI / 2 ) * py;
1272  ry = sin( box->alpha ) * px + sin( box->alpha - M_PI / 2 ) * py;
1273 
1274  rx += box->x[0];
1275  ry += box->y[0];
1276 
1277  // Only accept candidate that center is in the polygon
1278  if ( isPointInPolygon( mapShape->nbPoints, mapShape->x, mapShape->y, rx, ry ) )
1279  {
1280  // cost is set to minimal value, evaluated later
1281  positions->push_back( new LabelPosition( id++, rx - dlx, ry - dly, xrm, yrm, alpha, 0.0001, this ) ); // Polygon
1282  }
1283  }
1284  }
1285  } // forall box
1286 
1287  nbp = positions->size();
1288  if ( nbp == 0 )
1289  {
1290  dx /= 2;
1291  dy /= 2;
1292  num_try++;
1293  }
1294  }
1295  while ( nbp == 0 && num_try < max_try );
1296 
1297  nbp = positions->size();
1298 
1299  ( *lPos ) = new LabelPosition*[nbp];
1300  for ( i = 0; i < nbp; i++ )
1301  {
1302  ( *lPos )[i] = positions->pop_front();
1303  }
1304 
1305  for ( bbid = 0; bbid < j; bbid++ )
1306  {
1307  delete boxes[bbid];
1308  }
1309 
1310  delete[] boxes;
1311  delete positions;
1312  }
1313  else
1314  {
1315  nbp = 0;
1316  }
1317 
1318  delete shapes_final;
1319 
1320 #ifdef _DEBUG_FULL_
1321  std::cout << "NbLabelPosition: " << nbp << std::endl;
1322 #endif
1323  return nbp;
1324  }
1325 
1327  {
1328  int i, j;
1329  std::cout << "Geometry id : " << f->uid << std::endl;
1330  std::cout << "Type: " << type << std::endl;
1331  if ( x && y )
1332  {
1333  for ( i = 0; i < nbPoints; i++ )
1334  std::cout << x[i] << ", " << y[i] << std::endl;
1335  std::cout << "Obstacle: " << nbHoles << std::endl;
1336  for ( i = 0; i < nbHoles; i++ )
1337  {
1338  std::cout << " obs " << i << std::endl;
1339  for ( j = 0; j < holes[i]->nbPoints; j++ )
1340  {
1341  std::cout << holes[i]->x[j] << ";" << holes[i]->y[j] << std::endl;
1342  }
1343  }
1344  }
1345 
1346  std::cout << std::endl;
1347  }
1348 
1349  int FeaturePart::setPosition( double scale, LabelPosition ***lPos,
1350  double bbox_min[2], double bbox_max[2],
1351  PointSet *mapShape, RTree<LabelPosition*, double, 2, double> *candidates
1352 #ifdef _EXPORT_MAP_
1353  , std::ofstream &svgmap
1354 #endif
1355  )
1356  {
1357  int nbp = 0;
1358  int i;
1359  double bbox[4];
1360 
1361 #ifdef _EXPORT_MAP_
1362  int dpi = layer->pal->getDpi();
1363 #endif
1364 
1365  bbox[0] = bbox_min[0];
1366  bbox[1] = bbox_min[1];
1367  bbox[2] = bbox_max[0];
1368  bbox[3] = bbox_max[1];
1369 
1370  double delta = bbox_max[0] - bbox_min[0];
1371  double angle = f->fixedRotation ? f->fixedAngle : 0.0;
1372 
1373  if ( f->fixedPosition() )
1374  {
1375  nbp = 1;
1376  *lPos = new LabelPosition *[nbp];
1377  ( *lPos )[0] = new LabelPosition( 0, f->fixedPosX, f->fixedPosY, f->label_x, f->label_y, angle, 0.0, this );
1378  }
1379  else
1380  {
1381  switch ( type )
1382  {
1383  case GEOS_POINT:
1384  if ( f->layer->getArrangement() == P_POINT_OVER )
1385  nbp = setPositionOverPoint( x[0], y[0], scale, lPos, delta, angle );
1386  else
1387  nbp = setPositionForPoint( x[0], y[0], scale, lPos, delta, angle );
1388  break;
1389  case GEOS_LINESTRING:
1390  if ( f->layer->getArrangement() == P_CURVED )
1391  nbp = setPositionForLineCurved( lPos, mapShape );
1392  else
1393  nbp = setPositionForLine( scale, lPos, mapShape, delta );
1394  break;
1395 
1396  case GEOS_POLYGON:
1397  switch ( f->layer->getArrangement() )
1398  {
1399  case P_POINT:
1400  case P_POINT_OVER:
1401  double cx, cy;
1402  mapShape->getCentroid( cx, cy, f->layer->getCentroidInside() );
1403  if ( f->layer->getArrangement() == P_POINT_OVER )
1404  nbp = setPositionOverPoint( cx, cy, scale, lPos, delta, angle );
1405  else
1406  nbp = setPositionForPoint( cx, cy, scale, lPos, delta, angle );
1407  break;
1408  case P_LINE:
1409  nbp = setPositionForLine( scale, lPos, mapShape, delta );
1410  break;
1411  default:
1412  nbp = setPositionForPolygon( scale, lPos, mapShape, delta );
1413  break;
1414  }
1415  }
1416  }
1417 
1418  int rnbp = nbp;
1419 
1420  // purge candidates that are outside the bbox
1421  for ( i = 0; i < nbp; i++ )
1422  {
1423  bool outside = false;
1424  if ( f->layer->pal->getShowPartial() )
1425  outside = !( *lPos )[i]->isIntersect( bbox );
1426  else
1427  outside = !( *lPos )[i]->isInside( bbox );
1428  if ( outside )
1429  {
1430  rnbp--;
1431  ( *lPos )[i]->setCost( DBL_MAX ); // infinite cost => do not use
1432  }
1433  else // this one is OK
1434  {
1435  ( *lPos )[i]->insertIntoIndex( candidates );
1436  }
1437  }
1438 
1439  sort(( void** )( *lPos ), nbp, LabelPosition::costGrow );
1440 
1441  for ( i = rnbp; i < nbp; i++ )
1442  {
1443  delete( *lPos )[i];
1444  }
1445 
1446  return rnbp;
1447  }
1448 
1449  void FeaturePart::addSizePenalty( int nbp, LabelPosition** lPos, double bbx[4], double bby[4] )
1450  {
1451  GEOSContextHandle_t ctxt = geosContext();
1452  int geomType = GEOSGeomTypeId_r( ctxt, the_geom );
1453 
1454  double sizeCost = 0;
1455  if ( geomType == GEOS_LINESTRING )
1456  {
1457  double length;
1458  if ( GEOSLength_r( ctxt, the_geom, &length ) != 1 )
1459  return; // failed to calculate length
1460  double bbox_length = max( bbx[2] - bbx[0], bby[2] - bby[0] );
1461  if ( length >= bbox_length / 4 )
1462  return; // the line is longer than quarter of height or width - don't penalize it
1463 
1464  sizeCost = 1 - ( length / ( bbox_length / 4 ) ); // < 0,1 >
1465  }
1466  else if ( geomType == GEOS_POLYGON )
1467  {
1468  double area;
1469  if ( GEOSArea_r( ctxt, the_geom, &area ) != 1 )
1470  return;
1471  double bbox_area = ( bbx[2] - bbx[0] ) * ( bby[2] - bby[0] );
1472  if ( area >= bbox_area / 16 )
1473  return; // covers more than 1/16 of our view - don't penalize it
1474 
1475  sizeCost = 1 - ( area / ( bbox_area / 16 ) ); // < 0, 1 >
1476  }
1477  else
1478  return; // no size penalty for points
1479 
1480  //std::cout << "size cost " << sizeCost << std::endl;
1481 
1482  // apply the penalty
1483  for ( int i = 0; i < nbp; i++ )
1484  {
1485  lPos[i]->setCost( lPos[i]->getCost() + sizeCost / 100 );
1486  }
1487  }
1488 
1490  {
1491  return ( GEOSTouches_r( geosContext(), the_geom, p2->the_geom ) == 1 );
1492  }
1493 
1495  {
1496  GEOSContextHandle_t ctxt = geosContext();
1497  GEOSGeometry* g1 = GEOSGeom_clone_r( ctxt, the_geom );
1498  GEOSGeometry* g2 = GEOSGeom_clone_r( ctxt, other->the_geom );
1499  GEOSGeometry* geoms[2] = { g1, g2 };
1500  GEOSGeometry* g = GEOSGeom_createCollection_r( ctxt, GEOS_MULTILINESTRING, geoms, 2 );
1501  GEOSGeometry* gTmp = GEOSLineMerge_r( ctxt, g );
1502  GEOSGeom_destroy_r( ctxt, g );
1503 
1504  if ( GEOSGeomTypeId_r( ctxt, gTmp ) != GEOS_LINESTRING )
1505  {
1506  // sometimes it's not possible to merge lines (e.g. they don't touch at endpoints)
1507  GEOSGeom_destroy_r( ctxt, gTmp );
1508  return false;
1509  }
1510 
1511  if ( ownsGeom ) // delete old geometry if we own it
1512  GEOSGeom_destroy_r( ctxt, the_geom );
1513  // set up new geometry
1514  the_geom = gTmp;
1515  ownsGeom = true;
1516 
1517  deleteCoords();
1519  return true;
1520  }
1521 
1522 } // end namespace pal
Arrangement arrangement
optional flags used for some placement methods
Definition: layer.h:112
void getCentroid(double &px, double &py, bool forceInside=false)
Definition: pointset.cpp:979
int reorderPolygon(int nbPoints, double *x, double *y)
double fixedAngle
Definition: feature.h:124
double length
Definition: pointset.h:76
static unsigned index
double fixedPosY
Definition: feature.h:115
bool fixedRotation
Definition: feature.h:123
double max_char_angle_outside
Definition: feature.h:73
static void splitPolygons(LinkedList< PointSet * > *shapes_toProcess, LinkedList< PointSet * > *shapes_final, double xrm, double yrm, char *uid)
Definition: pointset.cpp:216
void setCost(double newCost)
Modify candidate's cost.
A layer of spacial entites.
Definition: layer.h:65
double offsetPosX
Definition: feature.h:120
bool ptrPSetCompare(PointSet *a, PointSet *b)
Definition: util.h:240
void offsetPosition(double xOffset, double yOffset)
shift the label by specified offset
friend class LabelPosition
Definition: pointset.h:95
static LabelPosition * _createCurvedCandidate(LabelPosition *lp, double angle, double dist)
Definition: feature.cpp:965
double quadOffsetY
Definition: feature.h:118
static bool costGrow(void *l, void *r)
CHullBox * compute_chull_bbox()
Definition: pointset.cpp:618
void removeDuplicatePoints()
find duplicate (or nearly duplicate points) and remove them.
Definition: feature.cpp:220
Layer * getLayer()
return the layer that feature belongs to
Definition: feature.cpp:267
bool offsetPos
Definition: feature.h:119
int setPositionOverPoint(double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle)
generate one candidate over specified point
Definition: feature.cpp:327
double distlabel
Definition: feature.h:108
void addSizePenalty(int nbp, LabelPosition **lPos, double bbx[4], double bby[4])
Definition: feature.cpp:1449
bool getCentroidInside() const
Definition: layer.h:299
arranges candidates around a point (centroid for polygon)
Definition: pal.h:98
bool isPointInPolygon(int npol, double *xp, double *yp, double x, double y)
CharacterInfo * char_info
Definition: feature.h:76
int setPositionForLine(double scale, LabelPosition ***lPos, PointSet *mapShape, double delta_width)
generate candidates for line feature Generate candidates for line features
Definition: feature.cpp:564
double unit_convert(double x, Units from, Units to, int dpi, double scale, double delta_canvas_width)
Definition: util.h:120
void deleteCoords()
Definition: pointset.cpp:157
Layer * layer
Definition: feature.h:104
double width
Definition: pointset.h:75
Feature * f
Definition: feature.h:142
FeaturePart(Feature *feat, const GEOSGeometry *geom)
create a new generic feature
Definition: feature.cpp:98
Units label_unit
Definition: layer.h:106
PointSet ** holes
Definition: feature.h:145
virtual ~FeaturePart()
Delete the feature.
Definition: feature.cpp:115
bool mergeWithFeaturePart(FeaturePart *other)
merge other (connected) part with this one and save the result in this part (other is unchanged)...
Definition: feature.cpp:1494
double getAlpha() const
get alpha
PointSet * parent
Definition: pointset.h:111
double * x
Definition: pointset.h:102
double ymax
Definition: pointset.h:122
double xmin
Definition: pointset.h:119
PointSet * holeOf
Definition: pointset.h:110
double ymin
Definition: pointset.h:121
int setPosition(double scale, LabelPosition ***lPos, double bbox_min[2], double bbox_max[2], PointSet *mapShape, RTree< LabelPosition *, double, 2, double > *candidates)
generic method to generate candidates This method will call either setPositionFromPoint(), setPositionFromLine or setPositionFromPolygon
Definition: feature.cpp:1349
void print()
Print feature information Print feature unique id, geometry type, points, and holes on screen...
Definition: feature.cpp:1326
double quadOffsetX
Definition: feature.h:117
int setPositionForLineCurved(LabelPosition ***lPos, PointSet *mapShape)
Generate curved candidates for line features.
Definition: feature.cpp:972
double dist_euc2d(double x1, double y1, double x2, double y2)
Definition: geomfunction.h:56
bool isConnected(FeaturePart *p2)
check whether this part is connected with some other part
Definition: feature.cpp:1489
arranges candidates over a point (centroid for polygon)
Definition: pal.h:100
double label_height
Definition: feature.h:74
Only for lines, labels along the line.
Definition: pal.h:102
double offsetPosY
Definition: feature.h:121
LabelPosition * getNextPart() const
GEOSContextHandle_t geosContext()
Get GEOS context handle to be used in all GEOS library calls with reentrant API.
Definition: pal.cpp:87
bool quadOffset
Definition: feature.h:116
const char * getUID()
get the unique id of the feature
Definition: feature.cpp:273
Main class to handle feature.
Definition: feature.h:138
pixel [px]
Definition: pal.h:70
int setPositionForPoint(double x, double y, double scale, LabelPosition ***lPos, double delta_width, double angle)
generate candidates for point feature Generate candidates for point features
Definition: feature.cpp:386
void setPartId(int id)
Arrangement getArrangement()
get arrangement policy
Definition: layer.cpp:152
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 extractCoords(const GEOSGeometry *geom)
read coordinates from a GEOS geom
Definition: feature.cpp:138
int setPositionForPolygon(double scale, LabelPosition ***lPos, PointSet *mapShape, double delta_width)
generate candidates for point feature Generate candidates for point features
Definition: feature.cpp:1089
int min(int a, int b)
Definition: util.h:93
double * y
Definition: pointset.h:103
LabelPosition * curvedPlacementAtOffset(PointSet *path_positions, double *path_distances, int orientation, int index, double distance)
Definition: feature.cpp:768
bool getShowPartial()
Get flag show partial label.
Definition: pal.cpp:979
void sort(double *heap, int *x, int *y, int N)
Definition: util.cpp:72
GEOSGeometry * the_geom
Definition: feature.h:147
Pal * pal
Definition: layer.h:96
void setNextPart(LabelPosition *next)
LabelInfo * labelInfo
Definition: feature.h:109
double x[4]
Definition: pointset.h:70
bool ptrLPosCompare(LabelPosition *a, LabelPosition *b)
Definition: util.h:235
double label_y
Definition: feature.h:107
double y[4]
Definition: pointset.h:71
double max_char_angle_inside
Definition: feature.h:72
char * uid
Definition: feature.h:111
double label_x
Definition: feature.h:106
#define EPSILON
Definition: util.h:84
void getPoint(double *d, double *ad, double dl, double *px, double *py)
Definition: pointset.h:190
Only for polygon, arranges candidates with respect of polygon orientation.
Definition: pal.h:103
void findLineCircleIntersection(double cx, double cy, double radius, double x1, double y1, double x2, double y2, double &xRes, double &yRes)
LabelPosition is a candidate feature label position.
Definition: labelposition.h:53
Quadrant
Position of label candidate relative to feature.
Definition: labelposition.h:63
Feature(Layer *l, const char *id, PalGeometry *userG, double lx, double ly)
Definition: feature.cpp:64
Interface that allows Pal to access user's geometries.
Definition: palgeometry.h:42
double alpha
Definition: pointset.h:73
#define M_PI
Definition: feature.cpp:59
double xmax
Definition: pointset.h:120
int max(int a, int b)
Definition: util.h:87
double fixedPosX
Definition: feature.h:114
int char_num
Definition: feature.h:75
bool fixedPosition() const
Definition: feature.h:96
double vabs(double x)
Definition: util.h:99
unsigned long getArrangementFlags() const
Definition: layer.h:184