From 0d1ba0fc1bc2c64c7aaaf71f98fd96f35b8b1bab Mon Sep 17 00:00:00 2001 From: user Date: Sun, 7 Apr 2024 23:10:05 +0200 Subject: add store heatmap --- src/db_handler.py | 13 +++++++++++++ src/shop_map_node5_net.py | 5 +++++ src/static/index.html | 1 + src/static/main.js | 18 +++++++++++++++--- 4 files changed, 34 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/db_handler.py b/src/db_handler.py index 0fd3ec1..5a2ab73 100644 --- a/src/db_handler.py +++ b/src/db_handler.py @@ -56,6 +56,19 @@ def get_supermarkets() -> (list[dict], list[str]): return supermarkets +def get_stores() -> (list[dict], list[str]): + with psycopg.connect(**db_con_params) as conn: + with conn.cursor() as cur: + cur.execute(""" + SELECT + ST_Y(ST_Transform(geom, 4326)) AS lat, ST_X(ST_Transform(geom, 4326)) AS long + FROM shop; + """) + + stores = cur.fetchall() + return stores + + def voronoi_polygons() -> (list[dict], list[str]): with psycopg.connect(**db_con_params, row_factory=psycopg.rows.dict_row) as conn: with conn.cursor() as cur: diff --git a/src/shop_map_node5_net.py b/src/shop_map_node5_net.py index 3be40e2..8ab97bc 100644 --- a/src/shop_map_node5_net.py +++ b/src/shop_map_node5_net.py @@ -27,6 +27,11 @@ def chains(): return json.dumps(color_by_name) +@app.route("/stores.json") +def stores(): + return db_handler.get_stores() + + @app.route("/supermarkets.json") def supermarkets(): return db_handler.get_supermarkets() diff --git a/src/static/index.html b/src/static/index.html index 115bc76..5bfcc1f 100644 --- a/src/static/index.html +++ b/src/static/index.html @@ -9,6 +9,7 @@ + diff --git a/src/static/main.js b/src/static/main.js index 26ba90b..27fa2eb 100644 --- a/src/static/main.js +++ b/src/static/main.js @@ -8,9 +8,13 @@ let known_store_chains; let icons = {}; var markers = L.featureGroup().addTo(map); +var polygons = L.featureGroup().addTo(map); +var heatmap = L.featureGroup().addTo(map); var overlayMaps = { - "Icons": markers + "Icons": markers, + "Polygons": polygons, + "Heatmap": heatmap }; var layerControl = L.control.layers({},overlayMaps).addTo(map); @@ -40,7 +44,7 @@ function addPolygon(store) { } else { color = 'grey'; } - L.polygon(store.polygon, {color: color}).addTo(map); + L.polygon(store.polygon, {color: color}).addTo(polygons); } function addShop(shop) { @@ -66,6 +70,12 @@ async function fetchAll() { shops.forEach((shop) => addShop(shop)); } +async function fetchShopHeatmap() { + const response = await fetch("stores.json"); + const shops = await response.json(); + var heat = L.heatLayer(shops, {radius: 30}).addTo(heatmap); +} + async function fetch_voronoi_polygons() { const response = await fetch("voronoi_polygons.json"); @@ -83,4 +93,6 @@ map.on('moveend', function() { update() }); -fetchChains() \ No newline at end of file +fetchChains() + +fetchShopHeatmap(); -- cgit v1.2.3