Skip to content

Commit 76eb29d

Browse files
committedJun 14, 2018
Unit tests and dox for QgsRasterIterator
1 parent bde8b43 commit 76eb29d

File tree

5 files changed

+318
-5
lines changed

5 files changed

+318
-5
lines changed
 

‎python/core/auto_generated/raster/qgsrasteriterator.sip.in

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Iterator for sequentially processing raster cells.
2020
public:
2121

2222
QgsRasterIterator( QgsRasterInterface *input );
23+
%Docstring
24+
Constructor for QgsRasterIterator, iterating over the specified ``input`` raster source.
25+
%End
2326

2427
void startRasterRead( int bandNumber, int nCols, int nRows, const QgsRectangle &extent, QgsRasterBlockFeedback *feedback = 0 );
2528
%Docstring
@@ -52,16 +55,53 @@ caller should delete the block.
5255

5356

5457
void stopRasterRead( int bandNumber );
58+
%Docstring
59+
Cancels the raster iteration and resets the iterator.
60+
%End
5561

5662
const QgsRasterInterface *input() const;
63+
%Docstring
64+
Returns the input raster interface which is being iterated over.
65+
%End
5766

5867
void setMaximumTileWidth( int w );
68+
%Docstring
69+
Sets the maximum tile width returned during iteration.
70+
71+
.. seealso:: :py:func:`maximumTileWidth`
72+
73+
.. seealso:: :py:func:`setMinimumTileWidth`
74+
%End
75+
5976
int maximumTileWidth() const;
77+
%Docstring
78+
Returns the maximum tile width returned during iteration.
79+
80+
.. seealso:: :py:func:`setMaximumTileWidth`
81+
82+
.. seealso:: :py:func:`minimumTileWidth`
83+
%End
6084

6185
void setMaximumTileHeight( int h );
86+
%Docstring
87+
Sets the minimum tile width returned during iteration.
88+
89+
.. seealso:: :py:func:`minimumTileWidth`
90+
91+
.. seealso:: :py:func:`setMaximumTileWidth`
92+
%End
93+
6294
int maximumTileHeight() const;
95+
%Docstring
96+
Returns the minimum tile width returned during iteration.
97+
98+
.. seealso:: :py:func:`setMinimumTileWidth`
99+
100+
.. seealso:: :py:func:`maximumTileWidth`
101+
%End
63102

64103
static const int DEFAULT_MAXIMUM_TILE_WIDTH;
104+
65105
static const int DEFAULT_MAXIMUM_TILE_HEIGHT;
66106

67107
};

‎src/core/raster/qgsrasteriterator.h

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ class CORE_EXPORT QgsRasterIterator
3434
{
3535
public:
3636

37+
/**
38+
* Constructor for QgsRasterIterator, iterating over the specified \a input raster source.
39+
*/
3740
QgsRasterIterator( QgsRasterInterface *input );
3841

3942
/**
4043
* Start reading of raster band. Raster data can then be retrieved by calling readNextRasterPart until it returns false.
41-
\param bandNumber number of raster band to read
42-
\param nCols number of columns
43-
\param nRows number of rows
44-
\param extent area to read
45-
\param feedback optional raster feedback object for cancelation/preview. Added in QGIS 3.0.
44+
* \param bandNumber number of raster band to read
45+
* \param nCols number of columns
46+
* \param nRows number of rows
47+
* \param extent area to read
48+
* \param feedback optional raster feedback object for cancelation/preview. Added in QGIS 3.0.
4649
*/
4750
void startRasterRead( int bandNumber, int nCols, int nRows, const QgsRectangle &extent, QgsRasterBlockFeedback *feedback = nullptr );
4851

@@ -81,17 +84,48 @@ class CORE_EXPORT QgsRasterIterator
8184
int &topLeftCol, int &topLeftRow,
8285
QgsRectangle *blockExtent = nullptr ) SIP_SKIP;
8386

