Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Further work

Russ Garrettb9d536533b32
diff --git a/main.py b/main.py
index 730976e..3b9ddb3 100644
--- a/main.py
+++ b/main.py
@@ -1,4 +1,5 @@
from decimal import Decimal
+from functools import wraps
from starlette.applications import Starlette
from starlette.exceptions import HTTPException
from starlette.templating import Jinja2Templates
@@ -26,9 +27,9 @@ app = Starlette(
def format_power(val):
if val is None:
- return ''
+ return ""
elif val >= 50e6:
- return "{:.0f} MW".format(val / Decimal(1e6))
+ return "{:,.0f} MW".format(val / Decimal(1e6))
elif val >= 1e6:
return "{:.2f} MW".format(val / Decimal(1e6))
else:
@@ -39,28 +40,39 @@ templates.env.filters["power"] = format_power
def osm_link(osm_id, geom_type):
- url = 'https://www.openstreetmap.org/'
+ url = "https://www.openstreetmap.org/"
if osm_id < 0:
osm_id = -osm_id
- url += 'relation'
- elif geom_type == 'ST_Point':
- url += 'node'
+ url += "relation"
+ elif geom_type == "ST_Point":
+ url += "node"
else:
- url += 'way'
- return url + '/' + str(osm_id)
+ url += "way"
+ return url + "/" + str(osm_id)
templates.env.globals["osm_link"] = osm_link
-async def main(request):
- return templates.TemplateResponse("index.html", {"request": request})
+def country_required(func):
+ @wraps(func)
+ async def wrap_country(request):
+ country = request.path_params["country"]
+
+ res = await database.fetch_one(
+ query='SELECT gid, "union" FROM countries.country_eez WHERE "union" = :union',
+ values={"union": country},
+ )
+ if not res:
+ raise HTTPException(404)
+ return await func(request, res)
-async def plants(request):
+ return wrap_country
+
+async def main(request):
countries = await database.fetch_all(
query="""SELECT "union" FROM countries.country_eez
WHERE "union" != \'Antarctica\'
@@ -69,26 +81,53 @@ async def plants(request):
)
return templates.TemplateResponse(
- "plants.html", {"request": request, "countries": countries}
+ "index.html", {"request": request, "countries": countries}
)
[email protected]("/plants/{country}")
-async def plants_country(request):
- country = request.path_params["country"]
[email protected]("/area/{country}")
+@country_required
+async def country(request, country):
+ plant_stats = await database.fetch_one(
+ query="""SELECT SUM(convert_power(output)) AS output, COUNT(*)
+ FROM power_plant
+ WHERE ST_Contains(
+ (SELECT ST_Transform(geom, 3857) FROM countries.country_eez where gid = :gid),
+ geometry)
+ """,
+ values={"gid": country["gid"]},
+ )
- res = await database.fetch_one(
- query='SELECT gid, "union" FROM countries.country_eez WHERE "union" = :union',
- values={"union": country},
+ plant_source_stats = await database.fetch_all(
+ query="""SELECT first_semi(source) AS source, sum(convert_power(output)) AS output, count(*)
+ FROM power_plant
+ WHERE ST_Contains(
+ (SELECT ST_Transform(geom, 3857) FROM countries.country_eez WHERE gid = :gid),
+ geometry)
+ GROUP BY first_semi(source)
+ ORDER BY SUM(convert_power(output)) DESC NULLS LAST""",
+ values={"gid": country["gid"]},
+ )
+
+ return templates.TemplateResponse(
+ "country.html",
+ {
+ "request": request,
+ "country": country['union'],
+ "plant_stats": plant_stats,
+ "plant_source_stats": plant_source_stats,
+ },
)
- if not res:
- raise HTTPException(404)
- gid = res[0]
[email protected]("/area/{country}/plants")
+@country_required
+async def plants_country(request, country):
+ gid = country[0]
plants = await database.fetch_all(
- query="""SELECT osm_id, name, convert_power(output) AS output,
+ query="""SELECT osm_id, name, tags->'name:en' AS name_en, tags->'wikidata' AS wikidata,
+ convert_power(output) AS output,
source, ST_GeometryType(geometry) AS geom_type
FROM power_plant
WHERE ST_Contains(
@@ -100,5 +139,5 @@ async def plants_country(request):
return templates.TemplateResponse(
"plants_country.html",
- {"request": request, "plants": plants, "country": country},
+ {"request": request, "plants": plants, "country": country["union"]},
)
diff --git a/static/main.css b/static/main.css
index b43613c..2390a7f 100644
--- a/static/main.css
+++ b/static/main.css
@@ -2,6 +2,7 @@ html, body {
margin: 0;
padding: 0;
border: 0;
+ font-size: 18px;
}
body {
@@ -15,21 +16,25 @@ a, a:visited {
header {
padding: 3px 8px;
+ border-bottom: 1px solid #ccc;
background-color: #eee;
}
-h1 {
+h1, h2 {
+ display: inline;
margin: 0;
font-size: 20px;
}
-h1 a {
+h1 a, h2 a {
text-decoration: none;
color: inherit;
}
#content {
- margin: 0px 8px;
+ margin: 0px auto;
+ max-width: 1000px;
+ min-height: 80vh;
}
footer {
@@ -42,10 +47,39 @@ footer, footer a, footer a:visited {
color: #666;
}
-.plants-table {
+input {
+ font-size: 1.2em;
}
-.plants-table tr {
- height: 40px;
+p.footnote {
+ font-size: 0.9em;
+ color: #444;
}
+table {
+ font-size: 0.9em;
+ margin-top: 8px;
+ width: 100%;
+ min-width: 600px;
+ table-layout: fixed;
+ border-collapse: collapse;
+ border: 1px solid #eee;
+}
+
+caption {
+ font-weight: bold;
+ font-size: 1.1em;
+ padding-bottom: 3px;
+}
+
+thead {
+ background-color: #eee;
+}
+
+tbody tr:nth-child(even) {
+ background-color: #fafafa;
+}
+
+th, td {
+ padding: 4px;
+}
diff --git a/templates/_base.html b/templates/_base.html
index f1a8748..c425f0f 100644
--- a/templates/_base.html
+++ b/templates/_base.html
@@ -8,7 +8,20 @@
</head>
<body>
<header>
- <h1><a href="{{url_for('main')}}">OpenInfraMap Stats</a></h1>
+ <h1><a href="https://openinframap.org">OpenInfraMap</a>
+ &rang; <a href="{{url_for('main')}}">Stats</a></h1>
+ {% if breadcrumbs %}
+ <h2>
+ {% for name, url in breadcrumbs %}
+ &rang;
+ {% if url %}
+ <a href="{{url}}">{{name}}</a>
+ {% else %}
+ {{name}}
+ {% endif %}
+ {% endfor %}
+ </h2>
+ {% endif %}
</header>
<div id="content">
{% block content %}{% endblock %}
diff --git a/templates/country.html b/templates/country.html
new file mode 100644
index 0000000..b511977
--- /dev/null
+++ b/templates/country.html
@@ -0,0 +1,29 @@
+{% extends "_base.html" %}
+{% set breadcrumbs = [(country, None)] %}
+{% block title %}{{country}}{% endblock %}
+{% block content -%}
+ <p><strong>{{country}}</strong> has {{plant_stats.count}} power plants totalling {{plant_stats.output|power}} mapped on OpenStreetMap.</p>
+
+ <table>
+ <caption>Power plants in {{country}} by source</caption>
+ <thead>
+ <tr>
+ <th>Source</th>
+ <th width="150px">Output</th>
+ <th width="100px">Count</th>
+ <tr>
+ </thead>
+ <tbody>
+ {% for row in plant_source_stats %}
+ <tr>
+ <td>{{row.source or ''}}</td>
+ <td>{{row.output|power}}</td>
+ <td>{{row.count}}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ <p><a href="{{url_for('plants_country', country=country)}}">See all plants</a>.</p>
+
+ <p class="footnote">If multiple sources are listed for a power plant, only the first source is used in this breakdown.</p>
+{% endblock %}
diff --git a/templates/index.html b/templates/index.html
index 188fb44..bf0074f 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -1,4 +1,38 @@
{% extends "_base.html" %}
{% block content -%}
- <a href="{{url_for('plants')}}">Power plants</a>
+ <p>Select an area to view stats:</p>
+ <form>
+ <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>
+ {% endfor %}
+ </datalist>
+ </form>
+
+ <p class="footnote">Areas used by this site are normally equivalent to countries, and include all objects within the country's
+ <a href="https://en.wikipedia.org/wiki/Exclusive_economic_zone" target="_blank">Exclusive Economic Zone</a>. If a country has
+ multiple disconnected regions, they are listed separately
+ (for example, Alaska and Hawaii are separate from the conterminous United States).</p>
+ <script>
+ var input = document.getElementById('area_select');
+ window.addEventListener('load', () => { input.value = '' });
+ input.addEventListener('change', function() {
+ var optionFound = false,
+ datalist = this.list;
+ for (var j = 0; j < datalist.options.length; j++) {
+ if (this.value == datalist.options[j].value) {
+ optionFound = true;
+ break;
+ }
+ }
+
+ if (optionFound) {
+ this.setCustomValidity("");
+ window.location.href = "/area/" + this.value;
+ } else {
+ this.setCustomValidity("Please select a valid value.");
+ }
+ });
+ </script>
{% endblock %}
diff --git a/templates/plants.html b/templates/plants.html
deleted file mode 100644
index 8aa4d77..0000000
--- a/templates/plants.html
+++ /dev/null
@@ -1,8 +0,0 @@
-{% extends "_base.html" %}
-{% block content -%}
- <ul>
- {% for country in countries %}
- <li><a href="{{url_for('plants_country', country=country.union)}}">{{country.union}}</a></li>
- {% endfor %}
- </ul>
-{% endblock %}
diff --git a/templates/plants_country.html b/templates/plants_country.html
index 1c54d88..6712b69 100644
--- a/templates/plants_country.html
+++ b/templates/plants_country.html
@@ -1,17 +1,27 @@
{% extends "_base.html" %}
+{% set breadcrumbs = [(country, url_for('country', country=country)), ("Power Plants", None)] %}
{% block title %}{{country}} Power Plants{% endblock %}
{% block content -%}
+{% set english_name = false %}
+{% if country not in ('United Kingdom', 'United States', 'Canada', 'Australia', 'New Zealand', 'Ireland') %}
+ {% set english_name = true %}
+{% endif %}
+
{% if plants|length == 0 %}
<p>There are currently no power plants mapped in {{country}} on OpenStreetMap.</p>
{% else %}
- <p>All {{plants|length}} power plants in {{country}}.</p>
<table class="plants-table">
+ <caption>All {{plants|length}} power plants in {{country}}</caption>
<thead>
<tr>
<th>Name</th>
- <th>Output</th>
- <th>Source</th>
+ {% if english_name %}
+ <th>English Name</th>
+ {% endif %}
+ <th style="width: 100px">Output</th>
+ <th style="width: 150px">Source</th>
+ <th style="width: 100px">Wikidata</th>
</tr>
</thead>
<tbody>
@@ -19,8 +29,16 @@
<tr>
<td><a href="{{osm_link(plant.osm_id, plant.geom_type)}}" target="_blank">
{{plant.name or '[unnamed]'}}</a></td>
+ {% if english_name %}
+ <td>{{plant.name_en or ''}}</td>
+ {% endif %}
<td>{{plant.output|power}}</td>
<td>{{plant.source}}</td>
+ <td>
+ {% if plant.wikidata %}
+ <a href="https://www.wikidata.org/wiki/{{plant.wikidata}}" target="_blank">{{plant.wikidata}}</a>
+ {% endif %}
+ </td>
</tr>
{% endfor %}
</tbody>