-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathenc_render_dump_test.go
More file actions
279 lines (256 loc) · 7.54 KB
/
enc_render_dump_test.go
File metadata and controls
279 lines (256 loc) · 7.54 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
package vc
import (
"bytes"
"context"
"fmt"
"image"
"image/draw"
"image/png"
"os"
"path/filepath"
"runtime"
"sync"
"sync/atomic"
"testing"
"go.viam.com/rdk/logging"
)
// TestDumpGreatSouthBay renders a small handful of stitched mosaics over Great
// South Bay so you can flip through them in Preview and see what the renderer
// is actually producing at each zoom. This is intentionally a few large images,
// not thousands of tiles, so it's easy to spot what's there and what's missing.
//
// Run:
//
// go test -count=1 -run TestDumpGreatSouthBay -v ./...
//
// Output (default /tmp/gsb-tiles/):
//
// gsb-z12.png broad view of the bay (8x8 tiles -> 2048x2048)
// gsb-z14.png mid zoom (8x8 tiles -> 2048x2048)
// gsb-z16.png detail / channels (8x8 tiles -> 2048x2048)
//
// Tunable via env vars:
//
// GSB_OUT_DIR /tmp/gsb-tiles
// GSB_ZOOMS 12,14,16
// GSB_GRID 8 (NxN tiles per mosaic)
// GSB_CENTER_LON -73.05
// GSB_CENTER_LAT 40.66
// GSB_CACHE_DIR $HOME/.cache/viam-chartplotter/noaa-enc
// GSB_PREFETCH 1 (set to 0 to skip prefetch)
func TestDumpGreatSouthBay(t *testing.T) {
if testing.Short() {
t.Skip("skipping debug tile dump in short mode")
}
outDir := envOr("GSB_OUT_DIR", "/tmp/gsb-tiles")
zooms := envOrInts(t, "GSB_ZOOMS", []int{12, 14, 16})
grid := envOrInt(t, "GSB_GRID", 8)
// Hollywood / Dania Beach area, FL — Port Everglades approach.
centerLon := envOrFloat(t, "GSB_CENTER_LON", -80.10809)
centerLat := envOrFloat(t, "GSB_CENTER_LAT", 26.11245)
cacheDir := envOr("GSB_CACHE_DIR", filepath.Join(mustUserCacheDir(t), "viam-chartplotter", "noaa-enc"))
doPrefetch := os.Getenv("GSB_PREFETCH") != "0"
t.Logf("cache: %s", cacheDir)
t.Logf("out: %s", outDir)
t.Logf("zooms: %v", zooms)
t.Logf("grid: %dx%d tiles per mosaic", grid, grid)
t.Logf("center: %.4f, %.4f", centerLon, centerLat)
if err := os.MkdirAll(outDir, 0o755); err != nil {
t.Fatalf("mkdir out: %v", err)
}
logger := logging.NewTestLogger(t)
catalog, err := NewENCCatalog(cacheDir, logger)
if err != nil {
t.Fatalf("catalog: %v", err)
}
store, err := NewENCStore(cacheDir, catalog, logger)
if err != nil {
t.Fatalf("store: %v", err)
}
renderer := NewENCRenderer(catalog, store, logger)
ctx := context.Background()
if err := catalog.EnsureFresh(ctx); err != nil {
t.Logf("catalog refresh: %v (continuing with whatever is on disk)", err)
}
// Prefetch using the bbox of the largest zoom's grid so all relevant cells
// are on disk before rendering.
if doPrefetch {
biggestZ := zooms[0]
for _, z := range zooms {
if z > biggestZ {
biggestZ = z
}
}
minLon, minLat, maxLon, maxLat := gridBBox(centerLon, centerLat, biggestZ, grid)
dl, sk, err := store.SyncBBox(ctx, minLon, minLat, maxLon, maxLat, 0, 0, 4)
if err != nil {
t.Logf("prefetch: %v", err)
} else {
t.Logf("prefetch: %d downloaded, %d skipped", dl, sk)
}
}
// Surface which cells actually overlap so we can rule out coverage gaps.
for _, z := range zooms {
minLon, minLat, maxLon, maxLat := gridBBox(centerLon, centerLat, z, grid)
cells := catalog.CellsForBBox(minLon, minLat, maxLon, maxLat, 0, 0)
t.Logf("z=%d bbox=[%.3f,%.3f,%.3f,%.3f] -> %d cells overlap",
z, minLon, minLat, maxLon, maxLat, len(cells))
for _, c := range cells {
s57 := store.S57Path(c.Name)
t.Logf(" %-12s scale=1:%-7d on_disk=%v", c.Name, c.CScale, s57 != "")
}
}
for _, z := range zooms {
path := filepath.Join(outDir, fmt.Sprintf("gsb-z%d.png", z))
if err := renderMosaic(t, renderer, centerLon, centerLat, z, grid, path); err != nil {
t.Errorf("z=%d mosaic: %v", z, err)
continue
}
t.Logf("wrote %s", path)
}
t.Logf("open output: open %s", outDir)
}
// gridBBox returns the lon/lat box covered by an NxN tile grid centered on the
// given lon/lat at the given zoom.
func gridBBox(centerLon, centerLat float64, z, n int) (float64, float64, float64, float64) {
cx, cy := lonLatToTile(centerLon, centerLat, z)
half := n / 2
x0, y0 := cx-half, cy-half
x1, y1 := x0+n-1, y0+n-1
xminMerc, yminMerc, _, _ := tileBBoxMercator(tileXYZ{x: x0, y: y1, z: z})
_, _, xmaxMerc, ymaxMerc := tileBBoxMercator(tileXYZ{x: x1, y: y0, z: z})
minLon, maxLat := mercToLonLat(xminMerc, ymaxMerc)
maxLon, minLat := mercToLonLat(xmaxMerc, yminMerc)
return minLon, minLat, maxLon, maxLat
}
// renderMosaic renders an NxN tile grid centered on the given lon/lat and
// stitches it into a single PNG so it's easy to scan visually.
func renderMosaic(t *testing.T, renderer *ENCRenderer, centerLon, centerLat float64, z, n int, outPath string) error {
cx, cy := lonLatToTile(centerLon, centerLat, z)
half := n / 2
x0, y0 := cx-half, cy-half
const tileSize = 256
mosaic := image.NewRGBA(image.Rect(0, 0, n*tileSize, n*tileSize))
type job struct{ row, col int }
jobs := make(chan job, n*n)
for row := range n {
for col := range n {
jobs <- job{row: row, col: col}
}
}
close(jobs)
var wg sync.WaitGroup
var rendered, empty, failed int64
workers := runtime.NumCPU()
for range workers {
wg.Add(1)
go func() {
defer wg.Done()
for j := range jobs {
tx := x0 + j.col
ty := y0 + j.row
pngBytes, err := renderer.RenderTile(z, tx, ty, RenderOptions{
SafeDepthM: 8.0 / feetPerMetre,
Style: StyleWMS,
})
if err != nil {
atomic.AddInt64(&failed, 1)
t.Logf(" z=%d/%d/%d: %v", z, tx, ty, err)
continue
}
if len(pngBytes) < 200 {
atomic.AddInt64(&empty, 1)
}
img, err := png.Decode(bytes.NewReader(pngBytes))
if err != nil {
atomic.AddInt64(&failed, 1)
t.Logf(" z=%d/%d/%d decode: %v", z, tx, ty, err)
continue
}
dst := image.Rect(j.col*tileSize, j.row*tileSize, (j.col+1)*tileSize, (j.row+1)*tileSize)
draw.Draw(mosaic, dst, img, image.Point{}, draw.Over)
atomic.AddInt64(&rendered, 1)
}
}()
}
wg.Wait()
t.Logf("z=%d: rendered=%d empty=%d failed=%d", z, rendered, empty, failed)
out, err := os.Create(outPath)
if err != nil {
return err
}
defer out.Close()
return png.Encode(out, mosaic)
}
func envOr(k, def string) string {
if v := os.Getenv(k); v != "" {
return v
}
return def
}
func envOrInt(t *testing.T, k string, def int) int {
v := os.Getenv(k)
if v == "" {
return def
}
var n int
if _, err := fmt.Sscanf(v, "%d", &n); err != nil {
t.Fatalf("env %s=%q: %v", k, v, err)
}
return n
}
func envOrInts(t *testing.T, k string, def []int) []int {
v := os.Getenv(k)
if v == "" {
return def
}
var out []int
for _, s := range splitComma(v) {
var n int
if _, err := fmt.Sscanf(s, "%d", &n); err != nil {
t.Fatalf("env %s=%q: %v", k, v, err)
}
out = append(out, n)
}
return out
}
func envOrFloat(t *testing.T, k string, def float64) float64 {
v := os.Getenv(k)
if v == "" {
return def
}
var f float64
if _, err := fmt.Sscanf(v, "%f", &f); err != nil {
t.Fatalf("env %s=%q: %v", k, v, err)
}
return f
}
func splitComma(s string) []string {
var out []string
cur := ""
for _, r := range s {
if r == ',' {
if cur != "" {
out = append(out, cur)
}
cur = ""
continue
}
cur += string(r)
}
if cur != "" {
out = append(out, cur)
}
return out
}
// mustUserCacheDir returns the same cache root the running Go server uses
// (~/Library/Caches on macOS, $XDG_CACHE_HOME or ~/.cache on Linux). Using a
// matching path means the test reads the cells the server has already
// downloaded instead of re-fetching into a parallel directory.
func mustUserCacheDir(t *testing.T) string {
d, err := os.UserCacheDir()
if err != nil {
t.Fatalf("user cache dir: %v", err)
}
return d
}