Skip to content

Commit

Permalink
r.in.gdal/r.external: add basic support for GDALs GDT_Int8 (OSGeo#4256)
Browse files Browse the repository at this point in the history
* add support for Int8 from GDAL >= 3.7

* add int8 test (skiped if GDAL < 3.7)
  • Loading branch information
ninsbl authored Sep 4, 2024
1 parent 9222822 commit 66389f0
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/raster/gdal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions lib/raster/get_row.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
\author Original author CERL
*/

#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions raster/r.external/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Binary file added raster/r.in.gdal/testsuite/data/int8.tif
Binary file not shown.
20 changes: 20 additions & 0 deletions raster/r.in.gdal/testsuite/data/int8.vrt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<VRTDataset rasterXSize="24" rasterYSize="24">
<SRS dataAxisToSRSAxisMapping="1,2">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"]]</SRS>
<GeoTransform> 0.0000000000000000e+00, 1.0000000000000000e+00, 0.0000000000000000e+00, 2.4000000000000000e+01, 0.0000000000000000e+00, -1.0000000000000000e+00</GeoTransform>
<Metadata>
<MDI key="AREA_OR_POINT">Area</MDI>
</Metadata>
<Metadata domain="IMAGE_STRUCTURE">
<MDI key="INTERLEAVE">BAND</MDI>
</Metadata>
<VRTRasterBand dataType="Int8" band="1">
<ColorInterp>Gray</ColorInterp>
<SimpleSource>
<SourceFilename relativeToVRT="1">./int8.tif</SourceFilename>
<SourceBand>1</SourceBand>
<SourceProperties RasterXSize="24" RasterYSize="24" DataType="Int8" BlockXSize="24" BlockYSize="24" />
<SrcRect xOff="0" yOff="0" xSize="24" ySize="24" />
<DstRect xOff="0" yOff="0" xSize="24" ySize="24" />
</SimpleSource>
</VRTRasterBand>
</VRTDataset>
42 changes: 42 additions & 0 deletions raster/r.in.gdal/testsuite/test_r_in_gdal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
@author Soeren Gebbert
"""

import unittest

from subprocess import check_output

from grass.gunittest.case import TestCase


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 66389f0

Please sign in to comment.