Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Add external IDs to stats site and link from info popup

Russ Garrettdd5fb9fbe233
diff --git a/web-backend/main.py b/web-backend/main.py
index 32ce4cc..ebb8362 100644
--- a/web-backend/main.py
+++ b/web-backend/main.py
@@ -1,4 +1,4 @@
-from starlette.responses import PlainTextResponse
+from starlette.responses import PlainTextResponse, RedirectResponse
from starlette.applications import Starlette
from starlette.templating import Jinja2Templates
from starlette.routing import Mount, Route
@@ -213,6 +213,29 @@ async def plants_construction_country(request, country):
)
[email protected]("/stats/object/plant/{id}")
+@cache_for(86400)
+async def stats_object(request):
+ try:
+ id = int(request.path_params["id"])
+ except ValueError:
+ raise HTTPException(400)
+
+ res = await database.fetch_one(
+ """SELECT country_eez."union" FROM power_plant, countries.country_eez WHERE
+ ST_Contains(ST_Transform(country_eez.geom, 3857), geometry)
+ AND power_plant.osm_id = :id""",
+ values={"id": id},
+ )
+
+ if not res:
+ raise HTTPException(404)
+
+ return RedirectResponse(
+ request.url_for("plant_detail", country=res["union"], id=id)
+ )
+
+
@app.route("/stats/area/{country}/plants/{id}")
@country_required
@cache_for(3600)
@@ -243,6 +266,12 @@ async def plant_detail(request, country):
wd["claims"]["P18"][0]["mainsnak"]["datavalue"]["value"], 400
)
+ ref_tags = []
+ for k, v in plant["tags"].items():
+ if k.startswith("ref:") or k in ["repd:id"]:
+ for split_val in v.split(";"):
+ ref_tags.append((k, split_val))
+
return templates.TemplateResponse(
"plant_detail.html",
{
@@ -253,6 +282,7 @@ async def plant_detail(request, country):
"country": country["union"],
"wikidata": wd,
"image_data": image_data,
+ "ref_tags": ref_tags,
},
)
diff --git a/web-backend/templates/plant_detail.html b/web-backend/templates/plant_detail.html
index 074046c..be52374 100644
--- a/web-backend/templates/plant_detail.html
+++ b/web-backend/templates/plant_detail.html
@@ -19,8 +19,9 @@
({{plant['name_en']}})
{% endif %}
is a {{plant['source'].split(';')|join(' and ')}}
- power plant in {{country|country_name()}}
-{%- if plant['output']%} with a total output of {{plant['output']|power}}{% endif %}.
+ power plant
+{%- if plant.tags.operator %} operated by <strong>{{plant.tags.operator}}</strong>{% endif %}
+{%- if plant['output']%} with a total output of <strong>{{plant['output']|power}}</strong>{% endif %}.
</p>
{% if image_data and image_data.imageinfo[0].responsiveUrls%}
{% set imginfo = image_data.imageinfo[0] %}
@@ -44,8 +45,9 @@
<li><a href="https://www.wikidata.org/wiki/{{plant.tags.wikidata}}">Wikidata</a></li>
{% endif %}
</ul>
- <h3>Generators</h3>
+
{% if generator_summary %}
+ <h3>Generators</h3>
<table>
<thead>
<tr>
@@ -67,4 +69,24 @@
</tbody>
</table>
{% endif %}
+
+ {% if ref_tags %}
+ <h3>External Identifiers</h3>
+ <table>
+ {% for tag, value in ref_tags %}
+ <tr>
+ <td><a href="https://wiki.openstreetmap.org/wiki/Key:{{tag}}">{{tag}}</a></td>
+ <td>
+ {% if tag == "ref:US:EIA" %}
+ <a href="https://data.catalyst.coop/pudl/plants_entity_eia/{{value}}">{{value}}</a>
+ {% elif tag == "repd:id" %}
+ <a href="https://repd.russss.dev/repd/repd/{{value}}">{{value}}</a>
+ {% else %}
+ {{value}}
+ {% endif %}
+ </td>
+ </tr>
+ {% endfor %}
+ </table>
+ {% endif %}
{% endblock %}
diff --git a/web-backend/util.py b/web-backend/util.py
index 8ee7202..e346761 100644
--- a/web-backend/util.py
+++ b/web-backend/util.py
@@ -1,5 +1,6 @@
from functools import wraps
from starlette.exceptions import HTTPException
+from urllib.parse import unquote_plus
from config import database
@@ -7,7 +8,7 @@ from config import database
def country_required(func):
@wraps(func)
async def wrap_country(request):
- country = request.path_params["country"]
+ country = unquote_plus(request.path_params["country"])
res = await database.fetch_one(
query='SELECT gid, "union" FROM countries.country_eez WHERE "union" = :union',
@@ -26,8 +27,9 @@ def cache_for(lifetime):
@wraps(func)
async def wrap_cache(*args, **kwargs):
response = await func(*args, **kwargs)
- response.headers['Cache-Control'] = f"public, max-age={lifetime}"
+ response.headers["Cache-Control"] = f"public, max-age={lifetime}"
return response
return wrap_cache
+
return cache_for_inner
diff --git a/web/.eslintrc.json b/web/.eslintrc.json
index 11b3647..1ce0730 100644
--- a/web/.eslintrc.json
+++ b/web/.eslintrc.json
@@ -1,4 +1,7 @@
{
+ "parserOptions": {
+ "ecmaVersion": 12
+ },
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error"
diff --git a/web/src/infopopup.js b/web/src/infopopup.js
index 2a7850a..dd052d7 100644
--- a/web/src/infopopup.js
+++ b/web/src/infopopup.js
@@ -3,8 +3,8 @@ import './infopopup.css';
import maplibregl from 'maplibre-gl';
import titleCase from 'title-case';
import browserLanguage from 'in-browser-language';
-import {local_name_tags} from './l10n.js';
-import {el, text, mount, unmount, setChildren, setStyle} from 'redom';
+import { local_name_tags } from './l10n.js';
+import { el, text, mount, unmount, setChildren, setStyle } from 'redom';
const hidden_keys = [
'osm_id',
@@ -111,7 +111,7 @@ class InfoPopup {
target: '_blank',
}), text(', ')),
);
- } else {
+ } else {
key = titleCase(key);
}
@@ -143,7 +143,7 @@ class InfoPopup {
}
popupHtml(feature) {
- let attrs_table = el('table', {class: 'item_info'});
+ let attrs_table = el('table', { class: 'item_info' });
setChildren(
attrs_table,
Object.keys(feature.properties)
@@ -191,6 +191,10 @@ class InfoPopup {
wikidata_div,
attrs_table,
);
+
+ if (feature.layer.id.startsWith("power_plant")) {
+ mount(content, el("a", "More info", { href: "/stats/object/plant/" + feature.properties['osm_id'], target: "_blank" }))
+ }
return content;
}
@@ -220,9 +224,8 @@ class InfoPopup {
src: data['thumbnail'],
}),
{
- href: `https://commons.wikimedia.org/wiki/File:${
- data['image']
- }`,
+ href: `https://commons.wikimedia.org/wiki/File:${data['image']
+ }`,
target: '_blank',
},
),
@@ -283,4 +286,4 @@ class InfoPopup {
}
}
-export {InfoPopup as default};
+export { InfoPopup as default };