Skip to content

Commit

Permalink
#9480: Force label position inside of polygons in PAL labeling library
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuarte47 committed May 27, 2014
1 parent 72b3d3f commit 1e54a46
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/core/pal/feature.cpp
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/layer.cpp
Expand Up @@ -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 )
{
Expand Down
4 changes: 4 additions & 0 deletions src/core/pal/layer.h
Expand Up @@ -101,6 +101,7 @@ namespace pal
bool active;
bool toLabel;
bool displayAll;
bool centroidInside;

Units label_unit;

Expand Down Expand Up @@ -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
*
Expand Down
31 changes: 30 additions & 1 deletion src/core/pal/pointset.cpp
Expand Up @@ -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/
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/pointset.h
Expand Up @@ -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; }
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgspallabeling.cpp
Expand Up @@ -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 )
Expand Down

0 comments on commit 1e54a46

Please sign in to comment.