-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (94 loc) · 2.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"fmt"
"image/png"
"os"
"os/exec"
"path"
"strings"
)
func realMain() error {
x := flag.Uint("x", 0, "x coordinate (default 0)")
y := flag.Uint("y", 0, "y coordinate (default 0)")
w := flag.Uint("w", 0, "width (default max)")
h := flag.Uint("h", 0, "height (default max)")
s := flag.Float64("s", 1, "scale vertically (default 1)")
zero := flag.Float64("z", 10, "translate the model down so that this is the lowest height")
diff := flag.String("d", "", "a second file to compare against")
visualize := flag.Bool("v", false, "visualize the model")
output := flag.String("output", "out.stl", "output STL file (default out.stl)")
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s [OPTIONS] <input geotiff file>:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if path.Ext(*output) != ".stl" && path.Ext(*output) != ".png" {
return fmt.Errorf("unsupported output format")
}
fmt.Println(flag.NArg())
switch flag.NArg() {
case 0:
flag.Usage()
return fmt.Errorf("no input file given")
case 1:
// Great
default:
flag.Usage()
return fmt.Errorf("unrecognised arguments %s", strings.Join(flag.Args()[1:], ", "))
}
input := flag.Arg(0)
pb, err := FromGeoTIFF(input, *x, *y, *w, *h)
if err != nil {
return err
}
if *diff != "" {
pb2, err := FromGeoTIFF(*diff, *x, *y, *w, *h)
if err != nil {
return err
}
pb.Diff(pb2)
}
fmt.Printf("Setting minimum height value to %f...", *zero)
pb.Zero(float32(*zero))
fmt.Println("done")
if *s != 1.0 {
fmt.Printf("Adjusting vertical scale by factor of %f...", *s)
pb.Scale(float32(*s))
fmt.Println("done")
}
switch path.Ext(*output) {
case ".stl":
fmt.Printf("Converting to STL file '%s'...", *output)
err = pb.toSTL().WriteFile(*output)
if err != nil {
return err
}
fmt.Println("done")
if *visualize {
fmt.Println("Launching visualisation")
return exec.Command("f3d", *output).Run()
}
case ".png":
fmt.Printf("Converting to PNG file '%s'...", *output)
img := pb.ToImage()
f, err := os.Create(*output)
if err != nil {
return err
}
defer f.Close()
err = png.Encode(f, img)
if err != nil {
return err
}
fmt.Println("done")
}
return nil
}
func main() {
err := realMain()
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
}