Skip to content

Commit

Permalink
Merge pull request #229 from homann/issue5895
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
homann committed Sep 12, 2012
2 parents 3cb4669 + c1f37e8 commit 2096813
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/core/qgsrasterprojector.cpp
Expand Up @@ -594,10 +594,21 @@ bool QgsRasterProjector::checkCols()
QgsPoint mySrcPoint3 = mCPMatrix[r+1][c];

QgsPoint mySrcApprox(( mySrcPoint1.x() + mySrcPoint3.x() ) / 2, ( mySrcPoint1.y() + mySrcPoint3.y() ) / 2 );
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
if ( mySqrDist > mSqrTolerance )
try
{
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
if ( mySqrDist > mSqrTolerance )
{
return false;
}
}
catch ( QgsCsException &e )
{
Q_UNUSED( e );
// Caught an error in transform
return false;
}
}
}
return true;
Expand All @@ -618,10 +629,21 @@ bool QgsRasterProjector::checkRows()
QgsPoint mySrcPoint3 = mCPMatrix[r][c+1];

QgsPoint mySrcApprox(( mySrcPoint1.x() + mySrcPoint3.x() ) / 2, ( mySrcPoint1.y() + mySrcPoint3.y() ) / 2 );
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
if ( mySqrDist > mSqrTolerance )
try
{
QgsPoint myDestApprox = mCoordinateTransform.transform( mySrcApprox, QgsCoordinateTransform::ReverseTransform );
double mySqrDist = myDestApprox.sqrDist( myDestPoint );
if ( mySqrDist > mSqrTolerance )
{
return false;
}
}
catch ( QgsCsException &e )
{
Q_UNUSED( e );
// Caught an error in transform
return false;
}
}
}
return true;
Expand Down
6 changes: 6 additions & 0 deletions src/core/raster/qgsrasteriterator.cpp
Expand Up @@ -63,6 +63,12 @@ bool QgsRasterIterator::readNextRasterPart( int bandNumber,

RasterPartInfo& pInfo = partIt.value();

// If we started with zero cols or zero rows, just return (avoids divide by zero below)
if ( 0 == pInfo.nCols || 0 == pInfo.nRows )
{
return false;
}

//remove last data block
// TODO: data are released somewhere else (check)
//free( pInfo.data );
Expand Down
1 change: 1 addition & 0 deletions tests/src/gui/CMakeLists.txt
Expand Up @@ -118,4 +118,5 @@ ADD_QGIS_TEST(zoomtest testqgsmaptoolzoom.cpp)
#ADD_EXECUTABLE(qgis_rendererv2gui ${rendererv2gui_SRCS} ${rendererv2gui_MOC_SRCS})

ADD_QGIS_TEST(histogramtest testqgsrasterhistogram.cpp)
ADD_QGIS_TEST(projectionissues testprojectionissues.cpp)

105 changes: 105 additions & 0 deletions tests/src/gui/testprojectionissues.cpp
@@ -0,0 +1,105 @@
/***************************************************************************
testprojectionissues.cpp
---------------------------
begin : September 2012
copyright : (C) 2012 by Magnus Homann
email : magnus at homann dot se
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsapplication.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerregistry.h"
#include "qgsmaprenderer.h"
#include "qgsmultibandcolorrenderer.h"
#include "qgsrasterlayer.h"
#include <QObject>
#include <QtTest>

class TestProjectionIssues: public QObject
{
Q_OBJECT;
private slots:
void initTestCase();// will be called before the first testfunction is executed.
void cleanupTestCase();// will be called after the last testfunction was executed.
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void issue5895();// test for #5895
private:
QgsRasterLayer* mRasterLayer;
QgsMapCanvas* mMapCanvas;
};

void TestProjectionIssues::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();

//create maplayer from testdata and add to layer registry
QFileInfo rasterFileInfo( QString( TEST_DATA_DIR ) + QDir::separator() + "checker360by180.asc" );
mRasterLayer = new QgsRasterLayer( rasterFileInfo.filePath(),
rasterFileInfo.completeBaseName() );
// Set to WGS84
QgsCoordinateReferenceSystem sourceCRS;
sourceCRS.createFromId( 4326, QgsCoordinateReferenceSystem::EpsgCrsId );
mRasterLayer->setCrs( sourceCRS, false);

QgsMultiBandColorRenderer* rasterRenderer = new QgsMultiBandColorRenderer( mRasterLayer->dataProvider(), 2, 3, 4 );
mRasterLayer->setRenderer( rasterRenderer );

QList<QgsMapLayer *> mapLayers;
mapLayers.append( mRasterLayer );
QgsMapLayerRegistry::instance()->addMapLayers( mapLayers );

// Add all layers in registry to the canvas
QList<QgsMapCanvasLayer> canvasLayers;
foreach ( QgsMapLayer* layer, QgsMapLayerRegistry::instance()->mapLayers().values() )
{
canvasLayers.append( QgsMapCanvasLayer( layer ) );
}

// create canvas
mMapCanvas = new QgsMapCanvas();
mMapCanvas->setLayerSet( canvasLayers );

//reproject to SWEDREF 99 TM
QgsCoordinateReferenceSystem destCRS;
destCRS.createFromId( 3006, QgsCoordinateReferenceSystem::EpsgCrsId );
mMapCanvas->mapRenderer()->setDestinationCrs( destCRS );
mMapCanvas->mapRenderer()->setProjectionsEnabled( true );

};

void TestProjectionIssues::cleanupTestCase()
{
delete mMapCanvas;
delete mRasterLayer;
};

void TestProjectionIssues::init()
{

};

void TestProjectionIssues::cleanup()
{

};

void TestProjectionIssues::issue5895()
{
QgsRectangle largeExtent( -610861, 5101721, 2523921, 6795055 );
mMapCanvas->setExtent( largeExtent );
mMapCanvas->zoomByFactor( 2.0 ); // Zoom out. This should exceed the transform limits.
};

QTEST_MAIN( TestProjectionIssues )
#include "moc_testprojectionissues.cxx"
21 changes: 21 additions & 0 deletions tests/testdata/checker360by180.asc
@@ -0,0 +1,21 @@
NCOLS 24
NROWS 12
XLLCENTER -172.5
YLLCENTER -82.5
DX 15
DY 15
NODATA_VALUE -9999
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
CRS
NOTES

0 comments on commit 2096813

Please sign in to comment.