From 9d146ec8b3cf47b0901bf327231b7c4a8d6e0c06 Mon Sep 17 00:00:00 2001 From: olivierdalang Date: Tue, 25 Feb 2020 14:04:49 +0100 Subject: [PATCH] improve labelling as per https://github.com/QGEP/QGEP/issues/524 --- 05_data_model_extensions.sql | 13 +++++ 06_symbology_functions.sql | 43 +++++++------- delta/delta_1.5.0_labelling.sql | 87 ++++++++++++++++++++++++++++ view/vw_qgep_wastewater_structure.py | 10 +++- 4 files changed, 129 insertions(+), 24 deletions(-) create mode 100644 delta/delta_1.5.0_labelling.sql diff --git a/05_data_model_extensions.sql b/05_data_model_extensions.sql index 9196ceec..63ae7a70 100644 --- a/05_data_model_extensions.sql +++ b/05_data_model_extensions.sql @@ -20,6 +20,19 @@ added solely for QGEP'; ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _label text; COMMENT ON COLUMN qgep_od.wastewater_structure._label IS 'not part of the VSA-DSS data model added solely for QGEP'; +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _cover_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._cover_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _input_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._input_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _output_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._output_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _bottom_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._bottom_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; + -- this column is an extension to the VSA data model and puts the _function_hierarchic in order ALTER TABLE qgep_vl.channel_function_hierarchic ADD COLUMN order_fct_hierarchic smallint; diff --git a/06_symbology_functions.sql b/06_symbology_functions.sql index 78d2fbf1..2e463dda 100644 --- a/06_symbology_functions.sql +++ b/06_symbology_functions.sql @@ -191,6 +191,7 @@ VOLATILE; ------ WHERE (_all OR NE.fk_wastewater_structure = _obj_id) and CH_to.function_hierarchic in (5062,5064,5066,5068,5069,5070,5071,5072,5074) ----label only reaches with function_hierarchic=pwwf.* + CREATE OR REPLACE FUNCTION qgep_od.update_wastewater_structure_label(_obj_id text, _all boolean default false) RETURNS VOID AS $BODY$ @@ -199,47 +200,47 @@ CREATE OR REPLACE FUNCTION qgep_od.update_wastewater_structure_label(_obj_id tex BEGIN UPDATE qgep_od.wastewater_structure ws -SET _label = label +SET _label = label, + _cover_label = cover_label, + _bottom_label = bottom_label, + _input_label = input_label, + _output_label = output_label FROM ( - SELECT ws_obj_id, - COALESCE(ws_identifier, '') || E'\n' || - array_to_string( - array_append(array_agg('C' || '=' || co_level::text ORDER BY co_level DESC), ''), - E'\n' - ) || - array_to_string( - array_append(array_agg(lbl_type || idx || '=' || rp_level ORDER BY lbl_type, idx), '') - , E'\n' - ) || - array_to_string( - array_append(array_agg('B' || '=' || bottom_level), '') - , E'\n' - ) - AS label + SELECT ws_obj_id, + COALESCE(ws_identifier, '') as label, + array_to_string(array_agg(E'\nC' || '=' || co_level::text ORDER BY co_level DESC), '', '') as cover_label, + array_to_string(array_agg(E'\nB' || '=' || bottom_level::text), '', '') as bottom_label, + array_to_string(array_agg(E'\nI' || idx || '=' || rpi_level::text ORDER BY idx), '', '') as input_label, + array_to_string(array_agg(E'\nO' || idx || '=' || rpo_level::text ORDER BY idx), '', '') as output_label FROM ( - SELECT ws.obj_id AS ws_obj_id, ws.identifier AS ws_identifier, parts.lbl_type, round(parts.co_level, 2) AS co_level, round(parts.rp_level, 2) AS rp_level, parts.obj_id, idx, round(bottom_level, 2) AS bottom_level + SELECT ws.obj_id AS ws_obj_id, ws.identifier AS ws_identifier, round(parts.co_level, 2) AS co_level, round(parts.rpi_level, 2) AS rpi_level, round(parts.rpo_level, 2) AS rpo_level, parts.obj_id, idx, round(bottom_level, 2) AS bottom_level FROM qgep_od.wastewater_structure WS LEFT JOIN ( - SELECT 'C' as lbl_type, CO.level AS co_level, NULL AS rp_level, SP.fk_wastewater_structure ws, SP.obj_id, row_number() OVER(PARTITION BY SP.fk_wastewater_structure) AS idx, NULL::numeric(5,3) AS bottom_level + -- Cover + SELECT CO.level AS co_level, NULL::numeric(5,3) AS rpi_level, NULL::numeric(5,3) AS rpo_level, SP.fk_wastewater_structure ws, SP.obj_id, row_number() OVER(PARTITION BY SP.fk_wastewater_structure) AS idx, NULL::numeric(5,3) AS bottom_level FROM qgep_od.structure_part SP RIGHT JOIN qgep_od.cover CO ON CO.obj_id = SP.obj_id WHERE _all OR SP.fk_wastewater_structure = _obj_id + -- Inputs UNION - SELECT 'I' as lbl_type, NULL, RP.level AS rp_level, NE.fk_wastewater_structure ws, RP.obj_id, row_number() OVER(PARTITION BY RP.fk_wastewater_networkelement ORDER BY ST_Azimuth(RP.situation_geometry,ST_LineInterpolatePoint(ST_CurveToLine(RE_to.progression_geometry),0.99))/pi()*180 ASC), NULL::numeric(5,3) AS bottom_level + SELECT NULL AS co_level, RP.level AS rpi_level, NULL::numeric(5,3) AS rpo_level, NE.fk_wastewater_structure ws, RP.obj_id, row_number() OVER(PARTITION BY RP.fk_wastewater_networkelement ORDER BY ST_Azimuth(RP.situation_geometry,ST_LineInterpolatePoint(ST_CurveToLine(RE_to.progression_geometry),0.99))/pi()*180 ASC), NULL::numeric(5,3) AS bottom_level FROM qgep_od.reach_point RP LEFT JOIN qgep_od.wastewater_networkelement NE ON RP.fk_wastewater_networkelement = NE.obj_id INNER JOIN qgep_od.reach RE_to ON RP.obj_id = RE_to.fk_reach_point_to LEFT JOIN qgep_od.wastewater_networkelement NE_to ON NE_to.obj_id = RE_to.obj_id LEFT JOIN qgep_od.channel CH_to ON NE_to.fk_wastewater_structure = CH_to.obj_id WHERE (_all OR NE.fk_wastewater_structure = _obj_id) and CH_to.function_hierarchic in (5062,5064,5066,5068,5069,5070,5071,5072,5074) ----label only reaches with function_hierarchic=pwwf.* + -- Outputs UNION - SELECT 'O' as lbl_type, NULL, RP.level AS rp_level, NE.fk_wastewater_structure ws, RP.obj_id, row_number() OVER(PARTITION BY RP.fk_wastewater_networkelement ORDER BY ST_Azimuth(RP.situation_geometry,ST_LineInterpolatePoint(ST_CurveToLine(RE_from.progression_geometry),0.99))/pi()*180 ASC), NULL::numeric(5,3) AS bottom_level + SELECT NULL AS co_level, NULL::numeric(5,3) AS rpi_level, RP.level AS rpo_level, NE.fk_wastewater_structure ws, RP.obj_id, row_number() OVER(PARTITION BY RP.fk_wastewater_networkelement ORDER BY ST_Azimuth(RP.situation_geometry,ST_LineInterpolatePoint(ST_CurveToLine(RE_from.progression_geometry),0.99))/pi()*180 ASC), NULL::numeric(5,3) AS bottom_level FROM qgep_od.reach_point RP LEFT JOIN qgep_od.wastewater_networkelement NE ON RP.fk_wastewater_networkelement = NE.obj_id INNER JOIN qgep_od.reach RE_from ON RP.obj_id = RE_from.fk_reach_point_from WHERE CASE WHEN _obj_id IS NULL THEN TRUE ELSE NE.fk_wastewater_structure = _obj_id END - UNION SELECT 'B' as lbl_type, NULL, NULL AS rp_level, ws1.obj_id ws, NULL, NULL, wn.bottom_level AS wn_bottom_level + -- Bottom + UNION + SELECT NULL AS co_level, NULL::numeric(5,3) AS rpi_level, NULL::numeric(5,3) AS rpo_level, ws1.obj_id ws, NULL, NULL, wn.bottom_level AS wn_bottom_level FROM qgep_od.wastewater_structure ws1 LEFT JOIN qgep_od.wastewater_node wn ON wn.obj_id = ws1.fk_main_wastewater_node WHERE _all OR ws1.obj_id = _obj_id diff --git a/delta/delta_1.5.0_labelling.sql b/delta/delta_1.5.0_labelling.sql new file mode 100644 index 00000000..de38ee82 --- /dev/null +++ b/delta/delta_1.5.0_labelling.sql @@ -0,0 +1,87 @@ + +-- TABLE wastewater_structure + +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _cover_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._cover_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _input_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._input_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _output_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._output_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; +ALTER TABLE qgep_od.wastewater_structure ADD COLUMN _bottom_label text; +COMMENT ON COLUMN qgep_od.wastewater_structure._bottom_label IS 'not part of the VSA-DSS data model +added solely for QGEP'; + + +-------------------------------------------------------- +-- UPDATE wastewater structure label +-- Argument: +-- * obj_id of wastewater structure or NULL to update all +-------------------------------------------------------- + +CREATE OR REPLACE FUNCTION qgep_od.update_wastewater_structure_label(_obj_id text, _all boolean default false) + RETURNS VOID AS + $BODY$ + DECLARE + myrec record; + +BEGIN +UPDATE qgep_od.wastewater_structure ws +SET _label = label, + _cover_label = cover_label, + _bottom_label = bottom_label, + _input_label = input_label, + _output_label = output_label +FROM ( + SELECT ws_obj_id, + COALESCE(ws_identifier, '') as label, + array_to_string(array_agg(E'\nC' || '=' || co_level::text ORDER BY co_level DESC), '', '') as cover_label, + array_to_string(array_agg(E'\nB' || '=' || bottom_level::text), '', '') as bottom_label, + array_to_string(array_agg(E'\nI' || idx || '=' || rpi_level::text ORDER BY idx), '', '') as input_label, + array_to_string(array_agg(E'\nO' || idx || '=' || rpo_level::text ORDER BY idx), '', '') as output_label + FROM ( + SELECT ws.obj_id AS ws_obj_id, ws.identifier AS ws_identifier, round(parts.co_level, 2) AS co_level, round(parts.rpi_level, 2) AS rpi_level, round(parts.rpo_level, 2) AS rpo_level, parts.obj_id, idx, round(bottom_level, 2) AS bottom_level + FROM qgep_od.wastewater_structure WS + + LEFT JOIN ( + -- Cover + SELECT CO.level AS co_level, NULL::numeric(5,3) AS rpi_level, NULL::numeric(5,3) AS rpo_level, SP.fk_wastewater_structure ws, SP.obj_id, row_number() OVER(PARTITION BY SP.fk_wastewater_structure) AS idx, NULL::numeric(5,3) AS bottom_level + FROM qgep_od.structure_part SP + RIGHT JOIN qgep_od.cover CO ON CO.obj_id = SP.obj_id + WHERE _all OR SP.fk_wastewater_structure = _obj_id + -- Inputs + UNION + SELECT NULL AS co_level, RP.level AS rpi_level, NULL::numeric(5,3) AS rpo_level, NE.fk_wastewater_structure ws, RP.obj_id, row_number() OVER(PARTITION BY RP.fk_wastewater_networkelement ORDER BY ST_Azimuth(RP.situation_geometry,ST_LineInterpolatePoint(ST_CurveToLine(RE_to.progression_geometry),0.99))/pi()*180 ASC), NULL::numeric(5,3) AS bottom_level + FROM qgep_od.reach_point RP + LEFT JOIN qgep_od.wastewater_networkelement NE ON RP.fk_wastewater_networkelement = NE.obj_id + INNER JOIN qgep_od.reach RE_to ON RP.obj_id = RE_to.fk_reach_point_to + LEFT JOIN qgep_od.wastewater_networkelement NE_to ON NE_to.obj_id = RE_to.obj_id + LEFT JOIN qgep_od.channel CH_to ON NE_to.fk_wastewater_structure = CH_to.obj_id + WHERE (_all OR NE.fk_wastewater_structure = _obj_id) and CH_to.function_hierarchic in (5062,5064,5066,5068,5069,5070,5071,5072,5074) ----label only reaches with function_hierarchic=pwwf.* + -- Outputs + UNION + SELECT NULL AS co_level, NULL::numeric(5,3) AS rpi_level, RP.level AS rpo_level, NE.fk_wastewater_structure ws, RP.obj_id, row_number() OVER(PARTITION BY RP.fk_wastewater_networkelement ORDER BY ST_Azimuth(RP.situation_geometry,ST_LineInterpolatePoint(ST_CurveToLine(RE_from.progression_geometry),0.99))/pi()*180 ASC), NULL::numeric(5,3) AS bottom_level + FROM qgep_od.reach_point RP + LEFT JOIN qgep_od.wastewater_networkelement NE ON RP.fk_wastewater_networkelement = NE.obj_id + INNER JOIN qgep_od.reach RE_from ON RP.obj_id = RE_from.fk_reach_point_from + WHERE CASE WHEN _obj_id IS NULL THEN TRUE ELSE NE.fk_wastewater_structure = _obj_id END + -- Bottom + UNION + SELECT NULL AS co_level, NULL::numeric(5,3) AS rpi_level, NULL::numeric(5,3) AS rpo_level, ws1.obj_id ws, NULL, NULL, wn.bottom_level AS wn_bottom_level + FROM qgep_od.wastewater_structure ws1 + LEFT JOIN qgep_od.wastewater_node wn ON wn.obj_id = ws1.fk_main_wastewater_node + WHERE _all OR ws1.obj_id = _obj_id + ) AS parts ON ws = ws.obj_id + WHERE _all OR ws.obj_id = _obj_id + ) parts + GROUP BY ws_obj_id, COALESCE(ws_identifier, '') +) labeled_ws +WHERE ws.obj_id = labeled_ws.ws_obj_id; + +END + +$BODY$ +LANGUAGE plpgsql +VOLATILE; \ No newline at end of file diff --git a/view/vw_qgep_wastewater_structure.py b/view/vw_qgep_wastewater_structure.py index 11bb9602..6f6fbdff 100755 --- a/view/vw_qgep_wastewater_structure.py +++ b/view/vw_qgep_wastewater_structure.py @@ -71,6 +71,10 @@ def vw_qgep_wastewater_structure(srid: int, {ne_cols}, ws._label, + ws._cover_label, + ws._bottom_label, + ws._input_label, + ws._output_label, ws._usage_current AS _channel_usage_current, ws._function_hierarchic AS _channel_function_hierarchic @@ -103,7 +107,7 @@ def vw_qgep_wastewater_structure(srid: int, table_alias='ws', remove_pkey=False, indent=4, - skip_columns=['identifier', 'fk_owner', 'status', '_label', '_usage_current', + skip_columns=['identifier', 'fk_owner', 'status', '_label', '_cover_label', '_bottom_label', '_input_label', '_output_label', '_usage_current', '_function_hierarchic', 'fk_main_cover', 'fk_main_wastewater_node', 'detail_geometry_geometry']), main_co_cols=select_columns(pg_cur=cursor, table_schema='qgep_od', @@ -234,7 +238,7 @@ def vw_qgep_wastewater_structure(srid: int, table_alias='ws', remove_pkey=False, indent=2, - skip_columns=['_label', '_usage_current', '_function_hierarchic', + skip_columns=['_label', '_cover_label', '_bottom_label', '_input_label', '_output_label', '_usage_current', '_function_hierarchic', 'fk_main_cover', 'fk_main_wastewater_node', 'detail_geometry_geometry']), insert_ma=insert_command(pg_cur=cursor, table_schema='qgep_od', @@ -454,7 +458,7 @@ def vw_qgep_wastewater_structure(srid: int, remove_pkey=False, indent=6, skip_columns=['detail_geometry_geometry', 'last_modification', - '_usage_current', '_function_hierarchic', '_label', + '_usage_current', '_function_hierarchic', '_label', '_cover_label', '_bottom_label', '_input_label', '_output_label', 'fk_main_cover', 'fk_main_wastewater_node', '_depth'], update_values={}), update_ma=update_command(pg_cur=cursor,