Skip to content

Commit 2096813

Browse files
committed
Merge pull request #229 from homann/issue5895
Issue 5895 fix and testcase for projection errors. Hopefully, we can add on other projection errors to this test case. If there are any more...
2 parents 3cb4669 + c1f37e8 commit 2096813

File tree

5 files changed

+161
-6
lines changed

5 files changed

+161
-6
lines changed

src/core/qgsrasterprojector.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,10 +594,21 @@ bool QgsRasterProjector::checkCols()
594594
QgsPoint mySrcPoint3 = mCPMatrix[r+1][c];
595595

596596
QgsPoint mySrcApprox(( mySrcPoint1.x() + mySrcPoint3.x() ) / 2, ( mySrcPoint1.y() + mySrcPoint3.y() ) / 2 );
597-
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
598-
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
599-
if ( mySqrDist > mSqrTolerance )
597+
try
598+
{
599+
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
600+
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
601+
if ( mySqrDist > mSqrTolerance )
602+
{
603+
return false;
604+
}
605+
}
606+
catch ( QgsCsException &e )
607+
{
608+
Q_UNUSED( e );
609+
// Caught an error in transform
600610
return false;
611+
}
601612
}
602613
}
603614
return true;
@@ -618,10 +629,21 @@ bool QgsRasterProjector::checkRows()
618629
QgsPoint mySrcPoint3 = mCPMatrix[r][c+1];
619630

620631
QgsPoint mySrcApprox(( mySrcPoint1.x() + mySrcPoint3.x() ) / 2, ( mySrcPoint1.y() + mySrcPoint3.y() ) / 2 );
621-
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
622-
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
623-
if ( mySqrDist > mSqrTolerance )
632+
try
633+
{
634+
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
635+
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
636+
if ( mySqrDist > mSqrTolerance )
637+
{
638+
return false;
639+
}
640+
}
641+
catch ( QgsCsException &e )
642+
{
643+
Q_UNUSED( e );
644+
// Caught an error in transform
624645
return false;
646+
}
625647
}
626648
}
627649
return true;

src/core/raster/qgsrasteriterator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ bool QgsRasterIterator::readNextRasterPart( int bandNumber,
6363

6464
RasterPartInfo& pInfo = partIt.value();
6565

66+
// If we started with zero cols or zero rows, just return (avoids divide by zero below)
67+
if ( 0 == pInfo.nCols || 0 == pInfo.nRows )
68+
{
69+
return false;
70+
}
71+
6672
//remove last data block
6773
// TODO: data are released somewhere else (check)
6874
//free( pInfo.data );

tests/src/gui/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,5 @@ ADD_QGIS_TEST(zoomtest testqgsmaptoolzoom.cpp)
118118
#ADD_EXECUTABLE(qgis_rendererv2gui ${rendererv2gui_SRCS} ${rendererv2gui_MOC_SRCS})
119119
120120
ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
121+
ADD_QGIS_TEST(projectionissues testprojectionissues.cpp)
121122
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/***************************************************************************
2+
testprojectionissues.cpp
3+
---------------------------
4+
begin : September 2012
5+
copyright : (C) 2012 by Magnus Homann
6+
email : magnus at homann dot se
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include "qgsapplication.h"
19+
#include "qgsmapcanvas.h"
20+
#include "qgsmaplayerregistry.h"
21+
#include "qgsmaprenderer.h"
22+
#include "qgsmultibandcolorrenderer.h"
23+
#include "qgsrasterlayer.h"
24+
#include <QObject>
25+
#include <QtTest>
26+
27+
class TestProjectionIssues: public QObject
28+
{
29+
Q_OBJECT;
30+
private slots:
31+
void initTestCase();// will be called before the first testfunction is executed.
32+
void cleanupTestCase();// will be called after the last testfunction was executed.
33+
void init();// will be called before each testfunction is executed.
34+
void cleanup();// will be called after every testfunction.
35+
void issue5895();// test for #5895
36+
private:
37+
QgsRasterLayer* mRasterLayer;
38+
QgsMapCanvas* mMapCanvas;
39+
};
40+
41+
void TestProjectionIssues::initTestCase()
42+
{
43+
QgsApplication::init();
44+
QgsApplication::initQgis();
45+
46+
//create maplayer from testdata and add to layer registry
47+
QFileInfo rasterFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "checker360by180.asc" );
48+
mRasterLayer = new QgsRasterLayer( rasterFileInfo.filePath(),
49+
rasterFileInfo.completeBaseName() );
50+
// Set to WGS84
51+
QgsCoordinateReferenceSystem sourceCRS;
52+
sourceCRS.createFromId( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );
53+
mRasterLayer->setCrs( sourceCRS, false);
54+
55+
QgsMultiBandColorRenderer* rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer->dataProvider(), 2, 3, 4 );
56+
mRasterLayer->setRenderer( rasterRenderer );
57+
58+
QList<QgsMapLayer *> mapLayers;
59+
mapLayers.append( mRasterLayer );
60+
QgsMapLayerRegistry::instance()->addMapLayers( mapLayers );
61+
62+
// Add all layers in registry to the canvas
63+
QList<QgsMapCanvasLayer> canvasLayers;
64+
foreach ( QgsMapLayer* layer, QgsMapLayerRegistry::instance()->mapLayers().values() )
65+
{
66+
canvasLayers.append( QgsMapCanvasLayer( layer ) );
67+
}
68+
69+
// create canvas
70+
mMapCanvas = new QgsMapCanvas();
71+
mMapCanvas->setLayerSet( canvasLayers );
72+
73+
//reproject to SWEDREF 99 TM
74+
QgsCoordinateReferenceSystem destCRS;
75+
destCRS.createFromId( 3006, QgsCoordinateReferenceSystem::EpsgCrsId );
76+
mMapCanvas->mapRenderer()->setDestinationCrs( destCRS );
77+
mMapCanvas->mapRenderer()->setProjectionsEnabled( true );
78+
79+
};
80+
81+
void TestProjectionIssues::cleanupTestCase()
82+
{
83+
delete mMapCanvas;
84+
delete mRasterLayer;
85+
};
86+
87+
void TestProjectionIssues::init()
88+
{
89+
90+
};
91+
92+
void TestProjectionIssues::cleanup()
93+
{
94+
95+
};
96+
97+
void TestProjectionIssues::issue5895()
98+
{
99+
QgsRectangle largeExtent( -610861, 5101721, 2523921, 6795055 );
100+
mMapCanvas->setExtent( largeExtent );
101+
mMapCanvas->zoomByFactor( 2.0 ); // Zoom out. This should exceed the transform limits.
102+
};
103+
104+
QTEST_MAIN( TestProjectionIssues )
105+
#include "moc_testprojectionissues.cxx"

tests/testdata/checker360by180.asc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
NCOLS 24
2+
NROWS 12
3+
XLLCENTER -172.5
4+
YLLCENTER -82.5
5+
DX 15
6+
DY 15
7+
NODATA_VALUE -9999
8+
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
9+
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
10+
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
11+
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
12+
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
13+
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
14+
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
15+
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
16+
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
17+
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
18+
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
19+
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
20+
CRS
21+
NOTES

0 commit comments

Comments
 (0)