Skip to content

Commit

Permalink
add RegisterPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tbonfort committed Oct 3, 2023
1 parent 933f4b2 commit 9129bd2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func ErrLogger(fn ErrorHandler) interface {
ClearStatisticsOption
GridOption
NearblackOption
RegisterPluginOption
} {
return errorCallback{fn}
}
Expand Down Expand Up @@ -372,6 +373,9 @@ func (ec errorCallback) setGridOpt(o *gridOpts) {
func (ec errorCallback) setNearblackOpt(o *nearBlackOpts) {
o.errorHandler = ec.fn
}
func (ec errorCallback) setRegisterPluginOpt(o *registerPluginOpts) {
o.errorHandler = ec.fn
}

type multiError struct {
errs []error
Expand Down
13 changes: 13 additions & 0 deletions godal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,19 @@ void godalRegisterPlugins(){
#endif
}

void godalRegisterPlugin(cctx *ctx, const char *name){
godalWrap(ctx);
#if GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(3, 8, 0)
CPLErr ret = GDALRegisterPlugin(name);
if (ret != 0) {
forceCPLError(ctx, ret);
}
#else
CPLError(CE_Failure, CPLE_NotSupported, "GDALRegisterPlugin is only supported in GDAL version >= 3.8");
#endif
godalUnwrap();
}

GDALDatasetH godalCreate(cctx *ctx, GDALDriverH drv, const char* name, int width, int height, int nbands,
GDALDataType dtype, char **options) {
godalWrap(ctx);
Expand Down
14 changes: 13 additions & 1 deletion godal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,10 +1253,22 @@ func RegisterAll() {
C.GDALAllRegister()
}

func GDALRegisterPlugins() {
func RegisterPlugins() {
C.godalRegisterPlugins()
}

func RegisterPlugin(name string, opts ...RegisterPluginOption) error {
ro := registerPluginOpts{}
for _, o := range opts {
o.setRegisterPluginOpt(&ro)
}
cgc := createCGOContext(nil, ro.errorHandler)
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
C.godalRegisterPlugin(cgc.cPointer(), cname)
return cgc.close()
}

// RegisterRaster registers a raster driver by name.
//
// Calling RegisterRaster(DriverName) with one of the predefined DriverNames provided by the library will
Expand Down
1 change: 1 addition & 0 deletions godal.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ extern "C" {
void godalClose(cctx *ctx, GDALDatasetH ds);
int godalRegisterDriver(const char *funcname);
void godalRegisterPlugins();
void godalRegisterPlugin(cctx *ctx, const char *name);
void godalRasterSize(GDALDatasetH ds, int *xsize, int *ysize);

//returns a null terminated list of bands. the caller must free the returned list
Expand Down
18 changes: 18 additions & 0 deletions godal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,24 @@ func TestRegisterDrivers(t *testing.T) {
assert.True(t, ok)
_, ok = VectorDriver("Mapinfo File")
assert.True(t, ok)

runtimeVersion := Version()
supported := runtimeVersion.Major() > 3 ||
(runtimeVersion.Major() == 3 && runtimeVersion.Minor() >= 8)

ehc := eh()
err = RegisterPlugin("foobarsljgsa", ErrLogger(ehc.ErrorHandler))
assert.Error(t, err)
if !supported {
assert.Contains(t, err.Error(), "GDALRegisterPlugin is only supported")
}
err = RegisterPlugin("foobarsljgsa")
assert.Error(t, err)
err = RegisterPlugin("dehazer")
assert.NoError(t, err)

//smoke test for RegisterPlugins
RegisterPlugins()
}

func TestVectorCreate(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,15 @@ type NearblackOption interface {
setNearblackOpt(nbOpt *nearBlackOpts)
}

type registerPluginOpts struct {
errorHandler ErrorHandler
}

// RegisterPluginOption is an option that can be passed to RegisterPlugin()
type RegisterPluginOption interface {
setRegisterPluginOpt(rpOpt *registerPluginOpts)
}

// RasterizeGeometryOption is an option that can be passed tp Dataset.RasterizeGeometry()
type RasterizeGeometryOption interface {
setRasterizeGeometryOpt(o *rasterizeGeometryOpts)
Expand Down

0 comments on commit 9129bd2

Please sign in to comment.