Skip to content

Commit

Permalink
Labeling: clip geometries to current extent if they are not completel…
Browse files Browse the repository at this point in the history
…y inside. Also fixes #4083
  • Loading branch information
wonder-sk committed Jul 14, 2011
1 parent 98562b3 commit 17e864b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/core/qgspallabeling.cpp
Expand Up @@ -131,7 +131,7 @@ class QgsPalGeometry : public PalGeometry
// -------------

QgsPalLayerSettings::QgsPalLayerSettings()
: palLayer( NULL ), fontMetrics( NULL ), ct( NULL )
: palLayer( NULL ), fontMetrics( NULL ), ct( NULL ), extentGeom( NULL )
{
placement = AroundPoint;
placementFlags = 0;
Expand Down Expand Up @@ -185,6 +185,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
dataDefinedProperties = s.dataDefinedProperties;
fontMetrics = NULL;
ct = NULL;
extentGeom = NULL;
}


Expand All @@ -194,6 +195,8 @@ QgsPalLayerSettings::~QgsPalLayerSettings()

delete fontMetrics;
delete ct;

delete extentGeom;
}

static QColor _readColor( QgsVectorLayer* layer, QString property )
Expand Down Expand Up @@ -466,15 +469,31 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
if ( ct ) // reproject the geometry if necessary
geom->transform( *ct );

GEOSGeometry* geos_geom = geom->asGeos();
if ( geos_geom == NULL )
return; // invalid geometry

if ( !checkMinimumSizeMM( context, geom, minFeatureSize ) )
{
return;
}

// CLIP the geometry if it is bigger than the extent
QgsGeometry* geomClipped = NULL;
GEOSGeometry* geos_geom;
bool do_clip = !extentGeom->contains( geom );
if ( do_clip )
{
geomClipped = geom->intersection( extentGeom ); // creates new geometry
geos_geom = geomClipped->asGeos();
}
else
{
geos_geom = geom->asGeos();
}

if ( geos_geom == NULL )
return; // invalid geometry
GEOSGeometry* geos_geom_clone = GEOSGeom_clone( geos_geom );
if ( do_clip )
delete geomClipped;

//data defined position / alignment / rotation?
bool dataDefinedPosition = false;
bool dataDefinedRotation = false;
Expand Down Expand Up @@ -569,7 +588,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
}
}

QgsPalGeometry* lbl = new QgsPalGeometry( f.id(), labelText, GEOSGeom_clone( geos_geom ) );
QgsPalGeometry* lbl = new QgsPalGeometry( f.id(), labelText, geos_geom_clone );

// record the created geometry - it will be deleted at the end.
geometries.append( lbl );
Expand Down Expand Up @@ -766,6 +785,9 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices,
lyr.ptZero = lyr.xform->toMapCoordinates( 0, 0 );
lyr.ptOne = lyr.xform->toMapCoordinates( 1, 0 );

// rect for clipping
lyr.extentGeom = QgsGeometry::fromRect( mMapRenderer->extent() );

return 1; // init successful
}

Expand Down
1 change: 1 addition & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -143,6 +143,7 @@ class CORE_EXPORT QgsPalLayerSettings
const QgsCoordinateTransform* ct;
QgsPoint ptZero, ptOne;
QList<QgsPalGeometry*> geometries;
QgsGeometry* extentGeom;

/**Stores field indices for data defined layer properties*/
QMap< DataDefinedProperties, int > dataDefinedProperties;
Expand Down

0 comments on commit 17e864b

Please sign in to comment.