87+
/**
88+
* Cancels the raster iteration and resets the iterator.
89+
*/
8490
void stopRasterRead( int bandNumber );
8591

92+
/**
93+
* Returns the input raster interface which is being iterated over.
94+
*/
8695
const QgsRasterInterface *input() const { return mInput; }
8796

97+
/**
98+
* Sets the maximum tile width returned during iteration.
99+
* \see maximumTileWidth()
100+
* \see setMinimumTileWidth()
101+
*/
88102
void setMaximumTileWidth( int w ) { mMaximumTileWidth = w; }
103+
104+
/**
105+
* Returns the maximum tile width returned during iteration.
106+
* \see setMaximumTileWidth()
107+
* \see minimumTileWidth()
108+
*/
89109
int maximumTileWidth() const { return mMaximumTileWidth; }
90110

111+
/**
112+
* Sets the minimum tile width returned during iteration.
113+
* \see minimumTileWidth()
114+
* \see setMaximumTileWidth()
115+
*/
91116
void setMaximumTileHeight( int h ) { mMaximumTileHeight = h; }
117+
118+
/**
119+
* Returns the minimum tile width returned during iteration.
120+
* \see setMinimumTileWidth()
121+
* \see maximumTileWidth()
122+
*/
92123
int maximumTileHeight() const { return mMaximumTileHeight; }
93124

125+
//! Default maximum tile width
94126
static const int DEFAULT_MAXIMUM_TILE_WIDTH = 2000;
127+
128+
//! Default maximum tile height
95129
static const int DEFAULT_MAXIMUM_TILE_HEIGHT = 2000;
96130

97131
private:

