Open Infrastructure Map, a view of infrastructure data in OpenStreetMap
Plant page
Russ Garrettc23e926996c5
diff --git a/data.py b/data.pyindex 29381c8..8895e5c 100644--- a/data.py+++ b/data.py@@ -1,3 +1,8 @@+import json+import re+import aiohttp+from async_lru import alru_cache+from starlette.exceptions import HTTPExceptionfrom config import databasefrom itertools import chainfrom more_itertools import windowed@@ -14,6 +19,49 @@ async def get_countries():)+async def get_plant(plant_id, country_gid):+ res = await database.fetch_one(+ """SELECT osm_id, ST_GeometryType(geometry) AS geom_type, name, source,+ convert_power(tags->'plant:output:electricity') AS output,+ hstore_to_json(tags) AS tags+ FROM power_plant, countries.country_eez+ WHERE gid = :country_gid+ AND power_plant.osm_id = :plant_id+ AND ST_Contains(country_eez.geom, ST_Transform(power_plant.geometry, 4326))""",+ {"plant_id": plant_id, "country_gid": country_gid},+ )+ if res is None:+ return None++ res = dict(res)+ res["tags"] = json.loads(res["tags"])+ return res+++async def get_plant_generator_summary(plant_id):+ res = await database.fetch_all(+ """SELECT g.source,+ convert_power(g.tags->'generator:output:electricity') AS output,+ sum(convert_power(g.tags->'generator:output:electricity')) AS total_output, count(*)+ FROM osm_power_generator g, osm_power_plant p+ WHERE p.osm_id = :plant_id and ST_Contains(p.geometry, g.geometry)+ GROUP BY g.source, convert_power(g.tags->'generator:output:electricity')""",+ {"plant_id": plant_id},+ )+ if not res and plant_id < 0:+ res = await database.fetch_all(+ """SELECT tags->'generator:source' AS source,+ convert_power(tags->'generator:output:electricity') AS output,+ sum(convert_power(tags->'generator:output:electricity')) AS total_output, count(*)+ FROM osm_power_plant_relation_member+ WHERE osm_id = :plant_id+ GROUP BY source, convert_power(tags->'generator:output:electricity')+ """,+ {"plant_id": plant_id},+ )+ return res++async def stats_power_line(country=None):stats_date = (await database.fetch_one("SELECT max(time) FROM stats.power_line"))[0]values = {"time": stats_date}@@ -59,3 +107,34 @@ async def stats_power_line(country=None):"unspecified": unspecified[0] or 0,}return data+++@alru_cache(maxsize=1000)+async def get_wikidata(wikidata_id):+ wikidata_id = wikidata_id.upper()+ if not re.match(r"^Q[0-9]+$", wikidata_id):+ return None++ async with aiohttp.ClientSession() as session:+ async with session.get(+ f"https://www.wikidata.org/entity/{wikidata_id}.json"+ ) as resp:+ if resp.status != 200:+ raise HTTPException(503, "Error while fetching wikidata")+ data = await resp.json()+ return data["entities"][wikidata_id]+++@alru_cache(maxsize=1000)+async def get_commons_thumbnail(filename, width=300):+ url = (+ "https://commons.wikimedia.org/w/api.php?"+ f"action=query&titles=Image:{filename}&prop=imageinfo"+ f"&iiprop=url&iiurlwidth={width}&format=json"+ )+ async with aiohttp.ClientSession() as session:+ async with session.get(url) as resp:+ if resp.status != 200:+ raise HTTPException(503, "Error while fetching wikimedia commons image")+ data = await resp.json()+ return list(data["query"]["pages"].values())[0]diff --git a/main.py b/main.pyindex 2807977..bade650 100644--- a/main.py+++ b/main.py@@ -3,6 +3,7 @@ from starlette.applications import Starlettefrom starlette.templating import Jinja2Templatesfrom starlette.routing import Mount, Routefrom starlette.staticfiles import StaticFiles+from starlette.exceptions import HTTPExceptionfrom template_functions import (format_power,@@ -15,7 +16,14 @@ from template_functions import (from config import database, configfrom util import cache_for, country_requiredfrom sitemap import sitemap-from data import get_countries, stats_power_line+from data import (+ get_countries,+ stats_power_line,+ get_plant,+ get_plant_generator_summary,+ get_wikidata,+ get_commons_thumbnail,+)DEBUG = config("DEBUG", cast=bool, default=False)@@ -109,7 +117,7 @@ async def country(request, country):"plant_stats": plant_stats,"plant_source_stats": plant_source_stats,"power_lines": power_lines,- "canonical": request.url_for("country", country=country['union'])+ "canonical": request.url_for("country", country=country["union"]),},)@@ -149,7 +157,7 @@ async def plants_country(request, country):"country": country["union"],"source": source,# 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_country", country=country["union"]),},)@@ -186,4 +194,49 @@ async def plants_construction_country(request, country):)[email protected]("/stats/area/{country}/plants/{id}")+@country_required+@cache_for(3600)+async def plant_detail(request, country):+ try:+ plant_id = int(request.path_params["id"])+ except ValueError:+ raise HTTPException(404, "Invalid plant ID")++ plant = await get_plant(plant_id, country["gid"])+ if plant is None:+ raise HTTPException(404, "Nonexistent power plant")++ generator_summary = await get_plant_generator_summary(plant_id)++ if "wikidata" in plant["tags"]:+ wd = await get_wikidata(plant["tags"]["wikidata"])+ else:+ wd = None++ image_data = None+ if (+ wd+ and "P18" in wd["claims"]+ and wd["claims"]["P18"][0]["mainsnak"]["datatype"] == "commonsMedia"+ ):+ image_data = await get_commons_thumbnail(+ wd["claims"]["P18"][0]["mainsnak"]["datavalue"]["value"],+ 400+ )++ return templates.TemplateResponse(+ "plant_detail.html",+ {+ "construction": True,+ "plant": plant,+ "request": request,+ "generator_summary": generator_summary,+ "country": country["union"],+ "wikidata": wd,+ "image_data": image_data,+ },+ )++import wikidata # noqadiff --git a/poetry.lock b/poetry.lockindex f306dd1..fa75cbc 100644--- a/poetry.lock+++ b/poetry.lock@@ -33,6 +33,14 @@ category = "main"optional = falsepython-versions = ">=3.7"+[[package]]+name = "async-lru"+version = "1.0.2"+description = "Simple lru_cache for asyncio"+category = "main"+optional = false+python-versions = "*"+[[package]]name = "async-timeout"version = "3.0.1"@@ -226,7 +234,7 @@ typing-extensions = {version = ">=3.7.4", markers = "python_version < \"3.8\""}[metadata]lock-version = "1.1"python-versions = ">=3.7"-content-hash = "52b37ef1c427e664912cf682f39d840231d20859fb56520f5280582e0fdcc1ba"+content-hash = "15e6f83ebab817e36c18579575a4f07f71718895a52c12439f1845d68d11833c"[metadata.files]aiofiles = [@@ -276,6 +284,9 @@ asgi-sitemaps = [{file = "asgi-sitemaps-0.3.2.tar.gz", hash = "sha256:ede30466cddb8d242f0e1169f9824ab810d56ac9b4beaf56ada2a90779439caf"},{file = "asgi_sitemaps-0.3.2-py3-none-any.whl", hash = "sha256:b20aa963f1553a1ff5b1757b4fb130fafcf11ec70f985668f95c62c63c1fb432"},]+async-lru = [+ {file = "async_lru-1.0.2.tar.gz", hash = "sha256:baa898027619f5cc31b7966f96f00e4fc0df43ba206a8940a5d1af5336a477cb"},+]async-timeout = [{file = "async-timeout-3.0.1.tar.gz", hash = "sha256:0c3c816a028d47f659d6ff5c745cb2acf1f966da1fe5c19c77a70282b25f4c5f"},{file = "async_timeout-3.0.1-py3-none-any.whl", hash = "sha256:4291ca197d287d274d0b6cb5d6f8f8f82d434ed288f962539ff18cc9012f9ea3"},diff --git a/pyproject.toml b/pyproject.tomlindex 2b48ab0..ca17dcb 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -15,6 +15,7 @@ uvicorn = "^0.12.2"asgi-sitemaps = "^0.3.2"aiohttp = "^3.7.3"more-itertools = "^8.6.0"+async_lru = "^1.0.2"[tool.poetry.dev-dependencies]diff --git a/static/main.css b/static/main.cssindex f8c34d2..a261b60 100644--- a/static/main.css+++ b/static/main.css@@ -35,6 +35,10 @@ h1 a, h2 a {color: inherit;}+h3 {+ clear: both;+}+#content {margin: 0px auto;margin-bottom: 20px;@@ -110,3 +114,22 @@ tfoot tr {th, td {padding: 4px;}++figure {+ margin: 5px;+ background-color: #eee;+ border: 1px #999 solid;+}++figcaption {+ font-size: 0.7em;+ padding: 2px 5px;+}++figcaption a, figcaption a:visited {+ color: #666;+}++figure.plant_image {+ float:right;+}diff --git a/templates/plant_detail.html b/templates/plant_detail.htmlnew file mode 100644index 0000000..507ce7f--- /dev/null+++ b/templates/plant_detail.html@@ -0,0 +1,66 @@+{% extends "_base.html" %}+{% if plant['name'] %}+ {% set name = plant['name'] %}+{% else %}+ {% set name = 'Unnamed power plant ' + plant['osm_id']|string %}+{% endif %}+{% set breadcrumbs = [+ ('Stats', url_for('stats')),+ (country, url_for('country', country=country)),+ ("Power Plants", url_for('plants_country', country=country)),+ (name, None)+]%}+{% block title %}{{name}}{% endblock %}+{% block description -%}+{%- endblock %}+{% block content -%}+ <p><strong>{{name}}</strong> 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 %}.+ </p>+ {% if image_data %}+ {% set imginfo = image_data.imageinfo[0] %}+ <figure class="plant_image">+ <img src="{{imginfo.thumburl}}"+ srcset="{{imginfo.responsiveUrls['1.5']}} 1.5x,+ {{imginfo.responsiveUrls['2']}} 2x"+ alt="Picture of {{name}}">+ <figcaption><a href="{{imginfo.descriptionurl}}">Wikimedia Commons</a></figcaption>+ </figure>+ {% endif %}+ <ul>+ <li><a href="{{osm_link(plant.osm_id, plant.geom_type)}}">OpenStreetMap</a></li>+ {% if plant.tags.url or plant.tags.website %}+ <li><a href="{{plant.tags.url or plant.tags.website}}">Website</a></li>+ {% endif %}+ {% if wikidata and wikidata.sitelinks.enwiki %}+ <li><a href="{{wikidata.sitelinks.enwiki.url}}">Wikipedia ({{wikidata.sitelinks.enwiki.title}})</a></li>+ {% endif %}+ {% if plant.tags.wikidata %}+ <li><a href="https://www.wikidata.org/wiki/{{plant.tags.wikidata}}">Wikidata</a></li>+ {% endif %}+ </ul>+ <h3>Generators</h3>+ {% if generator_summary %}+ <table>+ <thead>+ <tr>+ <th>Source</th>+ <th>Output</th>+ <th>Count</th>+ <th>Total output</th>+ </tr>+ </thead>+ <tbody>+ {% for gen in generator_summary %}+ <tr>+ <td>{{gen['source'] or "[unspecified]"}}</td>+ <td>{{gen['output']|power}}</td>+ <td>{{gen['count']}}</td>+ <td>{{gen['total_output']|power}}</td>+ </tr>+ {% endfor %}+ </tbody>+ </table>+ {% endif %}+{% endblock %}diff --git a/templates/plants_country.html b/templates/plants_country.htmlindex db02a5c..0cedab4 100644--- a/templates/plants_country.html+++ b/templates/plants_country.html@@ -48,7 +48,7 @@<tbody>{% for plant in plants -%}<tr>- <td><a href="{{osm_link(plant.osm_id, plant.geom_type)}}" target="_blank">+ <td><a href="{{url_for('plant_detail', country=country, id=plant.osm_id)}}">{{plant.name or '[unnamed]'}}</a></td>{% if english_name %}<td>{{plant.name_en or ''}}</td>diff --git a/wikidata.py b/wikidata.pyindex 2c7fa14..450a249 100644--- a/wikidata.py+++ b/wikidata.py@@ -1,46 +1,20 @@-""" Endpoints to proxy WikiData requests """-import aiohttp-import re+""" Endpoints to proxy WikiData requests for info popups on the map """from starlette.exceptions import HTTPExceptionfrom starlette.responses import JSONResponse+from data import get_wikidata, get_commons_thumbnailfrom util import cache_forfrom main import app-async def get_wikidata(wikidata_id):- async with aiohttp.ClientSession() as session:- async with session.get(- f"https://www.wikidata.org/entity/{wikidata_id}.json"- ) as resp:- if resp.status != 200:- raise HTTPException(503)- data = await resp.json()- return data["entities"][wikidata_id]---async def get_commons_thumbnail(filename, width=300):- url = (- "https://commons.wikimedia.org/w/api.php?"- f"action=query&titles=Image:{filename}&prop=imageinfo"- f"&iiprop=url&iiurlwidth={width}&format=json"- )- async with aiohttp.ClientSession() as session:- async with session.get(url) as resp:- if resp.status != 200:- raise HTTPException(503)- data = await resp.json()- return list(data["query"]["pages"].values())[0]["imageinfo"][0]["thumburl"]--@app.route("/wikidata/{wikidata_id}")@cache_for(86400)async def wikidata(request):wikidata_id = request.path_params['wikidata_id'].upper()- if not re.match(r"^Q[0-9]+$", wikidata_id):- raise HTTPException(404)response = {}data = await get_wikidata(wikidata_id)+ if data is None:+ raise HTTPException(404, "Wikidata item not found")response["sitelinks"] = data["sitelinks"]if (@@ -48,10 +22,12 @@ async def wikidata(request):and data["claims"]["P18"][0]["mainsnak"]["datatype"] == "commonsMedia"):response["image"] = data["claims"]["P18"][0]["mainsnak"]["datavalue"]["value"]- response["thumbnail"] = await get_commons_thumbnail(+ image_data = await get_commons_thumbnail(data["claims"]["P18"][0]["mainsnak"]["datavalue"]["value"])+ response["thumbnail"] = image_data["imageinfo"][0]["thumburl"]+return JSONResponse(response,headers={"Access-Control-Allow-Origin": "*"},