From 398693bc0797747eda967ee93a8fad74eb42b694 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Wed, 28 May 2025 18:50:12 +0100 Subject: [PATCH] hook the ui up to the overpass data --- pkgs/maps/main.fnl | 57 +++++++++++++++++++---------------- pkgs/maps/tiles.fnl | 73 ++++++++++++++++++++++++++++----------------- 2 files changed, 76 insertions(+), 54 deletions(-) diff --git a/pkgs/maps/main.fnl b/pkgs/maps/main.fnl index 316d115..fc2cf33 100644 --- a/pkgs/maps/main.fnl +++ b/pkgs/maps/main.fnl @@ -1,12 +1,12 @@ ; (local { : view } (require :fennel)) (local { : fdopen } (require :posix.stdio)) (local nmea (require :nmea)) +(local tiles (require :tiles)) (import-macros { : define-tests : expect : expect= } :assert) (local { : Gtk - : OsmGpsMap : Gdk : Gio : GLib @@ -52,20 +52,37 @@ label.readout { (local state-widgets { }) -(fn osm-widget [] - (let [w - (doto (OsmGpsMap.Map {}) - (tset :map-source OsmGpsMap.MapSource_t.OPENSTREETMAP) - (: :set_center_and_zoom 52.595 -0.1 17) - (: :layer_add (OsmGpsMap.MapOsd { - :show_copyright true - ; :show_coordinates true - :show_scale true - })) - )] - (tset state-widgets :osm w) - w)) +(local + app-state { + :time-of-day 0 + :elapsed-time 0 + :speed 14 + :lat 49 + :lon 0 + :course 22 + } + ) +(fn osm-widget [] + (let [height 256] + (Gtk.Label { + :width height :height height + :on_draw + (fn [self g] + (print app-state.lat app-state.lon ) + (let [lines (tiles.polylines app-state.lat app-state.lon 17)] + (g:set_source_rgb 0.2 0.2 0.4) + (g:set_line_width 3) + (each [_ line (ipairs 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)) + }))) (fn readout [name text] (let [w @@ -89,18 +106,6 @@ label.readout { (expect= (hhmmss (+ 45 (* 60 12) (* 60 60 3))) "3:12:45") -(local - app-state { - :time-of-day 0 - :elapsed-time 0 - :speed 14 - :lat 49 - :lon 0 - :course 22 - } - ) - - (fn merge [table1 table2] (collect [k v (pairs table2) &into table1] k v)) diff --git a/pkgs/maps/tiles.fnl b/pkgs/maps/tiles.fnl index 5325e85..79a444c 100644 --- a/pkgs/maps/tiles.fnl +++ b/pkgs/maps/tiles.fnl @@ -5,35 +5,8 @@ (import-macros { : define-tests : expect : expect= : expect-near } :assert) (local { : view } (require :fennel)) +(local f% string.format) -(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)) @@ -69,3 +42,47 @@ (expect= (math.floor x) 65343) (expect= (math.floor y) 43221)) + +(fn overpass [lat lon] + (let [n (+ lat 0.01) + w (- lon 0.01) + s lat + e lon] + (-> + [ + "[out:json];" + (f% "way(%f,%f,%f,%f)['highway'];" s w n e) + "(._;>;);" + "out;" + ] + (table.concat "\n")))) + +(fn canvas [elements offset-x offset-y] + (let [nodes {} + lines []] + (each [_ e (ipairs elements)] + (case e.type + :node (tset nodes e.id e) + :way + (table.insert + lines + (icollect [_ nd (ipairs e.nodes)] + (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)) ]))))) + lines)) + +(fn polylines [lat long zoom] + (let [r + (req.new_from_uri + "https://overpass-api.de/api/interpreter") + query { :data (overpass lat long zoom) }] + (tset r.headers ":method" "POST") + (r:set_body (dict_to_query query)) + (let [(headers stream) (r:go) + (tx ty) (latlon->tile lat long zoom) + data (json.decode (stream:get_body_as_string))] + (canvas data.elements (math.floor tx) (math.floor ty))))) + +{ : polylines }