Open Infrastructure Map, a view of infrastructure data in OpenStreetMap
Updates
Russ Garrettc0ed9192e831
diff --git a/web-backend/pyproject.toml b/web-backend/pyproject.tomlindex b1e1577..63bdb4e 100644--- a/web-backend/pyproject.toml+++ b/web-backend/pyproject.toml@@ -18,6 +18,8 @@ dependencies = ["bokeh==3.9.1","pycountry>=24.6.1","sqlalchemy[asyncio]>=2.0.50",+ "pydantic>=2.13.4",+ "geojson>=3.3.0",][tool.ruff]diff --git a/web-backend/src/data.py b/web-backend/src/data.pyindex 212dff1..c4c2a30 100644--- a/web-backend/src/data.py+++ b/web-backend/src/data.py@@ -2,12 +2,12 @@ import asyncioimport loggingimport refrom itertools import chain-from typing import Optional+from typing import Optional, Sequenceimport httpxfrom async_lru import alru_cachefrom more_itertools import windowed-from sqlalchemy import text+from sqlalchemy import Row, textfrom sqlalchemy.ext.asyncio import AsyncConnectionfrom starlette.exceptions import HTTPException@@ -49,7 +49,7 @@ async def get_plant(database: AsyncConnection, plant_id: int, country_gid: int):return res._mapping-async def get_plant_generator_summary(database: AsyncConnection, plant_id):+async def get_plant_generator_summary(database: AsyncConnection, plant_id: int) -> Sequence[Row]:res = await database.execute(text("""SELECT g.source,diff --git a/web-backend/src/main.py b/web-backend/src/main.pyindex 06b4875..4190318 100644--- a/web-backend/src/main.py+++ b/web-backend/src/main.py@@ -14,19 +14,17 @@ from .config import DATABASE_URL, DEBUGfrom .sitemap import sitemapfrom .views import routes-db_engine = create_async_engine(DATABASE_URL)-@contextlib.asynccontextmanagerasync def lifespan(app: Starlette) -> AsyncGenerator[State]:+ db_engine = create_async_engine(DATABASE_URL)async with (httpx.AsyncClient(headers={"User-Agent": "Open Infrastructure Map backend (https://openinframap.org)"}) as client,- db_engine.begin() as db,+ db_engine.connect() as db,):yield {"http_client": client, "db": db}- await db.rollback()app = Starlette(diff --git a/web-backend/src/views/api.py b/web-backend/src/views/api.pyindex ba0c1a5..aa0d9ea 100644--- a/web-backend/src/views/api.py+++ b/web-backend/src/views/api.py@@ -1,25 +1,117 @@-from starlette.requests import Request-from starlette.responses import JSONResponse, Response+from typing import Any++from pydantic import BaseModel+from sqlalchemy.sql import text+from starlette.exceptions import HTTPException+from starlette.responses import Responsefrom starlette.routing import Route+from .. import Requestfrom ..util import cache_for+class Circuit(BaseModel):+ id: int+ name: str | None+ name_en: str | None+ voltage: int | None+ frequency: int | None+ role: str | None+++class SubstationResponse(BaseModel):+ circuits: list[Circuit]++@cache_for(600)async def substation(request: Request) -> Response:- database = request.state.db+ database = request.state["db"]substation_id = request.path_params["substation_id"]- res = await database.fetch(- """SELECT relation.osm_id AS relation_id, relation.tags, member.role+ res = await database.execute(+ text(+ """SELECT relation.osm_id AS relation_id,+ relation.tags -> 'name' AS name,+ relation.tags -> 'name:en' AS name_en,+ relation.tags -> 'frequency' AS frequency,+ convert_voltage(relation.tags -> 'voltage') AS voltage,+ member.roleFROM osm_power_circuit_relation_member member, osm_power_circuit_relation relation- WHERE relation.osm_id = member.osm_id AND member.member = $1""",+ WHERE relation.osm_id = member.osm_id AND member.member = :member_id"""+ ),{"member_id": int(substation_id)},)- if res is not None:- for record in res:- print(record, repr(res[record]))- return JSONResponse({"status": "success"})+ circuits = [+ Circuit(+ id=r._mapping["relation_id"],+ name=r._mapping["name"],+ name_en=r._mapping["name_en"],+ voltage=r._mapping["voltage"] / 1000 if r._mapping["voltage"] else None,+ frequency=r._mapping["frequency"],+ role=r._mapping["role"],+ )+ for r in res+ ]++ return Response(SubstationResponse(circuits=circuits).model_dump_json(), media_type="application/json")+++class CircuitResponse(BaseModel):+ id: int+ name: str | None+ name_en: str | None+ voltage: int | None+ frequency: int | None+ length: float++ geometry: dict[str, Any]+++@cache_for(600)+async def circuit(request: Request) -> Response:+ database = request.state["db"]+ circuit_id = request.path_params["circuit_id"]+ res = (+ await database.execute(+ text("""+ SELECT relation.osm_id AS relation_id,+ relation.tags -> 'name' AS name,+ relation.tags -> 'name:en' AS name_en,+ relation.tags -> 'frequency' AS frequency,+ convert_voltage(relation.tags -> 'voltage') AS voltage,+ (SELECT json_build_object('type', 'FeatureCollection', 'features',+ json_agg(json_build_object('type', 'Feature',+ 'geometry', ST_AsGeoJSON(ST_Transform(ST_Simplify(geometry, 1), 4326),+ maxdecimaldigits=>6)::json)))+ FROM osm_power_circuit_relation_member WHERE osm_id = relation.osm_id) AS geojson,+ (SELECT sum(st_length(ST_Transform(geometry, 4326)::geography))+ from osm_power_circuit_relation_member WHERE role = 'section' AND+ osm_id = relation.osm_id) AS length++ FROM osm_power_circuit_relation relation+ WHERE relation.osm_id = :relation_id+ """),+ {"relation_id": int(circuit_id)},+ )+ ).fetchone()+ if not res:+ raise HTTPException(404)++ return Response(+ CircuitResponse(+ id=res._mapping["relation_id"],+ name=res._mapping["name"],+ name_en=res._mapping["name_en"],+ voltage=res._mapping["voltage"] / 1000 if res._mapping["voltage"] else None,+ frequency=res._mapping["frequency"],+ geometry=res._mapping["geojson"],+ length=round(res._mapping["length"] or 0 / 1000, 2),+ ).model_dump_json(),+ media_type="application/json",+ )-routes = [Route("/api/substation/{substation_id}", endpoint=substation)]+routes = [+ Route("/api/substation/{substation_id}", endpoint=substation),+ Route("/api/circuit/{circuit_id}", endpoint=circuit),+]diff --git a/web-backend/src/views/search.py b/web-backend/src/views/search.pyindex 3bc64c7..2bacd5d 100644--- a/web-backend/src/views/search.py+++ b/web-backend/src/views/search.py@@ -10,8 +10,7 @@ from starlette.exceptions import HTTPExceptionfrom starlette.responses import JSONResponse, Responsefrom starlette.routing import Route-from src import Request-+from .. import Requestfrom ..util import cache_for# Ensure the `create_indexes` script is run after changing this list.diff --git a/web-backend/uv.lock b/web-backend/uv.lockindex 1f0bbb4..d5ebf9b 100644--- a/web-backend/uv.lock+++ b/web-backend/uv.lock@@ -16,6 +16,15 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/bc/8a/340a1555ae33d7354dbca4faa54948d76d89a27ceef032c8c3bc661d003e/aiofiles-25.1.0-py3-none-any.whl", hash = "sha256:abe311e527c862958650f9438e859c1fa7568a141b22abcd015e120e86a85695", size = 14668, upload-time = "2025-10-09T20:51:03.174Z" },]+[[package]]+name = "annotated-types"+version = "0.7.0"+source = { registry = "https://pypi.org/simple" }+sdist = { url = "https://files.pythonhosted.org/packages/ee/67/531ea369ba64dcff5ec9c3402f9f51bf748cec26dde048a2f973a4eea7f5/annotated_types-0.7.0.tar.gz", hash = "sha256:aff07c09a53a08bc8cfccb9c85b05f1aa9a2a6f23728d790723543408344ce89", size = 16081, upload-time = "2024-05-20T21:33:25.928Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/78/b6/6307fbef88d9b5ee7421e68d78a9f162e0da4900bc5f5793f6d3d0e34fb8/annotated_types-0.7.0-py3-none-any.whl", hash = "sha256:1f02e8b43a8fbbc3f3e0d4f0f4bfc8131bcb4eebe8849b8e5c773f3a1c582a53", size = 13643, upload-time = "2024-05-20T21:33:24.1Z" },+]+[[package]]name = "anyio"version = "4.13.0"@@ -221,6 +230,15 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/c1/ea/53f2148663b321f21b5a606bd5f191517cf40b7072c0497d3c92c4a13b1e/executing-2.2.1-py2.py3-none-any.whl", hash = "sha256:760643d3452b4d777d295bb167ccc74c64a81df23fb5e08eff250c425a4b2017", size = 28317, upload-time = "2025-09-01T09:48:08.5Z" },]+[[package]]+name = "geojson"+version = "3.3.0"+source = { registry = "https://pypi.org/simple" }+sdist = { url = "https://files.pythonhosted.org/packages/13/a9/bd61eee2c7904947094b74866b569f3fd5a8d6ac907ecdfecef74b19d459/geojson-3.3.0.tar.gz", hash = "sha256:92e83b9cb378a450b42f1207bb9b2a031f9fc89185f335153c44369b8b8b71fd", size = 25141, upload-time = "2026-05-28T21:48:08.214Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/aa/5e/fdd72167b57158d743353f71d453200719744d1e75f18b1c8230508db370/geojson-3.3.0-py3-none-any.whl", hash = "sha256:a2d885187eeaa8b357600b3fcc9d963cb4300d1694196636dbd7eddc82fd0825", size = 15181, upload-time = "2026-05-28T21:48:06.648Z" },+]+[[package]]name = "greenlet"version = "3.5.1"@@ -505,11 +523,13 @@ dependencies = [{ name = "asyncpg" },{ name = "bokeh" },{ name = "databases" },+ { name = "geojson" },{ name = "httpx" },{ name = "jinja2" },{ name = "more-itertools" },{ name = "pandas" },{ name = "pycountry" },+ { name = "pydantic" },{ name = "sqlalchemy", extra = ["asyncio"] },{ name = "starlette" },{ name = "uvicorn" },@@ -532,13 +552,15 @@ requires-dist = [{ name = "asgi-sitemaps" },{ name = "async-lru" },{ name = "asyncpg" },- { name = "bokeh", specifier = "<4" },+ { name = "bokeh", specifier = "==3.9.1" },{ name = "databases" },+ { name = "geojson", specifier = ">=3.3.0" },{ name = "httpx" },{ name = "jinja2" },{ name = "more-itertools" },{ name = "pandas", specifier = ">=2.2.3" },{ name = "pycountry", specifier = ">=24.6.1" },+ { name = "pydantic", specifier = ">=2.13.4" },{ name = "sqlalchemy", extras = ["asyncio"], specifier = ">=2.0.50" },{ name = "starlette" },{ name = "uvicorn" },@@ -737,6 +759,47 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/9c/42/7703bd45b62fecd44cd7d3495423097e2f7d28bc2e99e7c1af68892ab157/pycountry-26.2.16-py3-none-any.whl", hash = "sha256:115c4baf7cceaa30f59a4694d79483c9167dbce7a9de4d3d571c5f3ea77c305a", size = 8044600, upload-time = "2026-02-17T03:42:49.777Z" },]+[[package]]+name = "pydantic"+version = "2.13.4"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "annotated-types" },+ { name = "pydantic-core" },+ { name = "typing-extensions" },+ { name = "typing-inspection" },+]+sdist = { url = "https://files.pythonhosted.org/packages/18/a5/b60d21ac674192f8ab0ba4e9fd860690f9b4a6e51ca5df118733b487d8d6/pydantic-2.13.4.tar.gz", hash = "sha256:c40756b57adaa8b1efeeced5c196f3f3b7c435f90e84ea7f443901bec8099ef6", size = 844775, upload-time = "2026-05-06T13:43:05.343Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/fd/7b/122376b1fd3c62c1ed9dc80c931ace4844b3c55407b6fb2d199377c9736f/pydantic-2.13.4-py3-none-any.whl", hash = "sha256:45a282cde31d808236fd7ea9d919b128653c8b38b393d1c4ab335c62924d9aba", size = 472262, upload-time = "2026-05-06T13:43:02.641Z" },+]++[[package]]+name = "pydantic-core"+version = "2.46.4"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "typing-extensions" },+]+sdist = { url = "https://files.pythonhosted.org/packages/9d/56/921726b776ace8d8f5db44c4ef961006580d91dc52b803c489fafd1aa249/pydantic_core-2.46.4.tar.gz", hash = "sha256:62f875393d7f270851f20523dd2e29f082bcc82292d66db2b64ea71f64b6e1c1", size = 471464, upload-time = "2026-05-06T13:37:06.98Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/51/a2/5d30b469c5267a17b39dec53208222f76a8d351dfac4af661888c5aee77d/pydantic_core-2.46.4-cp313-cp313-macosx_10_12_x86_64.whl", hash = "sha256:5d5902252db0d3cedf8d4a1bc68f70eeb430f7e4c7104c8c476753519b423008", size = 2106306, upload-time = "2026-05-06T13:37:48.029Z" },+ { url = "https://files.pythonhosted.org/packages/c1/81/4fa520eaffa8bd7d1525e644cd6d39e7d60b1592bc5b516693c7340b50f1/pydantic_core-2.46.4-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:c94f0688e7b8d0a67abf40e57a7eaaecd17cc9586706a31b76c031f63df052b4", size = 1951906, upload-time = "2026-05-06T13:37:17.012Z" },+ { url = "https://files.pythonhosted.org/packages/03/d5/fd02da45b659668b05923b17ba3a0100a0a3d5541e3bd8fcc4ecb711309e/pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f027324c56cd5406ca49c124b0db10e56c69064fec039acc571c29020cc87c76", size = 1976802, upload-time = "2026-05-06T13:37:35.113Z" },+ { url = "https://files.pythonhosted.org/packages/21/f2/95727e1368be3d3ed485eaab7adbd7dda408f33f7a36e8b48e0144002b91/pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:e739fee756ba1010f8bcccb534252e85a35fe45ae92c295a06059ce58b74ccd3", size = 2052446, upload-time = "2026-05-06T13:37:12.313Z" },+ { url = "https://files.pythonhosted.org/packages/9c/86/5d99feea3f77c7234b8718075b23db11532773c1a0dbd9b9490215dc2eeb/pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9d56801be94b86a9da183e5f3766e6310752b99ff647e38b09a9500d88e46e76", size = 2232757, upload-time = "2026-05-06T13:39:01.149Z" },+ { url = "https://files.pythonhosted.org/packages/d2/3a/508ac615935ef7588cf6d9e9b91309fdc2da751af865e02a9098de88258c/pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:2412e734dcb48da14d4e4006b82b46b74f2518b8a26ee7e58c6844a6cd6d03c4", size = 2309275, upload-time = "2026-05-06T13:37:41.406Z" },+ { url = "https://files.pythonhosted.org/packages/07/f8/41db9de19d7987d6b04715a02b3b40aea467000275d9d758ffaa31af7d50/pydantic_core-2.46.4-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:9551187363ffc0de2a00b2e47c25aeaeb1020b69b668762966df15fc5659dd5a", size = 2094467, upload-time = "2026-05-06T13:39:18.847Z" },+ { url = "https://files.pythonhosted.org/packages/2c/e2/f35033184cb11d0052daf4416e8e10a502ea2ac006fc4f459aee872727d1/pydantic_core-2.46.4-cp313-cp313-manylinux_2_31_riscv64.whl", hash = "sha256:0186750b482eefa11d7f435892b09c5c606193ef3375bcf94aa00ae6bfb66262", size = 2134417, upload-time = "2026-05-06T13:40:17.944Z" },+ { url = "https://files.pythonhosted.org/packages/7e/7b/6ceeb1cc90e193862f444ebe373d8fdf613f0a82572dde03fb10734c6c71/pydantic_core-2.46.4-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.whl", hash = "sha256:5855698a4856556d86e8e6cd8434bc3ac0314ee8e12089ae0e143f64c6256e4e", size = 2179782, upload-time = "2026-05-06T13:40:32.618Z" },+ { url = "https://files.pythonhosted.org/packages/5a/f2/c8d7773ede6af08036423a00ae0ceffce266c3c52a096c435d68c896083f/pydantic_core-2.46.4-cp313-cp313-musllinux_1_1_aarch64.whl", hash = "sha256:cbaf13819775b7f769bf4a1f066cb6df7a28d4480081a589828ef190226881cd", size = 2188782, upload-time = "2026-05-06T13:36:51.018Z" },+ { url = "https://files.pythonhosted.org/packages/59/31/0c864784e31f09f05cdd87606f08923b9c9e7f6e51dd27f20f62f975ce9f/pydantic_core-2.46.4-cp313-cp313-musllinux_1_1_armv7l.whl", hash = "sha256:633147d34cf4550417f12e2b1a0383973bdf5cdfde212cb09e9a581cf10820be", size = 2328334, upload-time = "2026-05-06T13:40:37.764Z" },+ { url = "https://files.pythonhosted.org/packages/c2/eb/4f6c8a41efa30baa755590f4141abf3a8c370fab610915733e74134a7270/pydantic_core-2.46.4-cp313-cp313-musllinux_1_1_x86_64.whl", hash = "sha256:82cf5301172168103724d49a1444d3378cb20cdee30b116a1bd6031236298a5d", size = 2372986, upload-time = "2026-05-06T13:39:34.152Z" },+ { url = "https://files.pythonhosted.org/packages/5b/24/b375a480d53113860c299764bfe9f349a3dc9108b3adc0d7f0d786492ebf/pydantic_core-2.46.4-cp313-cp313-win32.whl", hash = "sha256:9fa8ae11da9e2b3126c6426f147e0fba88d96d65921799bb30c6abd1cb2c97fb", size = 1973693, upload-time = "2026-05-06T13:37:55.072Z" },+ { url = "https://files.pythonhosted.org/packages/7e/e8/cff247591966f2d22ec8c003cd7587e27b7ba7b81ab2fb888e3ab75dc285/pydantic_core-2.46.4-cp313-cp313-win_amd64.whl", hash = "sha256:6b3ace8194b0e5204818c92802dcdca7fc6d88aabbb799d7c795540d9cd6d292", size = 2071819, upload-time = "2026-05-06T13:38:49.139Z" },+ { url = "https://files.pythonhosted.org/packages/c6/1a/f4aee670d5670e9e148e0c82c7db98d780be566c6e6a97ee8035528ca0b3/pydantic_core-2.46.4-cp313-cp313-win_arm64.whl", hash = "sha256:184c081504d17f1c1066e430e117142b2c77d9448a97f7b65c6ac9fd9aee238d", size = 2027411, upload-time = "2026-05-06T13:40:45.796Z" },+]+[[package]]name = "pygments"version = "2.20.0"@@ -924,6 +987,18 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548", size = 44614, upload-time = "2025-08-25T13:49:24.86Z" },]+[[package]]+name = "typing-inspection"+version = "0.4.2"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "typing-extensions" },+]+sdist = { url = "https://files.pythonhosted.org/packages/55/e3/70399cb7dd41c10ac53367ae42139cf4b1ca5f36bb3dc6c9d33acdb43655/typing_inspection-0.4.2.tar.gz", hash = "sha256:ba561c48a67c5958007083d386c3295464928b01faa735ab8547c5692e87f464", size = 75949, upload-time = "2025-10-01T02:14:41.687Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/dc/9b/47798a6c91d8bdb567fe2698fe81e0c6b7cb7ef4d13da4114b41d239f65d/typing_inspection-0.4.2-py3-none-any.whl", hash = "sha256:4ed1cacbdc298c220f1bd249ed5287caa16f34d44ef4e9c3d0cbad5b521545e7", size = 14611, upload-time = "2025-10-01T02:14:40.154Z" },+]+[[package]]name = "tzdata"version = "2026.2"