fetch enough tiles to cover the display

This commit is contained in:
Daniel Barlow 2025-05-29 18:13:47 +01:00
parent 995880e5a3
commit 86682a2ad6
2 changed files with 49 additions and 27 deletions

View File

@ -75,39 +75,51 @@ label.readout {
;; on the screen, and enough tiles either side of it
;; to fill the width of the screen plus a bit
(fn osm-widget []
(let [height tile-size
num-tiles-x (+ 1 (math.ceil (/ map-width tile-size)))
num-tiles-y (+ 1 (math.ceil (/ map-height tile-size)))
(fn cairo-the-map [self g]
(let [{ : lat : lon : zoom } app-state
num-tiles-x (+ 0 (math.ceil (/ map-width tile-size)))
num-tiles-y (+ 0 (math.ceil (/ map-height tile-size)))
; _ (print app-state.lat app-state.lon)
(tile-x tile-y) (tiles.latlon->tile app-state.lat app-state.lon app-state.zoom)
min-tile-x (math.floor (- tile-x (/ num-tiles-x 2)))
max-tile-x (math.ceil (+ tile-x (/ num-tiles-x 2)))
min-tile-y (math.floor (- tile-y (/ num-tiles-y 2)))
max-tile-y (math.ceil (+ tile-y (/ num-tiles-y 2)))
left-edge (- tile-x (/ map-width 2 tile-size))
top-edge (- tile-y (/ map-height 2 tile-size))
lines []]
(for [x min-tile-x max-tile-x]
(for [y min-tile-y max-tile-y]
(print :x x :y y)
(merge lines (tiles.polylines x y app-state.zoom))))
(merge lines (tiles.polylines x y zoom))))
(Gtk.Label {
:width height :height height
:on_draw
(fn [self g]
(print app-state.lat app-state.lon )
(g:set_source_rgb 0.2 0.2 0.4)
(g:set_line_width 3)
(each [_ line (pairs lines)]
(case line
[[sx sy] & more]
(do
(g:move_to sx sy)
(each [_ [x y] (ipairs more)]
(g:line_to x y)))))
(g:stroke)
true)
})))
;; put tile-x and tile-y in the centre of the visible area.
(g:translate (* tile-size (- min-tile-x left-edge))
(* tile-size (- min-tile-y top-edge)))
(g:set_source_rgb 0.2 0.2 0.4)
(g:set_line_width 3)
(each [_ line (pairs lines)]
(case line
[[sx sy] & more]
(do
(g:move_to (* tile-size (- sx min-tile-x))
(* tile-size (- sy min-tile-y)))
(each [_ [x y] (ipairs more)]
(let [x1 (* tile-size (- x min-tile-x))
y1 (* tile-size (- y min-tile-y))]
(g:line_to x1 y1))))))
(g:stroke)
true))
(fn osm-widget []
(Gtk.Label {
:width map-width :height map-height
:on_draw cairo-the-map
}))
(fn readout [name text]
(let [w

View File

@ -45,7 +45,6 @@
(fn overpass [lat lon zoom]
(let [width (/ 360 (^ 2 zoom))
_ (print :w zoom width)
n (+ lat width) ;XXX adjust for latitude
w (- lon width)
s lat
@ -73,13 +72,12 @@
(let [node (. nodes nd)
(tx ty) (latlon->tile node.lat node.lon 17)]
;;(print e.tags.name e.id e.name node.lat node.lon)
[ (* 256 (- tx offset-x)) (* 256 (- ty offset-y)) ])))))
[ tx ty ])))))
lines))
(fn polylines [x y zoom]
(fn polylines-from-net [x y zoom]
(let [(lat lon) (tile->latlon x y zoom)
o (overpass lat lon zoom)
_ (print :polylines x y o)
r
(req.new_from_uri
"https://overpass-api.de/api/interpreter")
@ -91,4 +89,16 @@
data (json.decode (stream:get_body_as_string))]
(canvas data.elements (math.floor tx) (math.floor ty)))))
(local cache {})
(fn polylines [x y zoom]
(let [k (.. x "/" y "/" zoom)
lines (. cache k)]
(print k (not (not lines)))
(if lines
(do (print "cached! " x y zoom) lines)
(let [lines (polylines-from-net x y zoom)]
(tset cache k lines)
lines))))
{ : polylines : latlon->tile }