1616#include " qgstest.h"
1717#include < QObject>
1818#include < QString>
19+ #include < QTemporaryFile>
1920
2021#include " qgsrasterlayer.h"
2122#include " qgsrasterdataprovider.h"
@@ -41,6 +42,7 @@ class TestQgsRasterBlock : public QObject
4142 void cleanup () {} // will be called after every testfunction.
4243
4344 void testBasic ();
45+ void testWrite ();
4446
4547 private:
4648
@@ -116,6 +118,95 @@ void TestQgsRasterBlock::testBasic()
116118 QCOMPARE ( data.at ( 1 ), ( char ) 5 );
117119 QCOMPARE ( data.at ( 10 ), ( char ) 27 );
118120
121+ // setData()
122+ QByteArray newData ( " \xaa\xbb\xcc\xdd " );
123+ block->setData ( newData, 1 );
124+ QByteArray data2 = block->data ();
125+ QCOMPARE ( data2.at ( 0 ), ( char ) 2 );
126+ QCOMPARE ( data2.at ( 1 ), ' \xaa ' );
127+ QCOMPARE ( data2.at ( 2 ), ' \xbb ' );
128+ QCOMPARE ( data2.at ( 10 ), ( char ) 27 );
129+
130+ delete block;
131+ }
132+
133+ void TestQgsRasterBlock::testWrite ()
134+ {
135+ QgsRectangle extent = mpRasterLayer->extent ();
136+ int nCols = mpRasterLayer->width (), nRows = mpRasterLayer->height ();
137+ double tform[] =
138+ {
139+ extent.xMinimum (), extent.width () / nCols, 0.0 ,
140+ extent.yMaximum (), 0.0 , -extent.height () / nRows
141+ };
142+
143+ // generate unique filename (need to open the file first to generate it)
144+ QTemporaryFile tmpFile;
145+ tmpFile.open ();
146+ tmpFile.close ();
147+
148+ // create a GeoTIFF - this will create data provider in editable mode
149+ QString filename = tmpFile.fileName ();
150+ QgsRasterDataProvider* dp = QgsRasterDataProvider::create ( " gdal" , filename, " GTiff" , 1 , Qgis::Byte, 10 , 10 , tform, mpRasterLayer->crs () );
151+
152+ QgsRasterBlock* block = mpRasterLayer->dataProvider ()->block ( 1 , mpRasterLayer->extent (), mpRasterLayer->width (), mpRasterLayer->height () );
153+
154+ QByteArray origData = block->data ();
155+ origData.detach (); // make sure we have private copy independent from independent block content
156+ QCOMPARE ( origData.at ( 0 ), ( char ) 2 );
157+ QCOMPARE ( origData.at ( 1 ), ( char ) 5 );
158+
159+ // change first two pixels
160+ block->setData ( QByteArray ( " \xa0\xa1 " ) );
161+ bool res = dp->writeBlock ( block, 1 );
162+ QVERIFY ( res );
163+
164+ QgsRasterBlock* block2 = dp->block ( 1 , mpRasterLayer->extent (), mpRasterLayer->width (), mpRasterLayer->height () );
165+ QByteArray newData2 = block2->data ();
166+ QCOMPARE ( newData2.at ( 0 ), ' \xa0 ' );
167+ QCOMPARE ( newData2.at ( 1 ), ' \xa1 ' );
168+
169+ delete block2;
170+ delete dp;
171+
172+ // newly open raster and verify the write was permanent
173+ QgsRasterLayer* rlayer = new QgsRasterLayer ( filename, " tmp" , " gdal" );
174+ QVERIFY ( rlayer->isValid () );
175+ QgsRasterBlock* block3 = rlayer->dataProvider ()->block ( 1 , rlayer->extent (), rlayer->width (), rlayer->height () );
176+ QByteArray newData3 = block3->data ();
177+ QCOMPARE ( newData3.at ( 0 ), ' \xa0 ' );
178+ QCOMPARE ( newData3.at ( 1 ), ' \xa1 ' );
179+
180+ QgsRasterBlock* block4 = new QgsRasterBlock ( Qgis::Byte, 1 , 2 );
181+ block4->setData ( QByteArray ( " \xb0\xb1 " ) );
182+
183+ // cannot write when provider is not editable
184+ res = rlayer->dataProvider ()->writeBlock ( block4, 1 );
185+ QVERIFY ( !res );
186+
187+ // make the provider editable
188+ QVERIFY ( !rlayer->dataProvider ()->isEditable () );
189+ rlayer->dataProvider ()->setEditable ( true );
190+ QVERIFY ( rlayer->dataProvider ()->isEditable () );
191+
192+ res = rlayer->dataProvider ()->writeBlock ( block4, 1 );
193+ QVERIFY ( res );
194+
195+ rlayer->dataProvider ()->setEditable ( false );
196+ QVERIFY ( !rlayer->dataProvider ()->isEditable () );
197+
198+ // verify the change is there
199+ QgsRasterBlock* block5 = rlayer->dataProvider ()->block ( 1 , rlayer->extent (), rlayer->width (), rlayer->height () );
200+ QByteArray newData5 = block5->data ();
201+ QCOMPARE ( newData5.at ( 0 ), ' \xb0 ' );
202+ QCOMPARE ( newData5.at ( 1 ), ' \xa1 ' ); // original data
203+ QCOMPARE ( newData5.at ( 10 ), ' \xb1 ' );
204+
205+ delete block3;
206+ delete block4;
207+ delete block5;
208+ delete rlayer;
209+
119210 delete block;
120211}
121212
0 commit comments