Skip to content

Commit

Permalink
added DEMProcessing utility func
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfelian committed Mar 4, 2021
1 parent c9883f7 commit bf52640
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Binary file added testdata/demproc.tif
Binary file not shown.
6 changes: 6 additions & 0 deletions testdata/demproc_colors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
2000 white
100% 235:220:175
50% 190 185 135
4 240 250 150
0 50 180 50
nv 0 0 0 0
35 changes: 35 additions & 0 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,40 @@ func Rasterize(dstDS string, sourceDS Dataset, options []string) (Dataset, error
return Dataset{}, fmt.Errorf("rasterize failed with code %d", cerr)
}
return Dataset{ds}, nil
}

func DEMProcessing(dstDS string, sourceDS Dataset, processing string, colorFileName string, options []string) (Dataset, error) {
if dstDS == "" {
dstDS = "MEM:::"
if !stringArrayContains(options, "-f") {
options = append([]string{"-of", "MEM"}, options...)
}
}
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
demprocessingopts := C.GDALDEMProcessingOptionsNew(
(**C.char)(unsafe.Pointer(&opts[0])),
(*C.GDALDEMProcessingOptionsForBinary)(unsafe.Pointer(nil)))
defer C.GDALDEMProcessingOptionsFree(demprocessingopts)

var cerr C.int
cdstDS := C.CString(dstDS)
defer C.free(unsafe.Pointer(cdstDS))

cprocessing := C.CString(processing)
defer C.free(unsafe.Pointer(cprocessing))
ds := C.GDALDEMProcessing(cdstDS,
sourceDS.cval,
cprocessing,
demprocessingopts,
&cerr)
if cerr != 0 {
return Dataset{}, fmt.Errorf("demprocessing failed with code %d", cerr)
}
return Dataset{ds}, nil
}
21 changes: 21 additions & 0 deletions utilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,24 @@ func TestTranslate(t *testing.T) {
}
dstDS.Close()
}

func TestDEMProcessing(t *testing.T) {
srcDS, err := Open("testdata/demproc.tif", ReadOnly)
if err != nil {
t.Errorf("Open: %v", err)
}

opts := []string{"-of", "GTiff"}

dstDS, err := DEMProcessing("/tmp/demproc_output.tif", srcDS, "color-relief", "testdata/demproc_colors.txt", opts)
if err != nil {
t.Errorf("DEMProcessing: %v", err)
}
dstDS.Close()

dstDS, err = Open("/tmp/demproc_output.tif", ReadOnly)
if err != nil {
t.Errorf("Open after raster DEM Processing: %v", err)
}
dstDS.Close()
}

0 comments on commit bf52640

Please sign in to comment.