LCOV - code coverage report
Current view: top level - autotest/cpp - test_gdal_aaigrid.cpp (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 110 112 98.2 %
Date: 2024-11-21 22:18:42 Functions: 31 31 100.0 %

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

Generated by: LCOV version 1.14