72 lines
1.7 KiB
Fennel
72 lines
1.7 KiB
Fennel
(local req (require :http.request))
|
|
(local { : dict_to_query } (require :http.util))
|
|
(local json (require :json))
|
|
|
|
(import-macros { : define-tests : expect : expect= : expect-near } :assert)
|
|
(local { : view } (require :fennel))
|
|
|
|
|
|
(local
|
|
query
|
|
(->
|
|
[
|
|
"[bbox:30.618338,-96.323712,30.591028,-96.330826]"
|
|
"[out:json]"
|
|
"[timeout:90];"
|
|
"("
|
|
"way ("
|
|
"30.626917110746,"
|
|
"-96.348809105664,"
|
|
"30.634468750236,"
|
|
"-96.339893442898"
|
|
");"
|
|
");"
|
|
"out ;"
|
|
]
|
|
(table.concat "\n")))
|
|
|
|
(let [r
|
|
(req.new_from_uri
|
|
"https://overpass-api.de/api/interpreter")]
|
|
(tset r.headers ":method" "POST")
|
|
(r:set_body (dict_to_query { :data query }))
|
|
(let [(headers stream) (r:go)]
|
|
(print (view headers))
|
|
(print (view (json.decode (stream:get_body_as_string))))))
|
|
|
|
(fn sinh [x] (/ (- 1 (math.exp (* -2 x))) (* 2 (math.exp (- x)))))
|
|
|
|
(expect (< (math.abs (- (sinh 2) 3.626860407847)) 0.001))
|
|
|
|
(fn tile->latlon [xtile ytile zoom]
|
|
(let [n (^ 2 zoom)
|
|
lon-deg (- (/ (* xtile 360) n) 180.0)
|
|
lat-rad (math.atan
|
|
(sinh (* math.pi (- 1 (/ (* 2 ytile) n))))
|
|
)]
|
|
|
|
(values (/ (* lat-rad 180) math.pi) lon-deg)))
|
|
|
|
(let [(lat lon) (tile->latlon 0 0 0)]
|
|
(expect= lon -180)
|
|
(expect-near lat 85.05112877)
|
|
)
|
|
|
|
(let [(lat lon) (tile->latlon 232798 103246 18)]
|
|
(expect-near lon 139.699401855)
|
|
(expect-near lat 35.6595278648)
|
|
)
|
|
|
|
(fn latlon->tile [lat lon zoom]
|
|
(let [n (^ 2 zoom)
|
|
x (* n (/ (+ lon 180) 360))
|
|
t1 (/ (* lat math.pi) 180)
|
|
t (math.log (+ (math.tan t1) (/ 1 (math.cos t1))))
|
|
y (* (- 1 (/ t math.pi)) (/ n 2))]
|
|
(values x y)))
|
|
|
|
(let [(x y) (latlon->tile 52.1234 -0.53 17)]
|
|
(expect= (math.floor x) 65343)
|
|
(expect= (math.floor y) 43221))
|
|
|