Skip to content

Commit

Permalink
Merge branch 'master' into margriet_138_force_geopackage
Browse files Browse the repository at this point in the history
  • Loading branch information
margrietpalm authored Jan 23, 2025
2 parents 4a22257 + e367743 commit d00a6c1
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 29 deletions.
18 changes: 18 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ Changelog of threedi-schema
- Convert spatialite to geopackage during upgrade


0.230.3 (2025-01-23)
--------------------

- Fix invalid setting of geometry types for lateral_2d and boundary_condition_2d


0.230.2 (2025-01-23)
--------------------

- Modify model names such that sqlite table names match to model names


0.230.1 (2025-01-21)
--------------------

- Fix invalid geometry types for measure_map, memory_control and table_control


0.230.0 (2025-01-16)
--------------------

Expand Down
2 changes: 1 addition & 1 deletion threedi_schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from .domain import constants, custom_types, models # NOQA

# fmt: off
__version__ = '0.230.1.dev0'
__version__ = '0.230.4.dev0'

# fmt: on
2 changes: 1 addition & 1 deletion threedi_schema/domain/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def get_legacy_value(self) -> str:
return f"v2_{self.value}"


class ControlTableActionTypes(Enum):
class TableControlActionTypes(Enum):
set_discharge_coefficients = "set_discharge_coefficients" # not pump
set_crest_level = "set_crest_level" # orifice, weir only
set_pump_capacity = "set_pump_capacity" # only pump, in API: set_pump_capacity
Expand Down
36 changes: 18 additions & 18 deletions threedi_schema/domain/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BoundaryConditions2D(Base):
geom = Column(Geometry("LINESTRING"), nullable=False)


class ControlMeasureLocation(Base):
class MeasureLocation(Base):
__tablename__ = "measure_location"
id = Column(Integer, primary_key=True)
connection_node_id = Column(Integer)
Expand All @@ -46,7 +46,7 @@ class ControlMeasureLocation(Base):
tags = Column(CSVText)


class ControlMeasureMap(Base):
class MeasureMap(Base):
__tablename__ = "measure_map"
id = Column(Integer, primary_key=True)
measure_location_id = Column(Integer)
Expand All @@ -59,12 +59,12 @@ class ControlMeasureMap(Base):
tags = Column(CSVText)


class ControlMemory(Base):
class MemoryControl(Base):
__tablename__ = "memory_control"
id = Column(Integer, primary_key=True)
upper_threshold = Column(Float)
lower_threshold = Column(Float)
action_type = Column(VarcharEnum(constants.ControlTableActionTypes))
action_type = Column(VarcharEnum(constants.TableControlActionTypes))
action_value_1 = Column(Float)
action_value_2 = Column(Float)
target_type = Column(VarcharEnum(constants.StructureControlTypes))
Expand All @@ -77,11 +77,11 @@ class ControlMemory(Base):
tags = Column(CSVText)


class ControlTable(Base):
class TableControl(Base):
__tablename__ = "table_control"
id = Column(Integer, primary_key=True)
action_table = Column(CSVTable)
action_type = Column(VarcharEnum(constants.ControlTableActionTypes))
action_type = Column(VarcharEnum(constants.TableControlActionTypes))
measure_operator = Column(VarcharEnum(constants.MeasureOperators))
target_type = Column(VarcharEnum(constants.StructureControlTypes))
target_id = Column(Integer, nullable=False)
Expand Down Expand Up @@ -120,7 +120,7 @@ def max_infiltration_capacity_file(self):
return self.max_infiltration_volume_file


class SurfaceParameter(Base):
class SurfaceParameters(Base):
__tablename__ = "surface_parameters"
id = Column(Integer, primary_key=True)
outflow_delay = Column(Float, nullable=False)
Expand Down Expand Up @@ -269,7 +269,7 @@ class ConnectionNode(Base):
hydraulic_conductivity_out = Column(Float)


class Lateral1d(Base):
class Lateral1D(Base):
__tablename__ = "lateral_1d"
id = Column(Integer, primary_key=True)
code = Column(Text)
Expand Down Expand Up @@ -317,7 +317,7 @@ class NumericalSettings(Base):
flooding_threshold = Column(Float)


class VegetationDrag(Base):
class VegetationDrag2D(Base):
__tablename__ = "vegetation_drag_2d"
id = Column(Integer, primary_key=True)

Expand Down Expand Up @@ -471,7 +471,7 @@ class Channel(Base):
hydraulic_conductivity_out = Column(Float)


