Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion database/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import uuid
from datetime import datetime
from typing import Annotated, Any, ClassVar
Expand All @@ -12,7 +13,7 @@

from database.enums import AttributeValueDataType

PROJECT_SRID = 3067
PROJECT_SRID = int(os.environ.get("PROJECT_SRID", "3067"))


class Base(DeclarativeBase):
Expand Down
1 change: 1 addition & 0 deletions infra/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ locals {
READ_FROM_AWS = 1
SYKE_APIKEY = var.syke_apikey
DB_SECRET_DBA_ARN = aws_secretsmanager_secret.hame-db-dba.arn
PROJECT_SRID = var.project_srid
}
ryhti_client_x-road_environment = var.enable_x_road ? {
XROAD_SERVER_ADDRESS = module.x-road["this"].dns_record
Expand Down
6 changes: 6 additions & 0 deletions infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ variable "prefix" {
type = string
}

variable "project_srid" {
description = "SRID to use for geometries in the database"
type = number
default = 3067
}

variable "extra_tags" {
description = "Some extra tags for all resources. Use JSON format"
type = any
Expand Down
51 changes: 40 additions & 11 deletions lambdas/ryhti_client/ryhti_client/database_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import datetime
import logging
from typing import TYPE_CHECKING, Any, TypeVar, cast
from typing import TYPE_CHECKING, Any, ClassVar, Protocol, TypeVar, cast
from uuid import UUID, uuid4
from zoneinfo import ZoneInfo

import simplejson as json
from geoalchemy2.shape import to_shape
from shapely import to_geojson
from sqlalchemy import create_engine, select
from sqlalchemy import Table, create_engine, select, text
from sqlalchemy.orm import sessionmaker

from database import base, codes, models
Expand All @@ -32,6 +32,7 @@
from collections.abc import Generator

from geoalchemy2 import WKBElement
from sqlalchemy.sql import FromClause

from database.base import DbId
from ryhti_client.ryhti_client import RyhtiResponse
Expand All @@ -46,6 +47,11 @@
MODEL = TypeVar("MODEL", bound=models.Base)


class Geometrical(Protocol):
geom: WKBElement
__table__: ClassVar[FromClause]


class PlanAlreadyExistsError(Exception):
def __init__(self, plan_id: str) -> None:
self.plan_id = plan_id
Expand Down Expand Up @@ -90,6 +96,7 @@ def __init__(self, connection_string: str, plan_uuid: str | None = None) -> None
self.plans: dict[DbId, models.Plan] = {}
# Cache plan dictionaries
self.plan_dictionaries: dict[DbId, RyhtiPlan] = {}
self._table_srids: dict[tuple[str, str], int] = {}

# We only ever need code uri values, not codes themselves, so let's not bother
# fetching codes from the database at all. URI is known from class and value.
Expand Down Expand Up @@ -138,26 +145,48 @@ def get_unique_plan_matters(self) -> Generator[models.PlanMatter]:
seen_plan_matter_ids.add(plan.plan_matter.id)
yield plan.plan_matter

def get_geojson(self, geometry: WKBElement) -> dict:
"""Returns geojson format dict with the correct SRID set."""
def _get_srid_of_table(self, table: Table) -> int:
table_schema = table.schema
table_name = table.name
if not table_schema:
raise ValueError(f"Table {table_name} does not have a schema defined.")

srid = self._table_srids.get((table_schema, table_name))
if srid is not None:
return srid

with self.Session() as session:
srid = session.execute(
text(
"SELECT srid FROM public.geometry_columns "
"WHERE f_table_schema = :schema AND f_table_name = :table"
),
{"schema": table_schema, "table": table_name},
).scalar_one()
self._table_srids[(table_schema, table_name)] = srid
return srid

def get_geometry_as_json(self, obj: Geometrical) -> dict[str, Any]:
"""Returns Ryhti formatted geom dict with the correct SRID and
geometry as geojson
"""
# We cannot use postgis geojson functions here, because the data has already
# been fetched from the database. So let's create geojson the python way, it's
# probably faster than doing extra database queries for the conversion.
# However, it seems that to_shape forgets to add the SRID information from the
# EWKB (https://github.com/geoalchemy/geoalchemy2/issues/235), so we have to
# paste the SRID back manually :/
shape = to_shape(geometry)

shape = to_shape(obj.geom)
srid = self._get_srid_of_table(cast("Table", obj.__table__))
if len(shape.geoms) == 1:
# Ryhti API may not allow single geometries in multigeometries in all cases.
# Let's make them into single geometries instead:
shape = shape.geoms[0]
# Also, we don't want to serialize the geojson quite yet. Looks like the only
# way to do get python dict to actually convert the json back to dict until we
# are ready to reserialize it :/
return {
"srid": str(base.PROJECT_SRID),
"geometry": json.loads(to_geojson(shape)),
}
return {"srid": str(srid), "geometry": json.loads(to_geojson(shape))}

def get_isoformat_value_with_z(self, datetime_value: datetime.datetime) -> str:
"""Returns isoformatted datetime in UTC with Z instead of +00:00."""
Expand Down Expand Up @@ -364,7 +393,7 @@ def get_plan_object(self, plan_object: models.PlanObjectBase) -> dict:
plan_object_dict["planObjectKey"] = plan_object.id
plan_object_dict["lifeCycleStatus"] = plan_object.lifecycle_status.uri
plan_object_dict["undergroundStatus"] = plan_object.type_of_underground.uri
plan_object_dict["geometry"] = self.get_geojson(plan_object.geom)
plan_object_dict["geometry"] = self.get_geometry_as_json(plan_object)
plan_object_dict["name"] = self.format_language_string_value(plan_object.name)
plan_object_dict["description"] = self.format_language_string_value(
plan_object.description
Expand Down Expand Up @@ -524,7 +553,7 @@ def get_plan_dictionary(self, plan: models.Plan) -> RyhtiPlan:
else None
)
plan_dictionary["scale"] = plan.scale
plan_dictionary["geographicalArea"] = self.get_geojson(plan.geom)
plan_dictionary["geographicalArea"] = self.get_geometry_as_json(plan)
# For reasons unknown, Ryhti does not allow multilanguage description.
plan_description = (
plan.description.get("fin") if isinstance(plan.description, dict) else None
Expand Down
1 change: 1 addition & 0 deletions lambdas/ryhti_client/ryhti_client/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ def get_code_instance_from_uri(self, code_uri: str) -> CodeBase | None:
return self.get_code_instance(CodeModel, code)

def deserialize_ryhti_geometry(self, geometry: RyhtiGeometry) -> WKBElement:
# TODO: Support on-the-fly reprojection if SRID is different
if int(geometry.srid) != PROJECT_SRID:
raise ValueError(
f"Unsupported SRID: {geometry.srid}, expected: {PROJECT_SRID}"
Expand Down