Skip to content

Commit

Permalink
Add GDALNearblack binding (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
pericles-tpt authored Sep 28, 2023
1 parent b8c08e0 commit 4a1ba9d
Show file tree
Hide file tree
Showing 6 changed files with 413 additions and 2 deletions.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func ErrLogger(fn ErrorHandler) interface {
SetStatisticsOption
ClearStatisticsOption
GridOption
NearblackOption
} {
return errorCallback{fn}
}
Expand Down Expand Up @@ -368,6 +369,9 @@ func (ec errorCallback) setGridCreateOpt(o *gridCreateOpts) {
func (ec errorCallback) setGridOpt(o *gridOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setNearblackOpt(o *nearBlackOpts) {
o.errorHandler = ec.fn
}

type multiError struct {
errs []error
Expand Down
21 changes: 21 additions & 0 deletions godal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,27 @@ GDALDatasetH godalGrid(cctx *ctx, const char *pszDest, GDALDatasetH hSrcDS, char
forceError(ctx);
}

godalUnwrap();
return ret;
}

GDALDatasetH godalNearblack(cctx *ctx, const char *pszDest, GDALDatasetH hDstDS, GDALDatasetH hSrcDS, char **switches) {
godalWrap(ctx);

GDALNearblackOptions *nbopts = GDALNearblackOptionsNew(switches,nullptr);
if(failed(ctx)) {
GDALNearblackOptionsFree(nbopts);
godalUnwrap();
return nullptr;
}

int usageErr=0;
GDALDatasetH ret = GDALNearblack(pszDest, hDstDS, hSrcDS, nbopts, &usageErr);
GDALNearblackOptionsFree(nbopts);
if(ret==nullptr || usageErr!=0) {
forceError(ctx);
}

godalUnwrap();
return ret;
}
78 changes: 76 additions & 2 deletions godal.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (band Band) ClearNoData(opts ...SetNoDataOption) error {
return cgc.close()
}

//SetScaleOffset sets the band's scale and offset
// SetScaleOffset sets the band's scale and offset
func (band Band) SetScaleOffset(scale, offset float64, opts ...SetScaleOffsetOption) error {
setterOpts := &setScaleOffsetOpts{}
for _, opt := range opts {
Expand Down Expand Up @@ -891,7 +891,7 @@ func (ds *Dataset) SetNoData(nd float64, opts ...SetNoDataOption) error {
return cgc.close()
}

//SetScale sets the band's scale and offset
// SetScaleOffset sets the band's scale and offset
func (ds *Dataset) SetScaleOffset(scale, offset float64, opts ...SetScaleOffsetOption) error {
setterOpts := &setScaleOffsetOpts{}
for _, opt := range opts {
Expand Down Expand Up @@ -3849,6 +3849,80 @@ func (ds *Dataset) Grid(destPath string, switches []string, opts ...GridOption)
return &Dataset{majorObject{C.GDALMajorObjectH(dsRet)}}, nil
}

// Nearblack runs the library version of nearblack
//
// See the nearblack doc page to determine the valid flags/opts that can be set in switches.
//
// Example switches :
//
// []string{"-white", "-near", "10"}
//
// Creation options and driver may be set in the switches slice with
//
// switches:=[]string{"-co","TILED=YES","-of","GTiff"}
//
// NOTE: Some switches are NOT compatible with this binding, as a `nullptr` is passed to a later call to
// `GDALNearblackOptionsNew()` (as the 2nd argument). Those switches are: "-o", "-q", "-quiet"
func (ds *Dataset) Nearblack(dstDS string, switches []string, opts ...NearblackOption) (*Dataset, error) {
nearBlackOpts := nearBlackOpts{}
for _, opt := range opts {
opt.setNearblackOpt(&nearBlackOpts)
}

cswitches := sliceToCStringArray(switches)
defer cswitches.free()

dest := unsafe.Pointer(C.CString(dstDS))
defer C.free(dest)

cgc := createCGOContext(nil, nearBlackOpts.errorHandler)

ret, err := C.godalNearblack(cgc.cPointer(), (*C.char)(dest), nil, ds.handle(), cswitches.cPointer())
if err = cgc.close(); err != nil {
return nil, err
}

return &Dataset{majorObject{C.GDALMajorObjectH(ret)}}, nil
}

// NearblackInto writes the provided `sourceDs` into the Dataset that this method was called on, and
// runs the library version of nearblack.
//
// See the nearblack doc page to determine the valid flags/opts that can be set in switches.
//
// Example switches :
//
// []string{"-white", "-near", "10"}
//
// Creation options and driver may be set in the switches slice with
//
// switches:=[]string{"-co","TILED=YES","-of","GTiff"}
//
// NOTE: Some switches are NOT compatible with this binding, as a `nullptr` is passed to a later call to
// `GDALNearblackOptionsNew()` (as the 2nd argument). Those switches are: "-o", "-q", "-quiet"
func (ds *Dataset) NearblackInto(sourceDs *Dataset, switches []string, opts ...NearblackOption) error {
nearBlackOpts := nearBlackOpts{}
for _, opt := range opts {
opt.setNearblackOpt(&nearBlackOpts)
}

cswitches := sliceToCStringArray(switches)
defer cswitches.free()

cgc := createCGOContext(nil, nearBlackOpts.errorHandler)

var srcDsHandle C.GDALDatasetH = nil
if sourceDs != nil {
srcDsHandle = sourceDs.handle()
}
_ = C.godalNearblack(cgc.cPointer(), nil, ds.handle(), srcDsHandle, cswitches.cPointer())
if err := cgc.close(); err != nil {
return err
}

return nil
}

type cgoContext struct {
cctx *C.cctx
opts cStringArray
Expand Down
1 change: 1 addition & 0 deletions godal.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ extern "C" {
void godalSetRasterStatistics(cctx *ctx, GDALRasterBandH bnd, double dfMin, double dfMax, double dfMean, double dfStdDev);
void godalGridCreate(cctx *ctx, char *pszAlgorithm, GDALGridAlgorithm eAlgorithm, GUInt32 nPoints, const double *padfX, const double *padfY, const double *padfZ, double dfXMin, double dfXMax, double dfYMin, double dfYMax, GUInt32 nXSize, GUInt32 nYSize, GDALDataType eType, void *pData);
GDALDatasetH godalGrid(cctx *ctx, const char *pszDest, GDALDatasetH hSrcDS, char **switches);
GDALDatasetH godalNearblack(cctx *ctx, const char *pszDest, GDALDatasetH hDstDS, GDALDatasetH hSrcDS, char **switches);
#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 4a1ba9d

Please sign in to comment.