replace in-memory cache with a persistent json cache
we just store the raw response from overpass
This commit is contained in:
parent
86682a2ad6
commit
6e61113366
@ -58,7 +58,7 @@
|
|||||||
]
|
]
|
||||||
(table.concat "\n"))))
|
(table.concat "\n"))))
|
||||||
|
|
||||||
(fn canvas [elements offset-x offset-y]
|
(fn canvas [elements]
|
||||||
(let [nodes {}
|
(let [nodes {}
|
||||||
lines {}]
|
lines {}]
|
||||||
(each [_ e (ipairs elements)]
|
(each [_ e (ipairs elements)]
|
||||||
@ -75,30 +75,43 @@
|
|||||||
[ tx ty ])))))
|
[ tx ty ])))))
|
||||||
lines))
|
lines))
|
||||||
|
|
||||||
(fn polylines-from-net [x y zoom]
|
|
||||||
(let [(lat lon) (tile->latlon x y zoom)
|
(fn file-exists? [name]
|
||||||
o (overpass lat lon zoom)
|
(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
|
r
|
||||||
(req.new_from_uri
|
(req.new_from_uri
|
||||||
"https://overpass-api.de/api/interpreter")
|
"https://overpass-api.de/api/interpreter")
|
||||||
query { :data o }]
|
query { :data o }]
|
||||||
(tset r.headers ":method" "POST")
|
(tset r.headers ":method" "POST")
|
||||||
(r:set_body (dict_to_query query))
|
(r:set_body (dict_to_query query))
|
||||||
(let [(headers stream) (r:go)
|
(let [(headers stream) (r:go)]
|
||||||
(tx ty) (latlon->tile lat lon zoom)
|
(stream:get_body_as_string))))
|
||||||
data (json.decode (stream:get_body_as_string))]
|
|
||||||
(canvas data.elements (math.floor tx) (math.floor ty)))))
|
|
||||||
|
|
||||||
(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 }
|
||||||
|
Loading…
Reference in New Issue
Block a user