-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (98 loc) · 2.95 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
package main
import (
"flag"
"log"
"os"
"path/filepath"
"strings"
"github.com/fsnotify/fsnotify"
)
var _source = filepath.Join(os.Getenv("HOME"), "dev", "rotblauer", "cattracks-split-cats-uniqcell-gz", "output")
var flagSourceDir = flag.String("source", _source, "Source directory containing .json.gz files")
var flagOutputRootFilepath = flag.String("output", filepath.Join(_source, "mbtiles"), "Output root dir")
var flagForce = flag.Bool("force", false, "Force overwrite of existing files (otherwise skip if .mbtiles is newer than .json.gz)")
var flagEnableFSWatch = flag.Bool("enable-fs-watch", false, "Enable watching of source directory for changes")
// walkDirRunTippe run tippecanoe to create .mbtiles for each .json.gz file that doesn't yet have one.
// Without --force it may be run idempotently, skipping all cases where file.mbiles is newer than file.json.gz.
// The layer name is the base filename of the .json.gz file, suffixed with "-layer", eg 'ia.json.gz-layer'.
func walkDirRunTippe(dir string, changedPath string) {
log.Println("source dir:", dir)
filepath.Walk(dir, func(path string, jsonGZInfo os.FileInfo, err error) error {
if err != nil {
log.Println("error walking path:", path, err)
return nil
}
if jsonGZInfo.IsDir() {
return nil
}
if filepath.Ext(path) != ".gz" || !strings.Contains(path, ".json.gz") {
return nil
}
tilesPath := strings.Replace(path, ".json.gz", ".mbtiles", 1)
out := filepath.Join(*flagOutputRootFilepath, filepath.Base(tilesPath))
// --force?
if !*flagForce {
if tilesInfo, err := os.Stat(out); err == nil {
// if the modtime of the .json.gz is older than the .mbtiles, skip
if jsonGZInfo.ModTime().Before(tilesInfo.ModTime()) {
log.Printf("skipping %s, already exists and is fresh: %s\n", path, tilesPath)
return nil
}
}
}
// want: ia.level-23.json.gz -> ia.level-23
layerName := strings.ReplaceAll(filepath.Base(path), ".json.gz", "")
log.Printf(`running tippecanoe:
- source: %s
- output: %s
- layer_name: %s
`, path, out, layerName)
in := path
if err := runTippe(out, in, layerName); err != nil {
log.Println(err)
}
return nil
})
}
func main() {
flag.Parse()
os.MkdirAll(*flagOutputRootFilepath, 0755)
walkDirRunTippe(*flagSourceDir, "")
if !*flagEnableFSWatch {
return
}
// Create new watcher.
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
// Start listening for events.
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Println("event:", event)
if event.Has(fsnotify.Write) {
log.Println("modified file:", event.Name)
walkDirRunTippe(*flagSourceDir, event.Name)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
// Add a path.
err = watcher.Add(*flagSourceDir)
if err != nil {
log.Fatal(err)
}
// Block main goroutine forever.
<-make(chan struct{})
}