Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix features right on the edges of geographic layers can disappear when
reprojected

Eg +/- 180 degree grid lines

Fixes #27571
  • Loading branch information
nyalldawson committed Dec 18, 2019
1 parent b566eac commit 971552a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/core/qgsmaprendererjob.cpp
Expand Up @@ -154,8 +154,12 @@ bool QgsMapRendererJob::reprojectToLayerExtent( const QgsMapLayer *ml, const Qgs
.arg( std::fabs( 1.0 - extent2.height() / extent.height() ) )
, 3 );

if ( std::fabs( 1.0 - extent2.width() / extent.width() ) < 0.5 &&
std::fabs( 1.0 - extent2.height() / extent.height() ) < 0.5 )
// can differ by a maximum of up to 20% of height/width
if ( qgsDoubleNear( extent2.xMinimum(), extent.xMinimum(), extent.width() * 0.2 )
&& qgsDoubleNear( extent2.xMaximum(), extent.xMaximum(), extent.width() * 0.2 )
&& qgsDoubleNear( extent2.yMinimum(), extent.yMinimum(), extent.height() * 0.2 )
&& qgsDoubleNear( extent2.yMaximum(), extent.yMaximum(), extent.height() * 0.2 )
)
{
extent = extent1;
}
Expand Down
31 changes: 31 additions & 0 deletions tests/src/core/testqgsmaprendererjob.cpp
Expand Up @@ -42,6 +42,7 @@
#include "qgsvectorlayerlabeling.h"
#include "qgsfontutils.h"
#include "qgsrasterlayer.h"
#include "qgssinglesymbolrenderer.h"

//qgs unit test utility class
#include "qgsrenderchecker.h"
Expand Down Expand Up @@ -84,6 +85,8 @@ class TestQgsMapRendererJob : public QObject
void stagedRenderer();
void stagedRendererWithStagedLabeling();

void vectorLayerBoundsWithReprojection();

private:
bool imageCheck( const QString &type, const QImage &image, int mismatchCount = 0 );

Expand Down Expand Up @@ -857,6 +860,34 @@ void TestQgsMapRendererJob::stagedRendererWithStagedLabeling()
QVERIFY( !job->renderCurrentPart( &painter ) );
}

void TestQgsMapRendererJob::vectorLayerBoundsWithReprojection()
{
std::unique_ptr< QgsVectorLayer > gridLayer = qgis::make_unique< QgsVectorLayer >( TEST_DATA_DIR + QStringLiteral( "/grid_4326.geojson" ),
QStringLiteral( "grid" ), QStringLiteral( "ogr" ) );
QVERIFY( gridLayer->isValid() );

std::unique_ptr< QgsLineSymbol > symbol = qgis::make_unique< QgsLineSymbol >();
symbol->setColor( QColor( 255, 0, 255 ) );
symbol->setWidth( 2 );
std::unique_ptr< QgsSingleSymbolRenderer > renderer = qgis::make_unique< QgsSingleSymbolRenderer >( symbol.release() );
gridLayer->setRenderer( renderer.release() );

QgsMapSettings mapSettings;

mapSettings.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ) );
mapSettings.setExtent( QgsRectangle( -37000835.1, -20182273.7, 37000835.1, 20182273.7 ) );
mapSettings.setOutputSize( QSize( 512, 512 ) );
mapSettings.setFlag( QgsMapSettings::DrawLabeling, false );
mapSettings.setOutputDpi( 96 );
mapSettings.setLayers( QList< QgsMapLayer * >() << gridLayer.get() );

QgsMapRendererSequentialJob renderJob( mapSettings );
renderJob.start();
renderJob.waitForFinished();
QImage img = renderJob.renderedImage();
QVERIFY( imageCheck( QStringLiteral( "vector_layer_bounds_with_reprojection" ), img ) );
}

bool TestQgsMapRendererJob::imageCheck( const QString &testName, const QImage &image, int mismatchCount )
{
mReport += "<h2>" + testName + "</h2>\n";
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions tests/testdata/grid_4326.geojson

Large diffs are not rendered by default.

0 comments on commit 971552a

Please sign in to comment.