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

Implemented GDALGridCreate CGo wrapper for all available algorithms #51

Merged
merged 9 commits into from
Jun 3, 2020
Prev Previous commit
Next Next commit
implementing and debugging GDALGridCreate wrapper...
  • Loading branch information
mtfelian committed Jun 2, 2020
commit 003e1ac10397d611946f877ec9c0457e65500abe
4 changes: 4 additions & 0 deletions algorithms.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ func CreateGrid(

arg := &goGDALProgressFuncProxyArgs{progress, data}

fmt.Println(">> calling C.GDALGridCreate")
fmt.Printf("lx=%.5f, hx=%.5f, ly=%.5f, hy=%.5f, nx=%d, ny=%d\n", lx, hx, ly, hy, nX, nY)
return C.GDALGridCreate(
C.GDALGridAlgorithm(algo),
unsafe.Pointer(&ooptions[0]),
Expand All @@ -398,6 +400,8 @@ func CreateGrid(
C.uint(nY),
C.GDALDataType(dataType),
dataPtr,
//unsafe.Pointer(nil),
//unsafe.Pointer(nil),
C.goGDALProgressFuncProxyB(),
unsafe.Pointer(arg),
).Err()
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ A simple program to create a georeferenced blank 256x256 GeoTIFF:
flag.Parse()
filename := flag.Arg(0)
if filename == "" {
fmt.Printf("Usage: test_tiff [filename]\n")
fmt.Printf("Usage: tiff [filename]\n")
return
}
buffer := make([]uint8, 256 * 256)
Expand Down
90 changes: 90 additions & 0 deletions examples/grid/grid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"strconv"
"strings"

"github.com/mtfelian/gdal"
)

func readFile(filename string) (x, y, z []float64, err error) {
var b []byte
if b, err = ioutil.ReadFile(filename); err != nil {
return
}
s := string(b)
arr := strings.Split(s, "\n")
x, y, z = make([]float64, len(arr)), make([]float64, len(arr)), make([]float64, len(arr))
for i, el := range arr {
xyz := strings.Split(el, ",")
if len(xyz) != 3 {
err = errors.New("wrong input file format, should be CSV with 3 columns: y,x,z")
}

if y[i], err = strconv.ParseFloat(xyz[0], 64); err != nil {
return
}
if x[i], err = strconv.ParseFloat(xyz[1], 64); err != nil {
return
}
if z[i], err = strconv.ParseFloat(xyz[2], 64); err != nil {
return
}
}
return
}

func writeFile(filename string, z []float64) error {
var s string
for i := range z {
s += fmt.Sprintf("%.5f\n", z[i])
}
return ioutil.WriteFile(filename, []byte(s), 0644)
}

func main() {
flag.Parse()
filename := flag.Arg(0)
if filename == "" {
fmt.Printf("Usage: grid [filename]\n")
return
}
fmt.Printf("Filename: %s\n", filename)

fmt.Printf("Reading file\n")
x, y, z, err := readFile(filename)
if err != nil {
fmt.Println(err.Error())
return
}
//var nX, nY uint = 420, 470
var nX, nY uint = 420, 470

fmt.Printf("Allocating buffer\n")
buffer := make([]float64, nX*nY)
fmt.Printf("Calling gdal.CreateGrid\n")
if err := gdal.CreateGrid(
gdal.GGA_Linear,
[]string{"0", "0.0"},
x, y, z,
nX, nY,
buffer,
gdal.DummyProgress,
nil,
); err != nil {
fmt.Println("gdal.CreateGrid error: ", err.Error())
return
}

fmt.Printf("Writing file\n")
if err := writeFile(filename+".out", buffer); err != nil {
fmt.Println(err.Error())
return
}

fmt.Printf("End program\n")
}
2 changes: 1 addition & 1 deletion examples/tiff/tiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {
flag.Parse()
filename := flag.Arg(0)
if filename == "" {
fmt.Printf("Usage: test_tiff [filename]\n")
fmt.Printf("Usage: tiff [filename]\n")
return
}
fmt.Printf("Filename: %s\n", filename)
Expand Down
4 changes: 2 additions & 2 deletions examples/translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func main() {
outputFile := flag.Arg(1)
options := flag.Args()[2:]
if inputFile == "" {
fmt.Printf("Usage: test_translate inputFile outputFile options\n")
fmt.Printf("Usage: translate inputFile outputFile options\n")
return
}
fmt.Printf("Input filename: %s\n", inputFile)
if outputFile == "" {
fmt.Printf("Usage: test_translate inputFile outputFile options\n")
fmt.Printf("Usage: translate inputFile outputFile options\n")
return
}
fmt.Printf("Output filename: %s\n", outputFile)
Expand Down
6 changes: 3 additions & 3 deletions examples/warp/warp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ func main() {
inputFile := flag.Arg(0)
outputFile := flag.Arg(1)
if len(flag.Args()) < 3 {
fmt.Printf("Usage: test_warp inputFile outputFile options\n")
fmt.Printf("Usage: warp inputFile outputFile options\n")
return
}
options := flag.Args()[2:]
if inputFile == "" {
fmt.Printf("Usage: test_warp inputFile outputFile options\n")
fmt.Printf("Usage: warp inputFile outputFile options\n")
return
}
fmt.Printf("Input filename: %s\n", inputFile)
if outputFile == "" {
fmt.Printf("Usage: test_warp inputFile outputFile options\n")
fmt.Printf("Usage: warp inputFile outputFile options\n")
return
}
fmt.Printf("Output filename: %s\n", outputFile)
Expand Down