Skip to content

Commit

Permalink
feat: implement SetSearchPaths
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mfbonfigli authored and twpayne committed Nov 2, 2024
1 parent 7a33fe0 commit 6d25e8c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
16 changes: 16 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
15 changes: 15 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
}

0 comments on commit 6d25e8c

Please sign in to comment.