Skip to content

Commit

Permalink
Fix #415; add range regulation to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikuoja committed Jan 22, 2025
1 parent a91bfb1 commit 37cc108
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 20 deletions.
11 changes: 5 additions & 6 deletions database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from geoalchemy2 import Geometry
from shapely.geometry import MultiLineString, MultiPoint, MultiPolygon
from sqlalchemy import ForeignKey, Index
from sqlalchemy.dialects.postgresql import JSONB, NUMRANGE, UUID, Range
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
Expand All @@ -32,7 +32,6 @@ class Base(DeclarativeBase):
MultiLineString: Geometry(geometry_type="MULTILINESTRING", srid=PROJECT_SRID),
MultiPoint: Geometry(geometry_type="MULTIPOINT", srid=PROJECT_SRID),
MultiPolygon: Geometry(geometry_type="MULTIPOLYGON", srid=PROJECT_SRID),
Range[float]: NUMRANGE,
datetime: TIMESTAMP(timezone=True),
}

Expand All @@ -45,7 +44,6 @@ class Base(DeclarativeBase):
]
unique_str = Annotated[str, mapped_column(unique=True, index=True)]
language_str = Annotated[dict[str, str], mapped_column(nullable=True)]
numeric_range = Annotated[Range[float], mapped_column(nullable=True)]
timestamp = Annotated[datetime, mapped_column(server_default=func.now())]

metadata = Base.metadata
Expand Down Expand Up @@ -167,9 +165,10 @@ def __table_args__(cls):

name: Mapped[language_str]
description: Mapped[language_str]
source_data_object: Mapped[str] = mapped_column(nullable=True)
height_range: Mapped[numeric_range]
height_unit: Mapped[str] = mapped_column(nullable=True)
source_data_object: Mapped[Optional[str]]
height_min: Mapped[Optional[float]]
height_max: Mapped[Optional[float]]
height_unit: Mapped[Optional[str]]
ordering: Mapped[Optional[int]]
type_of_underground_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey("codes.type_of_underground.id", name="type_of_underground_id_fkey"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
"""separate range fields
Revision ID: d7a23c8273dc
Revises: 8b6f3fb9c92e
Create Date: 2025-01-22 15:33:46.390311
"""
from typing import Sequence, Union

import geoalchemy2
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "d7a23c8273dc"
down_revision: Union[str, None] = "8b6f3fb9c92e"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"land_use_area",
sa.Column("height_min", sa.Float(), nullable=True),
schema="hame",
)
op.add_column(
"land_use_area",
sa.Column("height_max", sa.Float(), nullable=True),
schema="hame",
)
op.drop_column("land_use_area", "height_range", schema="hame")
op.add_column(
"land_use_point",
sa.Column("height_min", sa.Float(), nullable=True),
schema="hame",
)
op.add_column(
"land_use_point",
sa.Column("height_max", sa.Float(), nullable=True),
schema="hame",
)
op.drop_column("land_use_point", "height_range", schema="hame")
op.add_column(
"line",
sa.Column("height_min", sa.Float(), nullable=True),
schema="hame",
)
op.add_column(
"line",
sa.Column("height_max", sa.Float(), nullable=True),
schema="hame",
)
op.drop_column("line", "height_range", schema="hame")
op.add_column(
"other_area",
sa.Column("height_min", sa.Float(), nullable=True),
schema="hame",
)
op.add_column(
"other_area",
sa.Column("height_max", sa.Float(), nullable=True),
schema="hame",
)
op.drop_column("other_area", "height_range", schema="hame")
op.add_column(
"other_point",
sa.Column("height_min", sa.Float(), nullable=True),
schema="hame",
)
op.add_column(
"other_point",
sa.Column("height_max", sa.Float(), nullable=True),
schema="hame",
)
op.drop_column("other_point", "height_range", schema="hame")
op.add_column(
"plan_regulation",
sa.Column("numeric_range_min", sa.Integer(), nullable=True),
schema="hame",
)
op.add_column(
"plan_regulation",
sa.Column("numeric_range_max", sa.Integer(), nullable=True),
schema="hame",
)
op.drop_column("plan_regulation", "numeric_range", schema="hame")
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"plan_regulation",
sa.Column(
"numeric_range",
postgresql.NUMRANGE(),
autoincrement=False,
nullable=True,
),
schema="hame",
)
op.drop_column("plan_regulation", "numeric_range_max", schema="hame")
op.drop_column("plan_regulation", "numeric_range_min", schema="hame")
op.add_column(
"other_point",
sa.Column(
"height_range",
postgresql.NUMRANGE(),
autoincrement=False,
nullable=True,
),
schema="hame",
)
op.drop_column("other_point", "height_max", schema="hame")
op.drop_column("other_point", "height_min", schema="hame")
op.add_column(
"other_area",
sa.Column(
"height_range",
postgresql.NUMRANGE(),
autoincrement=False,
nullable=True,
),
schema="hame",
)
op.drop_column("other_area", "height_max", schema="hame")
op.drop_column("other_area", "height_min", schema="hame")
op.add_column(
"line",
sa.Column(
"height_range",
postgresql.NUMRANGE(),
autoincrement=False,
nullable=True,
),
schema="hame",
)
op.drop_column("line", "height_max", schema="hame")
op.drop_column("line", "height_min", schema="hame")
op.add_column(
"land_use_point",
sa.Column(
"height_range",
postgresql.NUMRANGE(),
autoincrement=False,
nullable=True,
),
schema="hame",
)
op.drop_column("land_use_point", "height_max", schema="hame")
op.drop_column("land_use_point", "height_min", schema="hame")
op.add_column(
"land_use_area",
sa.Column(
"height_range",
postgresql.NUMRANGE(),
autoincrement=False,
nullable=True,
),
schema="hame",
)
op.drop_column("land_use_area", "height_max", schema="hame")
op.drop_column("land_use_area", "height_min", schema="hame")
# ### end Alembic commands ###
10 changes: 5 additions & 5 deletions database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
PlanObjectBase,
VersionedBase,
language_str,
numeric_range,
timestamp,
)
from shapely.geometry import MultiLineString, MultiPoint, MultiPolygon
Expand Down Expand Up @@ -208,7 +207,7 @@ class PlanRegulationGroup(VersionedBase):
VersionedBase.__table_args__,
)

