-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmd.go
149 lines (145 loc) · 3.97 KB
/
cmd.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
// Diglet is a set of geospatial tools focused around rendering large feature sets efficiently.
package mbt
import (
"github.com/buckhx/diglet/util"
"github.com/codegangsta/cli"
"os"
"path/filepath"
"strings"
)
var (
CsvExt = "csv"
GeojsonExt = "geojson"
exts = []string{CsvExt, GeojsonExt}
)
var Cmd = cli.Command{
Name: "mbt",
Aliases: []string{"build"},
Usage: "Builds an mbtiles database from the input data source",
Description: "Builds an mbtiles database from the given format",
ArgsUsage: "input_source",
Action: func(c *cli.Context) { //TODO break out into functions
// get kwargs & vars
out := c.String("output")
desc := c.String("desc")
layer := c.String("layer-name")
zmin := uint(c.Int("min"))
zmax := uint(c.Int("max"))
extent := uint(c.Int("extent"))
upsert := c.Bool("upsert")
force := c.Bool("force")
// validate
if len(c.Args()) == 0 || out == "" {
util.Die(c, "input_source & --out required")
} else if zmax < zmin || zmin < 0 || zmax > 23 {
util.Die(c, "--max > --min, --min > 0 --max < 24 not satisfied")
}
// execute
source, err := getSource(c)
util.Check(err)
if force {
os.Remove(out)
}
tiles, err := InitTiles(out, upsert, desc, extent)
util.Check(err)
err = tiles.Build(source, layer, zmin, zmax)
util.Check(err)
// finalize
file, _ := os.Open(out)
defer file.Close()
stat, _ := file.Stat()
exp := float64(stat.Size()) / float64(1<<20)
util.Info("%s was successfully caught!", out)
util.Info("Diglet gained %f MB of EXP!", exp)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "o, output",
Usage: "REQUIRED: Path to write mbtiles to",
},
cli.StringFlag{
Name: "input-type",
Value: "sniff",
Usage: "Type of input files, 'sniff' will pick type based on the extension",
},
cli.BoolFlag{
Name: "f, force",
Usage: "Remove the existing .mbtiles file before running.",
},
cli.BoolFlag{
Name: "u, upsert",
Usage: "Upsert into mbtiles instead of replacing.",
},
cli.StringFlag{
Name: "layer-name",
Value: "features",
Usage: "Name of the layer for the features to be added to",
},
cli.StringFlag{
Name: "desc, description",
Value: "Generated from Diglet",
Usage: "Value inserted into the description entry of the mbtiles",
},
cli.IntFlag{
Name: "extent",
Value: 4096,
Usage: "Extent of tiles to be built. Default is 4096",
},
cli.IntFlag{
Name: "max, max-zoom",
Value: 10,
Usage: "Maximum zoom level to build tiles for",
},
cli.IntFlag{
Name: "min, min-zoom",
Value: 5,
Usage: "Minimum zoom level to build tiles from",
},
cli.StringFlag{
Name: "filter",
Usage: "Only include fields keys in this comma delimited list.\t" +
"EXAMPLE --filter name,date,case_number,id\t" +
"NOTE all fields are lowercased and non-word chars replaced with '_'",
},
cli.StringFlag{
Name: "csv-lat",
Usage: "Column containing a single longitude point",
},
cli.StringFlag{
Name: "csv-lon",
Usage: "Column containing a single longitude point",
},
cli.StringFlag{
Name: "csv-shape",
Usage: "Column containing shape in geojson-like 'coordinates' form.\t" +
"Does not support multi-geometries",
},
cli.StringFlag{
Name: "csv-delimiter",
Value: ",",
},
},
}
func getSource(c *cli.Context) (source FeatureSource, err error) {
path := c.Args()[0]
var filter []string
if len(c.String("filter")) > 0 {
filter = strings.Split(c.String("filter"), ",")
}
ext := filepath.Ext(path)[1:]
switch ext {
case CsvExt:
delim := c.String("csv-delimiter")
fields := GeoFields{"lat": c.String("csv-lat"), "lon": c.String("csv-lon"), "shape": c.String("csv-shape")}
if !fields.Validate() {
err = util.Errorf("csv-lat/csv-lon or csv-shape required")
break
}
source = NewCsvSource(path, filter, delim, fields)
case GeojsonExt:
source = NewGeojsonSource(path, filter)
default:
err = util.Errorf("Invalid source file extension %s %s", ext, strings.Join(exts, "|"))
}
return
}