-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
319 lines (294 loc) · 8.63 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"time"
"github.com/mfbonfigli/gocesiumtiler/v2/internal/utils"
"github.com/mfbonfigli/gocesiumtiler/v2/tiler"
"github.com/mfbonfigli/gocesiumtiler/v2/tiler/mutator"
"github.com/mfbonfigli/gocesiumtiler/v2/version"
"github.com/urfave/cli/v2"
)
// this global variable controls the tiler that will be used. Useful to inject mocks during tests.
var tilerProvider func() (tiler.Tiler, error) = func() (tiler.Tiler, error) {
return tiler.NewGoCesiumTiler()
}
var cmdVersion = "2.0.1"
// GitCommit is injected dynamically at build time via `go build -ldflags "-X main.GitCommit=XYZ"`
var GitCommit string = "(na)"
const logo = `
_ _ _ _
__ _ ___ ___ ___ ___(_)_ _ _ __ ___ | |_(_) | ___ _ __
/ _ |/ _ \ / __/ _ \/ __| | | | | '_ _ \| __| | |/ _ \ '__|
| (_| | (_) | (_| __/\__ \ | |_| | | | | | | |_| | | __/ |
\__, |\___/ \___\___||___/_|\__,_|_| |_| |_|\__|_|_|\___|_|
__| | A Cesium Point Cloud tile generator written in golang
|___/ Copyright YYYY - Massimo Federico Bonfigli
build: ZZZZ
`
var profilerEnabled = false
func main() {
printBanner()
getCli(defaultCliOptions()).Run(os.Args)
}
func getVersion() string {
return fmt.Sprintf("%s-%s", cmdVersion, GitCommit)
}
func getCli(c *cliOpts) *cli.App {
return &cli.App{
Name: "gocesiumtiler",
Usage: "transforms LAS files into Cesium.JS 3D Tiles",
Version: getVersion(),
Commands: []*cli.Command{
{
Name: "file",
Usage: "convert a LAS file into 3D tiles",
Flags: getFileFlags(c),
Action: func(cCtx *cli.Context) error {
fileCommand(c, cCtx.Args().First())
return nil
},
},
{
Name: "folder",
Usage: "convert all LAS files in a folder file into 3D tiles",
Flags: getFolderFlags(c),
Action: func(cCtx *cli.Context) error {
folderCommand(c, cCtx.Args().First())
return nil
},
},
},
EnableBashCompletion: true,
}
}
func getFileFlags(c *cliOpts) []cli.Flag {
return getFlags(c)
}
func getFolderFlags(c *cliOpts) []cli.Flag {
stdFlags := getFlags(c)
joinFlag := &cli.BoolFlag{
Name: "join",
Aliases: []string{"j"},
Value: c.join,
Usage: "merge the input LAS files in the folder into a single cloud. The LAS files must have the same properties (CRS etc)",
Destination: &c.join,
}
return append(stdFlags, joinFlag)
}
func getFlags(c *cliOpts) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "out",
Aliases: []string{"o"},
Value: c.output,
Usage: "full path of the output folder where to save the resulting Cesium tilesets",
Destination: &c.output,
},
&cli.StringFlag{
Name: "crs",
Aliases: []string{"e", "epsg"},
Value: c.crs,
Usage: "String representing the input CRS. For example, EPSG:4326 or a generic Proj4 string. Bare numbers will be interpreted as EPSG codes. If empty the system will attempt to autodetect the CRS from the LAS metadata. In case of multiple LAS files, the CRS must be consistent else an error will be thrown.",
Destination: &c.crs,
},
&cli.Float64Flag{
Name: "resolution",
Aliases: []string{"r"},
Value: c.resolution,
Usage: "minimum resolution of the 3d tiles, in meters. approximately represets the maximum sampling distance between any two points at the lowest level of detail",
Destination: &c.resolution,
},
&cli.Float64Flag{
Name: "z-offset",
Aliases: []string{"z"},
Value: c.zOffset,
Usage: "z offset to apply to the point, in meters. only use it if the input elevation is referred to the WGS84 ellipsoid or geoid",
Destination: &c.zOffset,
},
&cli.IntFlag{
Name: "depth",
Aliases: []string{"d"},
Value: c.maxDepth,
Usage: "maximum depth of the output tree.",
Destination: &c.maxDepth,
},
&cli.IntFlag{
Name: "min-points-per-tile",
Aliases: []string{"m"},
Value: c.minPoints,
Usage: "minimum number of points to enforce in each 3D tile",
Destination: &c.minPoints,
},
&cli.BoolFlag{
Name: "8-bit",
Value: c.eightBit,
Usage: "set to interpret the input points color as part of a 8bit color space",
Destination: &c.eightBit,
},
&cli.Float64Flag{
Name: "subsample",
Value: c.subsamplePct,
Usage: "Approximate percent of points to keep in the final point cloud, between 0.01 (1%) and 1 (100%)",
Destination: &c.subsamplePct,
},
&cli.StringFlag{
Name: "version",
Aliases: []string{"v"},
Value: c.version,
Usage: "sets the version of the tileset to generate. Could be either 1.0 or 1.1",
Destination: &c.version,
},
}
}
type cliOpts struct {
output string
crs string
maxDepth int
minPoints int
resolution float64
zOffset float64
subsamplePct float64
eightBit bool
join bool
version string
}
func defaultCliOptions() *cliOpts {
return &cliOpts{
crs: "",
maxDepth: 10,
minPoints: 5000,
resolution: 20,
subsamplePct: 1,
zOffset: 0,
eightBit: false,
join: false,
version: "1.0",
}
}
func (c *cliOpts) validate() {
if c.output == "" {
log.Fatal("output flag must be set")
}
if c.maxDepth <= 1 || c.maxDepth > 20 {
log.Fatal("depth should be between 1 and 20")
}
if c.minPoints < 1 {
log.Fatal("min-points-per-tile should be at least 1")
}
if c.resolution < 0.5 || c.resolution > 1000 {
log.Fatal("resolution should be between 1 and 1000 meters")
}
if c.subsamplePct < 0.01 || c.subsamplePct > 1 {
log.Fatal("subsample should be a value between 0.01 and 1")
}
if _, ok := version.Parse(c.version); !ok {
log.Fatal("invalid tileset version, the only allowed values are '1.0' and '1.1'")
}
}
func (c *cliOpts) print() {
crsMsg := c.crs
if c.crs == "" {
crsMsg = "(autodetect from LAS metadata)"
}
fmt.Printf(`*** Execution settings:
- Source CRS: %s,
- Max Depth: %d,
- Resolution: %f meters,
- Min Points per tile: %d
- Z-Offset: %f meters,
- 8Bit Color: %v
- Join Clouds: %v
- Tileset Version: %v
`, crsMsg, c.maxDepth, c.resolution, c.minPoints, c.zOffset, c.eightBit, c.join, c.version)
}
func (c *cliOpts) getTilerOptions() *tiler.TilerOptions {
c.validate()
v, ok := version.Parse(c.version)
if !ok {
log.Fatal("unrecongnized tileset version")
}
mutators := []mutator.Mutator{
mutator.NewZOffset(float32(c.zOffset)),
}
if c.subsamplePct < 1 {
mutators = append(mutators, mutator.NewSubsampler(c.subsamplePct))
}
return tiler.NewTilerOptions(
tiler.WithEightBitColors(c.eightBit),
tiler.WithMutators(mutators),
tiler.WithGridSize(c.resolution),
tiler.WithMaxDepth(c.maxDepth),
tiler.WithMinPointsPerTile(c.minPoints),
tiler.WithCallback(eventListener),
tiler.WithTilesetVersion(v),
)
}
func fileCommand(opts *cliOpts, filepath string) {
t, err := tilerProvider()
if err != nil {
log.Fatal(err)
}
fmt.Printf("*** Mode: File, process LAS file at %s\n", filepath)
opts.print()
tilerOpts := opts.getTilerOptions()
crs := opts.crs
if code, err := strconv.Atoi(crs); err == nil {
crs = fmt.Sprintf("EPSG:%d", code)
}
runnable := func(ctx context.Context) error {
return t.ProcessFiles([]string{filepath}, opts.output, crs, tilerOpts, ctx)
}
launch(runnable)
}
func folderCommand(opts *cliOpts, folderpath string) {
t, err := tilerProvider()
if err != nil {
log.Fatal(err)
}
fmt.Printf("*** Mode: Folder, process all files in %s\n", folderpath)
opts.print()
tilerOpts := opts.getTilerOptions()
crs := opts.crs
if code, err := strconv.Atoi(crs); err == nil {
crs = fmt.Sprintf("EPSG:%d", code)
}
runnable := func(ctx context.Context) error {
if opts.join {
files, err := utils.FindLasFilesInFolder(folderpath)
if err != nil {
return err
}
return t.ProcessFiles(files, opts.output, crs, tilerOpts, ctx)
}
return t.ProcessFolder(folderpath, opts.output, crs, tilerOpts, ctx)
}
launch(runnable)
}
func launch(function func(ctx context.Context) error) {
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := function(ctx)
if err != nil {
log.Fatal(err)
}
}()
wg.Wait()
}
func eventListener(e tiler.TilerEvent, filename string, elapsed int64, msg string) {
fmt.Printf("[%s] [%s] %s\n", time.Now().UTC().Format("2006-01-02 15:04:05.000"), filename, msg)
}
func printBanner() {
banner := strings.ReplaceAll(logo, "YYYY", strconv.Itoa(time.Now().Year()))
banner = strings.ReplaceAll(banner, "ZZZZ", getVersion())
fmt.Println(banner)
}