Open Infrastructure Map, a view of infrastructure data in OpenStreetMap
Rename country endpoints to region
Russ Garrettd8014852f7b4
diff --git a/web-backend/.gitignore b/web-backend/.gitignoreindex 674cab4..3a39954 100644--- a/web-backend/.gitignore+++ b/web-backend/.gitignore@@ -138,3 +138,4 @@ dmypy.jsoncython_debug/.DS_Store+.ruff_cachediff --git a/web-backend/main.py b/web-backend/main.pyindex 9e57c37..90b9ba0 100644--- a/web-backend/main.py+++ b/web-backend/main.py@@ -25,7 +25,7 @@ from template_functions import (format_external_url,)from config import database, config-from util import cache_for, country_required+from util import cache_for, region_requiredfrom sitemap import sitemapfrom data import (get_countries,@@ -149,10 +149,10 @@ async def stats_charts(request):)[email protected]("/stats/area/{country}")-@country_required[email protected]("/stats/area/{region}")+@region_required@cache_for(3600)-async def country(request, country):+async def region(request, region):async with asyncio.TaskGroup() as tg:plant_stats = tg.create_task(database.fetch_one(@@ -162,7 +162,7 @@ async def country(request, country):AND eez.gid = :gidAND tags -> 'construction:power' IS NULL""",- values={"gid": country["gid"]},+ values={"gid": region["gid"]},))@@ -175,23 +175,23 @@ async def country(request, country):AND tags -> 'construction:power' IS NULLGROUP BY first_semi(source)ORDER BY SUM(convert_power(output)) DESC NULLS LAST""",- values={"gid": country["gid"]},+ values={"gid": region["gid"]},))- power_lines = tg.create_task(stats_power_line(country["union"]))+ power_lines = tg.create_task(stats_power_line(region["union"]))grid_summary_chart = tg.create_task(- charts.country.grid_summary(country["union"])+ charts.country.grid_summary(region["union"]))plant_summary_chart = tg.create_task(- charts.country.plant_summary(country["union"])+ charts.country.plant_summary(region["union"]))return templates.TemplateResponse("country.html",{"request": request,- "country": country["union"],+ "country": region["union"],"plant_stats": plant_stats.result()._mapping,"plant_source_stats": plant_source_stats.result(),"power_lines": power_lines.result(),@@ -203,16 +203,16 @@ async def country(request, country):"plant_summary": json.dumps(json_item(plant_summary_chart.result(), "plant-summary", charts.theme)),- "canonical": request.url_for("country", country=country["union"]),+ "canonical": request.url_for("region", region=region["union"]),},)[email protected]("/stats/area/{country}/plants")-@country_required[email protected]("/stats/area/{region}/plants")+@region_required@cache_for(3600)-async def plants_country(request, country):- gid = country[0]+async def plants_region(request, region):+ gid = region[0]plants = await database.fetch_all(query="""SELECT osm_id, name, tags->'name:en' AS name_en, tags->'wikidata' AS wikidata,@@ -252,20 +252,20 @@ async def plants_country(request, country):{"request": request,"plants": plants,- "country": country["union"],+ "country": region["union"],"source": source,"min_output": min_output,# Canonical URL for all plants without the source filter, to avoid confusing Google.- "canonical": request.url_for("plants_country", country=country["union"]),+ "canonical": request.url_for("plants_region", region=region["union"]),},)[email protected]("/stats/area/{country}/plants/construction")-@country_required[email protected]("/stats/area/{region}/plants/construction")+@region_required@cache_for(3600)-async def plants_construction_country(request, country):- gid = country[0]+async def plants_construction_region(request, region):+ gid = region[0]plants = await database.fetch_all(query="""SELECT osm_id, name, tags->'name:en' AS name_en, tags->'wikidata' AS wikidata,@@ -288,7 +288,7 @@ async def plants_construction_country(request, country):"construction": True,"request": request,"plants": plants,- "country": country["union"],+ "country": region["union"],},)@@ -312,15 +312,13 @@ async def stats_object(request):if not res:raise HTTPException(404)- return RedirectResponse(- request.url_for("plant_detail", country=res["union"], id=id)- )+ return RedirectResponse(request.url_for("plant_detail", region=res["union"], id=id))[email protected]("/stats/area/{country}/plants/{id}")-@country_required[email protected]("/stats/area/{region}/plants/{id}")+@region_required@cache_for(3600)-async def plant_detail(request, country):+async def plant_detail(request, region):try:plant_id = int(request.path_params["id"])except ValueError:@@ -328,7 +326,7 @@ async def plant_detail(request, country):http_client = request.state.http_client- plant = await get_plant(plant_id, country["gid"])+ plant = await get_plant(plant_id, region["gid"])if plant is None:raise HTTPException(404, "Nonexistent power plant")@@ -362,7 +360,7 @@ async def plant_detail(request, country):"plant": plant,"request": request,"generator_summary": generator_summary,- "country": country["union"],+ "country": region["union"],"wikidata": wd,"image_data": image_data,"ref_tags": ref_tags,diff --git a/web-backend/templates/country.html b/web-backend/templates/country.htmlindex 7601477..bbc98c0 100644--- a/web-backend/templates/country.html+++ b/web-backend/templates/country.html@@ -29,7 +29,7 @@ Statistics on the electricity network in {{country|country_name}} from OpenStree<tbody>{% for row in plant_source_stats %}<tr>- <td><a href="{{url_for('plants_country', country=country)}}?source={{row.source or ''}}">{{row.source or '[unspecified]'}}</a></td>+ <td><a href="{{url_for('plants_region', region=country)}}?source={{row.source or ''}}">{{row.source or '[unspecified]'}}</a></td><td>{{row.output|power}}</td><td>{{row._mapping.count}}</td></tr>@@ -37,7 +37,7 @@ Statistics on the electricity network in {{country|country_name}} from OpenStree</tbody><tfoot><tr>- <td><a href="{{url_for('plants_country', country=country)}}">All</a></td>+ <td><a href="{{url_for('plants_region', region=country)}}">All</a></td><td>{{plant_stats.output|power}}</td><td>{{plant_stats.count}}</td></tr>@@ -45,7 +45,7 @@ Statistics on the electricity network in {{country|country_name}} from OpenStree</table><p class="footnote">If multiple sources are listed for a power plant, only the first source is used in this breakdown.</p></div>- <p><a href="{{url_for('plants_construction_country', country=country)}}">Show plants under construction</a></p>+ <p><a href="{{url_for('plants_construction_region', region=country)}}">Show plants under construction</a></p><div id="plant-summary"></div><script>Bokeh.embed.embed_item({{ plant_summary | safe }}, "plant-summary");diff --git a/web-backend/templates/index.html b/web-backend/templates/index.htmlindex 40442df..5a5e125 100644--- a/web-backend/templates/index.html+++ b/web-backend/templates/index.html@@ -17,7 +17,7 @@ Statistics on the global electricity network from OpenStreetMap<input list="areas" placeholder="Select area" id="area_select"><datalist id="areas">{% for country in countries %}- <option><a href="{{url_for('plants_country', country=country.union)}}">{{country.union}}</a></option>+ <option><a href="{{url_for('plants_region', region=country.union)}}">{{country.union}}</a></option>{% endfor %}</datalist></form>diff --git a/web-backend/templates/plant_detail.html b/web-backend/templates/plant_detail.htmlindex ac466a1..f39df19 100644--- a/web-backend/templates/plant_detail.html+++ b/web-backend/templates/plant_detail.html@@ -6,8 +6,8 @@{% endif %}{% set breadcrumbs = [('Stats', url_for('stats')),- (country, url_for('country', country=country)),- ("Power Plants", url_for('plants_country', country=country)),+ (country, url_for('region', region=country)),+ ("Power Plants", url_for('plants_region', region=country)),(name, None)]%}{% block title %}{{name}}{% endblock %}diff --git a/web-backend/templates/plants_country.html b/web-backend/templates/plants_country.htmlindex cf497bd..b297e50 100644--- a/web-backend/templates/plants_country.html+++ b/web-backend/templates/plants_country.html@@ -1,5 +1,5 @@{% extends "_base.html" %}-{% set breadcrumbs = [('Stats', url_for('stats')), (country, url_for('country', country=country)), ("Power Plants", None)] %}+{% set breadcrumbs = [('Stats', url_for('stats')), (country, url_for('region', region=country)), ("Power Plants", None)] %}{% if source == '' %}{% set pretty_source = 'unspecified' %}{% elif source %}@@ -49,7 +49,7 @@<tbody>{% for plant in plants -%}<tr>- <td class="row-name"><a href="{{url_for('plant_detail', country=country, id=plant.osm_id)}}">+ <td class="row-name"><a href="{{url_for('plant_detail', region=country, id=plant.osm_id)}}">{{plant.name or '[unnamed]'}}</a></td>{% if english_name %}<td class="row-subhead">{{plant.name_en or ''}}</td>@@ -73,7 +73,7 @@</tbody></table>{% if pretty_source %}- <p><a href="{{url_for('plants_country', country=country)}}">View all power plants in {{country|country_name}}</a>.</p>+ <p><a href="{{url_for('plants_region', region=country)}}">View all power plants in {{country|country_name}}</a>.</p>{% endif %}{% if construction %}<p class="footnote">Under-construction power plants are indicated based on the <code>construction:power</code> tag in OSM.</p>diff --git a/web-backend/util.py b/web-backend/util.pyindex e346761..9f7a487 100644--- a/web-backend/util.py+++ b/web-backend/util.py@@ -5,21 +5,21 @@ from urllib.parse import unquote_plusfrom config import database-def country_required(func):+def region_required(func):@wraps(func)- async def wrap_country(request):- country = unquote_plus(request.path_params["country"])+ async def wrap_region(request):+ region = unquote_plus(request.path_params["region"])res = await database.fetch_one(query='SELECT gid, "union" FROM countries.country_eez WHERE "union" = :union',- values={"union": country},+ values={"union": region},)if not res:raise HTTPException(404)return await func(request, res)- return wrap_country+ return wrap_regiondef cache_for(lifetime):