‎tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ SET(TESTS
163163
testqgis.cpp
164164
testqgsrasterfilewriter.cpp
165165
testqgsrasterfill.cpp
166+
testqgsrasteriterator.cpp
166167
testqgsrasterblock.cpp
167168
testqgsrasterlayer.cpp
168169
testqgsrastersublayer.cpp
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
/***************************************************************************
2+
testqgsrasteriterator.cpp
3+
--------------------------------------
4+
Date : June 2018
5+
Copyright : (C) 2018 by Nyall Dawson
6+
Email : nyall dot dawson at gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgstest.h"
17+
#include <QObject>
18+
#include <QString>
19+
#include <QTemporaryFile>
20+
21+
#include "qgsrasterlayer.h"
22+
#include "qgsrasterdataprovider.h"
23+
#include "qgsrasteriterator.h"
24+
25+
/**
26+
* \ingroup UnitTests
27+
* This is a unit test for the QgsRasterIterator class.
28+
*/
29+
class TestQgsRasterIterator : public QObject
30+
{
31+
Q_OBJECT
32+
public:
33+
TestQgsRasterIterator() = default;
34+
35+
private slots:
36+
void initTestCase();// will be called before the first testfunction is executed.
37+
void cleanupTestCase();// will be called after the last testfunction was executed.
38+
void init() {} // will be called before each testfunction is executed.
39+
void cleanup() {} // will be called after every testfunction.
40+
41+
void testBasic();
42+
43+
private:
44+
45+
QString mTestDataDir;
46+
QgsRasterLayer *mpRasterLayer = nullptr;
47+
};
48+
49+
50+
//runs before all tests
51+
void TestQgsRasterIterator::initTestCase()
52+
{
53+
// init QGIS's paths - true means that all path will be inited from prefix
54+
QgsApplication::init();
55+
QgsApplication::initQgis();
56+
57+
mTestDataDir = QStringLiteral( TEST_DATA_DIR ); //defined in CmakeLists.txt
58+
QString band1byteRaster = mTestDataDir + "/big_raster.tif";
59+
60+
mpRasterLayer = new QgsRasterLayer( band1byteRaster, QStringLiteral( "big_raster" ) );
61+
62+
QVERIFY( mpRasterLayer && mpRasterLayer->isValid() );
63+
}
64+
65+
//runs after all tests
66+
void TestQgsRasterIterator::cleanupTestCase()
67+
{
68+
delete mpRasterLayer;
69+
70+
QgsApplication::exitQgis();
71+
}
72+
73+
void TestQgsRasterIterator::testBasic()
74+
{
75+
QgsRasterDataProvider *provider = mpRasterLayer->dataProvider();
76+
QVERIFY( provider );
77+
QgsRasterIterator it( provider );
78+
79+
QCOMPARE( it.input(), provider );
80+
81+
it.setMaximumTileHeight( 2500 );
82+
QCOMPARE( it.maximumTileHeight(), 2500 );
83+
84+
it.setMaximumTileWidth( 3000 );
85+
QCOMPARE( it.maximumTileWidth(), 3000 );
86+
87+
it.startRasterRead( 1, mpRasterLayer->width(), mpRasterLayer->height(), mpRasterLayer->extent() );
88+
89+
int nCols;
90+
int nRows;
91+
int topLeftCol;
92+
int topLeftRow;
93+
QgsRectangle blockExtent;
94+
std::unique_ptr< QgsRasterBlock > block;
95+
96+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
97+
QCOMPARE( nCols, 3000 );
98+
QCOMPARE( nRows, 2500 );
99+
QCOMPARE( topLeftCol, 0 );
100+
QCOMPARE( topLeftRow, 0 );
101+
QVERIFY( block.get() );
102+
QVERIFY( block->isValid() );
103+
QCOMPARE( block->value( 1, 1 ), 1.0 );
104+
QCOMPARE( block->width(), 3000 );
105+
QCOMPARE( block->height(), 2500 );
106+
QCOMPARE( blockExtent.xMinimum(), 497470.0 );
107+
QCOMPARE( blockExtent.xMaximum(), 497770.0 );
108+
QCOMPARE( blockExtent.yMinimum(), 7050880.0 );
109+
QCOMPARE( blockExtent.yMaximum(), 7051130.0 );
110+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
111+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - nRows * mpRasterLayer->rasterUnitsPerPixelY() );
112+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
113+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
114+
115+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
116+
QCOMPARE( nCols, 3000 );
117+
QCOMPARE( nRows, 2500 );
118+
QCOMPARE( topLeftCol, 3000 );
119+
QCOMPARE( topLeftRow, 0 );
120+
QVERIFY( block.get() );
121+
QVERIFY( block->isValid() );
122+
QCOMPARE( block->value( 1, 1 ), 1.0 );
123+
QCOMPARE( block->width(), 3000 );
124+
QCOMPARE( block->height(), 2500 );
125+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
126+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - nRows * mpRasterLayer->rasterUnitsPerPixelY() );
127+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
128+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
129+
130+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
131+
QCOMPARE( nCols, 1200 );
132+
QCOMPARE( nRows, 2500 );
133+
QCOMPARE( topLeftCol, 6000 );
134+
QCOMPARE( topLeftRow, 0 );
135+
QVERIFY( block.get() );
136+
QVERIFY( block->isValid() );
137+
QCOMPARE( block->value( 1, 1 ), 1.0 );
138+
QCOMPARE( block->width(), 1200 );
139+
QCOMPARE( block->height(), 2500 );
140+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
141+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - nRows * mpRasterLayer->rasterUnitsPerPixelY() );
142+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
143+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
144+
145+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
146+
QCOMPARE( nCols, 3000 );
147+
QCOMPARE( nRows, 2500 );
148+
QCOMPARE( topLeftCol, 0 );
149+
QCOMPARE( topLeftRow, 2500 );
150+
QVERIFY( block.get() );
151+
QVERIFY( block->isValid() );
152+
QCOMPARE( block->value( 1, 1 ), 1.0 );
153+
QCOMPARE( block->width(), 3000 );
154+
QCOMPARE( block->height(), 2500 );
155+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
156+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - ( nRows + topLeftRow )* mpRasterLayer->rasterUnitsPerPixelY() );
157+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
158+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
159+
160+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
161+
QCOMPARE( nCols, 3000 );
162+
QCOMPARE( nRows, 2500 );
163+
QCOMPARE( topLeftCol, 3000 );
164+
QCOMPARE( topLeftRow, 2500 );
165+
QVERIFY( block.get() );
166+
QVERIFY( block->isValid() );
167+
QCOMPARE( block->value( 1, 1 ), 1.0 );
168+
QCOMPARE( block->width(), 3000 );
169+
QCOMPARE( block->height(), 2500 );
170+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
171+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - ( nRows + topLeftRow )* mpRasterLayer->rasterUnitsPerPixelY() );
172+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
173+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
174+
175+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
176+
QCOMPARE( nCols, 1200 );
177+
QCOMPARE( nRows, 2500 );
178+
QCOMPARE( topLeftCol, 6000 );
179+
QCOMPARE( topLeftRow, 2500 );
180+
QVERIFY( block.get() );
181+
QCOMPARE( block->width(), 1200 );
182+
QCOMPARE( block->height(), 2500 );
183+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
184+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - ( nRows + topLeftRow )* mpRasterLayer->rasterUnitsPerPixelY() );
185+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
186+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
187+
188+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
189+
QCOMPARE( nCols, 3000 );
190+
QCOMPARE( nRows, 450 );
191+
QCOMPARE( topLeftCol, 0 );
192+
QCOMPARE( topLeftRow, 5000 );
193+
QVERIFY( block.get() );
194+
QVERIFY( block->isValid() );
195+
QCOMPARE( block->value( 1, 1 ), 1.0 );
196+
QCOMPARE( block->width(), 3000 );
197+
QCOMPARE( block->height(), 450 );
198+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
199+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - ( nRows + topLeftRow )* mpRasterLayer->rasterUnitsPerPixelY() );
200+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
201+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
202+
203+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
204+
QCOMPARE( nCols, 3000 );
205+
QCOMPARE( nRows, 450 );
206+
QCOMPARE( topLeftCol, 3000 );
207+
QCOMPARE( topLeftRow, 5000 );
208+
QVERIFY( block.get() );
209+
QCOMPARE( block->width(), 3000 );
210+
QCOMPARE( block->height(), 450 );
211+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
212+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - ( nRows + topLeftRow )* mpRasterLayer->rasterUnitsPerPixelY() );
213+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
214+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
215+
216+
QVERIFY( it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
217+
QCOMPARE( nCols, 1200 );
218+
QCOMPARE( nRows, 450 );
219+
QCOMPARE( topLeftCol, 6000 );
220+
QCOMPARE( topLeftRow, 5000 );
221+
QVERIFY( block.get() );
222+
QVERIFY( block->isValid() );
223+
QCOMPARE( block->value( 1, 1 ), 1.0 );
224+
QCOMPARE( block->width(), 1200 );
225+
QCOMPARE( block->height(), 450 );
226+
QCOMPARE( blockExtent.xMinimum(), mpRasterLayer->extent().xMinimum() + topLeftCol * mpRasterLayer->rasterUnitsPerPixelX() );
227+
QCOMPARE( blockExtent.yMinimum(), mpRasterLayer->extent().yMaximum() - ( nRows + topLeftRow )* mpRasterLayer->rasterUnitsPerPixelY() );
228+
QCOMPARE( blockExtent.width(), nCols * mpRasterLayer->rasterUnitsPerPixelX() );
229+
QCOMPARE( blockExtent.height(), nRows * mpRasterLayer->rasterUnitsPerPixelY() );
230+
231+
QVERIFY( !it.readNextRasterPart( 1, nCols, nRows, block, topLeftCol, topLeftRow, &blockExtent ) );
232+
QVERIFY( !block.get() );
233+
}
234+
235+
236+
QGSTEST_MAIN( TestQgsRasterIterator )
237+
238+
#include "testqgsrasteriterator.moc"

‎tests/testdata/big_raster.tif

197 KB
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.