Skip to content

Commit 4a4eb7a

Browse files
committed
ensure control_measure_map.geom is a valid line
1 parent e37f006 commit 4a4eb7a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog of threedi-schema
77

88
- Swap start and end of control_measure_map geometries
99
- Modify geometry of controls associated with pumpstation to the pumpstation start node
10+
- Ensure control_measure_map.geom is a valid line
1011

1112

1213
0.224.6 (2024-09-25)

threedi_schema/migrations/versions/0224_upgrade_db_structure_control.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,41 @@ def add_control_geometries(control_name):
196196
op.execute(sa.text(query))
197197

198198

199+
def get_global_srid():
200+
conn = op.get_bind()
201+
use_0d_inflow = conn.execute(sa.text("SELECT use_0d_inflow FROM simulation_template_settings LIMIT 1")).fetchone()
202+
if use_0d_inflow is not None:
203+
srid = conn.execute(sa.text("SELECT epsg_code FROM model_settings LIMIT 1")).fetchone()
204+
if (srid is not None) and (srid[0] is not None):
205+
return srid[0]
206+
return 28992
207+
208+
199209
def set_geom_for_control_measure_map():
210+
srid = get_global_srid()
200211
for control in ['memory', 'table']:
201212
control_table = f'{control}_control'
202213
query = f"""
203214
UPDATE
204215
control_measure_map
205216
SET
206217
geom = (
207-
SELECT
208-
MakeLine(cml.geom, tc.geom)
218+
SELECT CASE
219+
WHEN ST_Equals(cml.geom, tc.geom) THEN
220+
-- Transform to EPSG:4326 for the projection, then back to the original SRID
221+
MakeLine(
222+
cml.geom,
223+
PointOnSurface(ST_Transform(
224+
ST_Translate(
225+
ST_Transform(tc.geom, {srid}),
226+
0, 1, 0
227+
),
228+
4326
229+
))
230+
)
231+
ELSE
232+
MakeLine(cml.geom, tc.geom)
233+
END
209234
FROM
210235
{control_table} AS tc
211236
JOIN

0 commit comments

Comments
 (0)