-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_comp_map.go
executable file
·501 lines (394 loc) · 13.6 KB
/
ui_comp_map.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this db except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
)
// https://wiki.openstreetmap.org/wiki/Raster_tile_providers
type UiMapLocator struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
Label string `json:"label"`
}
type UiMapSegmentTrk struct {
Lon float64 `json:"lon"`
Lat float64 `json:"lat"`
Ele float64 `json:"ele"`
Time string `json:"time"`
}
type UiMapSegment struct {
Trkpts []UiMapSegmentTrk `json:"trkpts"`
Label string `json:"label"`
}
type UiLayoutMapSegment struct {
in string
segs []UiMapSegment
//last use ....
}
type UiLayoutMap struct {
active *UiLayoutDiv
lonOld, latOld, zoomOld float64
start_pos OsV2f
start_tile OsV2f
start_zoom_time float64
cache_segments []*UiLayoutMapSegment
}
func NewUiLayoutMap() *UiLayoutMap {
mp := &UiLayoutMap{}
return mp
}
func (mp *UiLayoutMap) Destroy() {
}
func (mp *UiLayoutMap) GetSegment(in string) (*UiLayoutMapSegment, error) {
//find
for _, seg := range mp.cache_segments {
if seg.in == in {
return seg, nil
}
}
//add
var segs []UiMapSegment
err := json.Unmarshal([]byte(in), &segs) //slow 2 ............................
if err != nil {
return nil, fmt.Errorf("Unmarshal() failed: %w", err)
}
seg := &UiLayoutMapSegment{in: in, segs: segs}
mp.cache_segments = append(mp.cache_segments, seg)
return seg, nil
}
func (mp *UiLayoutMap) SetActive(ui *Ui) {
mp.active = ui.GetCall().call
}
func (mp *UiLayoutMap) GetAnim(lon float64, lat float64, zoom float64, ui *Ui) (float64, float64, float64, float64, bool) {
scale := float64(1)
isZooming, dt, ANIM_TIME := mp.isZooming()
if isZooming && mp.active == ui.GetCall().call {
t := dt / ANIM_TIME
if zoom > mp.zoomOld {
scale = 1 + t
} else {
scale = 1 - t/2
}
zoom = mp.zoomOld
lon = mp.lonOld + (lon-mp.lonOld)*t
lat = mp.latOld + (lat-mp.latOld)*t
ui.win.SetRedraw()
}
return lon, lat, zoom, scale, isZooming
}
func UiLayoutMap_metersPerPixel(lat, zoom float64) float64 {
return 156543.034 * math.Cos(lat/180*math.Pi) / math.Pow(2, zoom)
}
func UiLayoutMap_lonLatToPos(lon, lat, zoom float64) OsV2f {
x := (lon + 180) / 360 * math.Pow(2, zoom)
y := (1 - math.Log(math.Tan(lat*math.Pi/180)+1/math.Cos(lat*math.Pi/180))/math.Pi) / 2 * math.Pow(2, zoom)
return OsV2f{float32(x), float32(y)}
}
func UiLayoutMap_posToLonLat(pos OsV2f, zoom float64) (float64, float64) {
lon := float64(pos.X)/math.Pow(2, zoom)*360 - 180 //long
n := math.Pi - 2*math.Pi*float64(pos.Y)/math.Pow(2, zoom)
lat := 180 / math.Pi * math.Atan(0.5*(math.Exp(n)-math.Exp(n*-1))) //lat
return lon, lat
}
func UiLayoutMap_camBbox(res OsV2f, tile float64, lon, lat, zoom float64) (OsV2f, OsV2f, OsV2f) {
hres := res.MulV(0.5)
tilePos := UiLayoutMap_lonLatToPos(lon, lat, zoom)
max_res := math.Pow(2, zoom)
var start, end OsV2f
start.X = float32(OsClampFloat((float64(tilePos.X)*tile-float64(hres.X))/tile, 0, max_res))
start.Y = float32(OsClampFloat((float64(tilePos.Y)*tile-float64(hres.Y))/tile, 0, max_res))
end.X = float32(OsClampFloat((float64(tilePos.X)*tile+float64(hres.X))/tile, 0, max_res))
end.Y = float32(OsClampFloat((float64(tilePos.Y)*tile+float64(hres.Y))/tile, 0, max_res))
size := end.Sub(start)
return start, end, size
}
func UiLayoutMap_camCheck(res OsV2f, tile float64, lon, lat, zoom float64) (float64, float64) {
if res.X <= 0 || res.Y <= 0 {
return 0, 0
}
bbStart, bbEnd, bbSize := UiLayoutMap_camBbox(res, tile, lon, lat, zoom)
maxTiles := math.Pow(2, zoom)
def_bbox_size := OsV2f{res.X / float32(tile), res.Y / float32(tile)}
if bbStart.X <= 0 {
bbSize.X = def_bbox_size.X
bbStart.X = 0
}
if bbStart.Y <= 0 {
bbSize.Y = def_bbox_size.Y
bbStart.Y = 0
}
if bbEnd.X >= float32(maxTiles) {
bbSize.X = def_bbox_size.X
bbStart.X = float32(OsMaxFloat(0, maxTiles-float64(bbSize.X)))
}
if bbEnd.Y >= float32(maxTiles) {
bbSize.Y = def_bbox_size.Y
bbStart.Y = float32(OsMaxFloat(0, maxTiles-float64(bbSize.Y)))
}
return UiLayoutMap_posToLonLat(OsV2f{bbStart.X + bbSize.X/2, bbStart.Y + bbSize.Y/2}, zoom)
}
func UiLayoutMap_zoomClamp(z float64) float64 {
return OsClampFloat(z, 0, 19)
}
func (mp *UiLayoutMap) isZooming() (bool, float64, float64) {
ANIM_TIME := 0.4
dt := OsTime() - mp.start_zoom_time
return (dt < ANIM_TIME), dt, ANIM_TIME
}
func (ui *Ui) comp_mapLocators(cam_lon, cam_lat, cam_zoom float64, items []UiMapLocator, cd OsCd, dialogName string) error {
cell := ui.DivInfo_get(SA_DIV_GET_cell, 0)
width := ui.DivInfo_get(SA_DIV_GET_screenWidth, 0)
height := ui.DivInfo_get(SA_DIV_GET_screenHeight, 0)
coord := OsV2f{float32(width), float32(height)}
lon, lat, zoom, scale, _ := ui.mapp.GetAnim(cam_lon, cam_lat, cam_zoom, ui)
tile := 256 / cell * scale
tileW := tile / width
tileH := tile / height
UiLayoutMap_camCheck(coord, tile, lon, lat, zoom)
bbStart, _, _ := UiLayoutMap_camBbox(coord, tile, lon, lat, zoom)
for i, it := range items {
p := UiLayoutMap_lonLatToPos(it.Lon, it.Lat, zoom) //...
x := float64(p.X-bbStart.X) * tileW
y := float64(p.Y-bbStart.Y) * tileH
rad := 1.0
rad_x := rad / width
rad_y := rad / height
ui.Div_startEx(0, 0, 1, 1, x-rad_x/2, y-rad_y, rad_x, rad_y, strconv.Itoa(i))
{
//ui.Paint_file(0, 0, 1, 1, 0, "file:apps/base/resources/locator.png", InitOsCd32(200, 20, 20, 255), 1, 0, false) //red
dnm := dialogName + "_" + strconv.Itoa(i)
//cd ......
if ui.Comp_buttonIcon(0, 0, 1, 1, InitWinMedia_url("file:apps/base/resources/locator.png"), 0, it.Label, Comp_buttonProp()) > 0 {
ui.Dialog_open(dnm, 1)
}
if ui.Dialog_start(dnm) {
ui.Div_colMax(0, 5)
ui.Comp_text(0, 0, 1, 1, it.Label, 1)
ui.Comp_text(0, 1, 1, 1, fmt.Sprintf("Lon: %.3f", it.Lon), 0)
ui.Comp_text(0, 2, 1, 1, fmt.Sprintf("Lat: %.3f", it.Lat), 0)
ui.Dialog_end()
}
}
ui.Div_end()
}
return nil
}
func (ui *Ui) comp_mapSegments(cam_lon, cam_lat, cam_zoom float64, segs []UiMapSegment, cd OsCd) error {
cell := ui.DivInfo_get(SA_DIV_GET_cell, 0)
width := ui.DivInfo_get(SA_DIV_GET_screenWidth, 0)
height := ui.DivInfo_get(SA_DIV_GET_screenHeight, 0)
coord := OsV2f{float32(width), float32(height)}
lon, lat, zoom, scale, _ := ui.mapp.GetAnim(cam_lon, cam_lat, cam_zoom, ui)
tile := 256 / cell * scale
tileW := tile / width
tileH := tile / height
UiLayoutMap_camCheck(coord, tile, lon, lat, zoom)
bbStart, _, _ := UiLayoutMap_camBbox(coord, tile, lon, lat, zoom)
//rad := 0.2
//rad_x := rad / width
//rad_y := rad / height
for _, segs := range segs {
last_x := float64(0)
last_y := float64(0)
last_set := false
for _, pt := range segs.Trkpts {
p := UiLayoutMap_lonLatToPos(pt.Lon, pt.Lat, zoom) //...
x := float64(p.X-bbStart.X) * tileW
y := float64(p.Y-bbStart.Y) * tileH
//ui.Paint_circle(0, 0, 1, 1, 0, x, y, 0.1, InitOsCd32(200, 20, 20, 255), 0)
//ui.Paint_tooltip(x-rad_x/2, y-rad_y, rad_x, rad_y, fmt.Sprintf("%.2f, %.2f, %s", pt.Lon, pt.Lat, pt.Time))
if last_set {
ui.Paint_line(0, 0, 1, 1, 0, last_x, last_y, x, y, cd, 0.06)
}
last_set = true
last_x = x
last_y = y
}
}
return nil
}
func (ui *Ui) comp_map(cam_lon, cam_lat, cam_zoom *float64, file, url, copyright, copyright_url string) (bool, error) {
old_cam_lon := *cam_lon
old_cam_lat := *cam_lat
old_cam_zoom := *cam_zoom
*cam_zoom = UiLayoutMap_zoomClamp(*cam_zoom) //check
db, alreadyOpen, err := ui.win.disk.OpenDb(file)
if err != nil {
return false, fmt.Errorf("GetDb(%s) failed: %w", file, err)
}
if !alreadyOpen {
_, err = db.Write("CREATE TABLE IF NOT EXISTS tiles (name TEXT, file BLOB);")
if err != nil {
return false, fmt.Errorf("CREATE TABLE in db(%s) failed: %w", file, err)
}
}
mp := ui.mapp
lon, lat, zoom, scale, isZooming := mp.GetAnim(*cam_lon, *cam_lat, *cam_zoom, ui)
cell := ui.DivInfo_get(SA_DIV_GET_cell, 0)
width := ui.DivInfo_get(SA_DIV_GET_screenWidth, 0)
height := ui.DivInfo_get(SA_DIV_GET_screenHeight, 0)
touch_x := float32(ui.DivInfo_get(SA_DIV_GET_touchX, 0))
touch_y := float32(ui.DivInfo_get(SA_DIV_GET_touchY, 0))
inside := ui.DivInfo_get(SA_DIV_GET_touchInside, 0) > 0
active := ui.DivInfo_get(SA_DIV_GET_touchActive, 0) > 0
end := ui.DivInfo_get(SA_DIV_GET_touchEnd, 0) > 0
start := ui.DivInfo_get(SA_DIV_GET_touchStart, 0) > 0
wheel := ui.DivInfo_get(SA_DIV_GET_touchWheel, 0)
clicks := ui.DivInfo_get(SA_DIV_GET_touchClicks, 0)
canvas_size := OsV2f{float32(width), float32(height)}
tile := 256 / cell * scale
tileW := tile / width
tileH := tile / height
UiLayoutMap_camCheck(canvas_size, tile, *cam_lon, *cam_lat, *cam_zoom)
bbStart, bbEnd, bbSize := UiLayoutMap_camBbox(canvas_size, tile, lon, lat, zoom)
//draw tiles
for y := float64(int(bbStart.Y)); y < float64(bbEnd.Y); y++ {
for x := float64(int(bbStart.X)); x < float64(bbEnd.X); x++ {
if x < 0 || y < 0 {
continue
}
tileCoord_sx := (x - float64(bbStart.X)) * tileW
tileCoord_sy := (y - float64(bbStart.Y)) * tileH
name := strconv.Itoa(int(zoom)) + "-" + strconv.Itoa(int(x)) + "-" + strconv.Itoa(int(y)) + ".png"
db.Lock()
row := db.ReadRow_unsafe("SELECT rowid FROM tiles WHERE name=='" + name + "'")
rowid := int64(-1)
err = row.Scan(&rowid)
db.Unlock() //because down, there is db.Write() + ui.Paint_file() also lock db
if err != nil {
//download
u := url
u = strings.ReplaceAll(u, "{x}", strconv.Itoa(int(x)))
u = strings.ReplaceAll(u, "{y}", strconv.Itoa(int(y)))
u = strings.ReplaceAll(u, "{z}", strconv.Itoa(int(zoom)))
img, done, _, err := ui.win.disk.net.GetFile(u, "Skyalt/0.1")
if done {
if err == nil {
//insert into db
res, err := db.Write("INSERT INTO tiles(name, file) VALUES(?, ?);", name, img)
if err == nil {
rowid, err = res.LastInsertId()
if err != nil {
return false, fmt.Errorf("LastInsertId() failed: %w", err)
}
}
} else {
return false, err
}
}
}
if rowid > 0 {
//extra margin will fix white spaces during zooming
ui.Paint_file(tileCoord_sx, tileCoord_sy, tileW, tileH, OsTrnFloat(isZooming, -0.03, 0), InitWinMedia_url(fmt.Sprintf("db:%s:tiles/file/%d", file, rowid)), InitOsCdWhite(), OsV2{0, 0}, false, false)
}
}
}
//touch
if start && inside {
mp.start_pos.X = touch_x //rel, not pixels!
mp.start_pos.Y = touch_y
mp.start_tile = UiLayoutMap_lonLatToPos(lon, lat, zoom)
}
if wheel != 0 && inside && !isZooming {
mp.zoomOld = *cam_zoom
*cam_zoom = UiLayoutMap_zoomClamp(*cam_zoom - wheel)
if mp.zoomOld != *cam_zoom {
mp.SetActive(ui)
mp.lonOld = *cam_lon
mp.latOld = *cam_lat
//get touch lon and lat
touch_lon, touch_lat := UiLayoutMap_posToLonLat(OsV2f{bbStart.X + bbSize.X*touch_x, bbStart.Y + bbSize.Y*touch_y}, mp.zoomOld)
//get new zoom touch pos
pos := UiLayoutMap_lonLatToPos(touch_lon, touch_lat, *cam_zoom)
//get center
pos.X -= bbSize.X * (touch_x - 0.5)
pos.Y -= bbSize.Y * (touch_y - 0.5)
//get new zoom lon and lat
*cam_lon, *cam_lat = UiLayoutMap_posToLonLat(pos, *cam_zoom)
mp.start_zoom_time = OsTime()
}
}
if active {
mp.SetActive(ui)
var move OsV2f
move.X = mp.start_pos.X - touch_x
move.Y = mp.start_pos.Y - touch_y
rx := move.X * bbSize.X
ry := move.Y * bbSize.Y
tileX := mp.start_tile.X + rx
tileY := mp.start_tile.Y + ry
*cam_lon, *cam_lat = UiLayoutMap_posToLonLat(OsV2f{tileX, tileY}, *cam_zoom)
}
//double click
if clicks > 1 && end && !isZooming {
mp.zoomOld = *cam_zoom
*cam_zoom = UiLayoutMap_zoomClamp(*cam_zoom + 1)
if mp.zoomOld != *cam_zoom {
mp.SetActive(ui)
mp.lonOld = *cam_lon
mp.latOld = *cam_lat
//get touch lon and lat
touch_lon, touch_lat := UiLayoutMap_posToLonLat(OsV2f{bbStart.X + bbSize.X*touch_x, bbStart.Y + bbSize.Y*touch_y}, mp.zoomOld)
//get new zoom touch pos
pos := UiLayoutMap_lonLatToPos(touch_lon, touch_lat, *cam_zoom)
//get center
pos.X -= bbSize.X * (touch_x - 0.5)
pos.Y -= bbSize.Y * (touch_y - 0.5)
//get new zoom lon and lat
*cam_lon, *cam_lat = UiLayoutMap_posToLonLat(pos, *cam_zoom)
mp.start_zoom_time = OsTime()
}
}
//copyright
if copyright != "" {
h := (1 / height) * 0.75
ui.Div_startEx(0, 0, 1, 1, 0, 1-h, 1, h, "copyright")
ui.DivInfo_set(SA_DIV_SET_scrollHshow, 0, 0)
ui.DivInfo_set(SA_DIV_SET_scrollVshow, 0, 0)
ui.Div_colMax(0, 100)
ui.Div_col(1, 5.5)
ui.Div_row(0, 0.75)
ui.Comp_buttonText(1, 0, 1, 1, copyright, Comp_buttonProp().Tooltip(copyright_url))
ui.Div_end()
}
return (old_cam_lon != *cam_lon || old_cam_lat != *cam_lat || old_cam_zoom != *cam_zoom), nil
}
/*type UiMapConvertGPX struct {
Trkseg []UiMapConvertTrkseg `xml:"trk>trkseg" json:"segments"`
}
type UiMapConvertTrkseg struct {
Trkpt []struct {
Lat float32 `xml:"lat,attr" json:"lat"`
Lon float32 `xml:"lon,attr" json:"lon"`
Ele float32 `xml:"ele" json:"ele,omitempty"`
Time string `xml:"time" json:"time,omitempty"`
} `xml:"trkpt"`
}
func UiMap_GpxToJson(gpx []byte) ([]byte, error) {
//gpx -> struct
var g UiMapConvertGPX
err := xml.Unmarshal(gpx, &g)
if err != nil {
return nil, fmt.Errorf("Unmarshal() failed: %w", err)
}
//struct -> json
js, err := json.Marshal(g.Trkseg)
if err != nil {
return nil, fmt.Errorf("Marshal() failed: %w", err)
}
return js, nil
}*/