Skip to content
Permalink
Browse files
#9480: Force label position inside of polygons in PAL labeling library
  • Loading branch information
ahuarte47 committed May 27, 2014
1 parent 72b3d3f commit 1e54a460a163766dcece933ac441fe2dc24363f3
Showing with 40 additions and 4 deletions.
  1. +1 −1 src/core/pal/feature.cpp
  2. +1 −1 src/core/pal/layer.cpp
  3. +4 −0 src/core/pal/layer.h
  4. +30 −1 src/core/pal/pointset.cpp
  5. +1 −1 src/core/pal/pointset.h
  6. +3 −0 src/core/qgspallabeling.cpp
@@ -1344,7 +1344,7 @@ namespace pal
case P_POINT:
case P_POINT_OVER:
double cx, cy;
mapShape->getCentroid( cx, cy );
mapShape->getCentroid( cx, cy, f->layer->getCentroidInside() );
if ( f->layer->getArrangement() == P_POINT_OVER )
nbp = setPositionOverPoint( cx, cy, scale, lPos, delta, angle );
else
@@ -61,7 +61,7 @@ namespace pal

Layer::Layer( const char *lyrName, double min_scale, double max_scale, Arrangement arrangement, Units label_unit, double defaultPriority, bool obstacle, bool active, bool toLabel, Pal *pal, bool displayAll )
: pal( pal ), obstacle( obstacle ), active( active ),
toLabel( toLabel ), displayAll( displayAll ), label_unit( label_unit ),
toLabel( toLabel ), displayAll( displayAll ), centroidInside( false ), label_unit( label_unit ),
min_scale( min_scale ), max_scale( max_scale ),
arrangement( arrangement ), arrangementFlags( 0 ), mode( LabelPerFeature ), mergeLines( false )
{
@@ -101,6 +101,7 @@ namespace pal
bool active;
bool toLabel;
bool displayAll;
bool centroidInside;

Units label_unit;

@@ -291,6 +292,9 @@ namespace pal
void setUpsidedownLabels( UpsideDownLabels ud ) { upsidedownLabels = ud; }
UpsideDownLabels getUpsidedownLabels() const { return upsidedownLabels; }

void setCentroidInside( bool forceInside ) { centroidInside = forceInside; }
bool getCentroidInside() const { return centroidInside; }

/**
* \brief register a feature in the layer
*
@@ -956,7 +956,7 @@ namespace pal



void PointSet::getCentroid( double &px, double &py )
void PointSet::getCentroid( double &px, double &py, bool forceInside )
{
// for explanation see this page:
// http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
@@ -984,6 +984,35 @@ namespace pal
px = cx / ( 3 * A ) + x[0];
py = cy / ( 3 * A ) + y[0];
}

// check if centroid inside in polygon
if ( forceInside && !isPointInPolygon( nbPoints, x, y, px, py ) )
{
GEOSCoordSequence *coord = GEOSCoordSeq_create( nbPoints, 2 );

for ( int i = 0; i < nbPoints; ++i )
{
GEOSCoordSeq_setX( coord, i, x[i] );
GEOSCoordSeq_setY( coord, i, y[i] );
}

GEOSGeometry *geom = GEOSGeom_createPolygon( GEOSGeom_createLinearRing( coord ), 0, 0 );

if ( geom )
{
GEOSGeometry *pointGeom = GEOSPointOnSurface( geom );

if ( pointGeom )
{
const GEOSCoordSequence *coordSeq = GEOSGeom_getCoordSeq( pointGeom );
GEOSCoordSeq_getX( coordSeq, 0, &px );
GEOSCoordSeq_getY( coordSeq, 0, &py );

GEOSGeom_destroy( pointGeom );
}
GEOSGeom_destroy( geom );
}
}
}

} // end namespace
@@ -160,7 +160,7 @@ namespace pal

//double getDistInside(double px, double py);

void getCentroid( double &px, double &py );
void getCentroid( double &px, double &py, bool forceInside = false );


int getGeosType() const { return type; }
@@ -3389,6 +3389,9 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QStringList& attrNames,
// set whether adjacent lines should be merged
l->setMergeConnectedLines( lyr.mergeLines );

// force the location of the centroid inside of polygons
l->setCentroidInside( true );

// set how to show upside-down labels
Layer::UpsideDownLabels upsdnlabels;
switch ( lyr.upsidedownLabels )

0 comments on commit 1e54a46

Please sign in to comment.