Skip to content

Open Infrastructure Map, a view of infrastructure data in OpenStreetMap

Backend updates, routes for new endpoints

Russ Garretta0d7df3e3d6a
diff --git a/web-backend/pyproject.toml b/web-backend/pyproject.toml
index 661b257..f07b784 100644
--- a/web-backend/pyproject.toml
+++ b/web-backend/pyproject.toml
@@ -21,6 +21,7 @@ dependencies = [
"pydantic>=2.13.4",
"geojson>=3.3.0",
"asgiref>=3.11.1",
+ "shapely>=2.1.2",
]
[tool.ruff]
diff --git a/web-backend/src/__init__.py b/web-backend/src/__init__.py
index f97c55e..8c184d3 100644
--- a/web-backend/src/__init__.py
+++ b/web-backend/src/__init__.py
@@ -1,7 +1,8 @@
-from typing import TypedDict, cast
+from typing import Type, TypedDict, cast
import httpx
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
+from starlette.exceptions import HTTPException
from starlette.requests import Request as StarletteRequest
@@ -15,3 +16,10 @@ Request = StarletteRequest[State]
def get_db(request: Request) -> AsyncConnection:
return cast(AsyncConnection, request.scope.get("db_connection"))
+
+
+def path_param[T](request: Request, name: str, typ: Type[T]) -> T:
+ try:
+ return typ(request.path_params[name]) # type: ignore
+ except ValueError:
+ raise HTTPException(400, f"Invalid value for {name}")
diff --git a/web-backend/src/views/api.py b/web-backend/src/views/api.py
index c9a59ba..075eb8c 100644
--- a/web-backend/src/views/api.py
+++ b/web-backend/src/views/api.py
@@ -1,53 +1,71 @@
from typing import Any
+from numpy import number
from pydantic import BaseModel
+from shapely import from_wkt
+from sqlalchemy.dialects.postgresql import HSTORE
from sqlalchemy.sql import text
from starlette.exceptions import HTTPException
from starlette.responses import Response
from starlette.routing import Route
-from .. import Request, get_db
+from .. import Request, get_db, path_param
from ..util import cache_for
+def extract_local_names(tags: dict[str, str]) -> dict[str, str]:
+ result = {}
+ for key in tags:
+ if key.startswith("name:"):
+ _, code = key.split(":", 1)
+ result[code] = tags[key]
+ return result
+
+
class Circuit(BaseModel):
id: int
name: str | None
- name_en: str | None
+ local_names: dict[str, str]
voltage: int | None
frequency: int | None
+
+
+class CircuitMembership(BaseModel):
+ circuit: Circuit
role: str | None
class SubstationResponse(BaseModel):
- circuits: list[Circuit]
+ circuits: list[CircuitMembership]
@cache_for(600)
async def substation(request: Request) -> Response:
database = get_db(request)
- substation_id = request.path_params["substation_id"]
+ substation_id = path_param(request, "substation_id", int)
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,
+ relation.tags,
convert_voltage(relation.tags -> 'voltage') AS voltage,
member.role
FROM osm_power_circuit_relation_member member, osm_power_circuit_relation relation
WHERE relation.osm_id = member.osm_id AND member.member = :member_id"""
- ),
- {"member_id": int(substation_id)},
+ ).columns(tags=HSTORE),
+ {"member_id": substation_id},
)
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"],
+ CircuitMembership(
+ circuit=Circuit(
+ id=r._mapping["relation_id"],
+ name=r._mapping["name"],
+ local_names=extract_local_names(r._mapping["tags"]),
+ voltage=r._mapping["voltage"] / 1000 if r._mapping["voltage"] else None,
+ frequency=r._mapping["frequency"],
+ ),
role=r._mapping["role"],
)
for r in res
@@ -56,28 +74,36 @@ async def substation(request: Request) -> Response:
return Response(SubstationResponse(circuits=circuits).model_dump_json(), media_type="application/json")
-class CircuitResponse(BaseModel):
+class CircuitSubstation(BaseModel):
id: int
name: str | None
- name_en: str | None
- voltage: int | None
- frequency: int | None
+ local_names: dict[str, str]
+ location: tuple[float, float]
+
+
+class CircuitResponse(Circuit):
length: float
+ type: str
+ wikidata: str | None
- geometry: dict[str, Any]
+ substations: list[CircuitSubstation]
+
+ geometry: dict[str, Any] # GeoJSON
@cache_for(600)
async def circuit(request: Request) -> Response:
database = get_db(request)
- circuit_id = request.path_params["circuit_id"]
- res = (
+ circuit_id = path_param(request, "circuit_id", int)
+ relation = (
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,
+ relation.tags -> 'type' AS type,
+ relation.tags -> 'wikidata' AS wikidata,
+ relation.tags,
convert_voltage(relation.tags -> 'voltage') AS voltage,
(SELECT json_build_object('type', 'FeatureCollection', 'features',
json_agg(json_build_object('type', 'Feature',
@@ -86,47 +112,78 @@ async def circuit(request: Request) -> Response:
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
+ osm_id = relation.osm_id) AS length,
+ (select ST_AsText(st_envelope(st_collect(st_transform(geometry, 4326)))) from
+ osm_power_circuit_relation_member where osm_id = relation.osm_id) AS bbox
FROM osm_power_circuit_relation relation
WHERE relation.osm_id = :relation_id
- """),
- {"relation_id": int(circuit_id)},
+ AND relation.tags -> 'type' IN ('power', 'route')
+ """).columns(tags=HSTORE),
+ {"relation_id": circuit_id},
)
).fetchone()
- if not res:
+ if not relation:
raise HTTPException(404)
+ geojson = relation._mapping["geojson"]
+
+ bbox = from_wkt(relation._mapping["bbox"])
+ geojson["bbox"] = bbox.bounds
+
+ substations = await database.execute(
+ text("""
+ SELECT id, tags -> 'name' AS name, tags,
+ ST_AsText(ST_Transform(ST_Centroid(geometry),4326)) AS location
+ FROM osm_power_circuit_relation_member
+ WHERE osm_id = :relation_id AND role = 'substation'
+ """).columns(tags=HSTORE),
+ {"relation_id": circuit_id},
+ )
+
+ circuit_substations = [
+ CircuitSubstation(
+ id=s._mapping["id"],
+ name=s._mapping["name"],
+ local_names=extract_local_names(s._mapping["tags"]),
+ location=from_wkt(s._mapping["location"]).coords[0],
+ )
+ for s in substations
+ ]
+
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),
+ id=relation._mapping["relation_id"],
+ name=relation._mapping["name"],
+ local_names=extract_local_names(relation._mapping["tags"]),
+ voltage=relation._mapping["voltage"] / 1000 if relation._mapping["voltage"] else None,
+ frequency=relation._mapping["frequency"],
+ geometry=geojson,
+ length=round((relation._mapping["length"] or 0) / 1000, 2),
+ substations=circuit_substations,
+ type=relation._mapping["type"],
+ wikidata=relation._mapping["wikidata"],
).model_dump_json(),
media_type="application/json",
)
class LineResponse(BaseModel):
- circuits: list[Circuit]
+ circuits: list[CircuitMembership]
@cache_for(600)
async def line(request: Request) -> Response:
database = get_db(request)
- line_id = request.path_params["line_id"]
+ line_id = path_param(request, "line_id", int)
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,
+ relation.tags AS tags,
convert_voltage(relation.tags -> 'voltage') AS voltage,
member.role
FROM osm_power_circuit_relation_member member,
@@ -135,16 +192,18 @@ async def line(request: Request) -> Response:
AND member.osm_id = relation.osm_id
"""
),
- {"line_id": int(line_id)},
+ {"line_id": line_id},
)
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"],
+ CircuitMembership(
+ circuit=Circuit(
+ id=r._mapping["relation_id"],
+ name=r._mapping["name"],
+ local_names=extract_local_names(r._mapping["tags"]),
+ voltage=r._mapping["voltage"] / 1000 if r._mapping["voltage"] else None,
+ frequency=r._mapping["frequency"],
+ ),
role=r._mapping["role"],
)
for r in res
diff --git a/web-backend/uv.lock b/web-backend/uv.lock
index 5eaa209..d481ce8 100644
--- a/web-backend/uv.lock
+++ b/web-backend/uv.lock
@@ -540,6 +540,7 @@ dependencies = [
{ name = "pandas" },
{ name = "pycountry" },
{ name = "pydantic" },
+ { name = "shapely" },
{ name = "sqlalchemy", extra = ["asyncio"] },
{ name = "starlette" },
{ name = "uvicorn" },
@@ -572,6 +573,7 @@ requires-dist = [
{ name = "pandas", specifier = ">=2.2.3" },
{ name = "pycountry", specifier = ">=24.6.1" },
{ name = "pydantic", specifier = ">=2.13.4" },
+ { name = "shapely", specifier = ">=2.1.2" },
{ name = "sqlalchemy", extras = ["asyncio"], specifier = ">=2.0.50" },
{ name = "starlette" },
{ name = "uvicorn" },
@@ -903,6 +905,33 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/2d/c7/c53e8dbff9c9dc4b7928773421ae294a5d28fcb8dcda1a089579d3a7e510/ruff-0.15.17-py3-none-win_arm64.whl", hash = "sha256:f3be1fbb34bcdfd146240d8fb92a709d4c2c8191348580a3c044ec60fa0b4456", size = 11355275, upload-time = "2026-06-11T17:54:43.635Z" },
]
+[[package]]
+name = "shapely"
+version = "2.1.2"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "numpy" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/4d/bc/0989043118a27cccb4e906a46b7565ce36ca7b57f5a18b78f4f1b0f72d9d/shapely-2.1.2.tar.gz", hash = "sha256:2ed4ecb28320a433db18a5bf029986aa8afcfd740745e78847e330d5d94922a9", size = 315489, upload-time = "2025-09-24T13:51:41.432Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/c3/90/98ef257c23c46425dc4d1d31005ad7c8d649fe423a38b917db02c30f1f5a/shapely-2.1.2-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:b510dda1a3672d6879beb319bc7c5fd302c6c354584690973c838f46ec3e0fa8", size = 1832644, upload-time = "2025-09-24T13:50:44.886Z" },
+ { url = "https://files.pythonhosted.org/packages/6d/ab/0bee5a830d209adcd3a01f2d4b70e587cdd9fd7380d5198c064091005af8/shapely-2.1.2-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:8cff473e81017594d20ec55d86b54bc635544897e13a7cfc12e36909c5309a2a", size = 1642887, upload-time = "2025-09-24T13:50:46.735Z" },
+ { url = "https://files.pythonhosted.org/packages/2d/5e/7d7f54ba960c13302584c73704d8c4d15404a51024631adb60b126a4ae88/shapely-2.1.2-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:fe7b77dc63d707c09726b7908f575fc04ff1d1ad0f3fb92aec212396bc6cfe5e", size = 2970931, upload-time = "2025-09-24T13:50:48.374Z" },
+ { url = "https://files.pythonhosted.org/packages/f2/a2/83fc37e2a58090e3d2ff79175a95493c664bcd0b653dd75cb9134645a4e5/shapely-2.1.2-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:7ed1a5bbfb386ee8332713bf7508bc24e32d24b74fc9a7b9f8529a55db9f4ee6", size = 3082855, upload-time = "2025-09-24T13:50:50.037Z" },
+ { url = "https://files.pythonhosted.org/packages/44/2b/578faf235a5b09f16b5f02833c53822294d7f21b242f8e2d0cf03fb64321/shapely-2.1.2-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a84e0582858d841d54355246ddfcbd1fce3179f185da7470f41ce39d001ee1af", size = 3979960, upload-time = "2025-09-24T13:50:51.74Z" },
+ { url = "https://files.pythonhosted.org/packages/4d/04/167f096386120f692cc4ca02f75a17b961858997a95e67a3cb6a7bbd6b53/shapely-2.1.2-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:dc3487447a43d42adcdf52d7ac73804f2312cbfa5d433a7d2c506dcab0033dfd", size = 4142851, upload-time = "2025-09-24T13:50:53.49Z" },
+ { url = "https://files.pythonhosted.org/packages/48/74/fb402c5a6235d1c65a97348b48cdedb75fb19eca2b1d66d04969fc1c6091/shapely-2.1.2-cp313-cp313-win32.whl", hash = "sha256:9c3a3c648aedc9f99c09263b39f2d8252f199cb3ac154fadc173283d7d111350", size = 1541890, upload-time = "2025-09-24T13:50:55.337Z" },
+ { url = "https://files.pythonhosted.org/packages/41/47/3647fe7ad990af60ad98b889657a976042c9988c2807cf322a9d6685f462/shapely-2.1.2-cp313-cp313-win_amd64.whl", hash = "sha256:ca2591bff6645c216695bdf1614fca9c82ea1144d4a7591a466fef64f28f0715", size = 1722151, upload-time = "2025-09-24T13:50:57.153Z" },
+ { url = "https://files.pythonhosted.org/packages/3c/49/63953754faa51ffe7d8189bfbe9ca34def29f8c0e34c67cbe2a2795f269d/shapely-2.1.2-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:2d93d23bdd2ed9dc157b46bc2f19b7da143ca8714464249bef6771c679d5ff40", size = 1834130, upload-time = "2025-09-24T13:50:58.49Z" },
+ { url = "https://files.pythonhosted.org/packages/7f/ee/dce001c1984052970ff60eb4727164892fb2d08052c575042a47f5a9e88f/shapely-2.1.2-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:01d0d304b25634d60bd7cf291828119ab55a3bab87dc4af1e44b07fb225f188b", size = 1642802, upload-time = "2025-09-24T13:50:59.871Z" },
+ { url = "https://files.pythonhosted.org/packages/da/e7/fc4e9a19929522877fa602f705706b96e78376afb7fad09cad5b9af1553c/shapely-2.1.2-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:8d8382dd120d64b03698b7298b89611a6ea6f55ada9d39942838b79c9bc89801", size = 3018460, upload-time = "2025-09-24T13:51:02.08Z" },
+ { url = "https://files.pythonhosted.org/packages/a1/18/7519a25db21847b525696883ddc8e6a0ecaa36159ea88e0fef11466384d0/shapely-2.1.2-cp313-cp313t-manylinux2014_x86_64.manylinux_2_17_x86_64.whl", hash = "sha256:19efa3611eef966e776183e338b2d7ea43569ae99ab34f8d17c2c054d3205cc0", size = 3095223, upload-time = "2025-09-24T13:51:04.472Z" },
+ { url = "https://files.pythonhosted.org/packages/48/de/b59a620b1f3a129c3fecc2737104a0a7e04e79335bd3b0a1f1609744cf17/shapely-2.1.2-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:346ec0c1a0fcd32f57f00e4134d1200e14bf3f5ae12af87ba83ca275c502498c", size = 4030760, upload-time = "2025-09-24T13:51:06.455Z" },
+ { url = "https://files.pythonhosted.org/packages/96/b3/c6655ee7232b417562bae192ae0d3ceaadb1cc0ffc2088a2ddf415456cc2/shapely-2.1.2-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:6305993a35989391bd3476ee538a5c9a845861462327efe00dd11a5c8c709a99", size = 4170078, upload-time = "2025-09-24T13:51:08.584Z" },
+ { url = "https://files.pythonhosted.org/packages/a0/8e/605c76808d73503c9333af8f6cbe7e1354d2d238bda5f88eea36bfe0f42a/shapely-2.1.2-cp313-cp313t-win32.whl", hash = "sha256:c8876673449f3401f278c86eb33224c5764582f72b653a415d0e6672fde887bf", size = 1559178, upload-time = "2025-09-24T13:51:10.73Z" },
+ { url = "https://files.pythonhosted.org/packages/36/f7/d317eb232352a1f1444d11002d477e54514a4a6045536d49d0c59783c0da/shapely-2.1.2-cp313-cp313t-win_amd64.whl", hash = "sha256:4a44bc62a10d84c11a7a3d7c1c4fe857f7477c3506e24c9062da0db0ae0c449c", size = 1739756, upload-time = "2025-09-24T13:51:12.105Z" },
+]
+
[[package]]
name = "six"
version = "1.17.0"
diff --git a/web-router/Dockerfile b/web-router/Dockerfile
index 4604016..44e65dc 100644
--- a/web-router/Dockerfile
+++ b/web-router/Dockerfile
@@ -1,3 +1,3 @@
-FROM nginx:1.26-alpine
+FROM nginx:1-alpine
-COPY ./nginx.conf /etc/nginx/nginx.conf
\ No newline at end of file
+COPY ./nginx.conf /etc/nginx/nginx.conf
diff --git a/web-router/nginx.conf b/web-router/nginx.conf
index 49d7ba6..35d7c13 100644
--- a/web-router/nginx.conf
+++ b/web-router/nginx.conf
@@ -67,6 +67,10 @@ http {
proxy_pass http://oim-web-backend;
}
+ location /api {
+ proxy_pass http://oim-web-backend;
+ }
+
location /search {
proxy_pass http://oim-web-backend;
}