Skip to content

Commit

Permalink
Fix inverted polygon is distorted when map is rotated
Browse files Browse the repository at this point in the history
Fixes #26381
  • Loading branch information
nyalldawson committed Jun 4, 2019
1 parent 3cb3e9b commit b2d3bfc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
19 changes: 9 additions & 10 deletions src/core/symbology/qgsinvertedpolygonrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,23 @@ void QgsInvertedPolygonRenderer::startRender( QgsRenderContext &context, const Q
// We compute coordinates of the extent which will serve as exterior ring
// for the final polygon
// It must be computed in the destination CRS if reprojection is enabled.
const QgsMapToPixel &mtp( context.mapToPixel() );

if ( !context.painter() )
{
return;
}

// convert viewport to dest CRS
QRect e( context.painter()->viewport() );
// add some space to hide borders and tend to infinity
e.adjust( -e.width() * 5, -e.height() * 5, e.width() * 5, e.height() * 5 );
const double buffer = std::max( context.mapExtent().width(), context.mapExtent().height() ) * 0.1;
const QRectF outer = context.mapExtent().buffered( buffer ).toRectF();
QgsPolylineXY exteriorRing;
exteriorRing << mtp.toMapCoordinates( e.topLeft() );
exteriorRing << mtp.toMapCoordinates( e.topRight() );
exteriorRing << mtp.toMapCoordinates( e.bottomRight() );
exteriorRing << mtp.toMapCoordinates( e.bottomLeft() );
exteriorRing << mtp.toMapCoordinates( e.topLeft() );
exteriorRing.reserve( 5 );
exteriorRing << outer.topLeft();
exteriorRing << outer.topRight();
exteriorRing << outer.bottomRight();
exteriorRing << outer.bottomLeft();
exteriorRing << outer.topLeft();

// copy the rendering context
mContext = context;
Expand All @@ -142,8 +142,7 @@ void QgsInvertedPolygonRenderer::startRender( QgsRenderContext &context, const Q
// disable projection
mContext.setCoordinateTransform( QgsCoordinateTransform() );
// recompute extent so that polygon clipping is correct
QRect v( context.painter()->viewport() );
mContext.setExtent( QgsRectangle( mtp.toMapCoordinates( v.topLeft() ), mtp.toMapCoordinates( v.bottomRight() ) ) );
mContext.setExtent( context.mapExtent() );
// do we have to recompute the MapToPixel ?
}

Expand Down
12 changes: 11 additions & 1 deletion tests/src/core/testqgsinvertedpolygonrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class TestQgsInvertedPolygon : public QObject
void preprocess();
void projectionTest();
void curvedPolygons();
void rotationTest();

private:
bool mTestHasError = false ;
Expand Down Expand Up @@ -154,7 +155,6 @@ void TestQgsInvertedPolygon::projectionTest()
mMapSettings.setDestinationCrs( mpPolysLayer->crs() );
}

// This test relies on GDAL support of curved polygons
void TestQgsInvertedPolygon::curvedPolygons()
{
QString myCurvedPolysFileName = mTestDataDir + "curved_polys.gpkg";
Expand All @@ -170,6 +170,16 @@ void TestQgsInvertedPolygon::curvedPolygons()
mMapSettings.setLayers( QList< QgsMapLayer * >() << mpPolysLayer );
}

void TestQgsInvertedPolygon::rotationTest()
{
mReport += QLatin1String( "<h2>Inverted polygon renderer, rotation test</h2>\n" );
mMapSettings.setRotation( 45 );
QVERIFY( setQml( mpPolysLayer, "inverted_polys_single.qml" ) );
QVERIFY( imageCheck( "inverted_polys_rotation" ) );
mMapSettings.setRotation( 0 );
}


//
// Private helper functions not called directly by CTest
//
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b2d3bfc

Please sign in to comment.