aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoruser <user@node5.net>2024-12-28 20:16:24 +0100
committeruser <user@node5.net>2024-12-28 20:16:24 +0100
commitdb513ee7a3bd240a70c5b75456773a4a123e9b97 (patch)
treeb48960e3a8f02d8435aa7cb571c96012c07b86e7 /src
parentef52a7a40c50916cc0b52e7a212277d0ddd70904 (diff)
Categories - Add category type prefix e.g. shop:supermarket
Diffstat (limited to 'src')
-rw-r--r--src/db_handler.py32
-rw-r--r--src/map_node5_net.py2
-rw-r--r--src/static/main.js2
-rw-r--r--src/templates/options.html2
4 files changed, 20 insertions, 18 deletions
diff --git a/src/db_handler.py b/src/db_handler.py
index 83cdad8..fd68c39 100644
--- a/src/db_handler.py
+++ b/src/db_handler.py
@@ -16,15 +16,16 @@ with open(os.path.join('configs', 'database.yml'), 'r') as file:
with psycopg.connect(**db_con_params) as conn:
with conn.cursor() as cur:
cur.execute("""
- SELECT subclass
- FROM poi
- WHERE subclass NOT LIKE '%;%'
- GROUP BY class, subclass
- HAVING COUNT(*) > 1
- ;
+SELECT class, subclass
+FROM poi
+WHERE subclass NOT LIKE '%;%'
+GROUP BY class, subclass
+HAVING COUNT(*) > 1
+ORDER BY class, subclass
+;
""")
categories = cur.fetchall()
- categories = [category[0] for category in categories]
+ categories = [(f"{category[0]}:{category[1]}") for category in categories]
print(f"Loaded: {len(categories)} categories")
cur.execute("""
@@ -54,14 +55,15 @@ def get_all(country: str, category: str) -> (list[dict]):
raise IllegalInstructionException("Category not found")
if country not in countries:
raise IllegalInstructionException("Country not found")
- else:
- with psycopg.connect(**db_con_params, row_factory=psycopg.rows.dict_row) as conn:
- with conn.cursor() as cur:
- cur.execute("""
+ class_, sub_class = category.split(':')
+ with psycopg.connect(**db_con_params, row_factory=psycopg.rows.dict_row) as conn:
+ with conn.cursor() as cur:
+ cur.execute("""
WITH filtered AS (
SELECT osm_id, name, brand, geom, class, subclass
FROM poi
-WHERE subclass = %(subclass)s
+WHERE class = %(class)s
+AND subclass = %(subclass)s
AND country = %(country)s
)
@@ -79,8 +81,8 @@ SELECT
FROM filtered
) polygon ON ST_Contains(polygon.geom, filtered.geom)
;
- """, {'subclass': category, 'country': country})
+ """, {'class': class_, 'subclass': sub_class, 'country': country})
- all = cur.fetchall()
- return all
+ all = cur.fetchall()
+ return all
diff --git a/src/map_node5_net.py b/src/map_node5_net.py
index aba576e..fa8a425 100644
--- a/src/map_node5_net.py
+++ b/src/map_node5_net.py
@@ -65,7 +65,7 @@ def chains():
@app.route("/categories.json")
def categories():
categories = db_handler.categories
- return categories
+ return json.dumps(categories)
@app.route("/all.json")
def all():
diff --git a/src/static/main.js b/src/static/main.js
index ab22590..3ef9cf1 100644
--- a/src/static/main.js
+++ b/src/static/main.js
@@ -21,7 +21,7 @@ const heatmap_radius_element = document.getElementById("HeatmapRadius");
var shops;
-const DefaultCategory = "supermarket";
+const DefaultCategory = "shop:supermarket";
const DefaultCountry = "Denmark";
var Categories = []
diff --git a/src/templates/options.html b/src/templates/options.html
index 85ae8e9..aaec4cc 100644
--- a/src/templates/options.html
+++ b/src/templates/options.html
@@ -1,7 +1,7 @@
<h1>POI map</h1>
<form id="QueryForm" disabled>
<input class="form-control" list="CountriesDataList" id="Country" name="Country" placeholder="Type to search..."
- value="Denmark"> {# Value set to empty string is important for JS function #}
+ value="Denmark" autocomplete="off"> {# Value set to empty string is important for JS function #}
<datalist id="CountriesDataList">
<option value="Denmark">Denmark</option>
<option value="Germany">Germany</option>