From 38a9bf4dcbddaa88dfcbca38fcec80e877f5f2c5 Mon Sep 17 00:00:00 2001 From: Margriet Palm Date: Mon, 9 Dec 2024 17:34:55 +0100 Subject: [PATCH 1/5] Rename usage of geom back to the_geom so this rename is only reflected in the interface --- threedigrid_builder/grid/boundary_conditions.py | 4 ++-- threedigrid_builder/grid/zero_d.py | 4 ++-- threedigrid_builder/interface/db.py | 7 ++++--- threedigrid_builder/tests/test_boundary_conditions.py | 8 ++++---- threedigrid_builder/tests/test_zero_d.py | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/threedigrid_builder/grid/boundary_conditions.py b/threedigrid_builder/grid/boundary_conditions.py index 633fec0d..55f50762 100644 --- a/threedigrid_builder/grid/boundary_conditions.py +++ b/threedigrid_builder/grid/boundary_conditions.py @@ -98,14 +98,14 @@ def apply(self, grid): class BoundaryCondition2D: id: int type: BoundaryType - geom: shapely.Geometry + the_geom: shapely.Geometry class BoundaryConditions2D(Array[BoundaryCondition2D]): def get_intersecting_node_idx( self, idx: int, cell_tree: shapely.STRtree ) -> np.ndarray: - bc_geom = self.geom[idx] + bc_geom = self.the_geom[idx] x1, y1, x2, y2 = shapely.bounds(bc_geom) is_horizontal = (x2 - x1) > (y2 - y1) diff --git a/threedigrid_builder/grid/zero_d.py b/threedigrid_builder/grid/zero_d.py index a4cbbdb8..d82dbbdd 100644 --- a/threedigrid_builder/grid/zero_d.py +++ b/threedigrid_builder/grid/zero_d.py @@ -208,7 +208,7 @@ class BaseSurface: code: str display_name: str area: float - geom: shapely.Geometry + the_geom: shapely.Geometry connection_node_id: int connection_node_the_geom: shapely.Geometry @@ -267,7 +267,7 @@ def as_grid_surfaces( if extra_fields is None: extra_fields = {} - centroids = shapely.centroid(self.geom) + centroids = shapely.centroid(self.the_geom) no_centroid_mask = shapely.is_missing(centroids) if np.any(no_centroid_mask): diff --git a/threedigrid_builder/interface/db.py b/threedigrid_builder/interface/db.py index d68c3c3f..17fc74ed 100644 --- a/threedigrid_builder/interface/db.py +++ b/threedigrid_builder/interface/db.py @@ -383,10 +383,10 @@ def get_surfaces(self) -> Surfaces: # reproject arr["geom"] = self.reproject(arr["geom"]) - + attr_dict = arr_to_attr_dict(arr,{"geom": "the_geom"}) return Surfaces( id=np.arange(0, len(arr["surface_id"] + 1), dtype=int), - **{name: arr[name] for name in arr.dtype.names}, + **attr_dict, ) def get_boundary_conditions_1d(self) -> BoundaryConditions1D: @@ -418,9 +418,10 @@ def get_boundary_conditions_2d(self) -> BoundaryConditions2D: .as_structarray() ) arr["geom"] = self.reproject(arr["geom"]) + attr_dict = arr_to_attr_dict(arr,{"geom": "the_geom"}) # transform to a BoundaryConditions1D object - return BoundaryConditions2D(**{name: arr[name] for name in arr.dtype.names}) + return BoundaryConditions2D(**attr_dict) def get_channels(self) -> Channels: """Return Channels""" diff --git a/threedigrid_builder/tests/test_boundary_conditions.py b/threedigrid_builder/tests/test_boundary_conditions.py index f7a1683f..075b3cf6 100644 --- a/threedigrid_builder/tests/test_boundary_conditions.py +++ b/threedigrid_builder/tests/test_boundary_conditions.py @@ -279,7 +279,7 @@ def test_2d_boundary_condition( boundary_conditions_2d = BoundaryConditions2D( id=range(len(bc_coords)), type=[3] * len(bc_coords), - geom=shapely.linestrings(bc_coords), + the_geom=shapely.linestrings(bc_coords), ) nodes, lines = boundary_conditions_2d.get_nodes_and_lines( @@ -320,7 +320,7 @@ def test_2d_boundary_condition_err(grid2d, bc_coords, expected_message): boundary_conditions_2d = BoundaryConditions2D( id=range(len(bc_coords)), type=[3] * len(bc_coords), - geom=shapely.linestrings(bc_coords), + the_geom=shapely.linestrings(bc_coords), ) with pytest.raises(SchematisationError, match=expected_message): @@ -384,7 +384,7 @@ def test_2d_boundary_condition_types(grid2d_gw, boundary_type, node_type, kcu, l boundary_conditions_2d = BoundaryConditions2D( id=[1], type=[boundary_type], - geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])], + the_geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])], ) nodes, lines = boundary_conditions_2d.get_nodes_and_lines( grid2d_gw.nodes, @@ -412,7 +412,7 @@ def test_2d_boundary_condition_combined(grid2d_gw): boundary_conditions_2d = BoundaryConditions2D( id=[1, 2], type=[BoundaryType.WATERLEVEL, BoundaryType.GROUNDWATERLEVEL], - geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])] * 2, + the_geom=[shapely.linestrings([(12.0, 3.0), (12.0, 8.0)])] * 2, ) nodes, lines = boundary_conditions_2d.get_nodes_and_lines( diff --git a/threedigrid_builder/tests/test_zero_d.py b/threedigrid_builder/tests/test_zero_d.py index 86405a62..4eb282e6 100644 --- a/threedigrid_builder/tests/test_zero_d.py +++ b/threedigrid_builder/tests/test_zero_d.py @@ -57,7 +57,7 @@ def test_surfaces(grid_all): # nr_of_inhabitants=[1000.0, 2000.0, 2000.0], area=[100.0, 200.0, 200.0], # dry_weather_flow=[1.0, 2.0, 2.0], - geom=[shapely.points([1.0, 2.0]), None, None], + the_geom=[shapely.points([1.0, 2.0]), None, None], connection_node_id=[1, 2, 1], connection_node_the_geom=[ shapely.points([1.0, 2.0]), From e241e8978bea4cc418a2a43cdaab76c46b6bfb2c Mon Sep 17 00:00:00 2001 From: Margriet Palm Date: Mon, 9 Dec 2024 17:45:47 +0100 Subject: [PATCH 2/5] Rename the_geom to geom upon loading from sqlite instead of later and some cleanup --- threedigrid_builder/interface/db.py | 73 ++++++++++++----------------- 1 file changed, 30 insertions(+), 43 deletions(-) diff --git a/threedigrid_builder/interface/db.py b/threedigrid_builder/interface/db.py index 17fc74ed..f6598b6d 100644 --- a/threedigrid_builder/interface/db.py +++ b/threedigrid_builder/interface/db.py @@ -356,7 +356,7 @@ def get_surfaces(self) -> Surfaces: models.Surface.code, models.Surface.display_name, models.Surface.area, - models.Surface.geom, + models.Surface.geom.label("the_geom"), models.SurfaceParameter.outflow_delay, models.SurfaceParameter.surface_layer_thickness, models.SurfaceParameter.infiltration, @@ -382,11 +382,10 @@ def get_surfaces(self) -> Surfaces: ) # reproject - arr["geom"] = self.reproject(arr["geom"]) - attr_dict = arr_to_attr_dict(arr,{"geom": "the_geom"}) + arr["the_geom"] = self.reproject(arr["the_geom"]) return Surfaces( id=np.arange(0, len(arr["surface_id"] + 1), dtype=int), - **attr_dict, + **{name: arr[name] for name in arr.dtype.names} ) def get_boundary_conditions_1d(self) -> BoundaryConditions1D: @@ -412,21 +411,20 @@ def get_boundary_conditions_2d(self) -> BoundaryConditions2D: session.query( models.BoundaryConditions2D.id, models.BoundaryConditions2D.type, - models.BoundaryConditions2D.geom, + models.BoundaryConditions2D.geom.label("the_geom") ) .order_by(models.BoundaryConditions2D.id) .as_structarray() ) - arr["geom"] = self.reproject(arr["geom"]) - attr_dict = arr_to_attr_dict(arr,{"geom": "the_geom"}) + arr["the_geom"] = self.reproject(arr["the_geom"]) # transform to a BoundaryConditions1D object - return BoundaryConditions2D(**attr_dict) + return BoundaryConditions2D(**{name: arr[name] for name in arr.dtype.names}) def get_channels(self) -> Channels: """Return Channels""" cols = [ - models.Channel.geom, + models.Channel.geom.label("the_geom"), models.Channel.calculation_point_distance, models.Channel.id, models.Channel.code, @@ -442,7 +440,7 @@ def get_channels(self) -> Channels: with self.get_session() as session: arr = session.query(*cols).order_by(models.Channel.id).as_structarray() - arr["geom"] = self.reproject(arr["geom"]) + arr["the_geom"] = self.reproject(arr["the_geom"]) # map "old" calculation types (100, 101, 102, 105) to (0, 1, 2, 5) arr["exchange_type"][arr["exchange_type"] >= 100] -= 100 arr["hydraulic_conductivity_out"] /= DAY_IN_SECONDS @@ -451,7 +449,6 @@ def get_channels(self) -> Channels: attr_dict = arr_to_attr_dict( arr, { - "geom": "the_geom", "exchange_type": "calculation_type", "calculation_point_distance": "dist_calc_points", "connection_node_id_start": "connection_node_start_id", @@ -465,7 +462,7 @@ def get_channels(self) -> Channels: def get_connection_nodes(self) -> ConnectionNodes: """Return ConnectionNodes""" cols = [ - models.ConnectionNode.geom, + models.ConnectionNode.geom.label("the_geom"), models.ConnectionNode.id, models.ConnectionNode.code, models.ConnectionNode.storage_area, @@ -486,7 +483,7 @@ def get_connection_nodes(self) -> ConnectionNodes: session.query(*cols).order_by(models.ConnectionNode.id).as_structarray() ) - arr["geom"] = self.reproject(arr["geom"]) + arr["the_geom"] = self.reproject(arr["the_geom"]) # replace -9999.0 with NaN in initial_water_level arr["initial_water_level"][arr["initial_water_level"] == -9999.0] = np.nan @@ -576,7 +573,7 @@ def get_cross_section_locations(self) -> CrossSectionLocations: session.query( models.CrossSectionLocation.id, models.CrossSectionLocation.code, - models.CrossSectionLocation.geom, + models.CrossSectionLocation.geom.label("the_geom"), models.CrossSectionLocation.channel_id, models.CrossSectionLocation.reference_level, models.CrossSectionLocation.bank_level, @@ -590,19 +587,18 @@ def get_cross_section_locations(self) -> CrossSectionLocations: .order_by(models.CrossSectionLocation.id) .as_structarray() ) - arr["geom"] = self.reproject(arr["geom"]) + arr["the_geom"] = self.reproject(arr["the_geom"]) - attr_dict = arr_to_attr_dict(arr, {"geom": "the_geom"}) # transform to a CrossSectionLocations object - return CrossSectionLocations(**attr_dict) + return CrossSectionLocations(**{name: arr[name] for name in arr.dtype.names}) def get_culverts(self) -> Culverts: """Return Culverts""" cols = [ models.Culvert.id, models.Culvert.code, - models.Culvert.geom, + models.Culvert.geom.label("the_geom"), models.Culvert.calculation_point_distance, models.Culvert.connection_node_id_start, models.Culvert.connection_node_id_end, @@ -642,7 +638,7 @@ def get_culverts(self) -> Culverts: .as_structarray() ) - arr["geom"] = self.reproject(arr["geom"]) + arr["the_geom"] = self.reproject(arr["the_geom"]) # map friction_type 4 to friction_type 2 to match crosssectionlocation enum arr["friction_type"][arr["friction_type"] == 4] = 2 @@ -659,7 +655,6 @@ def get_culverts(self) -> Culverts: "calculation_point_distance": "dist_calc_points", "invert_level_start": "invert_level_start_point", "invert_level_end": "invert_level_end_point", - "geom": "the_geom", "connection_node_id_start": "connection_node_start_id", "connection_node_id_end": "connection_node_end_id", }, @@ -674,24 +669,23 @@ def get_exchange_lines(self) -> ExchangeLines: session.query( models.ExchangeLine.id, models.ExchangeLine.channel_id, - models.ExchangeLine.geom, + models.ExchangeLine.geom.label("the_geom"), models.ExchangeLine.exchange_level, ) .order_by(models.ExchangeLine.id) .as_structarray() ) - arr["geom"] = self.reproject(arr["geom"]) - attr_dict = arr_to_attr_dict(arr, {"geom": "the_geom"}) + arr["the_geom"] = self.reproject(arr["the_geom"]) # transform to a Channels object - return ExchangeLines(**attr_dict) + return ExchangeLines(**{name: arr[name] for name in arr.dtype.names}) def get_grid_refinements(self) -> GridRefinements: """Return Gridrefinement and GridRefinementArea concatenated into one array.""" with self.get_session() as session: arr1 = ( session.query( - models.GridRefinementLine.geom, + models.GridRefinementLine.geom.label("the_geom"), models.GridRefinementLine.id, models.GridRefinementLine.code, models.GridRefinementLine.display_name, @@ -706,7 +700,7 @@ def get_grid_refinements(self) -> GridRefinements: ) arr2 = ( session.query( - models.GridRefinementArea.geom, + models.GridRefinementArea.geom.label("the_geom"), models.GridRefinementArea.id, models.GridRefinementArea.code, models.GridRefinementArea.display_name, @@ -722,11 +716,11 @@ def get_grid_refinements(self) -> GridRefinements: arr = np.concatenate((arr1, arr2)) # reproject - arr["geom"] = self.reproject(arr["geom"]) + arr["the_geom"] = self.reproject(arr["the_geom"]) arr["id"] = np.arange(len(arr["grid_level"])) attr_dict = arr_to_attr_dict( - arr, {"geom": "the_geom", "grid_level": "refinement_level"} + arr, {"grid_level": "refinement_level"} ) return GridRefinements(**attr_dict) @@ -736,20 +730,19 @@ def get_dem_average_areas(self) -> DemAverageAreas: arr = ( session.query( models.DemAverageArea.id, - models.DemAverageArea.geom, + models.DemAverageArea.geom.label("the_geom"), ) .order_by(models.DemAverageArea.id) .as_structarray() ) - arr["geom"] = self.reproject(arr["geom"]) - attr_dict = arr_to_attr_dict(arr, {"geom": "the_geom"}) - return DemAverageAreas(**attr_dict) + arr["the_geom"] = self.reproject(arr["the_geom"]) + return DemAverageAreas(**{name: arr[name] for name in arr.dtype.names}) def get_obstacles(self) -> Obstacles: with self.get_session() as session: arr = ( session.query( - models.Obstacle.geom, + models.Obstacle.geom.label("the_geom"), models.Obstacle.id, models.Obstacle.crest_level, models.Obstacle.affects_2d, @@ -761,10 +754,9 @@ def get_obstacles(self) -> Obstacles: ) # reproject - arr["geom"] = self.reproject(arr["geom"]) - attr_dict = arr_to_attr_dict(arr, {"geom": "the_geom"}) + arr["the_geom"] = self.reproject(arr["the_geom"]) # transform to a Channels object - return Obstacles(**attr_dict) + return Obstacles(**{name: arr[name] for name in arr.dtype.names}) def get_orifices(self) -> Orifices: """Return Orifices""" @@ -816,7 +808,6 @@ def get_orifices(self) -> Orifices: "calculation_point_distance": "dist_calc_points", "connection_node_id_start": "connection_node_start_id", "connection_node_id_end": "connection_node_end_id", - "geom": "the_geom", }, ) return Orifices(**attr_dict) @@ -878,7 +869,6 @@ def get_pipes(self) -> Pipes: "invert_level_end": "invert_level_end_point", "connection_node_id_start": "connection_node_start_id", "connection_node_id_end": "connection_node_end_id", - "geom": "the_geom", }, ) @@ -913,7 +903,6 @@ def get_pumps(self) -> Pumps: { "connection_node_id": "connection_node_start_id", "connection_node_id_end": "connection_node_end_id", - "geom": "the_geom", }, ) @@ -966,7 +955,6 @@ def get_weirs(self) -> Weirs: "calculation_point_distance": "dist_calc_points", "connection_node_id_start": "connection_node_start_id", "connection_node_id_end": "connection_node_end_id", - "geom": "the_geom", }, ) return Weirs(**attr_dict) @@ -1001,7 +989,7 @@ def get_potential_breaches(self) -> PotentialBreaches: models.PotentialBreach.id, models.PotentialBreach.code, models.PotentialBreach.display_name, - models.PotentialBreach.geom, + models.PotentialBreach.geom.label("the_geom"), models.PotentialBreach.channel_id, ] @@ -1020,7 +1008,7 @@ def get_potential_breaches(self) -> PotentialBreaches: ) # reproject - arr["geom"] = self.reproject(arr["geom"]) + arr["the_geom"] = self.reproject(arr["the_geom"]) # derive maximum_breach_depth from initial and final exchange level # and overwrite final_exchange_level because adding a field is more work arr["final_exchange_level"] = ( @@ -1029,7 +1017,6 @@ def get_potential_breaches(self) -> PotentialBreaches: attr_dict = arr_to_attr_dict( arr, { - "geom": "the_geom", "initial_exchange_level": "exchange_level", "final_exchange_level": "maximum_breach_depth", }, From 43a27cf251caab25066516803ee3c8218d1e4994 Mon Sep 17 00:00:00 2001 From: Margriet Palm Date: Mon, 9 Dec 2024 17:48:44 +0100 Subject: [PATCH 3/5] Rename the_geom to geom upon loading from sqlite instead of later and some cleanup --- threedigrid_builder/interface/db.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/threedigrid_builder/interface/db.py b/threedigrid_builder/interface/db.py index f6598b6d..0535f738 100644 --- a/threedigrid_builder/interface/db.py +++ b/threedigrid_builder/interface/db.py @@ -385,7 +385,7 @@ def get_surfaces(self) -> Surfaces: arr["the_geom"] = self.reproject(arr["the_geom"]) return Surfaces( id=np.arange(0, len(arr["surface_id"] + 1), dtype=int), - **{name: arr[name] for name in arr.dtype.names} + **{name: arr[name] for name in arr.dtype.names}, ) def get_boundary_conditions_1d(self) -> BoundaryConditions1D: @@ -411,7 +411,7 @@ def get_boundary_conditions_2d(self) -> BoundaryConditions2D: session.query( models.BoundaryConditions2D.id, models.BoundaryConditions2D.type, - models.BoundaryConditions2D.geom.label("the_geom") + models.BoundaryConditions2D.geom.label("the_geom"), ) .order_by(models.BoundaryConditions2D.id) .as_structarray() @@ -493,7 +493,6 @@ def get_connection_nodes(self) -> ConnectionNodes: attr_dict = arr_to_attr_dict( arr, { - "geom": "the_geom", "initial_water_level": "initial_waterlevel", "exchange_type": "calculation_type", "exchange_level": "drain_level", @@ -589,7 +588,6 @@ def get_cross_section_locations(self) -> CrossSectionLocations: ) arr["the_geom"] = self.reproject(arr["the_geom"]) - # transform to a CrossSectionLocations object return CrossSectionLocations(**{name: arr[name] for name in arr.dtype.names}) @@ -719,9 +717,7 @@ def get_grid_refinements(self) -> GridRefinements: arr["the_geom"] = self.reproject(arr["the_geom"]) arr["id"] = np.arange(len(arr["grid_level"])) - attr_dict = arr_to_attr_dict( - arr, {"grid_level": "refinement_level"} - ) + attr_dict = arr_to_attr_dict(arr, {"grid_level": "refinement_level"}) return GridRefinements(**attr_dict) def get_dem_average_areas(self) -> DemAverageAreas: From d9506300e1c621c99576e674d705c1fa723abe30 Mon Sep 17 00:00:00 2001 From: Margriet Palm Date: Mon, 9 Dec 2024 18:04:14 +0100 Subject: [PATCH 4/5] Replace usage of arr_to_attr_dict with label in sql query --- threedigrid_builder/interface/db.py | 205 ++++++++-------------------- 1 file changed, 60 insertions(+), 145 deletions(-) diff --git a/threedigrid_builder/interface/db.py b/threedigrid_builder/interface/db.py index 0535f738..55bd4ac4 100644 --- a/threedigrid_builder/interface/db.py +++ b/threedigrid_builder/interface/db.py @@ -4,7 +4,7 @@ from contextlib import contextmanager from enum import Enum from functools import lru_cache -from typing import Callable, ContextManager, Dict, List, Optional, Union +from typing import Callable, ContextManager, Dict, List, Union import numpy as np import shapely @@ -94,20 +94,6 @@ def _set_initialization_type( dct[type_field] = None -def arr_to_attr_dict( - arr: np.ndarray, rename_dict: Optional[Dict[str, str]] = None -) -> Dict[str, str]: - """ - Convert structured array to dict with optional rename of the keys - """ - if rename_dict is None: - rename_dict = {} - return { - rename_dict[name] if name in rename_dict else name: arr[name] - for name in arr.dtype.names - } - - def map_cross_section_definition( objects: List[Union[CrossSectionLocations, Pipes, Weirs, Orifices, Culverts]], definition_map: Dict[str, Dict[int, int]], @@ -425,12 +411,12 @@ def get_channels(self) -> Channels: """Return Channels""" cols = [ models.Channel.geom.label("the_geom"), - models.Channel.calculation_point_distance, + models.Channel.calculation_point_distance.label("dist_calc_points"), models.Channel.id, models.Channel.code, - models.Channel.connection_node_id_start, - models.Channel.connection_node_id_end, - models.Channel.exchange_type, + models.Channel.connection_node_id_start.label("connection_node_start_id"), + models.Channel.connection_node_id_end.label("connection_node_end_id"), + models.Channel.exchange_type.label("calculation_type"), models.Channel.display_name, models.Channel.exchange_thickness, models.Channel.hydraulic_conductivity_out, @@ -442,22 +428,12 @@ def get_channels(self) -> Channels: arr["the_geom"] = self.reproject(arr["the_geom"]) # map "old" calculation types (100, 101, 102, 105) to (0, 1, 2, 5) - arr["exchange_type"][arr["exchange_type"] >= 100] -= 100 + arr["calculation_type"][arr["calculation_type"] >= 100] -= 100 arr["hydraulic_conductivity_out"] /= DAY_IN_SECONDS arr["hydraulic_conductivity_in"] /= DAY_IN_SECONDS - attr_dict = arr_to_attr_dict( - arr, - { - "exchange_type": "calculation_type", - "calculation_point_distance": "dist_calc_points", - "connection_node_id_start": "connection_node_start_id", - "connection_node_id_end": "connection_node_end_id", - }, - ) - # transform to a Channels object - return Channels(**attr_dict) + return Channels(**{name: arr[name] for name in arr.dtype.names}) def get_connection_nodes(self) -> ConnectionNodes: """Return ConnectionNodes""" @@ -466,12 +442,12 @@ def get_connection_nodes(self) -> ConnectionNodes: models.ConnectionNode.id, models.ConnectionNode.code, models.ConnectionNode.storage_area, - models.ConnectionNode.initial_water_level, - models.ConnectionNode.exchange_type, + models.ConnectionNode.initial_water_level.label("initial_waterlevel"), + models.ConnectionNode.exchange_type.label("calculation_type"), models.ConnectionNode.bottom_level, - models.ConnectionNode.exchange_level, - models.ConnectionNode.visualisation, - models.ConnectionNode.manhole_surface_level, + models.ConnectionNode.exchange_level.label("drain_level"), + models.ConnectionNode.visualisation.label("manhole_indicator"), + models.ConnectionNode.manhole_surface_level.label("surface_level"), models.ConnectionNode.display_name, models.ConnectionNode.exchange_thickness, models.ConnectionNode.hydraulic_conductivity_out, @@ -486,22 +462,11 @@ def get_connection_nodes(self) -> ConnectionNodes: arr["the_geom"] = self.reproject(arr["the_geom"]) # replace -9999.0 with NaN in initial_water_level - arr["initial_water_level"][arr["initial_water_level"] == -9999.0] = np.nan + arr["initial_waterlevel"][arr["initial_waterlevel"] == -9999.0] = np.nan arr["hydraulic_conductivity_out"] /= DAY_IN_SECONDS arr["hydraulic_conductivity_in"] /= DAY_IN_SECONDS - attr_dict = arr_to_attr_dict( - arr, - { - "initial_water_level": "initial_waterlevel", - "exchange_type": "calculation_type", - "exchange_level": "drain_level", - "visualisation": "manhole_indicator", - "manhole_surface_level": "surface_level", - }, - ) - - return ConnectionNodes(**attr_dict) + return ConnectionNodes(**{name: arr[name] for name in arr.dtype.names}) def get_cross_section_definition_for_table(self, table) -> np.ndarray: with self.get_session() as session: @@ -597,12 +562,12 @@ def get_culverts(self) -> Culverts: models.Culvert.id, models.Culvert.code, models.Culvert.geom.label("the_geom"), - models.Culvert.calculation_point_distance, - models.Culvert.connection_node_id_start, - models.Culvert.connection_node_id_end, - models.Culvert.exchange_type, - models.Culvert.invert_level_start, - models.Culvert.invert_level_end, + models.Culvert.calculation_point_distance.label("dist_calc_points"), + models.Culvert.connection_node_id_start.label("connection_node_start_id"), + models.Culvert.connection_node_id_end.label("connection_node_end_id"), + models.Culvert.exchange_type.label("calculation_type"), + models.Culvert.invert_level_start.label("invert_level_start_point"), + models.Culvert.invert_level_end.label("invert_level_end_point"), models.Culvert.discharge_coefficient_negative, models.Culvert.discharge_coefficient_positive, models.Culvert.display_name, @@ -642,24 +607,14 @@ def get_culverts(self) -> Culverts: arr["friction_type"][arr["friction_type"] == 4] = 2 # When no calculation type is provides we default to isolated - arr["exchange_type"][arr["exchange_type"] == -9999] = LineType.LINE_1D_ISOLATED + arr["calculation_type"][ + arr["calculation_type"] == -9999 + ] = LineType.LINE_1D_ISOLATED # map "old" calculation types (100, 101, 102, 105) to (0, 1, 2, 5) - arr["exchange_type"][arr["exchange_type"] >= 100] -= 100 - - attr_dict = arr_to_attr_dict( - arr, - { - "exchange_type": "calculation_type", - "calculation_point_distance": "dist_calc_points", - "invert_level_start": "invert_level_start_point", - "invert_level_end": "invert_level_end_point", - "connection_node_id_start": "connection_node_start_id", - "connection_node_id_end": "connection_node_end_id", - }, - ) + arr["calculation_type"][arr["calculation_type"] >= 100] -= 100 - # transform to a CrossSectionLocations object - return Culverts(**attr_dict) + # transform to a Culverts object + return Culverts(**{name: arr[name] for name in arr.dtype.names}) def get_exchange_lines(self) -> ExchangeLines: with self.get_session() as session: @@ -687,7 +642,7 @@ def get_grid_refinements(self) -> GridRefinements: models.GridRefinementLine.id, models.GridRefinementLine.code, models.GridRefinementLine.display_name, - models.GridRefinementLine.grid_level, + models.GridRefinementLine.grid_level.label("refinement_level"), ) .filter( models.GridRefinementLine.geom.isnot(None), @@ -702,7 +657,7 @@ def get_grid_refinements(self) -> GridRefinements: models.GridRefinementArea.id, models.GridRefinementArea.code, models.GridRefinementArea.display_name, - models.GridRefinementArea.grid_level, + models.GridRefinementArea.grid_level.label("refinement_level"), ) .filter( models.GridRefinementArea.geom.isnot(None), @@ -715,10 +670,9 @@ def get_grid_refinements(self) -> GridRefinements: # reproject arr["the_geom"] = self.reproject(arr["the_geom"]) - arr["id"] = np.arange(len(arr["grid_level"])) + arr["id"] = np.arange(len(arr["refinement_level"])) - attr_dict = arr_to_attr_dict(arr, {"grid_level": "refinement_level"}) - return GridRefinements(**attr_dict) + return GridRefinements(**{name: arr[name] for name in arr.dtype.names}) def get_dem_average_areas(self) -> DemAverageAreas: """Return DemAverageAreas""" @@ -751,7 +705,7 @@ def get_obstacles(self) -> Obstacles: # reproject arr["the_geom"] = self.reproject(arr["the_geom"]) - # transform to a Channels object + # transform to a Obstacles object return Obstacles(**{name: arr[name] for name in arr.dtype.names}) def get_orifices(self) -> Orifices: @@ -759,8 +713,8 @@ def get_orifices(self) -> Orifices: cols = [ models.Orifice.id, models.Orifice.code, - models.Orifice.connection_node_id_start, - models.Orifice.connection_node_id_end, + models.Orifice.connection_node_id_start.label("connection_node_start_id"), + models.Orifice.connection_node_id_end.label("connection_node_end_id"), models.Orifice.crest_level, models.Orifice.crest_type, models.Orifice.discharge_coefficient_negative, @@ -797,16 +751,8 @@ def get_orifices(self) -> Orifices: ) # map friction_type 4 to friction_type 2 to match crosssectionlocation enum arr["friction_type"][arr["friction_type"] == 4] = 2 - attr_dict = arr_to_attr_dict( - arr, - { - "exchange_type": "calculation_type", - "calculation_point_distance": "dist_calc_points", - "connection_node_id_start": "connection_node_start_id", - "connection_node_id_end": "connection_node_end_id", - }, - ) - return Orifices(**attr_dict) + + return Orifices(**{name: arr[name] for name in arr.dtype.names}) def get_pipes(self) -> Pipes: """Return Pipes""" @@ -814,17 +760,17 @@ def get_pipes(self) -> Pipes: models.Pipe.id, models.Pipe.code, models.Pipe.sewerage_type, - models.Pipe.exchange_type, - models.Pipe.invert_level_start, - models.Pipe.invert_level_end, - models.Pipe.calculation_point_distance, - models.Pipe.connection_node_id_start, - models.Pipe.connection_node_id_end, + models.Pipe.exchange_type.label("calculation_type"), + models.Pipe.invert_level_start.label("invert_level_start_point"), + models.Pipe.invert_level_end.label("invert_level_end_point"), + models.Pipe.calculation_point_distance.label("dist_calc_points"), + models.Pipe.connection_node_id_start.label("connection_node_start_id"), + models.Pipe.connection_node_id_end.label("connection_node_end_id"), models.Pipe.display_name, models.Pipe.exchange_thickness, models.Pipe.hydraulic_conductivity_out, models.Pipe.hydraulic_conductivity_in, - models.Pipe.material_id, + models.Pipe.material_id.label("material"), case( { models.Pipe.friction_value.isnot(None) @@ -855,21 +801,8 @@ def get_pipes(self) -> Pipes: arr["hydraulic_conductivity_out"] /= DAY_IN_SECONDS arr["hydraulic_conductivity_in"] /= DAY_IN_SECONDS - attr_dict = arr_to_attr_dict( - arr, - { - "exchange_type": "calculation_type", - "calculation_point_distance": "dist_calc_points", - "material_id": "material", - "invert_level_start": "invert_level_start_point", - "invert_level_end": "invert_level_end_point", - "connection_node_id_start": "connection_node_start_id", - "connection_node_id_end": "connection_node_end_id", - }, - ) - # transform to a Pipes object - return Pipes(**attr_dict) + return Pipes(**{name: arr[name] for name in arr.dtype.names}) def get_pumps(self) -> Pumps: with self.get_session() as session: @@ -878,8 +811,10 @@ def get_pumps(self) -> Pumps: models.Pump.id, models.Pump.code, models.Pump.capacity, - models.Pump.connection_node_id, - models.PumpMap.connection_node_id_end, + models.Pump.connection_node_id.label("connection_node_start_id"), + models.PumpMap.connection_node_id_end.label( + "connection_node_end_id" + ), models.Pump.type_, models.Pump.start_level, models.Pump.lower_stop_level, @@ -894,24 +829,16 @@ def get_pumps(self) -> Pumps: # Pump capacity is entered as L/s but we need m3/s. arr["capacity"] = arr["capacity"] / 1000 - attr_dict = arr_to_attr_dict( - arr, - { - "connection_node_id": "connection_node_start_id", - "connection_node_id_end": "connection_node_end_id", - }, - ) - # transform to a Pumps object - return Pumps(**attr_dict) + return Pumps(**{name: arr[name] for name in arr.dtype.names}) def get_weirs(self) -> Weirs: """Return Weirs""" cols = [ models.Weir.id, models.Weir.code, - models.Weir.connection_node_id_start, - models.Weir.connection_node_id_end, + models.Weir.connection_node_id_start.label("connection_node_start_id"), + models.Weir.connection_node_id_end.label("connection_node_end_id"), models.Weir.crest_level, models.Weir.crest_type, models.Weir.discharge_coefficient_negative, @@ -944,16 +871,8 @@ def get_weirs(self) -> Weirs: ) # map friction_type 4 to friction_type 2 to match crosssectionlocation enum arr["friction_type"][arr["friction_type"] == 4] = 2 - attr_dict = arr_to_attr_dict( - arr, - { - "exchange_type": "calculation_type", - "calculation_point_distance": "dist_calc_points", - "connection_node_id_start": "connection_node_start_id", - "connection_node_id_end": "connection_node_end_id", - }, - ) - return Weirs(**attr_dict) + + return Weirs(**{name: arr[name] for name in arr.dtype.names}) def get_windshieldings(self) -> Windshieldings: with self.get_session() as session: @@ -991,8 +910,10 @@ def get_potential_breaches(self) -> PotentialBreaches: if self.get_version() >= 212: cols += [ - models.PotentialBreach.initial_exchange_level, - models.PotentialBreach.final_exchange_level, + models.PotentialBreach.initial_exchange_level.label("exchange_level"), + models.PotentialBreach.final_exchange_level.label( + "maximum_breach_depth" + ), models.PotentialBreach.levee_material, ] @@ -1007,17 +928,11 @@ def get_potential_breaches(self) -> PotentialBreaches: arr["the_geom"] = self.reproject(arr["the_geom"]) # derive maximum_breach_depth from initial and final exchange level # and overwrite final_exchange_level because adding a field is more work - arr["final_exchange_level"] = ( - arr["initial_exchange_level"] - arr["final_exchange_level"] + arr["maximum_breach_depth"] = ( + arr["exchange_level"] - arr["maximum_breach_depth"] ) - attr_dict = arr_to_attr_dict( - arr, - { - "initial_exchange_level": "exchange_level", - "final_exchange_level": "maximum_breach_depth", - }, - ) - return PotentialBreaches(**attr_dict) + + return PotentialBreaches(**{name: arr[name] for name in arr.dtype.names}) # Constructing a Transformer takes quite long, so we use caching here. The From fdbd1e8207ed837895e00c76b348d500833cca66 Mon Sep 17 00:00:00 2001 From: Margriet Palm Date: Tue, 10 Dec 2024 10:30:43 +0100 Subject: [PATCH 5/5] Update changes --- CHANGES.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 26cd8e04..3c153c69 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,7 +5,7 @@ Changelog of threedigrid-builder 1.21.1 (unreleased) ------------------- -- Nothing changed yet. +- Remove internal changes made for previous schema upgrades and limit changes to db interface 1.21.0 (2024-11-25)