Skip to content

Commit

Permalink
Add Int8 support (since gdal>=3.7.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvaroquauxads committed Jul 11, 2024
1 parent b0a20d0 commit 4137d35
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
13 changes: 13 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,19 @@ func Example_rasterTutorial() {
poDstDS.Bands()[0].Write(0, 0, abyRaster, 512, 512)
poDstDS.Close()

poDstDSInt8, err := godal.Create(godal.GTiff, pszDstFilename, 1, godal.Int8, 512, 512)
if godal.CheckMinVersion(3, 7, 0) {
if err != nil {
panic(err)
}
defer poDstDSInt8.Close() //Close can be defered / called more than once (second+ calls are no-ops)

// ... now populate with data
poDstDSInt8.Bands()[0].Write(0, 0, abyRaster, 512, 512)
poDstDSInt8.Close()
} else if err == nil {
panic("godal.Int8 is not supported with GDAL<3.7.0, but godal.Create returns no error")
}
// Output:
// Size is 10x10x3
// Projection is 'GEOGCS["WGS 84",DATU...'
Expand Down
10 changes: 9 additions & 1 deletion godal.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const (
Byte = DataType(C.GDT_Byte)
//UInt16 DataType
UInt16 = DataType(C.GDT_UInt16)
//Int8 DataType (GDAL>=3.7.0)
// [RFC 87]: https://gdal.org/development/rfc/rfc87_signed_int8.html
Int8 = DataType(C.GDT_Int8)
//Int16 DataType
Int16 = DataType(C.GDT_Int16)
//UInt32 DataType
Expand Down Expand Up @@ -89,7 +92,7 @@ func (dtype DataType) String() string {
// Size retruns the number of bytes needed for one instance of DataType
func (dtype DataType) Size() int {
switch dtype {
case Byte:
case Byte, Int8:
return 1
case Int16, UInt16:
return 2
Expand Down Expand Up @@ -1780,6 +1783,8 @@ func bufferType(buffer interface{}) DataType {
switch buffer.(type) {
case []byte:
return Byte
case []int8:
return Int8
case []int16:
return Int16
case []uint16:
Expand Down Expand Up @@ -1813,6 +1818,9 @@ func cBuffer(buffer interface{}, minsize int) unsafe.Pointer {
case []byte:
sizecheck(len(buf))
return unsafe.Pointer(&buf[0])
case []int8:
sizecheck(len(buf))
return unsafe.Pointer(&buf[0])
case []int16:
sizecheck(len(buf))
return unsafe.Pointer(&buf[0])
Expand Down
4 changes: 4 additions & 0 deletions godal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

#if GDAL_VERSION_NUM < 3000000
#error "this code is only compatible with gdal version >= 3.0"
#elif GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(3, 7, 0)
typedef enum {
/*! 8-bit signed integer (GDAL >= 3.7) */ GDT_Int8 = 14
} FutureGDALDataType;
#endif

#ifdef __cplusplus
Expand Down
6 changes: 6 additions & 0 deletions godal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func TestCBuffer(t *testing.T) {
assert.Equal(t, 1, bufferType(buf).Size())
assert.Panics(t, func() { cBuffer(buf, 101) })

buf = make([]int8, 100)
_ = cBuffer(buf, 100)
assert.Equal(t, Int8, bufferType(buf))
assert.Equal(t, 1, bufferType(buf).Size())
assert.Panics(t, func() { cBuffer(buf, 101) })

buf = make([]int16, 100)
_ = cBuffer(buf, 100)
assert.Equal(t, Int16, bufferType(buf))
Expand Down

0 comments on commit 4137d35

Please sign in to comment.