-
Notifications
You must be signed in to change notification settings - Fork 54
/
utils.go
183 lines (154 loc) · 4.18 KB
/
utils.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/maptile"
"github.com/paulmach/orb/maptile/tilecover"
log "github.com/sirupsen/logrus"
)
func saveToMBTile(tile Tile, db *sql.DB) error {
_, err := db.Exec("insert into tiles (zoom_level, tile_column, tile_row, tile_data) values (?, ?, ?, ?);", tile.T.Z, tile.T.X, tile.flipY(), tile.C)
// _, err := db.Exec("insert or ignore into tiles (zoom_level, tile_column, tile_row, tile_data) values (?, ?, ?, ?);", tile.T.Z, tile.T.X, tile.flipY(), tile.C)
if err != nil {
return err
}
return nil
}
func saveToFiles(tile Tile, task *Task) error {
dir := filepath.Join(task.File, fmt.Sprintf(`%d`, tile.T.Z), fmt.Sprintf(`%d`, tile.T.X))
os.MkdirAll(dir, os.ModePerm)
fileName := filepath.Join(dir, fmt.Sprintf(`%d.%s`, tile.T.Y, task.TileMap.Format))
err := os.WriteFile(fileName, tile.C, os.ModePerm)
if err != nil {
return err
}
log.Println(fileName)
return nil
}
func optimizeConnection(db *sql.DB) error {
// _, err := db.Exec("PRAGMA synchronous=0")
// if err != nil {
// return err
// }
_, err := db.Exec("PRAGMA locking_mode=EXCLUSIVE")
if err != nil {
return err
}
_, err = db.Exec("PRAGMA journal_mode=DELETE")
if err != nil {
return err
}
return nil
}
func optimizeDatabase(db *sql.DB) error {
_, err := db.Exec("ANALYZE;")
if err != nil {
return err
}
_, err = db.Exec("VACUUM;")
if err != nil {
return err
}
return nil
}
func loadFeature(path string) *geojson.Feature {
data, err := os.ReadFile(path)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
f, err := geojson.UnmarshalFeature(data)
if err == nil {
return f
}
fc, err := geojson.UnmarshalFeatureCollection(data)
if err == nil {
if len(fc.Features) != 1 {
log.Fatalf("must have 1 feature: %v", len(fc.Features))
}
return fc.Features[0]
}
g, err := geojson.UnmarshalGeometry(data)
if err != nil {
log.Fatalf("unable to unmarshal feature: %v", err)
}
return geojson.NewFeature(g.Geometry())
}
func loadFeatureCollection(path string) *geojson.FeatureCollection {
data, err := os.ReadFile(path)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
fc, err := geojson.UnmarshalFeatureCollection(data)
if err != nil {
log.Fatalf("unable to unmarshal feature: %v", err)
}
count := 0
for i := range fc.Features {
if fc.Features[i].Properties["name"] != "original" {
fc.Features[count] = fc.Features[i]
count++
}
}
fc.Features = fc.Features[:count]
return fc
}
func loadCollection(path string) orb.Collection {
data, err := os.ReadFile(path)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
fc, err := geojson.UnmarshalFeatureCollection(data)
if err != nil {
log.Fatalf("unable to unmarshal feature: %v", err)
}
var collection orb.Collection
for _, f := range fc.Features {
collection = append(collection, f.Geometry)
}
return collection
}
// output gets called if there is a test failure for debugging.
func output(name string, r *geojson.FeatureCollection) {
f := loadFeature("./data/" + name + ".geojson")
if f.Properties == nil {
f.Properties = make(geojson.Properties)
}
f.Properties["fill"] = "#FF0000"
f.Properties["fill-opacity"] = "0.5"
f.Properties["stroke"] = "#FF0000"
f.Properties["name"] = "original"
r.Append(f)
data, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Fatalf("error marshalling json: %v", err)
}
err = os.WriteFile("failure_"+name+".geojson", data, 0644)
if err != nil {
log.Fatalf("write file failure: %v", err)
}
}
// output gets called if there is a test failure for debugging.
func output2(name string, r *geojson.FeatureCollection, wg *sync.WaitGroup) {
defer wg.Done()
data, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Fatalf("error marshalling json: %v", err)
}
err = os.WriteFile(name+".geojson", data, 0644)
if err != nil {
log.Fatalf("write file failure: %v", err)
}
}
func getZoomCount(g orb.Geometry, minz int, maxz int) map[int]int64 {
info := make(map[int]int64)
for z := minz; z <= maxz; z++ {
info[z] = tilecover.GeometryCount(g, maptile.Zoom(z))
}
return info
}