short_name: Mapped[str] = mapped_column(nullable=True)
short_name: Mapped[Optional[str]]
name: Mapped[language_str]

plan_id: Mapped[uuid.UUID] = mapped_column(
Expand Down Expand Up @@ -465,10 +464,11 @@ class PlanRegulation(PlanBase):
lazy="joined",
)

numeric_range: Mapped[numeric_range]
unit: Mapped[str] = mapped_column(nullable=True)
text_value: Mapped[language_str]
numeric_value: Mapped[float] = mapped_column(nullable=True)
numeric_value: Mapped[Optional[float]]
numeric_range_min: Mapped[Optional[int]]
numeric_range_max: Mapped[Optional[int]]
unit: Mapped[Optional[str]]
ordering: Mapped[Optional[int]]
subject_identifiers: Mapped[List[str]] = mapped_column(server_default="{}")

Expand Down
18 changes: 15 additions & 3 deletions database/ryhti_client/ryhti_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,18 @@ def get_plan_regulation(self, plan_regulation: models.PlanRegulation) -> Dict:
"number": plan_regulation.numeric_value,
"unitOfMeasure": plan_regulation.unit,
}
elif plan_regulation.numeric_range_min or plan_regulation.numeric_range_max:
regulation_dict["value"] = {
"dataType": "positiveNumericRange"
if (
plan_regulation.numeric_range_min
and plan_regulation.numeric_range_min >= 0
)
else "numericRange",
"minimumValue": plan_regulation.numeric_range_min,
"maximumValue": plan_regulation.numeric_range_max,
"unitOfMeasure": plan_regulation.unit,
}
return regulation_dict

def get_plan_regulation_group(
Expand Down Expand Up @@ -646,12 +658,12 @@ def get_plan_object(self, plan_object: base.PlanObjectBase) -> Dict:
plan_object_dict["periodOfValidity"] = self.get_last_period(
self.get_lifecycle_periods(plan_object, self.valid_status_value)
)
if plan_object.height_range:
if plan_object.height_min or plan_object.height_max:
plan_object_dict["verticalLimit"] = {
"dataType": "decimalRange",
# we have to use simplejson because numbers are Decimal
"minimumValue": plan_object.height_range.lower,
"maximumValue": plan_object.height_range.upper,
"minimumValue": plan_object.height_min,
"maximumValue": plan_object.height_max,
"unitOfMeasure": plan_object.height_unit,
}
return plan_object_dict
Expand Down
Loading

0 comments on commit 37cc108

Please sign in to comment.