-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
testqgsgdalutils.cpp
304 lines (243 loc) · 12.2 KB
/
testqgsgdalutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/***************************************************************************
testqgsgdalutils.cpp
--------------------
begin : September 2018
copyright : (C) 2018 Even Rouault
email : even.rouault at spatialys.com
***************************************************************************
* *
* 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 "qgstest.h"
#include <QObject>
#include <QString>
#include <QStringList>
#include <QSettings>
#include <gdal.h>
#include "qgsgdalutils.h"
#include "qgsapplication.h"
#include "qgsrasterlayer.h"
class TestQgsGdalUtils: 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 supportsRasterCreate();
void testCreateSingleBandMemoryDataset();
void testCreateMultiBandMemoryDataset();
void testCreateSingleBandTiffDataset();
void testResampleSingleBandRaster();
void testImageToDataset();
void testResampleImageToImage();
private:
double identify( GDALDatasetH dataset, int band, int px, int py );
};
void TestQgsGdalUtils::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis();
GDALAllRegister();
}
void TestQgsGdalUtils::cleanupTestCase()
{
QgsApplication::exitQgis();
}
void TestQgsGdalUtils::init()
{
}
void TestQgsGdalUtils::cleanup()
{
}
void TestQgsGdalUtils::supportsRasterCreate()
{
QVERIFY( QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "GTiff" ) ) );
QVERIFY( QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "GPKG" ) ) );
// special case
QVERIFY( !QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "SQLite" ) ) );
// create-only
QVERIFY( !QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "DTED" ) ) );
// vector-only
QVERIFY( !QgsGdalUtils::supportsRasterCreate( GDALGetDriverByName( "ESRI Shapefile" ) ) );
}
#if PROJ_VERSION_MAJOR>=6
#define EPSG_4326_WKT \
"GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," \
"AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433," \
"AUTHORITY[\"EPSG\",\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],AUTHORITY[\"EPSG\",\"4326\"]]"
#else
#define EPSG_4326_WKT \
"GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]]," \
"AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433," \
"AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"
#endif
void TestQgsGdalUtils::testCreateSingleBandMemoryDataset()
{
gdal::dataset_unique_ptr ds1 = QgsGdalUtils::createSingleBandMemoryDataset( GDT_Float32, QgsRectangle( 1, 1, 21, 11 ), 40, 20, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( ds1 );
QCOMPARE( GDALGetRasterCount( ds1.get() ), 1 );
QCOMPARE( GDALGetRasterXSize( ds1.get() ), 40 );
QCOMPARE( GDALGetRasterYSize( ds1.get() ), 20 );
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( GDALGetProjectionRef( ds1.get() ), R"""(GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]])""" );
#else
QCOMPARE( GDALGetProjectionRef( ds1.get() ), EPSG_4326_WKT );
#endif
double geoTransform[6];
double geoTransformExpected[] = { 1, 0.5, 0, 11, 0, -0.5 };
QCOMPARE( GDALGetGeoTransform( ds1.get(), geoTransform ), CE_None );
QVERIFY( memcmp( geoTransform, geoTransformExpected, sizeof( double ) * 6 ) == 0 );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( ds1.get(), 1 ) ), GDT_Float32 );
}
void TestQgsGdalUtils::testCreateMultiBandMemoryDataset()
{
gdal::dataset_unique_ptr ds1 = QgsGdalUtils::createMultiBandMemoryDataset( GDT_Float32, 4, QgsRectangle( 1, 1, 21, 11 ), 40, 20, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( ds1 );
QCOMPARE( GDALGetRasterCount( ds1.get() ), 4 );
QCOMPARE( GDALGetRasterXSize( ds1.get() ), 40 );
QCOMPARE( GDALGetRasterYSize( ds1.get() ), 20 );
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( GDALGetProjectionRef( ds1.get() ), R"""(GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]])""" );
#else
QCOMPARE( GDALGetProjectionRef( ds1.get() ), EPSG_4326_WKT );
#endif
double geoTransform[6];
double geoTransformExpected[] = { 1, 0.5, 0, 11, 0, -0.5 };
QCOMPARE( GDALGetGeoTransform( ds1.get(), geoTransform ), CE_None );
QVERIFY( memcmp( geoTransform, geoTransformExpected, sizeof( double ) * 6 ) == 0 );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( ds1.get(), 1 ) ), GDT_Float32 );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( ds1.get(), 2 ) ), GDT_Float32 );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( ds1.get(), 3 ) ), GDT_Float32 );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( ds1.get(), 4 ) ), GDT_Float32 );
}
void TestQgsGdalUtils::testCreateSingleBandTiffDataset()
{
QString filename = QDir::tempPath() + "/qgis_test_single_band_raster.tif";
QFile::remove( filename );
QVERIFY( !QFile::exists( filename ) );
gdal::dataset_unique_ptr ds1 = QgsGdalUtils::createSingleBandTiffDataset( filename, GDT_Float32, QgsRectangle( 1, 1, 21, 11 ), 40, 20, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( ds1 );
QCOMPARE( GDALGetRasterCount( ds1.get() ), 1 );
QCOMPARE( GDALGetRasterXSize( ds1.get() ), 40 );
QCOMPARE( GDALGetRasterYSize( ds1.get() ), 20 );
#if PROJ_VERSION_MAJOR>=6
QCOMPARE( GDALGetProjectionRef( ds1.get() ), R"""(GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]])""" );
#else
QCOMPARE( GDALGetProjectionRef( ds1.get() ), EPSG_4326_WKT );
#endif
double geoTransform[6];
double geoTransformExpected[] = { 1, 0.5, 0, 11, 0, -0.5 };
QCOMPARE( GDALGetGeoTransform( ds1.get(), geoTransform ), CE_None );
QVERIFY( memcmp( geoTransform, geoTransformExpected, sizeof( double ) * 6 ) == 0 );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( ds1.get(), 1 ) ), GDT_Float32 );
ds1.reset(); // makes sure the file is fully written
QVERIFY( QFile::exists( filename ) );
std::unique_ptr<QgsRasterLayer> layer( new QgsRasterLayer( filename, "test", "gdal" ) );
QVERIFY( layer->isValid() );
QCOMPARE( layer->extent(), QgsRectangle( 1, 1, 21, 11 ) );
QCOMPARE( layer->width(), 40 );
QCOMPARE( layer->height(), 20 );
layer.reset(); // let's clean up before removing the file
QFile::remove( filename );
}
void TestQgsGdalUtils::testResampleSingleBandRaster()
{
QString inputFilename = QString( TEST_DATA_DIR ) + "/float1-16.tif";
gdal::dataset_unique_ptr srcDS( GDALOpen( inputFilename.toUtf8().constData(), GA_ReadOnly ) );
QVERIFY( srcDS );
QString outputFilename = QDir::tempPath() + "/qgis_test_float1-16_resampled.tif";
QgsRectangle outputExtent( 106.25, -6.75, 106.55, -6.45 );
gdal::dataset_unique_ptr dstDS = QgsGdalUtils::createSingleBandTiffDataset( outputFilename, GDT_Float32, outputExtent, 2, 2, QgsCoordinateReferenceSystem( "EPSG:4326" ) );
QVERIFY( dstDS );
QgsGdalUtils::resampleSingleBandRaster( srcDS.get(), dstDS.get(), GRA_NearestNeighbour, nullptr );
dstDS.reset();
std::unique_ptr<QgsRasterLayer> layer( new QgsRasterLayer( outputFilename, "test", "gdal" ) );
QVERIFY( layer );
std::unique_ptr<QgsRasterBlock> block( layer->dataProvider()->block( 1, outputExtent, 2, 2 ) );
QVERIFY( block );
QCOMPARE( block->value( 0, 0 ), 6. );
QCOMPARE( block->value( 1, 1 ), 11. );
layer.reset();
QFile::remove( outputFilename );
}
void TestQgsGdalUtils::testImageToDataset()
{
QString inputFilename = QString( TEST_DATA_DIR ) + "/rgb256x256.png";
QImage src = QImage( inputFilename );
src = src.convertToFormat( QImage::Format_ARGB32 );
QVERIFY( !src.isNull() );
gdal::dataset_unique_ptr dstDS = QgsGdalUtils::imageToMemoryDataset( QImage() );
QVERIFY( !dstDS );
dstDS = QgsGdalUtils::imageToMemoryDataset( src );
QVERIFY( dstDS );
QCOMPARE( GDALGetRasterCount( dstDS.get() ), 4 );
QCOMPARE( GDALGetRasterXSize( dstDS.get() ), 256 );
QCOMPARE( GDALGetRasterYSize( dstDS.get() ), 256 );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( dstDS.get(), 1 ) ), GDT_Byte );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( dstDS.get(), 2 ) ), GDT_Byte );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( dstDS.get(), 3 ) ), GDT_Byte );
QCOMPARE( GDALGetRasterDataType( GDALGetRasterBand( dstDS.get(), 4 ) ), GDT_Byte );
QCOMPARE( identify( dstDS.get(), 1, 50, 50 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 1, 200, 50 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 1, 50, 200 ), 0.0 );
QCOMPARE( identify( dstDS.get(), 1, 200, 200 ), 0.0 );
QCOMPARE( identify( dstDS.get(), 2, 50, 50 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 2, 200, 50 ), 0.0 );
QCOMPARE( identify( dstDS.get(), 2, 50, 200 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 2, 200, 200 ), 0.0 );
QCOMPARE( identify( dstDS.get(), 3, 50, 50 ), 0.0 );
QCOMPARE( identify( dstDS.get(), 3, 200, 50 ), 0.0 );
QCOMPARE( identify( dstDS.get(), 3, 50, 200 ), 0.0 );
QCOMPARE( identify( dstDS.get(), 3, 200, 200 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 4, 50, 50 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 4, 200, 50 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 4, 50, 200 ), 255.0 );
QCOMPARE( identify( dstDS.get(), 4, 200, 200 ), 255.0 );
}
void TestQgsGdalUtils::testResampleImageToImage()
{
QString inputFilename = QString( TEST_DATA_DIR ) + "/rgb256x256.png";
QImage src = QImage( inputFilename );
src = src.convertToFormat( QImage::Format_ARGB32 );
QVERIFY( !src.isNull() );
QImage res = QgsGdalUtils::resampleImage( QImage(), QSize( 50, 50 ), GRIORA_NearestNeighbour );
QVERIFY( res.isNull() );
res = QgsGdalUtils::resampleImage( src, QSize( 50, 50 ), GRIORA_NearestNeighbour );
QVERIFY( !res.isNull() );
QCOMPARE( res.width(), 50 );
QCOMPARE( res.height(), 50 );
QCOMPARE( qRed( res.pixel( 10, 10 ) ), 255 );
QCOMPARE( qGreen( res.pixel( 10, 10 ) ), 255 );
QCOMPARE( qBlue( res.pixel( 10, 10 ) ), 0 );
QCOMPARE( qAlpha( res.pixel( 10, 10 ) ), 255 );
QCOMPARE( qRed( res.pixel( 40, 10 ) ), 255 );
QCOMPARE( qGreen( res.pixel( 40, 10 ) ), 0 );
QCOMPARE( qBlue( res.pixel( 40, 10 ) ), 0 );
QCOMPARE( qAlpha( res.pixel( 40, 10 ) ), 255 );
QCOMPARE( qRed( res.pixel( 10, 40 ) ), 0 );
QCOMPARE( qGreen( res.pixel( 10, 40 ) ), 255 );
QCOMPARE( qBlue( res.pixel( 10, 40 ) ), 0 );
QCOMPARE( qAlpha( res.pixel( 10, 40 ) ), 255 );
QCOMPARE( qRed( res.pixel( 40, 40 ) ), 0 );
QCOMPARE( qGreen( res.pixel( 40, 40 ) ), 0 );
QCOMPARE( qBlue( res.pixel( 40, 40 ) ), 255 );
QCOMPARE( qAlpha( res.pixel( 40, 40 ) ), 255 );
}
double TestQgsGdalUtils::identify( GDALDatasetH dataset, int band, int px, int py )
{
GDALRasterBandH hBand = GDALGetRasterBand( dataset, band );
float *pafScanline = ( float * ) CPLMalloc( sizeof( float ) );
CPLErr err = GDALRasterIO( hBand, GF_Read, px, py, 1, 1,
pafScanline, 1, 1, GDT_Float32, 0, 0 );
double value = err == CE_None ? pafScanline[0] : std::numeric_limits<double>::quiet_NaN();
CPLFree( pafScanline );
return value;
}
QGSTEST_MAIN( TestQgsGdalUtils )
#include "testqgsgdalutils.moc"