How godal works and its API design
mainGodal is an idiomatic Go wrapper around the GDAL library designed to minimize the overhead of Cgo calls. It follows these design principles:
- Idiomatic Error Handling: Function calls return a result and an
error. The result is only valid iferr == nil. The error message contains the root cause from GDAL. - Reduced Cgo Overhead: Instead of exposing every individual GDAL C function, Godal groups frequently used calls into single Cgo functions. This allows you to retrieve complex structures (like dataset dimensions or band information) in a single call rather than multiple individual calls.
- Option Pattern: Unfrequently used or non-default parameters are passed using an options pattern (e.g.,
godal.Update()). - VSI Support: Godal exposes a VSI handler that allows you to treat an
io.ReaderAtas a filename that GDAL can open. This is useful for integrating cloud storage (likegs://URIs viaosio) directly into GDAL workflows.
// Example of the idiomatic grouping pattern
hDS, err := godal.Open(filename)
if err != nil {
panic(err)
}
// Instead of multiple C calls, use Structure() to get metadata at once
structure := hDS.Structure()
fmt.Printf("dataset size: %dx%dx%d\n", structure.SizeX, structure.SizeY, structure.NBands)
for _, band := range hDS.Bands() {
for o, ovr := range band.Overviews() {
bstruct := ovr.Structure()
fmt.Printf("overview %d size: %dx%d\n", o, bstruct.SizeX, bstruct.SizeY)
}
}