replace in-memory cache with a persistent json cache

we just store the raw response from overpass
This commit is contained in:
Daniel Barlow 2025-05-29 18:43:48 +01:00
parent 86682a2ad6
commit 6e61113366

View File

@ -58,7 +58,7 @@
]
(table.concat "\n"))))
(fn canvas [elements offset-x offset-y]
(fn canvas [elements]
(let [nodes {}
lines {}]
(each [_ e (ipairs elements)]
@ -75,30 +75,43 @@
[ tx ty ])))))
lines))
(fn polylines-from-net [x y zoom]
(let [(lat lon) (tile->latlon x y zoom)
o (overpass lat lon zoom)
(fn file-exists? [name]
(match (io.open name :r)
f (do (f:close) true)
_ false))
(fn unparsed-from-disk [x y zoom fetch-fn]
(let [k (.. x "_" y "_" zoom)
pathname (.. "/tmp/tiles/" k ".json")]
(if (file-exists? pathname)
(with-open [i (io.open pathname :r)]
(i:read "*a"))
(with-open [j (io.open pathname :w)]
(let [g (fetch-fn)]
(j:write g)
g)))))
(fn unparsed-for-xyz [x y zoom]
(let [(lat lon) (tile->latlon x y zoom)
o (overpass lat lon zoom)
r
(req.new_from_uri
"https://overpass-api.de/api/interpreter")
query { :data o }]
(tset r.headers ":method" "POST")
(r:set_body (dict_to_query query))
(let [(headers stream) (r:go)
(tx ty) (latlon->tile lat lon zoom)
data (json.decode (stream:get_body_as_string))]
(canvas data.elements (math.floor tx) (math.floor ty)))))
(let [(headers stream) (r:go)]
(stream:get_body_as_string))))
(local cache {})
(fn polylines-from-net [x y zoom]
(let [s (unparsed-from-disk
x y zoom
(fn []
(unparsed-for-xyz x y zoom)))
;_ (print :unoparsed (s:sub 1 40))
data (json.decode s)]
(canvas data.elements)))
(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 }
{ :polylines polylines-from-net : latlon->tile }