From 66389f0ba80af1de97aeced1c86c76f15e132fd4 Mon Sep 17 00:00:00 2001 From: Stefan Blumentrath Date: Wed, 4 Sep 2024 02:56:09 +0200 Subject: [PATCH] r.in.gdal/r.external: add basic support for GDALs GDT_Int8 (#4256) * add support for Int8 from GDAL >= 3.7 * add int8 test (skiped if GDAL < 3.7) --- lib/raster/gdal.c | 4 ++ lib/raster/get_row.c | 7 ++++ raster/r.external/link.c | 8 ++++ raster/r.in.gdal/testsuite/data/int8.tif | Bin 0 -> 948 bytes raster/r.in.gdal/testsuite/data/int8.vrt | 20 +++++++++ raster/r.in.gdal/testsuite/test_r_in_gdal.py | 42 +++++++++++++++++++ 6 files changed, 81 insertions(+) create mode 100644 raster/r.in.gdal/testsuite/data/int8.tif create mode 100644 raster/r.in.gdal/testsuite/data/int8.vrt diff --git a/lib/raster/gdal.c b/lib/raster/gdal.c index a3bfce1b3b8..7fa85a9836c 100644 --- a/lib/raster/gdal.c +++ b/lib/raster/gdal.c @@ -125,6 +125,10 @@ struct GDAL_link *Rast_get_gdal_link(const char *name, const char *mapset) switch (type) { case GDT_Byte: +/* GDT_Int8 was introduced in GDAL 3.7 */ +#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 7, 0) + case GDT_Int8: +#endif case GDT_Int16: case GDT_UInt16: case GDT_Int32: diff --git a/lib/raster/get_row.c b/lib/raster/get_row.c index 1f08a1203f3..d95eadc6e2f 100644 --- a/lib/raster/get_row.c +++ b/lib/raster/get_row.c @@ -11,6 +11,7 @@ \author Original author CERL */ +#include #include #include #include @@ -368,6 +369,12 @@ static void gdal_values_int(int fd, const unsigned char *data, case GDT_Byte: c[i] = *(GByte *)d; break; +/* GDT_Int8 was introduced in GDAL 3.7 */ +#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 7, 0) + case GDT_Int8: + c[i] = *(int8_t *)d; + break; +#endif case GDT_Int16: c[i] = *(GInt16 *)d; break; diff --git a/raster/r.external/link.c b/raster/r.external/link.c index 4ff0799fe69..bde45546fc2 100644 --- a/raster/r.external/link.c +++ b/raster/r.external/link.c @@ -30,6 +30,14 @@ void query_band(GDALRasterBandH hBand, const char *output, cellhd->format = 0; break; +/* GDT_Int8 was introduced in GDAL 3.7 */ +#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 7, 0) + case GDT_Int8: + info->data_type = CELL_TYPE; + cellhd->format = 1; + break; +#endif + case GDT_Int16: case GDT_UInt16: info->data_type = CELL_TYPE; diff --git a/raster/r.in.gdal/testsuite/data/int8.tif b/raster/r.in.gdal/testsuite/data/int8.tif new file mode 100644 index 0000000000000000000000000000000000000000..84f3e88ce634cb0a89c706b8eac1e79fa3e486e4 GIT binary patch literal 948 zcmebD)MDUZU|`^9U|?isU<9%xfS3`9%>-qGR53%@Aa!g=Y(YjAu-+1&gea1@7?ce% zQyi+sfr)`Z21!jYlnoSS;A`e#UFAD1}z}l z3*-nm_+SrV!f3bz0fvPGOaUX<2kZ4$q literal 0 HcmV?d00001 diff --git a/raster/r.in.gdal/testsuite/data/int8.vrt b/raster/r.in.gdal/testsuite/data/int8.vrt new file mode 100644 index 00000000000..5a0b5ebe836 --- /dev/null +++ b/raster/r.in.gdal/testsuite/data/int8.vrt @@ -0,0 +1,20 @@ + + PROJCS["NAD83(HARN) / North Carolina",GEOGCS["NAD83(HARN)",DATUM["NAD83_High_Accuracy_Reference_Network",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6152"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4152"]],PROJECTION["Lambert_Conformal_Conic_2SP"],PARAMETER["latitude_of_origin",33.75],PARAMETER["central_meridian",-79],PARAMETER["standard_parallel_1",36.1666666666667],PARAMETER["standard_parallel_2",34.3333333333333],PARAMETER["false_easting",609601.22],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","3358"]] + 0.0000000000000000e+00, 1.0000000000000000e+00, 0.0000000000000000e+00, 2.4000000000000000e+01, 0.0000000000000000e+00, -1.0000000000000000e+00 + + Area + + + BAND + + + Gray + + ./int8.tif + 1 + + + + + + diff --git a/raster/r.in.gdal/testsuite/test_r_in_gdal.py b/raster/r.in.gdal/testsuite/test_r_in_gdal.py index 2b8bee0982a..355e9b74eda 100644 --- a/raster/r.in.gdal/testsuite/test_r_in_gdal.py +++ b/raster/r.in.gdal/testsuite/test_r_in_gdal.py @@ -3,6 +3,10 @@ @author Soeren Gebbert """ +import unittest + +from subprocess import check_output + from grass.gunittest.case import TestCase @@ -306,6 +310,44 @@ def test_netCDF_3d_5(self): self.assertLooksLike(map_list, text_from_file) + @unittest.skipIf( + tuple( + map( + int, + check_output(["gdal-config", "--version"]) + .decode("UTF8") + .split(".")[0:2], + ) + ) + < (3, 7), + "GDAL version too old. Int8 support was introduced in GDAL 3.7", + ) + def test_int8_data(self): + """Test that Int8 VRTs are imported""" + + self.assertModule( + "r.in.gdal", + input="data/int8.vrt", + output="test_gdal_import_map", + ) + + # Output of r.info + info_string = """north=24 + south=0 + east=24 + west=0 + nsres=1 + ewres=1 + rows=24 + cols=24 + cells=576 + datatype=CELL + ncats=0""" + + self.assertRasterFitsInfo( + raster="test_gdal_import_map", reference=info_string, precision=3 + ) + class TestGdalImportFails(TestCase): def test_error_handling_1(self):