Skip to content

Commit

Permalink
Improve polygon perimeter labeling
Browse files Browse the repository at this point in the history
This commit changes the way polygon perimeter labeling works.
Previously the polygon would be clipped to the view extent, and
then the boundary of the clipped polygon used for perimeter
labeling.

This tended to push the labels to the outside of the map extent
(the clip boundary), since that's the longest straight section
of the resultant geometry.

Now, the boundary is taken *before* the clip, so that perimeter
based labels will never be placed on the artificial boundary
created at the map's extent.

(cherry-picked from 27697e6)
  • Loading branch information
nyalldawson committed Aug 9, 2016
1 parent 5ab62a9 commit 4e41f1a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/core/qgspallabeling.cpp
Expand Up @@ -2442,6 +2442,16 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
}
}

// if using perimeter based labeling for polygons, get the polygon's
// linear boundary and use that for the label geometry
if (( geom->type() == QGis::Polygon )
&& ( placement == Line || placement == PerimeterCurved ) )
{
QgsGeometry* boundaryGeom = new QgsGeometry( geom->geometry()->boundary() );
geom = boundaryGeom;
scopedClonedGeom.reset( boundaryGeom );
}

// whether we're going to create a centroid for polygon
bool centroidPoly = (( placement == QgsPalLayerSettings::AroundPoint
|| placement == QgsPalLayerSettings::OverPoint )
Expand All @@ -2461,6 +2471,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
if ( QgsPalLabeling::geometryRequiresPreparation( geom, context, ct, doClip ? extentGeom : nullptr ) )
{
scopedPreparedGeom.reset( QgsPalLabeling::prepareGeometry( geom, context, ct, doClip ? extentGeom : nullptr ) );

if ( !scopedPreparedGeom.data() )
return;
preparedGeom = scopedPreparedGeom.data();
Expand Down Expand Up @@ -2516,16 +2527,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
}
}

GEOSGeometry* geos_geom_clone;
GEOSGeomTypes geomType = ( GEOSGeomTypes ) GEOSGeomTypeId_r( QgsGeometry::getGEOSHandler(), geos_geom );
if (( geomType == GEOS_POLYGON || geomType == GEOS_MULTIPOLYGON ) && repeatDistance > 0 && ( placement == Line || placement == PerimeterCurved ) )
{
geos_geom_clone = GEOSBoundary_r( QgsGeometry::getGEOSHandler(), geos_geom );
}
else
{
geos_geom_clone = GEOSGeom_clone_r( QgsGeometry::getGEOSHandler(), geos_geom );
}
GEOSGeometry* geos_geom_clone = GEOSGeom_clone_r( QgsGeometry::getGEOSHandler(), geos_geom );
GEOSGeometry* geosObstacleGeomClone = nullptr;
if ( geosObstacleGeom )
{
Expand Down

0 comments on commit 4e41f1a

Please sign in to comment.