-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
118 lines (103 loc) · 2.73 KB
/
db.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
package main
import (
"database/sql"
"strconv"
_ "github.com/mattn/go-sqlite3"
"github.com/paulmach/orb/encoding/mvt"
"github.com/paulmach/orb/geojson"
)
func createConn(fp string) (*dbConn, error) {
db, err := sql.Open("sqlite3", fp)
if err != nil {
return nil, err
}
return &dbConn{db: db}, nil
}
func (conn *dbConn) find(keyValue *kv, userZoomLevel *int) ([]result, error) {
rows, err := conn.db.Query("SELECT zoom_level, tile_data, tile_id FROM images;")
if err != nil {
return nil, err
}
defer rows.Close()
var results []result
for rows.Next() {
var zoomLevel int
var tileData []byte
var tileId string
err = rows.Scan(&zoomLevel, &tileData, &tileId)
if err != nil {
panic(err)
}
l, err := mvt.UnmarshalGzipped(tileData)
if err != nil {
panic(err)
}
layers := l.ToFeatureCollections()
newLayers := make(map[string]*geojson.FeatureCollection, len(layers))
for i := range layers {
layer := layers[i]
newLayer := geojson.NewFeatureCollection()
for _, f := range layer.Features {
newFeature := geojson.NewFeature(f.Geometry)
newFeature.Properties = f.Properties
// Keep copy of original id
idOg := newFeature.Properties["id"]
newFeature.Properties["id"] = strconv.FormatInt(int64(newFeature.Properties["id"].(float64)), 10)
if userZoomLevel != nil {
// Zoom level provided
if newFeature.Properties[keyValue.key] != keyValue.value && zoomLevel == *userZoomLevel {
newLayer.Append(newFeature)
}
} else {
// No zoom level provided
if newFeature.Properties[keyValue.key] != keyValue.value {
newLayer.Append(newFeature)
}
}
// Id back to original
newFeature.Properties["id"] = idOg
}
newLayers[i] = newLayer
}
newTileData, err := mvt.MarshalGzipped(mvt.NewLayers(newLayers))
if err != nil {
return nil, err
}
results = append(results, result{tileId: tileId, tileData: newTileData})
}
err = rows.Err()
if err != nil {
return nil, err
}
return results, nil
}
func (conn *dbConn) update(results []result, userZoomLevel *int) error {
if userZoomLevel != nil {
// Zoom level provided
stmt, err := conn.db.Prepare("UPDATE images SET tile_data = ? WHERE tile_id = ? AND zoom_level = ?;")
if err != nil {
return err
}
defer stmt.Close()
for _, result := range results {
_, err = stmt.Exec(result.tileData, result.tileId, userZoomLevel)
if err != nil {
return err
}
}
} else {
// No zoom level provided
stmt, err := conn.db.Prepare("UPDATE images SET tile_data = ? WHERE tile_id = ?;")
if err != nil {
return err
}
defer stmt.Close()
for _, result := range results {
_, err = stmt.Exec(result.tileData, result.tileId)
if err != nil {
return err
}
}
}
return nil
}