class Windshielding(Base):
class Windshielding1D(Base):
__tablename__ = "windshielding_1d"
id = Column(Integer, primary_key=True)
north = Column(Float)
Expand Down Expand Up @@ -702,10 +702,10 @@ class Material(Base):
BoundaryConditions2D,
Channel,
ConnectionNode,
ControlMeasureLocation,
ControlMeasureMap,
ControlMemory,
ControlTable,
MeasureLocation,
MeasureMap,
MemoryControl,
TableControl,
CrossSectionLocation,
Culvert,
DemAverageArea,
Expand All @@ -719,7 +719,7 @@ class Material(Base):
InitialConditions,
Interception,
Interflow,
Lateral1d,
Lateral1D,
Lateral2D,
Material,
ModelSettings,
Expand All @@ -735,10 +735,10 @@ class Material(Base):
SimulationTemplateSettings,
Surface,
SurfaceMap,
SurfaceParameter,
SurfaceParameters,
Tags,
TimeStepSettings,
VegetationDrag,
VegetationDrag2D,
Weir,
Windshielding,
Windshielding1D,
]
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@
},
}

GEOMETRY_TYPES = {"lateral_2d": "POINT",
"lateral_1d": "POINT",
"boundary_condition_1d": "POINT",
"boundary_condition_2d": "LINESTRING"}


def rename_tables(table_sets: List[Tuple[str, str]]):
# no checks for existence are done, this will fail if a source table doesn't exist
Expand Down Expand Up @@ -166,7 +171,6 @@ def rename_columns(table_name: str, columns: List[Tuple[str, str]]):
columns_dict = dict(columns)

old_columns_list = [entry["name"] for entry in old_columns]

if not all(e in old_columns_list for e in columns_dict):
raise ValueError(f"Cannot rename columns {columns_dict.keys()} in table {table_name}; table does not contain all these columns")
new_columns = deepcopy(old_columns)
Expand All @@ -185,16 +189,16 @@ def rename_columns(table_name: str, columns: List[Tuple[str, str]]):
if entry['name'] in ["geom", "id"]:
entry_string += f" NOT NULL"
new_columns_list_sql_formatted.append(entry_string)

temp_name = f'_temp_225_{uuid.uuid4().hex}'
create_table_query = f"""CREATE TABLE {temp_name} ({', '.join(new_columns_list_sql_formatted)});"""
op.execute(sa.text(create_table_query))
op.execute(sa.text(f"INSERT INTO {temp_name} ({','.join(new_columns_list)}) SELECT {','.join(old_columns_list)} from {table_name};"))
drop_geo_table(op, table_name)
op.execute(sa.text(f"ALTER TABLE {temp_name} RENAME TO {table_name};"))

for entry in new_columns:
if entry["name"] == "geom":
op.execute(sa.text(f"""SELECT RecoverGeometryColumn('{table_name}', '{entry["name"]}', 4326, '{entry["type"]}', 'XY')"""))
if table_name is not GEOMETRY_TYPES:
op.execute(sa.text(f"""SELECT RecoverGeometryColumn('{table_name}', 'geom', 4326, '{GEOMETRY_TYPES[table_name]}', 'XY')"""))



Expand Down
25 changes: 20 additions & 5 deletions threedi_schema/migrations/versions/0230_reproject_geometries.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,27 @@ def get_model_srid() -> int:


def get_geom_type(table_name, geo_col_name):
# map geometry numbers to names
geom_type_map = {
1: 'POINT',
2: 'LINESTRING',
3: 'POLYGON',
4: 'MULTIPOINT',
5: 'MULTILINESTRING',
6: 'MULTIPOLYGON',
7: 'GEOMETRYCOLLECTION',
}
connection = op.get_bind()
columns = connection.execute(sa.text(f"PRAGMA table_info('{table_name}')")).fetchall()
for col in columns:
if col[1] == geo_col_name:
return col[2]

# use metadata to determine spatialite version because the geometry type column differs
srs_wkt_exists = connection.execute(sa.text("select count(name) from pragma_table_info('spatial_ref_sys') where name is 'srs_wkt'")).scalar() == 1
# spatialite 3
if srs_wkt_exists:
return connection.execute(
sa.text(f"SELECT type from geometry_columns where f_table_name='{table_name}'")).fetchone()[0]
else:
geom_type_num = connection.execute(
sa.text(f"SELECT geometry_type from geometry_columns where f_table_name='{table_name}'")).fetchone()[0]
return geom_type_map.get(geom_type_num, 'GEOMETRY')

def add_geometry_column(table: str, name: str, srid: int, geometry_type: str):
# Adding geometry columns via alembic doesn't work
Expand Down

0 comments on commit d00a6c1

Please sign in to comment.