From 6d25e8cd8d40e511cf88f4398fa14e2aaa96727c Mon Sep 17 00:00:00 2001 From: mfbonfigli Date: Sat, 2 Nov 2024 18:27:54 +0000 Subject: [PATCH] feat: implement SetSearchPaths Implements SetSearchPaths matching the C function proj_context_set_search_paths, which allows to set the locations that PROJ will use to search the PROJ data files. --- context.go | 16 ++++++++++++++++ context_test.go | 15 +++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/context.go b/context.go index fd11638c..4bf2ab9b 100644 --- a/context.go +++ b/context.go @@ -39,6 +39,22 @@ func (c *Context) Destroy() { } } +// SetSearchPaths sets the paths PROJ should be exploring to find the PROJ Data files. +func (c *Context) SetSearchPaths(paths []string) { + c.Lock() + defer c.Unlock() + cPaths := make([]*C.char, len(paths)) + var pathPtr unsafe.Pointer + for i, path := range paths { + cPaths[i] = C.CString(path) + defer C.free(unsafe.Pointer(cPaths[i])) + } + if len(paths) > 0 { + pathPtr = unsafe.Pointer(&cPaths[0]) + } + C.proj_context_set_search_paths(c.pjContext, C.int(len(cPaths)), (**C.char)(pathPtr)) +} + func (c *Context) Lock() { c.mutex.Lock() } diff --git a/context_test.go b/context_test.go index e9595793..34467d25 100644 --- a/context_test.go +++ b/context_test.go @@ -139,3 +139,18 @@ func TestContext_NewFromArgs(t *testing.T) { }) } } + +func TestContext_SetSearchPaths(t *testing.T) { + defer runtime.GC() + + context := proj.NewContext() + assert.NotZero(t, context) + + // The C function does not return any error so we only validate + // that executing the SetSearchPaths function call + // does not panic considering various boundary conditions + context.SetSearchPaths(nil) + context.SetSearchPaths([]string{}) + context.SetSearchPaths([]string{"/tmp/data"}) + context.SetSearchPaths([]string{"/tmp/data", "/tmp/data2"}) +}