Line data Source code
1 : ///////////////////////////////////////////////////////////////////////////////
2 : //
3 : // Project: C++ Test Suite for GDAL/OGR
4 : // Purpose: Test Arc/Info ASCII Grid support. Ported from gdrivers/aaigrid.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 <string>
38 : #include <vector>
39 :
40 : #include "gtest_include.h"
41 :
42 : namespace
43 : {
44 :
45 : // Common fixture with test data
46 : struct test_gdal_aaigrid : public ::testing::Test
47 : {
48 : struct raster_t
49 : {
50 : std::string file_;
51 : int band_;
52 : int checksum_;
53 :
54 21 : raster_t(std::string const &f, int b, int c)
55 21 : : file_(f), band_(b), checksum_(c)
56 : {
57 21 : }
58 : };
59 :
60 : typedef std::vector<raster_t> rasters_t;
61 :
62 : GDALDriverH drv_;
63 : std::string drv_name_;
64 : std::string data_;
65 : std::string data_tmp_;
66 : rasters_t grids_;
67 : rasters_t rasters_;
68 :
69 7 : test_gdal_aaigrid() : drv_(nullptr), drv_name_("AAIGrid")
70 : {
71 7 : drv_ = GDALGetDriverByName(drv_name_.c_str());
72 :
73 : // Compose data path for test group
74 7 : data_ = tut::common::data_basedir;
75 7 : data_tmp_ = tut::common::tmp_basedir;
76 :
77 : // Collection of test AAIGrid grids
78 7 : grids_.push_back(raster_t("byte.tif.grd", 1, 4672));
79 7 : grids_.push_back(raster_t("pixel_per_line.asc", 1, 1123));
80 :
81 : // Collection of non-AAIGrid rasters
82 7 : rasters_.push_back(raster_t("byte.tif", 1, 4672));
83 7 : }
84 :
85 7 : void SetUp() override
86 : {
87 7 : if (drv_ == nullptr)
88 0 : GTEST_SKIP() << "AAIGrid driver missing";
89 : }
90 : };
91 :
92 : // Test open dataset
93 4 : TEST_F(test_gdal_aaigrid, open)
94 : {
95 3 : for (const auto &raster : grids_)
96 : {
97 2 : std::string file(data_ + SEP);
98 2 : file += raster.file_;
99 2 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
100 2 : ASSERT_TRUE(nullptr != ds);
101 2 : GDALClose(ds);
102 : }
103 : }
104 :
105 : // Test dataset checksums
106 4 : TEST_F(test_gdal_aaigrid, checksum)
107 : {
108 3 : for (const auto &raster : grids_)
109 : {
110 2 : std::string file(data_ + SEP);
111 2 : file += raster.file_;
112 :
113 2 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
114 2 : ASSERT_TRUE(nullptr != ds);
115 :
116 2 : GDALRasterBandH band = GDALGetRasterBand(ds, raster.band_);
117 2 : ASSERT_TRUE(nullptr != band);
118 :
119 2 : const int xsize = GDALGetRasterXSize(ds);
120 2 : const int ysize = GDALGetRasterYSize(ds);
121 2 : const int checksum = GDALChecksumImage(band, 0, 0, xsize, ysize);
122 :
123 2 : EXPECT_EQ(checksum, raster.checksum_);
124 :
125 2 : GDALClose(ds);
126 : }
127 : }
128 :
129 : // Test affine transformation coefficients
130 4 : TEST_F(test_gdal_aaigrid, geotransform)
131 : {
132 : // Index of test file being tested
133 1 : const std::size_t fileIdx = 1;
134 :
135 1 : std::string file(data_ + SEP);
136 1 : file += grids_.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] = {100000.0, 50, 0, 650600.0, 0, -50};
148 1 : EXPECT_NEAR(expect[0], geoTransform[0], maxError);
149 1 : EXPECT_NEAR(expect[1], geoTransform[1], maxError);
150 1 : EXPECT_NEAR(expect[2], geoTransform[2], maxError);
151 1 : EXPECT_NEAR(expect[3], geoTransform[3], maxError);
152 1 : EXPECT_NEAR(expect[4], geoTransform[4], maxError);
153 1 : EXPECT_NEAR(expect[5], geoTransform[5], maxError);
154 :
155 1 : GDALClose(ds);
156 : }
157 :
158 : // Test projection definition
159 4 : TEST_F(test_gdal_aaigrid, projection)
160 : {
161 : // Index of test file being tested
162 1 : const std::size_t fileIdx = 1;
163 :
164 1 : std::string file(data_ + SEP);
165 1 : file += grids_.at(fileIdx).file_;
166 :
167 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
168 1 : ASSERT_TRUE(nullptr != ds);
169 :
170 1 : std::string proj(GDALGetProjectionRef(ds));
171 1 : ASSERT_EQ(proj.empty(), false);
172 :
173 : std::string expect(
174 : "PROJCS[\"unnamed\",GEOGCS[\"NAD83\","
175 : "DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS 1980\","
176 : "6378137,298.257222101,AUTHORITY[\"EPSG\",\"7019\"]],"
177 : "AUTHORITY[\"EPSG\",\"6269\"]],"
178 : "PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],"
179 : "UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],"
180 : "AUTHORITY[\"EPSG\",\"4269\"]],"
181 : "PROJECTION[\"Albers_Conic_Equal_Area\"],"
182 : "PARAMETER[\"latitude_of_center\",59],"
183 : "PARAMETER[\"longitude_of_center\",-132.5],"
184 : "PARAMETER[\"standard_parallel_1\",61.6666666666667],"
185 : "PARAMETER[\"standard_parallel_2\",68],"
186 : "PARAMETER[\"false_easting\",500000],"
187 : "PARAMETER[\"false_northing\",500000],"
188 : "UNIT[\"METERS\",1],"
189 2 : "AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]");
190 :
191 1 : EXPECT_EQ(proj, expect);
192 :
193 1 : GDALClose(ds);
194 : }
195 :
196 : // Test band data type and NODATA value
197 4 : TEST_F(test_gdal_aaigrid, nodata)
198 : {
199 : // Index of test file being tested
200 1 : const std::size_t fileIdx = 1;
201 :
202 1 : std::string file(data_ + SEP);
203 1 : file += grids_.at(fileIdx).file_;
204 :
205 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
206 1 : ASSERT_TRUE(nullptr != ds);
207 :
208 1 : GDALRasterBandH band = GDALGetRasterBand(ds, grids_.at(fileIdx).band_);
209 1 : ASSERT_TRUE(nullptr != band);
210 :
211 1 : const double noData = GDALGetRasterNoDataValue(band, nullptr);
212 1 : EXPECT_EQ(noData, -99999);
213 :
214 1 : EXPECT_EQ(GDALGetRasterDataType(band), GDT_Float32);
215 :
216 1 : GDALClose(ds);
217 : }
218 :
219 : // Create simple copy and check
220 4 : TEST_F(test_gdal_aaigrid, copy)
221 : {
222 : // Index of test file being tested
223 1 : const std::size_t fileIdx = 0;
224 :
225 1 : std::string src(data_ + SEP);
226 1 : src += rasters_.at(fileIdx).file_;
227 :
228 1 : GDALDatasetH dsSrc = GDALOpen(src.c_str(), GA_ReadOnly);
229 1 : ASSERT_TRUE(nullptr != dsSrc);
230 :
231 1 : std::string dst(data_tmp_ + SEP);
232 1 : dst += rasters_.at(fileIdx).file_;
233 1 : dst += ".grd";
234 :
235 1 : GDALDatasetH dsDst = nullptr;
236 1 : dsDst = GDALCreateCopy(drv_, dst.c_str(), dsSrc, FALSE, nullptr, nullptr,
237 : nullptr);
238 1 : GDALClose(dsSrc);
239 1 : ASSERT_TRUE(nullptr != dsDst);
240 :
241 1 : std::string proj(GDALGetProjectionRef(dsDst));
242 1 : ASSERT_EQ(proj.empty(), false);
243 :
244 : std::string expect(
245 : "PROJCS[\"NAD27 / UTM zone 11N\",GEOGCS[\"NAD27\","
246 : "DATUM[\"North_American_Datum_1927\",SPHEROID[\"Clarke 1866\","
247 : "6378206.4,294.978698213898,AUTHORITY[\"EPSG\",\"7008\"]],"
248 : "AUTHORITY[\"EPSG\",\"6267\"]],PRIMEM[\"Greenwich\",0],"
249 : "UNIT[\"Degree\",0.0174532925199433]],"
250 : "PROJECTION[\"Transverse_Mercator\"],"
251 : "PARAMETER[\"latitude_of_origin\",0],"
252 : "PARAMETER[\"central_meridian\",-117],"
253 : "PARAMETER[\"scale_factor\",0.9996],"
254 : "PARAMETER[\"false_easting\",500000],"
255 : "PARAMETER[\"false_northing\",0],"
256 : "UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],"
257 1 : "AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]");
258 :
259 1 : ASSERT_EQ(proj, expect);
260 :
261 1 : GDALRasterBandH band = GDALGetRasterBand(dsDst, rasters_.at(fileIdx).band_);
262 1 : ASSERT_TRUE(nullptr != band);
263 :
264 1 : const int xsize = GDALGetRasterXSize(dsDst);
265 1 : const int ysize = GDALGetRasterYSize(dsDst);
266 1 : const int checksum = GDALChecksumImage(band, 0, 0, xsize, ysize);
267 :
268 1 : ASSERT_EQ(checksum, rasters_.at(fileIdx).checksum_);
269 :
270 1 : GDALClose(dsDst);
271 : }
272 :
273 : // Test subwindow read and the tail recursion problem.
274 4 : TEST_F(test_gdal_aaigrid, subwindow_read)
275 : {
276 : // Index of test file being tested
277 1 : const std::size_t fileIdx = 1;
278 :
279 1 : std::string file(data_ + SEP);
280 1 : file += grids_.at(fileIdx).file_;
281 :
282 1 : GDALDatasetH ds = GDALOpen(file.c_str(), GA_ReadOnly);
283 1 : ASSERT_TRUE(nullptr != ds);
284 :
285 1 : GDALRasterBandH band = GDALGetRasterBand(ds, grids_.at(fileIdx).band_);
286 1 : ASSERT_TRUE(nullptr != band);
287 :
288 : // Sub-windows size
289 : const int win[4] = {5, 5, 5, 5};
290 : // subwindow checksum
291 1 : const int winChecksum = 187;
292 : const int checksum =
293 1 : GDALChecksumImage(band, win[0], win[1], win[2], win[3]);
294 :
295 1 : EXPECT_EQ(checksum, winChecksum);
296 :
297 1 : GDALClose(ds);
298 : }
299 :
300 : } // namespace
|