Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configurable error level handling and custom loggers #20

Merged
merged 8 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ package godal_test

import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
Expand Down Expand Up @@ -514,3 +516,39 @@ func Example_vectorTutorial() {
// geom: POLYGON ((100 0,101 0,101 1,100 1,100 0))
// created geometry { "type": "Point", "coordinates": [ 1.0, 1.0 ] }
}

//ExampleErrorHandler_sentinel is an example to make godal.Open return a specific golang
//error when the gdal emitted error/log matches certain criteria
func ExampleErrorHandler_sentinel() {
sentinel := errors.New("noent")
eh := func(ec godal.ErrorCategory, code int, msg string) error {
/* do some advanced checking of ec, code and msg to determine if this is an actual error */
haveError := true
if !haveError {
log.Println(msg)
return nil
}
return sentinel
}
_, err := godal.Open("nonexistent.tif", godal.ErrLogger(eh))
if err == sentinel {
fmt.Println(err.Error())
}

// Output:
// noent
}

//ExampleErrorHandler_warnings is an example to set up an error handler that ignores gdal warnings
func ExampleErrorHandler_warnings() {
eh := func(ec godal.ErrorCategory, code int, msg string) error {
if ec <= godal.CE_Warning {
log.Println(msg)
return nil
}
return fmt.Errorf("GDAL %d: %s", code, msg)
}
_, err := godal.Open("nonexistent.tif", godal.ErrLogger(eh))
// err if returned will not arise from a gdal warning
_ = err
}
138 changes: 138 additions & 0 deletions driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package godal

//DriverName is GDAL driver
type DriverName string

const (
//GTiff GeoTIFF
GTiff DriverName = "GTiff"
//GeoJSON RFCxxxx geojson
GeoJSON DriverName = "GeoJSON"
//Memory in memory driver
Memory DriverName = "Memory"
//VRT is a VRT
VRT DriverName = "VRT"
//Shapefile is an ESRI Shapefile
Shapefile DriverName = "ESRI Shapefile"
//GeoPackage is a geo-package
GeoPackage DriverName = "GPKG"
//JP2KAK is a Kakadu Jpeg2000
JP2KAK DriverName = "JP2KAK"
//OpenJPEG is an OpenJPEG JPEG2000
OpenJPEG DriverName = "OpenJPEG"
//DIMAP is a Dimap
DIMAP DriverName = "DIMAP"
//HFA is an erdas img
HFA DriverName = "HFA"
//Mitab is a mapinfo mif/tab file
Mitab DriverName = "Mitab"
)

type driverMapping struct {
rasterName string
vectorName string
rasterRegister string
vectorRegister string
}

var driverMappings = map[DriverName]driverMapping{
GTiff: {
rasterName: "GTiff",
rasterRegister: "GDALRegister_GTiff",
},
Memory: {
rasterName: "MEM",
vectorName: "Memory",
rasterRegister: "GDALRegister_MEM",
vectorRegister: "RegisterOGRMEM",
},
GeoJSON: {
vectorName: "GeoJSON",
vectorRegister: "RegisterOGRGeoJSON",
},
VRT: {
rasterName: "VRT",
vectorName: "OGR_VRT",
rasterRegister: "GDALRegister_VRT",
vectorRegister: "RegisterOGRVRT",
},
Shapefile: {
vectorName: "ESRI Shapefile",
vectorRegister: "RegisterOGRShape",
},
GeoPackage: {
rasterName: "GPKG",
vectorName: "GPKG",
rasterRegister: "RegisterOGRGeoPackage",
vectorRegister: "RegisterOGRGeoPackage",
},
JP2KAK: {
rasterName: "JP2KAK",
rasterRegister: "GDALRegister_JP2KAK",
},
OpenJPEG: {
rasterName: "OpenJPEG",
rasterRegister: "GDALRegister_JP2OpenJPEG",
},
DIMAP: {
rasterName: "DIMAP",
rasterRegister: "GDALRegister_DIMAP",
},
HFA: {
rasterName: "HFA",
rasterRegister: "GDALRegister_HFA",
},
Mitab: {
vectorName: "Mapinfo File",
vectorRegister: "RegisterOGRTAB",
},
}

func (dn DriverName) setDatasetVectorTranslateOpt(to *dsVectorTranslateOpts) {
to.driver = dn
}

func (dn DriverName) setDatasetTranslateOpt(to *dsTranslateOpts) {
to.driver = dn
}

func (dn DriverName) setDatasetWarpOpt(to *dsWarpOpts) {
to.driver = dn
}

func (dn DriverName) setRasterizeOpt(to *rasterizeOpts) {
to.driver = dn
}

type driversOpt struct {
drivers []string
}

//Drivers specifies the list of drivers that are allowed to try opening the dataset
func Drivers(drivers ...string) interface {
OpenOption
} {
return driversOpt{drivers}
}
func (do driversOpt) setOpenOpt(oo *openOpts) {
oo.drivers = append(oo.drivers, do.drivers...)
}

type driverOpenOption struct {
oo []string
}

//DriverOpenOption adds a list of Open Options (-oo switch) to the open command. Each keyval must
//be provided in a "KEY=value" format
func DriverOpenOption(keyval ...string) interface {
OpenOption
BuildVRTOption
} {
return driverOpenOption{keyval}
}
func (doo driverOpenOption) setOpenOpt(oo *openOpts) {
oo.options = append(oo.options, doo.oo...)
}
func (doo driverOpenOption) setBuildVRTOpt(bvo *buildVRTOpts) {
bvo.openOptions = append(bvo.openOptions, doo.oo...)
}
Loading