Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Initial circuits page

Russ Garrettf8d48417bb5d
diff --git a/web-backend/src/templates/circuits.html b/web-backend/src/templates/circuits.html
new file mode 100644
index 0000000..7e7cc9c
--- /dev/null
+++ b/web-backend/src/templates/circuits.html
@@ -0,0 +1,29 @@
+{% extends "_base.html" %} {% import "_tables.html" as tables %}
+{% set breadcrumbs = [('Stats', url_for('stats')), ('Circuits', None)] %}
+{% block title%}Circuits {%- endblock %}
+{% block content -%}
+
+<div class="table-wrapper">
+ <table>
+ <caption>Circuits</caption>
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Length</th>
+ <th>Substations</th>
+ <th>Wikidata</th>
+ <tr>
+ </thead>
+ <tbody>
+ {% for row in circuits %}
+ <tr>
+ <td>{{row.name}}{% if row.name_en%}{% if row.name %}<br>{% endif %}({{row.name_en}}){% endif %}</td>
+ <td>{{row.circuit_length|round|int}}&nbsp;km</td>
+ <td>{{row.substations}}</td>
+ <td>{% if row.wikidata %}{{row.wikidata}}{% endif %}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+</div>
+{% endblock %}
diff --git a/web-backend/src/views/__init__.py b/web-backend/src/views/__init__.py
index 3cdc0cc..832ecb8 100644
--- a/web-backend/src/views/__init__.py
+++ b/web-backend/src/views/__init__.py
@@ -2,10 +2,10 @@ from itertools import chain
from starlette.routing import BaseRoute
-from . import api, area, base, country, search, wikidata
+from . import api, area, base, circuit, country, search, wikidata
routes: list[BaseRoute] = list(
chain.from_iterable(
- [base.routes, api.routes, wikidata.routes, search.routes, area.routes, country.routes]
+ [base.routes, api.routes, wikidata.routes, search.routes, area.routes, country.routes, circuit.routes]
)
)
diff --git a/web-backend/src/views/circuit.py b/web-backend/src/views/circuit.py
new file mode 100644
index 0000000..242cff5
--- /dev/null
+++ b/web-backend/src/views/circuit.py
@@ -0,0 +1,40 @@
+from sqlalchemy import text
+from starlette.responses import Response
+from starlette.routing import Route
+
+from .. import get_db
+from ..templates import render_template
+from ..util import cache_for
+
+
+@cache_for(hours=1)
+async def circuits(request) -> Response:
+ database = get_db(request)
+
+ result = (
+ await database.execute(
+ text(
+ """SELECT r.tags->'type' AS type, r.osm_id, r.name, r.tags->'name:en' AS name_en,
+ r.tags->'wikidata' AS wikidata,
+ COALESCE((SELECT sum(st_length(st_transform(m.geometry, 4326)::geography)) / 1000
+ FROM osm_power_circuit_relation_member m
+ WHERE m.osm_id = r.osm_id
+ AND m.role = 'section'), 0) AS circuit_length,
+ (SELECT count(*)
+ FROM osm_power_circuit_relation_member m
+ WHERE m.osm_id = r.osm_id
+ AND m.role = 'substation') AS substations
+ FROM osm_power_circuit_relation r
+ order by circuit_length desc
+ limit 50
+ """
+ )
+ )
+ ).fetchall()
+
+ return render_template(request, "circuits.html", {"request": request, "circuits": result})
+
+
+routes = [Route("/stats/circuit", endpoint=circuits)]
+
+__all__ = ["routes"]