From 6e6111336623ac582af5ce5b324f66a1b401a297 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 29 May 2025 18:43:48 +0100 Subject: [PATCH] replace in-memory cache with a persistent json cache we just store the raw response from overpass --- pkgs/maps/tiles.fnl | 51 ++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/pkgs/maps/tiles.fnl b/pkgs/maps/tiles.fnl index 3ff47e9..f6351c1 100644 --- a/pkgs/maps/tiles.fnl +++ b/pkgs/maps/tiles.fnl @@ -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 }