-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmain.go
More file actions
283 lines (243 loc) · 6.2 KB
/
main.go
File metadata and controls
283 lines (243 loc) · 6.2 KB
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
package main
import (
"encoding/json"
"fmt"
"math"
"strconv"
"syscall/js"
"github.com/akhenakh/oureadb/index/geodata"
"github.com/akhenakh/oureadb/s2tools"
"github.com/golang/geo/s2"
"github.com/twpayne/go-geom/encoding/geojson"
)
const earthCircumferenceMeter = 40075017
var document js.Value
func init() {
document = js.Global().Get("document")
}
func getCoverParams() (minLevel, maxLevel, maxCells int, inside bool) {
minS := document.Call("getElementById", "minRange").Get("value").String()
minLevel, err := strconv.Atoi(minS)
if err != nil {
println(err.Error())
return
}
maxS := document.Call("getElementById", "maxRange").Get("value").String()
maxLevel, err = strconv.Atoi(maxS)
if err != nil {
println(err.Error())
return
}
maxCS := document.Call("getElementById", "maxCellsRange").Get("value").String()
maxCells, err = strconv.Atoi(maxCS)
if err != nil {
println(err.Error())
return
}
coverCS := document.Call("getElementById", "icover").Get("checked").Bool()
if coverCS {
inside = true
}
return minLevel, maxLevel, maxCells, inside
}
func geoFeaturesJSONToCells(this js.Value, i []js.Value) interface{} {
var fc geojson.FeatureCollection
b := js.ValueOf(i[0]).String()
err := json.Unmarshal([]byte(b), &fc)
if err != nil {
println(err.Error())
return nil
}
var res s2.CellUnion
for _, f := range fc.Features {
cu, err := computeFeatureCells(f)
if err != nil {
println("error computing cells", err)
return nil
}
res = append(res, cu...)
}
jsonb := s2tools.CellUnionToGeoJSON(res)
if len(jsonb) == 0 {
println("empty result")
return nil
}
updateUIWithData(string(jsonb))
return nil
}
func geoCircleToCells(this js.Value, i []js.Value) interface{} {
lng := js.ValueOf(i[0]).Float()
lat := js.ValueOf(i[1]).Float()
radius := js.ValueOf(i[2]).Float()
center := s2.PointFromLatLng(s2.LatLngFromDegrees(lat, lng))
cap := s2.CapFromCenterArea(center, s2RadialAreaMeters(radius))
minLevel, maxLevel, maxCells, inside := getCoverParams()
coverer := &s2.RegionCoverer{MinLevel: minLevel, MaxLevel: maxLevel, MaxCells: maxCells}
var cu s2.CellUnion
if inside {
cu = coverer.InteriorCovering(cap)
} else {
cu = coverer.Covering(cap)
}
jsonb := s2tools.CellUnionToGeoJSON(cu)
updateUIWithData(string(jsonb))
return nil
}
func geoJSONToCells(this js.Value, i []js.Value) interface{} {
var f geojson.Feature
b := js.ValueOf(i[0]).String()
err := json.Unmarshal([]byte(b), &f)
if err != nil {
println(err.Error())
return nil
}
cu, err := computeFeatureCells(&f)
if err != nil {
println("error computing cells", err)
return nil
}
jsonb := s2tools.CellUnionToGeoJSON(cu)
if len(jsonb) == 0 {
println("can't generate cells")
return nil
}
updateUIWithData(string(jsonb))
return nil
}
func computeFeatureCells(f *geojson.Feature) (s2.CellUnion, error) {
gd := &geodata.GeoData{}
err := geodata.GeoJSONFeatureToGeoData(f, gd)
if err != nil {
return nil, err
}
minLevel, maxLevel, maxCells, insideCover := getCoverParams()
coverer := &s2.RegionCoverer{MinLevel: minLevel, MaxLevel: maxLevel, MaxCells: maxCells}
var cu s2.CellUnion
if insideCover {
icu, err := gd.InteriorCover(coverer)
if err != nil {
return nil, fmt.Errorf("error in Interior Cover %w", err)
}
cu = icu
} else {
icu, err := gd.Cover(coverer)
if err != nil {
return nil, fmt.Errorf("error in Exterior Cover %w", err)
}
cu = icu
}
return cu, nil
}
func drawCells(this js.Value, i []js.Value) interface{} {
un := make(map[s2.CellID]struct{})
for _, cs := range i {
cs := js.ValueOf(cs).String()
if cs != "" {
c := s2tools.ParseCellID(cs)
if c == nil {
continue
}
un[*c] = struct{}{}
}
}
cells := make(s2.CellUnion, len(un))
count := 0
for c := range un {
cells[count] = c
count++
}
b := s2tools.CellUnionToGeoJSON(cells)
updateUIWithData(string(b))
return nil
}
type FeatureCollection struct {
Type string `json:"type"`
Features []Feature `json:"features"`
}
type Feature struct {
Type string `json:"type"`
Geometry Geometry `json:"geometry"`
Properties map[string]interface{} `json:"properties"`
}
type Geometry struct {
Type string `json:"type"`
Coordinates json.RawMessage `json:"coordinates"`
}
func normalizeGeoJSON(data []byte) []byte {
var fc FeatureCollection
if err := json.Unmarshal(data, &fc); err != nil {
return data
}
for i := range fc.Features {
if fc.Features[i].Geometry.Type == "Polygon" {
var coords [][][]float64
if err := json.Unmarshal(fc.Features[i].Geometry.Coordinates, &coords); err != nil {
continue
}
for j := range coords {
coords[j] = normalizeRing(coords[j])
}
newCoords, _ := json.Marshal(coords)
fc.Features[i].Geometry.Coordinates = newCoords
}
}
result, _ := json.Marshal(fc)
return result
}
func normalizeRing(ring [][]float64) [][]float64 {
if len(ring) < 2 {
return ring
}
// Determine which side of antimeridian the majority of points are on
eastCount := 0
westCount := 0
for _, coord := range ring {
lon := coord[0]
if lon < -90 || (lon >= 0 && lon < 90) {
eastCount++
} else {
westCount++
}
}
// Normalize all coordinates to the majority side
for i := range ring {
lon := ring[i][0]
if eastCount >= westCount {
// Keep in -180 to 180 range, convert +180 to -180
if lon > 180 {
ring[i][0] = lon - 360
} else if lon == 180 {
ring[i][0] = -180
}
} else {
// Keep in 0 to 360 range, convert -180 to +180
if lon < -180 {
ring[i][0] = lon + 360
} else if lon == -180 {
ring[i][0] = 180
}
}
}
return ring
}
func updateUIWithData(data string) {
jsonb := normalizeGeoJSON([]byte(data))
js.Global().Set("data", string(jsonb))
js.Global().Call("updateui")
}
func registerCallbacks() {
js.Global().Set("drawcells", js.FuncOf(drawCells))
js.Global().Set("circlecell", js.FuncOf(geoCircleToCells))
js.Global().Set("geocell", js.FuncOf(geoJSONToCells))
js.Global().Set("geofeaturescell", js.FuncOf(geoFeaturesJSONToCells))
}
func s2RadialAreaMeters(radius float64) float64 {
r := (radius / earthCircumferenceMeter) * math.Pi * 2
return math.Pi * r * r
}
func main() {
c := make(chan struct{}, 0)
println("Wasm ready")
registerCallbacks()
<-c
}