Skip to content

Commit

Permalink
Add int8 (since gdal 3.7.0) (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentvaroquauxads authored Jul 11, 2024
1 parent b0a20d0 commit d2a4ab0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
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
6 changes: 6 additions & 0 deletions godal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
#error "this code is only compatible with gdal version >= 3.0"
#endif

#if 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
extern "C" {
#endif
Expand Down
19 changes: 19 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 Expand Up @@ -223,6 +229,19 @@ func TestCreate(t *testing.T) {
if st1.Size() == st2.Size() {
t.Errorf("sizes: %d/%d", st1.Size(), st2.Size())
}

ehc = eh()
tmpname3 := tempfile()
defer os.Remove(tmpname3)
ds, err = Create(GTiff, tmpname3, 1, Int8, 20, 20, ErrLogger(ehc.ErrorHandler))
if CheckMinVersion(3, 7, 0) {
assert.NoError(t, err)

err = ds.Close(ErrLogger(ehc.ErrorHandler))
assert.NoError(t, err)
} else {
assert.Error(t, err, "godal.Int8 is not supported with GDAL<3.7.0, but godal.Create did not return an error")
}
}

func TestRegisterDrivers(t *testing.T) {
Expand Down

0 comments on commit d2a4ab0

Please sign in to comment.