diff options
| -rw-r--r-- | create_database.sql | 23 | ||||
| -rw-r--r-- | src/db_handler.py | 9 | ||||
| -rw-r--r-- | src/shop_map_node5_net.py | 16 | ||||
| -rw-r--r-- | src/static/main.js | 19 |
4 files changed, 41 insertions, 26 deletions
diff --git a/create_database.sql b/create_database.sql index 191506b..50f11de 100644 --- a/create_database.sql +++ b/create_database.sql @@ -1,14 +1,23 @@ CREATE database shop; CREATE EXTENSION postgis; -CREATE EXTENSION uint; -CREATE TYPE color AS (red uint1, green uint1, blue uint1); +CREATE TABLE chain (name text, color text); -CREATE TABLE shop_color (shop text, color color); +INSERT INTO chain(name, color) VALUES + ('Netto', 'rgb(255, 238, 82)'), +('365discount', '#00aa46'), +('Rema 1000', '#002154'), +('Min Købmand', '#054628'), +('Føtex', '#242930'), +('Dagli''Brugsen', '#c31414'), +('Brugsen', '#c31414'), +('SuperBrugsen', '#c31414'), +('Spar', '#ed1c24'), +('Føtex Food', '#242930'), +('Meny', '#ce0029'), +('Kvickly', '#c31414'), +('Løvbjerg', '#a21837'), +('Lidl', '#0050aa') -INSERT INTO shop_color(shop, color) VALUES -('Netto', (255, 238, 82)), -('365discount', (0, 170, 70)), -('Rema 1000', (0, 36, 88)) ;
\ No newline at end of file diff --git a/src/db_handler.py b/src/db_handler.py index 5798c66..0fd3ec1 100644 --- a/src/db_handler.py +++ b/src/db_handler.py @@ -7,6 +7,15 @@ with open(os.path.join('configs', 'database.yml'), 'r') as file: db_con_params = yaml.safe_load(file.read()) +def get_chains() -> (list[dict]): + with psycopg.connect(**db_con_params, row_factory=psycopg.rows.dict_row) as conn: + with conn.cursor() as cur: + cur.execute(""" + SELECT name, color FROM chain + """) + chains = cur.fetchall() + return chains + def get_all() -> (list[dict]): 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 44c6cb5..3be40e2 100644 --- a/src/shop_map_node5_net.py +++ b/src/shop_map_node5_net.py @@ -15,10 +15,16 @@ def index(): @app.route("/chains") def chains(): - icons = os.listdir(os.path.join(os.getcwd(), 'src', 'static', 'icons')) - icons.pop(icons.index('Unknown.png')) - chains = [os.path.splitext(a)[0] for a in icons] - return json.dumps(chains) + files = os.listdir(os.path.join(os.getcwd(), 'src', 'static', 'icons')) + files.pop(files.index('Unknown.png')) + icon_names = set([os.path.splitext(a)[0] for a in files]) + + chains = db_handler.get_chains() + chain_names = set((a['name'] for a in chains)) + if icon_names != chain_names: + print(f'WARNING ICON MISSING, OR DB ENTRY MISSING') + color_by_name = {a['name']: a['color'] for a in chains} + return json.dumps(color_by_name) @app.route("/supermarkets.json") @@ -35,10 +41,8 @@ def voronoi_polygons(): def all(): rows = db_handler.get_all() for row in rows: - print(row['polygon']) coordinates = [] for coordinate in json.loads(row['polygon'])['coordinates'][0]: coordinates.append([coordinate[1], coordinate[0]]) row['polygon'] = coordinates - print(row['polygon']) return json.dumps(rows) diff --git a/src/static/main.js b/src/static/main.js index c37c92b..82d474e 100644 --- a/src/static/main.js +++ b/src/static/main.js @@ -10,14 +10,12 @@ let icons = {}; async function fetchChains() { const response = await fetch("chains"); known_store_chains = await response.json(); - icons["Unknown"] = L.icon({ iconUrl: 'icons/Unknown.png', iconSize: [10, 16], iconAnchor: [16, 16], }); - - for (let known_store_chain of known_store_chains) { + for (let known_store_chain in known_store_chains) { icons[known_store_chain] = L.icon({ iconUrl: `icons/${known_store_chain}.png`, iconAnchor: [8, 8], @@ -28,24 +26,19 @@ async function fetchChains() { function addPolygon(store) { - console.log(store) let color; - if (store.brand === 'Rema 1000'){ - color = 'blue'; - } else if (store.brand === 'Netto'){ - color = 'yellow' + if (known_store_chains.hasOwnProperty(store.brand)) { + color = known_store_chains[store.brand]; + } else { + color = 'grey'; } - else if (store.brand === 'Coop 365discount'){ - color = 'green'; - }else{ - color = 'red'} L.polygon(store.polygon, {color: color}).addTo(map); } function addShop(shop) { let icon; - if (known_store_chains.includes(shop.brand)){ + if (known_store_chains.hasOwnProperty(shop.brand)){ icon = icons[shop.brand]; } else { icon = icons["Unknown"]; |
