Line data Source code
1 : ///////////////////////////////////////////////////////////////////////////////
2 : //
3 : // Project: C++ Test Suite for GDAL/OGR
4 : // Purpose: Test DTED support. Ported from gdrivers/dted.py.
5 : // Author: Mateusz Loskot <mateusz@loskot.net>
6 : //
7 : ///////////////////////////////////////////////////////////////////////////////
8 : // Copyright (c) 2006, Mateusz Loskot <mateusz@loskot.net>
9 : // Copyright (c) 2010, Even Rouault <even dot rouault at spatialys.com>
10 : /*
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #include "gdal_unit_test.h"
31 :
32 : #include "cpl_string.h"
33 : #include "gdal_alg.h"
34 : #include "gdal_priv.h"
35 : #include "gdal.h"
36 :
37 : #include <vector>
38 :
39 : #include "gtest_include.h"
40 :
41 : namespace
42 : {
43 :
44 : // Common fixture with test data
45 : struct test_gdal_dted : public ::testing::Test
46 : {
47 : struct raster_t
48 : {
49 : std::string file_;
50 : int band_;
51 : int checksum_;
52 :
53 7 : raster_t(std::string const &f, int b, int c)
54 7 : : file_(f), band_(b), checksum_(c)
55 : {
56 7 : }
57 : };
58 :
59 : typedef std::vector<raster_t> rasters_t;
60 :
61 : GDALDriverH drv_;
62 : std::string drv_name_;
63 : std::string data_;
64 : std::string data_tmp_;
65 : rasters_t rasters_;
66 :
67 7 : test_gdal_dted() : drv_(nullptr), drv_name_("DTED")
68 : {
69 7 : drv_ = GDALGetDriverByName(drv_name_.c_str());
70 :
71 : // Compose data path for test group
72 7 : data_ = tut::common::data_basedir;
73 7 : data_tmp_ = tut::common::tmp_basedir;
74 :
75 : // Collection of test DEM datasets
76 :
77 : // TODO: Verify value of this checksum
78 7 : rasters_.push_back(raster_t("n43.dt0", 1, 49187));
79 7 : }
80 :
81 7 : void SetUp() override
82 : {
83 7 : if (drv_ == nullptr)
84 0 : GTEST_SKIP() << "DTED driver missing";
85 : }
86 : };
87 :
88 : // Test open dataset
89 4 : TEST_F(test_gdal_dted, open)
90 : {
91 1 : if (drv_ == nullptr)
92 0 : return;
93 2 : for (const auto &raster : rasters_)
94 : {
95 1 : std::string file(data_ + SEP);
96 1 : file += raster.file_;
97 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
98 1 : ASSERT_TRUE(nullptr != ds);
99 1 : GDALClose(ds);
100 : }
101 : }
102 :
103 : // Test dataset checksums
104 4 : TEST_F(test_gdal_dted, checksums)
105 : {
106 1 : if (drv_ == nullptr)
107 0 : return;
108 2 : for (const auto &raster : rasters_)
109 : {
110 1 : std::string file(data_ + SEP);
111 1 : file += raster.file_;
112 :
113 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
114 1 : ASSERT_TRUE(nullptr != ds);
115 :
116 1 : GDALRasterBandH band = GDALGetRasterBand(ds, raster.band_);
117 1 : ASSERT_TRUE(nullptr != band);
118 :
119 1 : const int xsize = GDALGetRasterXSize(ds);
120 1 : const int ysize = GDALGetRasterYSize(ds);
121 1 : const int checksum = GDALChecksumImage(band, 0, 0, xsize, ysize);
122 :
123 1 : EXPECT_EQ(checksum, raster.checksum_);
124 :
125 1 : GDALClose(ds);
126 : }
127 : }
128 :
129 : // Test affine transformation coefficients
130 4 : TEST_F(test_gdal_dted, geotransform)
131 : {
132 : // Index of test file being tested
133 1 : const std::size_t fileIdx = 0;
134 :
135 1 : std::string file(data_ + SEP);
136 1 : file += rasters_.at(fileIdx).file_;
137 :
138 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
139 1 : ASSERT_TRUE(nullptr != ds);
140 :
141 1 : double geoTransform[6] = {0};
142 1 : CPLErr err = GDALGetGeoTransform(ds, geoTransform);
143 1 : ASSERT_EQ(err, CE_None);
144 :
145 : // Test affine transformation coefficients
146 1 : const double maxError = 0.000001;
147 : const double expect[6] = {
148 : -80.004166666666663, 0.0083333333333333332, 0, 44.00416666666667, 0,
149 : -0.0083333333333333332};
150 1 : EXPECT_NEAR(expect[0], geoTransform[0], maxError);
151 1 : EXPECT_NEAR(expect[1], geoTransform[1], maxError);
152 1 : EXPECT_NEAR(expect[2], geoTransform[2], maxError);
153 1 : EXPECT_NEAR(expect[3], geoTransform[3], maxError);
154 1 : EXPECT_NEAR(expect[4], geoTransform[4], maxError);
155 1 : EXPECT_NEAR(expect[5], geoTransform[5], maxError);
156 :
157 1 : GDALClose(ds);
158 : }
159 :
160 : // Test projection definition
161 4 : TEST_F(test_gdal_dted, projection)
162 : {
163 : // Index of test file being tested
164 1 : const std::size_t fileIdx = 0;
165 :
166 1 : std::string file(data_ + SEP);
167 1 : file += rasters_.at(fileIdx).file_;
168 :
169 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
170 1 : ASSERT_TRUE(nullptr != ds);
171 :
172 1 : std::string proj(GDALGetProjectionRef(ds));
173 1 : ASSERT_TRUE(!proj.empty());
174 :
175 : std::string expect(
176 : "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS "
177 : "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
178 : "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
179 : "\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\","
180 : "\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
181 2 : "AUTHORITY[\"EPSG\",\"4326\"]]");
182 1 : EXPECT_EQ(proj, expect);
183 :
184 1 : GDALClose(ds);
185 : }
186 :
187 : // Test band data type and NODATA value
188 4 : TEST_F(test_gdal_dted, nodata)
189 : {
190 : // Index of test file being tested
191 1 : const std::size_t fileIdx = 0;
192 :
193 1 : std::string file(data_ + SEP);
194 1 : file += rasters_.at(fileIdx).file_;
195 :
196 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
197 1 : ASSERT_TRUE(nullptr != ds);
198 :
199 1 : GDALRasterBandH band = GDALGetRasterBand(ds, rasters_.at(fileIdx).band_);
200 1 : ASSERT_TRUE(nullptr != band);
201 :
202 1 : const double noData = GDALGetRasterNoDataValue(band, nullptr);
203 1 : EXPECT_EQ(noData, -32767);
204 :
205 1 : EXPECT_EQ(GDALGetRasterDataType(band), GDT_Int16);
206 :
207 1 : GDALClose(ds);
208 : }
209 :
210 : // Create simple copy and check
211 4 : TEST_F(test_gdal_dted, copy)
212 : {
213 : // Index of test file being tested
214 1 : const std::size_t fileIdx = 0;
215 :
216 1 : std::string src(data_ + SEP);
217 1 : src += rasters_.at(fileIdx).file_;
218 :
219 1 : GDALDatasetH dsSrc = GDALOpen(src.c_str(), GA_ReadOnly);
220 1 : ASSERT_TRUE(nullptr != dsSrc);
221 :
222 1 : std::string dst(data_tmp_ + SEP);
223 1 : dst += rasters_.at(fileIdx).file_;
224 :
225 1 : GDALDatasetH dsDst = nullptr;
226 1 : dsDst = GDALCreateCopy(drv_, dst.c_str(), dsSrc, FALSE, nullptr, nullptr,
227 : nullptr);
228 1 : GDALClose(dsSrc);
229 1 : ASSERT_TRUE(nullptr != dsDst);
230 :
231 1 : std::string proj(GDALGetProjectionRef(dsDst));
232 1 : ASSERT_TRUE(!proj.empty());
233 :
234 : std::string expect(
235 : "GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS "
236 : "84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY["
237 : "\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\","
238 : "\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\","
239 : "\"9122\"]],AXIS[\"Latitude\",NORTH],AXIS[\"Longitude\",EAST],"
240 1 : "AUTHORITY[\"EPSG\",\"4326\"]]");
241 1 : EXPECT_EQ(proj, expect);
242 :
243 1 : GDALRasterBandH band = GDALGetRasterBand(dsDst, rasters_.at(fileIdx).band_);
244 1 : ASSERT_TRUE(nullptr != band);
245 :
246 1 : const int xsize = GDALGetRasterXSize(dsDst);
247 1 : const int ysize = GDALGetRasterYSize(dsDst);
248 1 : const int checksum = GDALChecksumImage(band, 0, 0, xsize, ysize);
249 :
250 1 : EXPECT_EQ(checksum, rasters_.at(fileIdx).checksum_);
251 :
252 1 : GDALClose(dsDst);
253 : }
254 :
255 : // Test subwindow read and the tail recursion problem.
256 4 : TEST_F(test_gdal_dted, subwindow_read)
257 : {
258 : // Index of test file being tested
259 1 : const std::size_t fileIdx = 0;
260 :
261 1 : std::string file(data_ + SEP);
262 1 : file += rasters_.at(fileIdx).file_;
263 :
264 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
265 1 : ASSERT_TRUE(nullptr != ds);
266 :
267 1 : GDALRasterBandH band = GDALGetRasterBand(ds, rasters_.at(fileIdx).band_);
268 1 : ASSERT_TRUE(nullptr != band);
269 :
270 : // Sub-windows size
271 : const int win[4] = {5, 5, 5, 5};
272 : // subwindow checksum
273 1 : const int winChecksum = 305;
274 : const int checksum =
275 1 : GDALChecksumImage(band, win[0], win[1], win[2], win[3]);
276 :
277 1 : EXPECT_EQ(checksum, winChecksum);
278 :
279 1 : GDALClose(ds);
280 : }
281 :
282 : } // namespace
|