From 514e5a3298d9d7fde708adbe1de2e3253594edfa Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Mon, 29 Aug 2016 17:28:30 +0200 Subject: [PATCH 1/9] Remove valve from nodes - first release Valve exit node: Tests corrections Added delta for model evolution Valve out of node: scalability tests --- ordinary_data/functions/fn_node_set_type.sql | 2 +- ordinary_data/valve/od_valve.sql | 39 +++++++++++++- ordinary_data/valve/tr_valve_pipe.sql | 2 +- .../views/draw_lines/vw_valve_lines.sql | 2 +- ordinary_data/views/drop_views.sql | 1 - ordinary_data/views/export/export_valve.py | 2 +- ordinary_data/views/export/search_view.sql | 8 +-- .../inheritance/od_element_inheritance.py | 10 ---- tests/test_add_node.sql | 53 ++++++++++++------- tests/test_alternative_geometry.sql | 2 +- tests/test_move_node_end_pipe.expected.sql | 6 +++ tests/test_move_node_end_pipe.sql | 34 ++++++++---- tests/test_node_orientation.sql | 43 ++++++++++++--- tests/test_scalability_multithread.py | 19 ++++--- tests/test_scenarii.expected.sql | 14 ++--- tests/test_scenarii.sql | 27 ++++------ tests/test_scenarii_scalability.model | 4 +- tests/test_scenarii_scalability_init.sql | 19 ++++++- update/delta/delta_0.1.1.sql | 51 ++++++++++++++++++ 19 files changed, 236 insertions(+), 102 deletions(-) create mode 100644 update/delta/delta_0.1.1.sql diff --git a/ordinary_data/functions/fn_node_set_type.sql b/ordinary_data/functions/fn_node_set_type.sql index eefed317..256e79a8 100644 --- a/ordinary_data/functions/fn_node_set_type.sql +++ b/ordinary_data/functions/fn_node_set_type.sql @@ -26,7 +26,7 @@ $BODY$ _lin_ref float; BEGIN - -- get the geoemetry + -- get the geometry _node_geom := geometry FROM qwat_od.node WHERE id = _node_id; -- count the functional pipes associated to this node diff --git a/ordinary_data/valve/od_valve.sql b/ordinary_data/valve/od_valve.sql index 6ebaae31..ccf16971 100644 --- a/ordinary_data/valve/od_valve.sql +++ b/ordinary_data/valve/od_valve.sql @@ -5,11 +5,12 @@ */ /* create */ -CREATE TABLE qwat_od.valve (); +CREATE TABLE qwat_od.valve (id serial PRIMARY KEY); COMMENT ON TABLE qwat_od.valve IS 'Table for valve. Inherits from node.'; /* columns */ -ALTER TABLE qwat_od.valve ADD COLUMN id integer NOT NULL REFERENCES qwat_od.network_element(id) PRIMARY KEY; +ALTER TABLE qwat_od.valve ADD COLUMN fk_district integer; +ALTER TABLE qwat_od.valve ADD COLUMN fk_pressurezone integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_valve_type integer not null; ALTER TABLE qwat_od.valve ADD COLUMN fk_valve_function integer not null; ALTER TABLE qwat_od.valve ADD COLUMN fk_valve_actuation integer not null; @@ -17,12 +18,22 @@ ALTER TABLE qwat_od.valve ADD COLUMN fk_pipe integer ; ALTER TABLE qwat_od.valve ADD COLUMN fk_handle_precision integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_handle_precisionalti integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_maintenance integer[]; --TODO should use n:m relations! +ALTER TABLE qwat_od.valve ADD COLUMN fk_distributor integer not null; +ALTER TABLE qwat_od.valve ADD COLUMN fk_precision integer not null; +ALTER TABLE qwat_od.valve ADD COLUMN fk_precisionalti integer; +ALTER TABLE qwat_od.valve ADD COLUMN fk_status integer not null; +ALTER TABLE qwat_od.valve ADD COLUMN fk_object_reference integer; +ALTER TABLE qwat_od.valve ADD COLUMN fk_folder integer; ALTER TABLE qwat_od.valve ADD COLUMN diameter_nominal varchar(10); ALTER TABLE qwat_od.valve ADD COLUMN closed boolean default false; ALTER TABLE qwat_od.valve ADD COLUMN networkseparation boolean default false; ALTER TABLE qwat_od.valve ADD COLUMN handle_altitude decimal(10,3); ALTER TABLE qwat_od.valve ADD COLUMN handle_geometry geometry(PointZ,:SRID); +ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); +ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; +/* Schema view */ +SELECT qwat_sys.fn_enable_schemaview( 'valve' ); /* constraints */ ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_type FOREIGN KEY (fk_valve_type) REFERENCES qwat_vl.valve_type(id) MATCH FULL; CREATE INDEX fki_valve_fk_type ON qwat_od.valve(fk_valve_type); @@ -31,11 +42,35 @@ ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_valve_actuation FOREIGN KEY (f ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_pipe FOREIGN KEY (fk_pipe) REFERENCES qwat_od.pipe(id) MATCH FULL; CREATE INDEX fki_valve_fk_pipe ON qwat_od.valve(fk_pipe); ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_handle_precision FOREIGN KEY (fk_handle_precision) REFERENCES qwat_vl.precision(id) MATCH FULL; CREATE INDEX fki_valve_fk_handle_precision ON qwat_od.valve(fk_handle_precision); ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_handle_precisionalti FOREIGN KEY (fk_handle_precisionalti) REFERENCES qwat_vl.precisionalti(id) MATCH FULL; CREATE INDEX fki_valve_fk_handle_precisionalti ON qwat_od.valve(fk_handle_precisionalti); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_district FOREIGN KEY (fk_district) REFERENCES qwat_od.district(id) MATCH FULL; CREATE INDEX fki_valve_fk_district ON qwat_od.valve(fk_district); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_pressurezone FOREIGN KEY (fk_pressurezone) REFERENCES qwat_od.pressurezone(id) MATCH FULL; CREATE INDEX fki_valve_fk_pressurezone ON qwat_od.valve(fk_pressurezone); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_distributor FOREIGN KEY (fk_distributor) REFERENCES qwat_od.distributor(id) MATCH FULL; CREATE INDEX fki_valve_fk_distributor ON qwat_od.valve(fk_distributor); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precision FOREIGN KEY (fk_precision) REFERENCES qwat_vl.precision(id) MATCH FULL; CREATE INDEX fki_valve_fk_precision ON qwat_od.valve(fk_precision); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_status FOREIGN KEY (fk_status) REFERENCES qwat_vl.status(id) MATCH FULL; CREATE INDEX fki_valve_fk_status ON qwat_od.valve(fk_status); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_object_reference FOREIGN KEY (fk_object_reference) REFERENCES qwat_vl.object_reference(id) MATCH FULL; CREATE INDEX fki_valve_fk_object_reference ON qwat_od.valve(fk_object_reference); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_folder FOREIGN KEY (fk_folder) REFERENCES qwat_od.folder(id) MATCH FULL; CREATE INDEX fki_valve_fk_folder ON qwat_od.valve(fk_folder); +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precisionalti FOREIGN KEY (fk_precisionalti) REFERENCES qwat_vl.precisionalti(id) MATCH FULL; CREATE INDEX fki_valve_fk_precisionalti ON qwat_od.valve(fk_precisionalti); /* cannot create constraint on arrays yet ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_maintenance FOREIGN KEY (fk_maintenance) REFERENCES qwat_vl.valve_maintenance(id) MATCH FULL; CREATE INDEX fki_valve_fk_maintenance ON qwat_od.valve(fk_maintenance); */ +/* altitude - fk_object_reference, fk_altitude_precision constraints */ +ALTER TABLE qwat_od.valve ADD CONSTRAINT chk_valve_altitude_obj_ref CHECK (fk_object_reference IS NOT NULL OR altitude IS NULL ); +ALTER TABLE qwat_od.valve ADD CONSTRAINT chk_valve_altitude_precisionalti CHECK (fk_precisionalti IS NOT NULL OR altitude IS NULL ); + +/* GEOMETRY */ +ALTER TABLE qwat_od.valve ADD COLUMN geometry geometry('POINTZ',:SRID) NOT NULL; +ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt1 geometry('POINTZ',:SRID); +ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt2 geometry('POINTZ',:SRID); +ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt1 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated +ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated + + +/* GEOM INDEXES */ +CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); +CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); +CREATE INDEX valve_geoidx_alt2 ON qwat_od.valve USING GIST ( geometry_alt2 ); /* NODE TRIGGER */ CREATE OR REPLACE FUNCTION qwat_od.ft_valve_node_set_type() RETURNS TRIGGER AS diff --git a/ordinary_data/valve/tr_valve_pipe.sql b/ordinary_data/valve/tr_valve_pipe.sql index 4cb6c564..c3fa9a68 100644 --- a/ordinary_data/valve/tr_valve_pipe.sql +++ b/ordinary_data/valve/tr_valve_pipe.sql @@ -31,7 +31,7 @@ LANGUAGE plpgsql; CREATE OR REPLACE FUNCTION qwat_od.ft_valve_pipe_update() RETURNS TRIGGER AS $BODY$ BEGIN - UPDATE qwat_od.vw_element_valve valve SET fk_pipe = qwat_od.fn_pipe_get_id(geometry) WHERE fk_pipe = OLD.id OR ST_Distance(geometry, OLD.geometry) < 1e-4; + UPDATE qwat_od.valve SET fk_pipe = qwat_od.fn_pipe_get_id(geometry) WHERE fk_pipe = OLD.id OR ST_Distance(geometry, OLD.geometry) < 1e-4; RETURN NULL; END; $BODY$ diff --git a/ordinary_data/views/draw_lines/vw_valve_lines.sql b/ordinary_data/views/draw_lines/vw_valve_lines.sql index 33769b8c..8c4ea298 100644 --- a/ordinary_data/views/draw_lines/vw_valve_lines.sql +++ b/ordinary_data/views/draw_lines/vw_valve_lines.sql @@ -8,7 +8,7 @@ CREATE OR REPLACE VIEW qwat_od.vw_valve_lines AS SELECT valve.id, ST_MakeLine(ST_Force2D(valve.handle_geometry), valve.geometry)::geometry(LineString, :SRID) AS geometry - FROM qwat_od.vw_element_valve valve + FROM qwat_od.valve WHERE valve.handle_geometry IS NOT NULL AND valve.geometry IS NOT NULL; COMMENT ON VIEW qwat_od.vw_valve_lines IS diff --git a/ordinary_data/views/drop_views.sql b/ordinary_data/views/drop_views.sql index 9f9ef90f..9d6535e4 100644 --- a/ordinary_data/views/drop_views.sql +++ b/ordinary_data/views/drop_views.sql @@ -35,7 +35,6 @@ drop view if exists qwat_od.vw_element_meter; drop view if exists qwat_od.vw_element_part; drop view if exists qwat_od.vw_element_samplingpoint; drop view if exists qwat_od.vw_element_subscriber; -drop view if exists qwat_od.vw_element_valve; drop view if exists qwat_od.vw_installation_chamber; drop view if exists qwat_od.vw_installation_pressurecontrol; diff --git a/ordinary_data/views/export/export_valve.py b/ordinary_data/views/export/export_valve.py index f77419b2..5c7be441 100755 --- a/ordinary_data/views/export/export_valve.py +++ b/ordinary_data/views/export/export_valve.py @@ -13,7 +13,7 @@ name: qwat_od.vw_export_valve -from: qwat_od.vw_element_valve +from: qwat_od.valve joins: district: diff --git a/ordinary_data/views/export/search_view.sql b/ordinary_data/views/export/search_view.sql index d5ab76eb..ea2b7116 100644 --- a/ordinary_data/views/export/search_view.sql +++ b/ordinary_data/views/export/search_view.sql @@ -31,10 +31,4 @@ CREATE OR REPLACE VIEW qwat_od.vw_search_view AS 'Compteur' as layer_name, COALESCE(district_prefix||'_')||meter.identification AS search_text, ST_Force2d(meter.geometry) AS geometry - FROM qwat_od.vw_export_meter meter - - UNION SELECT - 'Vannes' as layer_name, - valve_function_value_fr || ' ' || identification || ' ' || district_name as search_text, - ST_Force2d(geometry) AS geometry - FROM qwat_od.vw_export_valve WHERE identification IS NOT NULL; + FROM qwat_od.vw_export_meter meter; diff --git a/ordinary_data/views/inheritance/od_element_inheritance.py b/ordinary_data/views/inheritance/od_element_inheritance.py index 74b997c2..55f8c77f 100755 --- a/ordinary_data/views/inheritance/od_element_inheritance.py +++ b/ordinary_data/views/inheritance/od_element_inheritance.py @@ -26,13 +26,6 @@ table: qwat_od.vw_qwat_installation pkey: id - valve: - table: qwat_od.valve - pkey: id - alter: - fk_pipe: - write: qwat_od.fn_pipe_get_id(NEW.geometry) - hydrant: table: qwat_od.hydrant pkey: id @@ -66,19 +59,16 @@ subscriber: parcel installation: parcel networkseparation: - valve: networkseparation installation: networkseparation fk_pipe: meter: fk_pipe part: fk_pipe subscriber: fk_pipe - valve: fk_pipe """ print pgiv.PGInheritanceView(pg_service, qwat_elements).sql_all() # print pgiv.PGInheritanceView(pg_service, qwat_elements).sql_join_insert_trigger("installation") -# print pgiv.PGInheritanceView(pg_service, qwat_elements).sql_join_insert_trigger("valve") # print pgiv.PGInheritanceView(pg_service, qwat_elements).sql_join_insert_trigger("hydrant") # print pgiv.PGInheritanceView(pg_service, qwat_elements).sql_join_insert_trigger("part") # print pgiv.PGInheritanceView(pg_service, qwat_elements).sql_join_insert_trigger("meter") diff --git a/tests/test_add_node.sql b/tests/test_add_node.sql index 692c1586..1deac440 100644 --- a/tests/test_add_node.sql +++ b/tests/test_add_node.sql @@ -18,33 +18,48 @@ INSERT INTO qwat_od.pipe (id, fk_node_a, fk_node_b, 101, 101, 101, 1, 101, 101, 101, 101, st_setsrid('linestring(530000 138260 0,530050 138270 0)'::geometry, 21781)); -INSERT INTO qwat_od.vw_element_valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, - fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, - geometry) - VALUES (3, 1, 1, 1, 1, - 101, 101, 101, 101, 6108, 101, - st_setsrid('point(530025 138265 0)'::geometry,21781)); +INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, + underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, + pressure_static, pressure_dynamic, flow, + observation_date, observation_source, + geometry) + VALUES (2016, 1, 101, 101, + True, 9.5, 101, 101, 101, 101, 101, 101, 101, + 12.3, 12.3, 12.3, + '2016/01/01', 'Test', + st_setsrid('POINT(530025 138265 0)'::geometry,21781) + ); -- Now we have 3 vertices on the pipe SELECT 'num_points_after_add1', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; -INSERT INTO qwat_od.vw_element_valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, - fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, - geometry) - VALUES (4, 1, 1, 1, 1, - 101, 101, 101, 101, 6108, 101, - st_setsrid('point(530040 138268 0)'::geometry,21781)); +INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, + underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, + pressure_static, pressure_dynamic, flow, + observation_date, observation_source, + geometry) + VALUES (2016, 1, 101, 101, + True, 9.5, 101, 101, 101, 101, 101, 101, 101, + 12.3, 12.3, 12.3, + '2016/01/01', 'Test', + st_setsrid('POINT(530040 138268 0)'::geometry,21781) + ); -- Now we have 4 vertices on the pipe SELECT 'num_points_after_add2', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; -- What if the intersection test fails but the node is close enough to the pipe ? -INSERT INTO qwat_od.vw_element_valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, - fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, - geometry) - VALUES (5, 1, 1, 1, 1, - 101, 101, 101, 101, 6108, 101, - st_setsrid('point(530025.2 138265.04 0)'::geometry,21781)); +INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, + underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, + pressure_static, pressure_dynamic, flow, + observation_date, observation_source, + geometry) + VALUES (2016, 1, 101, 101, + True, 9.5, 101, 101, 101, 101, 101, 101, 101, + 12.3, 12.3, 12.3, + '2016/01/01', 'Test', + st_setsrid('POINT(530025.2 138265.04 0)'::geometry,21781) + ); -- Another point added SELECT 'num_points_after_add3', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; @@ -83,7 +98,7 @@ INSERT INTO qwat_od.pipe (id, fk_node_a, fk_node_b, UPDATE qwat_od.pipe SET geometry = st_setsrid('linestring(530000 148260 0, 530001 148260 0)'::geometry, 21781) where id=11; -- restore the initial state -DELETE FROM qwat_od.vw_element_valve; +DELETE FROM qwat_od.vw_element_hydrant; DELETE FROM qwat_od.pipe; DELETE FROM qwat_od.pressurezone; DELETE FROM qwat_od.consumptionzone; diff --git a/tests/test_alternative_geometry.sql b/tests/test_alternative_geometry.sql index 6ea85a51..9176db45 100644 --- a/tests/test_alternative_geometry.sql +++ b/tests/test_alternative_geometry.sql @@ -38,7 +38,7 @@ UPDATE qwat_od.pipe SET geometry_alt1 = st_setsrid('linestring(530004 138260 0,5 SELECT 'test5', st_equals(geometry,geometry_alt1) IS FALSE, _geometry_alt1_used IS TRUE, st_equals(geometry,geometry_alt2) IS TRUE, _geometry_alt2_used IS FALSE from qwat_od.pipe; -- restore the initial state -DELETE FROM qwat_od.vw_element_valve; +DELETE FROM qwat_od.valve; DELETE FROM qwat_od.pipe; DELETE FROM qwat_od.pressurezone; DELETE FROM qwat_od.consumptionzone; diff --git a/tests/test_move_node_end_pipe.expected.sql b/tests/test_move_node_end_pipe.expected.sql index 9410de49..945ac4c7 100644 --- a/tests/test_move_node_end_pipe.expected.sql +++ b/tests/test_move_node_end_pipe.expected.sql @@ -5,3 +5,9 @@ INSERT 0 1 INSERT 0 1 INSERT 0 1 UPDATE 1 +DELETE 0 +DELETE 1 +DELETE 1 +DELETE 1 +DELETE 1 +DELETE 1 diff --git a/tests/test_move_node_end_pipe.sql b/tests/test_move_node_end_pipe.sql index 6418773b..4cde9fe6 100644 --- a/tests/test_move_node_end_pipe.sql +++ b/tests/test_move_node_end_pipe.sql @@ -17,15 +17,27 @@ INSERT INTO qwat_od.pipe (id, fk_node_a, fk_node_b, geometry) VALUES (1, 1, 2, 101, 101, 101, 1, 101, 101, 101, 101, st_setsrid('linestring(530000 140000 0,530000 140010 0)'::geometry, 21781)); - --- insert a valve at the start of the pipe -INSERT INTO qwat_od.vw_element_valve (fk_district, fk_pressurezone, fk_distributor, fk_pipe, - fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, - geometry) - VALUES (1, 1, 1, 1, - 101, 101, 101, 101, 6108, 101, - st_setsrid('point(530000 140000 0)'::geometry,21781)); - --- move valve away -UPDATE qwat_od.vw_element_valve set geometry = st_setsrid('point(530001 140000 0)'::geometry,21781) where geometry = st_setsrid('point(530000 140000 0)'::geometry,21781); +-- insert an hydrant at the start of the pipe +INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, + underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, + pressure_static, pressure_dynamic, flow, + observation_date, observation_source, + geometry) + VALUES (2016, 1, 101, 101, + True, 9.5, 101, 101, 101, 101, 101, 101, 101, + 12.3, 12.3, 12.3, + '2016/01/01', 'Test', + st_setsrid('POINT(530000 140000 0)'::geometry,21781) + ); + +-- move hydrant away +UPDATE qwat_od.vw_element_hydrant set geometry = st_setsrid('point(530001 140000 0)'::geometry,21781) where geometry = st_setsrid('point(530000 140000 0)'::geometry,21781); + +-- restore the initial state +DELETE FROM qwat_od.vw_element_hydrant; +DELETE FROM qwat_od.pipe; +DELETE FROM qwat_od.pressurezone; +DELETE FROM qwat_od.consumptionzone; +DELETE FROM qwat_od.district; +DELETE FROM qwat_od.distributor; \ No newline at end of file diff --git a/tests/test_node_orientation.sql b/tests/test_node_orientation.sql index 9a72b53a..b639e6a1 100644 --- a/tests/test_node_orientation.sql +++ b/tests/test_node_orientation.sql @@ -13,17 +13,44 @@ INSERT INTO qwat_od.pipe (geometry, fk_function, fk_material, fk_distributor, fk SELECT ST_SetSRID(ST_GeomFromText('LINESTRING(559980 149980 0, 559979.975263114 149979.915516003 0, 559979.947476254 149979.823744764 0)'), 21781), 4105, 101, 1, 1301, 1502, 101, 101, 101; -INSERT INTO qwat_od.vw_element_valve (geometry, fk_distributor, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_precision) -SELECT ST_SetSRID(ST_GeomFromText('POINT(559990 149990 0)'), 21781), 1, 1301, 101, 101, 101, 101; +INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, + underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, + pressure_static, pressure_dynamic, flow, + observation_date, observation_source, + geometry) + VALUES (2016, 1, 101, 101, + True, 9.5, 101, 101, 101, 101, 101, 101, 101, + 12.3, 12.3, 12.3, + '2016/01/01', 'Test', + st_setsrid('POINT(559990 149990 0)'::geometry,21781) + ); -INSERT INTO qwat_od.vw_element_valve (geometry, fk_distributor, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_precision) -SELECT ST_SetSRID(ST_GeomFromText('POINT(559990 150010 0)'), 21781), 1, 1301, 101, 101, 101, 101; +INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, + underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, + pressure_static, pressure_dynamic, flow, + observation_date, observation_source, + geometry) + VALUES (2016, 1, 101, 101, + True, 9.5, 101, 101, 101, 101, 101, 101, 101, + 12.3, 12.3, 12.3, + '2016/01/01', 'Test', + st_setsrid('POINT(559990 150010 0)'::geometry,21781) + ); -INSERT INTO qwat_od.vw_element_valve (geometry, fk_distributor, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_precision) -SELECT ST_SetSRID(ST_GeomFromText('POINT(559979.975263114 149979.915516003 0)'), 21781), 1, 1301, 101, 101, 101, 101; +INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, + underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, + pressure_static, pressure_dynamic, flow, + observation_date, observation_source, + geometry) + VALUES (2016, 1, 101, 101, + True, 9.5, 101, 101, 101, 101, 101, 101, 101, + 12.3, 12.3, 12.3, + '2016/01/01', 'Test', + st_setsrid('POINT(559979.975263114 149979.915516003 0)'::geometry,21781) + ); -- The CALCULATED orientation -SELECT _pipe_orientation AS qwat_calculated_orientation FROM qwat_od.vw_element_valve order by id; +SELECT _pipe_orientation AS qwat_calculated_orientation FROM qwat_od.vw_element_hydrant order by id; -- expected ? -- SELECT degrees(ST_Azimuth(ST_GeomFromText('POINT(559980 149980)'), ST_GeomFromText('POINT(559990 149990)'))) AS real_orientation; @@ -31,7 +58,7 @@ SELECT _pipe_orientation AS qwat_calculated_orientation FROM qwat_od.vw_element_ -- SELECT (degrees(ST_Azimuth(ST_GeomFromText('POINT(559980 149980)'), ST_GeomFromText('POINT(559979.975263114 149979.915516003)'))) + degrees(ST_Azimuth(ST_GeomFromText('POINT(559979.975263114 149979.915516003)'), ST_GeomFromText('POINT(559979.947476254 149979.823744764)'))))/2 AS real_orientation; -- restore the initial state -DELETE FROM qwat_od.vw_element_valve; +DELETE FROM qwat_od.vw_element_hydrant; DELETE FROM qwat_od.pipe; DELETE FROM qwat_od.node; DELETE FROM qwat_od.distributor; diff --git a/tests/test_scalability_multithread.py b/tests/test_scalability_multithread.py index 3c5e3cf7..bc98927e 100644 --- a/tests/test_scalability_multithread.py +++ b/tests/test_scalability_multithread.py @@ -74,7 +74,9 @@ 'id_cp': 1, 'id_node_a': 1, - 'id_node_b': 2 + 'id_node_b': 2, + + 'valve_id': 1 } # Var list to replace in SQL @@ -121,7 +123,8 @@ 'DEL_Z1', 'DEL_X2', 'DEL_Y2', - 'DEL_Z2' + 'DEL_Z2', + 'VALVE_ID' ] @@ -157,7 +160,7 @@ def test_scalability(pgService, nbIterations): def _execute_statements(cur, conn, threadName, threadId, origin, nbIterations): count = 1 p = sqlParams.copy() - while nbIterations: + while count < nbIterations: print "{tname} - Iteration {nb}".format(tname=threadName, nb=count) # Modify values (coords, IDs) @@ -201,10 +204,11 @@ def _execute_statements(cur, conn, threadName, threadId, origin, nbIterations): p['co_x1'] += origin['x'] + OFFSET p['co_y1'] += origin['y'] + OFFSET - p['id_cp'] += count * threadId - p['id_node_a'] = count * OFFSET_ID * threadId - p['id_node_b'] = count * OFFSET_ID * threadId - p['installation_id'] = "{name}_{num}".format(name=threadName, num=count * OFFSET_ID * threadId) + p['id_cp'] = threadId * nbIterations + count + p['id_node_a'] = threadId * nbIterations + count + p['id_node_b'] = threadId * nbIterations + count + p['installation_id'] = "{name}_{num}".format(name=threadName, num=threadId * nbIterations + count) + p['valve_id'] = threadId * nbIterations + count for statement in open(TEST_SCRIPT).read().split(";;")[:-1]: if statement != '' and statement[:2] != '--': @@ -228,7 +232,6 @@ def _execute_statements(cur, conn, threadName, threadId, origin, nbIterations): origin['x'] = 0 origin['y'] = 0 - nbIterations -= 1 count += 1 diff --git a/tests/test_scenarii.expected.sql b/tests/test_scenarii.expected.sql index dbc4bb59..9525254d 100644 --- a/tests/test_scenarii.expected.sql +++ b/tests/test_scenarii.expected.sql @@ -1,14 +1,6 @@ -DELETE 0 -DELETE 0 -DELETE 0 -DELETE 1 -DELETE 0 -DELETE 0 -DELETE 0 -DELETE 0 -DELETE 1 -DELETE 1 -DELETE 0 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 INSERT 0 1 INSERT 0 1 INSERT 0 1 diff --git a/tests/test_scenarii.sql b/tests/test_scenarii.sql index f93616b1..89780456 100644 --- a/tests/test_scenarii.sql +++ b/tests/test_scenarii.sql @@ -1,25 +1,20 @@ --- get clean state -DELETE FROM qwat_dr.constructionpoint; -DELETE FROM qwat_od.vw_element_part; -DELETE FROM qwat_od.vw_element_valve; -DELETE FROM qwat_od.pipe; -DELETE FROM qwat_od.vw_element_hydrant; -DELETE FROM qwat_od.cover; -DELETE FROM qwat_od.vw_element_installation; -DELETE FROM qwat_od.network_element; -DELETE FROM qwat_od.pressurezone; -DELETE FROM qwat_od.distributor; --- DELETE FROM qwat_od.leak; -DELETE FROM qwat_vl.part_type WHERE value_fr = 'Point d''introduction'; - -- add a construction point INSERT INTO qwat_dr.constructionpoint (id, geometry) VALUES (1, ST_Setsrid('point(530000 140000 0)'::geometry,21781)); +-- add a district +INSERT INTO qwat_od.district (id, name) VALUES (1, 'My district'); + -- create pipe -- add a distributor INSERT INTO qwat_od.distributor (id, name) VALUES (1, 'Demo Distributor'); +-- add a consumption zone +INSERT INTO qwat_od.consumptionzone(id, name) VALUES (1, 'A consumption zone'); + +-- add a pressure zone +INSERT INTO qwat_od.pressurezone (id, name, fk_distributor, fk_consumptionzone) VALUES (1, 'A pressure zone', 1, 1); + -- add the pipe INSERT INTO qwat_od.pipe (id, fk_node_a, fk_node_b, fk_function, fk_installmethod, fk_material, fk_distributor, fk_precision, fk_bedding, @@ -57,7 +52,7 @@ INSERT INTO qwat_od.vw_element_part (year, _pipe_orientation, fk_part_type, fk_s SELECT 2016, 1.5, (SELECT id FROM qwat_vl.part_type WHERE value_fr = 'Point d''introduction'), 101, 1, 101, st_setsrid('point(630000 140000 0)'::geometry, 21781); -- create valve -INSERT INTO qwat_od.vw_element_valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, +INSERT INTO qwat_od.valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_object_reference, year, closed, fk_maintenance,altitude,schema_force_visible, geometry) @@ -118,7 +113,7 @@ INSERT INTO qwat_od.cover (identification, form_dimension, fk_cover_type, circul /* DELETE FROM qwat_dr.constructionpoint; DELETE FROM qwat_od.vw_element_part; -DELETE FROM qwat_od.vw_element_valve; +DELETE FROM qwat_od.valve; DELETE FROM qwat_od.pipe; DELETE FROM qwat_od.vw_element_hydrant; DELETE FROM qwat_od.cover; diff --git a/tests/test_scenarii_scalability.model b/tests/test_scenarii_scalability.model index 336c7b33..80432822 100644 --- a/tests/test_scenarii_scalability.model +++ b/tests/test_scenarii_scalability.model @@ -32,10 +32,10 @@ INSERT INTO qwat_od.vw_element_part (year, _pipe_orientation, fk_part_type, fk_s SELECT 2016, 1.5, (SELECT id FROM qwat_vl.part_type WHERE value_fr = 'Point d''introduction'), 101, 1, 101, st_setsrid('point(IP_X1 IP_Y1 IP_Z1)'::geometry, 21781);; -- create valve -INSERT INTO qwat_od.vw_element_valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, +INSERT INTO qwat_od.valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_object_reference, year, closed, fk_maintenance,altitude,schema_force_visible, geometry) - VALUES (1, 1, 1, 1, 1, + VALUES (VALVE_ID, 1, 1, 1, 1, 101, 101, 101, 101, 6108, 101, 101, 2016, True, '{1,2}', 9.5, True, st_setsrid('point(CP_X2 CP_Y2 CP_Z2)'::geometry,21781));; diff --git a/tests/test_scenarii_scalability_init.sql b/tests/test_scenarii_scalability_init.sql index 1d081c99..7f52aaf2 100644 --- a/tests/test_scenarii_scalability_init.sql +++ b/tests/test_scenarii_scalability_init.sql @@ -1,5 +1,20 @@ --- add a distributor -INSERT INTO qwat_od.distributor (id, name) VALUES (1, 'Demo Distributor');; -- create an introduction point INSERT INTO qwat_vl.part_type (id, value_fr) VALUES (999, 'Point d''introduction');; + +-- add a construction point +INSERT INTO qwat_dr.constructionpoint (id, geometry) + VALUES (1, ST_Setsrid('point(530000 140000 0)'::geometry,21781)); + +-- add a district +INSERT INTO qwat_od.district (id, name) VALUES (1, 'My district'); + +-- create pipe +-- add a distributor +INSERT INTO qwat_od.distributor (id, name) VALUES (1, 'Demo Distributor'); + +-- add a consumption zone +INSERT INTO qwat_od.consumptionzone(id, name) VALUES (1, 'A consumption zone'); + +-- add a pressure zone +INSERT INTO qwat_od.pressurezone (id, name, fk_distributor, fk_consumptionzone) VALUES (1, 'A pressure zone', 1, 1); diff --git a/update/delta/delta_0.1.1.sql b/update/delta/delta_0.1.1.sql new file mode 100644 index 00000000..872149d0 --- /dev/null +++ b/update/delta/delta_0.1.1.sql @@ -0,0 +1,51 @@ +ALTER TABLE qwat_od.valve ADD COLUMN fk_district integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_district FOREIGN KEY (fk_district) REFERENCES qwat_od.district(id) MATCH FULL; +CREATE INDEX fki_valve_fk_district ON qwat_od.valve(fk_district); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_pressurezone integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_pressurezone FOREIGN KEY (fk_pressurezone) REFERENCES qwat_od.pressurezone(id) MATCH FULL; +CREATE INDEX fki_valve_fk_pressurezone ON qwat_od.valve(fk_pressurezone); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_distributor integer not null; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_distributor FOREIGN KEY (fk_distributor) REFERENCES qwat_od.distributor(id) MATCH FULL; +CREATE INDEX fki_valve_fk_distributor ON qwat_od.valve(fk_distributor); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_precision integer not null; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precision FOREIGN KEY (fk_precision) REFERENCES qwat_vl.precision(id) MATCH FULL; +CREATE INDEX fki_valve_fk_precision ON qwat_od.valve(fk_precision); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_status integer not null; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_status FOREIGN KEY (fk_status) REFERENCES qwat_vl.status(id) MATCH FULL; +CREATE INDEX fki_valve_fk_status ON qwat_od.valve(fk_status); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_object_reference integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_object_reference FOREIGN KEY (fk_object_reference) REFERENCES qwat_vl.object_reference(id) MATCH FULL; +CREATE INDEX fki_valve_fk_object_reference ON qwat_od.valve(fk_object_reference); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_folder integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_folder FOREIGN KEY (fk_folder) REFERENCES qwat_od.folder(id) MATCH FULL; +CREATE INDEX fki_valve_fk_folder ON qwat_od.valve(fk_folder); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_precisionalti integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precisionalti FOREIGN KEY (fk_precisionalti) REFERENCES qwat_vl.precisionalti(id) MATCH FULL; +CREATE INDEX fki_valve_fk_precisionalti ON qwat_od.valve(fk_precisionalti); + +ALTER TABLE qwat_od.valve ADD COLUMN geometry geometry('POINTZ',:SRID) NOT NULL; +ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt1 geometry('POINTZ',:SRID); +ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt2 geometry('POINTZ',:SRID); +ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt1 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated +ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated + +CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); +CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); +CREATE INDEX valve_geoidx_alt2 ON qwat_od.valve USING GIST ( geometry_alt2 ); + +ALTER TABLE qwat_od.valve ALTER COLUMN id serial; +-- integer NOT NULL REFERENCES qwat_od.network_element(id) PRIMARY KEY; + +ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); +ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; + +SELECT qwat_sys.fn_enable_schemaview( 'valve' ); + + From c7b7eb85aa3f88bfee8ba75f09bed9f5baff087e Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Wed, 31 Aug 2016 16:11:36 +0200 Subject: [PATCH 2/9] Scalability Test: no more deadlocking Scalability tests continue Valve outside node, scalability tests Orientation test adapted for nodes Tests OK pour valve outside nodes --- init_qwat.sh | 1 + ordinary_data/functions/fn_node_create.sql | 4 +- ordinary_data/nodes/od_node.sql | 15 ++- .../valve/fn_valve_set_orientation.sql | 102 ++++++++++++++++++ ordinary_data/valve/od_valve.sql | 56 +++++++++- ordinary_data/valve/tr_valve_pipe.sql | 42 ++++++++ tests/test_add_node.expected.sql | 5 +- tests/test_add_node.sql | 48 +++++---- tests/test_node_orientation.expected.sql | 6 +- tests/test_node_orientation.sql | 6 +- tests/test_scalability.py | 8 +- tests/test_scalability_multithread.py | 16 ++- tests/test_scenarii.expected.sql | 12 +++ tests/test_scenarii.sql | 5 +- tests/test_valve_orientation.expected.sql | 21 ++++ tests/test_valve_orientation.sql | 63 +++++++++++ tests/tests.sh | 1 + tests/tests_scalability_multithread.sh | 2 +- update/delta/delta_0.1.1.sql | 17 +++ 19 files changed, 392 insertions(+), 38 deletions(-) create mode 100644 ordinary_data/valve/fn_valve_set_orientation.sql create mode 100644 tests/test_valve_orientation.expected.sql create mode 100644 tests/test_valve_orientation.sql diff --git a/init_qwat.sh b/init_qwat.sh index 361a9fc3..5e880ff3 100755 --- a/init_qwat.sh +++ b/init_qwat.sh @@ -235,6 +235,7 @@ psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/samplingpoint/od_sampling psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/surveypoint/od_surveypoint.sql psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/leak/od_leak.sql +psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/valve/fn_valve_set_orientation.sql psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/valve/tr_valve_pipe.sql diff --git a/ordinary_data/functions/fn_node_create.sql b/ordinary_data/functions/fn_node_create.sql index d3f5cfea..54b8ba74 100644 --- a/ordinary_data/functions/fn_node_create.sql +++ b/ordinary_data/functions/fn_node_create.sql @@ -13,11 +13,11 @@ $BODY$ IF _node_id IS NULL THEN IF deactivate_node_add_pipe_vertex THEN -- if we are called from a pipe creation, do not try to add a vertex on the pipe - ALTER TABLE qwat_od.node DISABLE TRIGGER tr_node_add_pipe_vertex_insert; + --ALTER TABLE qwat_od.node DISABLE TRIGGER tr_node_add_pipe_vertex_insert; END IF; INSERT INTO qwat_od.node (geometry) VALUES (ST_Force3D(_point)) RETURNING id INTO _node_id; IF deactivate_node_add_pipe_vertex THEN - ALTER TABLE qwat_od.node ENABLE TRIGGER tr_node_add_pipe_vertex_insert; + --ALTER TABLE qwat_od.node ENABLE TRIGGER tr_node_add_pipe_vertex_insert; END IF; IF _node_id IS NULL THEN RAISE EXCEPTION 'Node is null although it should have been created'; diff --git a/ordinary_data/nodes/od_node.sql b/ordinary_data/nodes/od_node.sql index b5c8e23b..4c85b33a 100644 --- a/ordinary_data/nodes/od_node.sql +++ b/ordinary_data/nodes/od_node.sql @@ -85,15 +85,24 @@ COMMENT ON TRIGGER tr_node_geom_update ON qwat_od.node IS 'Trigger: updates auto /* --------------------------------------------*/ /* --- ADD VERTEX TO PIPE AT NODE LOCATION ----*/ +/* CREATE OR REPLACE FUNCTION qwat_od.ft_node_add_pipe_vertex() RETURNS trigger AS $BODY$ + DECLARE + pipe_id integer; BEGIN + pipe_id := 0; + SELECT id INTO pipe_id FROM qwat_od.pipe WHERE fk_node_a = NEW.id OR fk_node_b = NEW.id FOR UPDATE; -- add a vertex to the corresponding pipe if it intersects -- when the node is close enough to the pipe (< 1 micrometer) the node is considered to intersect the pipe -- it allows to deal with intersections that cannot be represented by floating point numbers - UPDATE qwat_od.pipe SET geometry = ST_Snap(geometry, NEW.geometry, 1e-6) WHERE ST_Distance(geometry, NEW.geometry) < 1e-6; - RETURN NEW; + RAISE NOTICE '############# node id = %', NEW.id; + IF pipe_id is null THEN + RAISE NOTICE '------------------------------------ pipe id = %', pipe_id; + UPDATE qwat_od.pipe SET geometry = ST_Snap(geometry, NEW.geometry, 1e-6) WHERE ST_Distance(geometry, NEW.geometry) < 1e-6; + END IF; + RETURN NEW; END; $BODY$ LANGUAGE plpgsql; @@ -112,7 +121,7 @@ CREATE TRIGGER tr_node_add_pipe_vertex_update WHEN (ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) EXECUTE PROCEDURE qwat_od.ft_node_add_pipe_vertex(); COMMENT ON TRIGGER tr_node_add_pipe_vertex_update ON qwat_od.node IS 'Trigger: updates auto fields after geom update.'; - +*/ diff --git a/ordinary_data/valve/fn_valve_set_orientation.sql b/ordinary_data/valve/fn_valve_set_orientation.sql new file mode 100644 index 00000000..c519568e --- /dev/null +++ b/ordinary_data/valve/fn_valve_set_orientation.sql @@ -0,0 +1,102 @@ +/* + qWat - QGIS Water Module + + SQL file :: valve functions +*/ + + +/* define valve orientation type */ +CREATE OR REPLACE FUNCTION qwat_od.fn_valve_set_orientation(_valve_id integer) RETURNS void AS +$BODY$ + DECLARE + _pipeitem record; + _pipe_id integer; + _grouped record; + _year integer; + _material varchar(50); + _diameter smallint; + _looppos integer := 0; + _type qwat_od.pipe_connection; + _orientation double precision := 0; + _orientation2 double precision := 0; + _node_geom geometry; + _pipe_geom geometry; + _sub_geom geometry; + _lin_ref float; + BEGIN + + -- get the geometry + _node_geom := geometry FROM qwat_od.valve WHERE id = _valve_id; + + -- count the active pipes associated to this valve + SELECT + COUNT(*) AS count, + bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible + INTO _grouped + FROM qwat_od.pipe + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id + WHERE (pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id)) + AND status.active IS TRUE; + + -- if not connected to any pipe, do nothing + IF _grouped.count <= 2 THEN + /* loop over them, and take the 2 first/last vertices + of the pipe to determine orientation (used for symbology) */ + FOR _pipeitem IN ( + SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, + ST_StartPoint(geometry) AS point_1, + ST_PointN(geometry,2) AS point_2 + FROM qwat_od.pipe + INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + WHERE pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id) AND status.active IS TRUE + ) LOOP + IF _looppos=0 THEN + -- first pipe + _type := 'pipe_end'::qwat_od.pipe_connection; + _year := _pipeitem.year; + _material := _pipeitem.material; + _diameter := _pipeitem.diameter; + _pipe_id := _pipeitem.id; + _looppos := 1; + _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); + ELSE + -- second pipe if exists + IF _material = _pipeitem.material AND _diameter = _pipeitem.diameter AND _year = _pipeitem.year THEN + _type := 'couple_same'::qwat_od.pipe_connection; + ELSIF _material = _pipeitem.material AND _diameter = _pipeitem.diameter THEN + _type := 'couple_year'::qwat_od.pipe_connection; + ELSIF _material = _pipeitem.material THEN + _type := 'couple_diameter'::qwat_od.pipe_connection; + ELSIF _diameter = _pipeitem.diameter THEN + _type := 'couple_material'::qwat_od.pipe_connection; + ELSE + _type := 'couple_other'; + END IF; + _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + _orientation2 := pi() + _orientation2; -- reverse angle + -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); + _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); + -- RAISE NOTICE 'mean: %', degrees(_orientation ); + -- reverse arrow according to diameter reduction + IF _pipeitem.diameter > _diameter THEN + _orientation := _orientation + pi(); + END IF; + END IF; + END LOOP; + END IF; + + -- update the valve table + UPDATE qwat_od.valve SET + orientation = degrees(_orientation) + --_pipe_schema_visible = _grouped.schema_visible + WHERE id = _valve_id; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation for a valve.'; + + + diff --git a/ordinary_data/valve/od_valve.sql b/ordinary_data/valve/od_valve.sql index ccf16971..b375af19 100644 --- a/ordinary_data/valve/od_valve.sql +++ b/ordinary_data/valve/od_valve.sql @@ -6,7 +6,7 @@ /* create */ CREATE TABLE qwat_od.valve (id serial PRIMARY KEY); -COMMENT ON TABLE qwat_od.valve IS 'Table for valve. Inherits from node.'; +COMMENT ON TABLE qwat_od.valve IS 'Table for valve.'; /* columns */ ALTER TABLE qwat_od.valve ADD COLUMN fk_district integer; @@ -31,6 +31,7 @@ ALTER TABLE qwat_od.valve ADD COLUMN handle_altitude decimal(10,3); ALTER TABLE qwat_od.valve ADD COLUMN handle_geometry geometry(PointZ,:SRID); ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; +ALTER TABLE qwat_od.valve ADD COLUMN orientation float default null; /* Schema view */ SELECT qwat_sys.fn_enable_schemaview( 'valve' ); @@ -73,6 +74,7 @@ CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); CREATE INDEX valve_geoidx_alt2 ON qwat_od.valve USING GIST ( geometry_alt2 ); /* NODE TRIGGER */ +/* CREATE OR REPLACE FUNCTION qwat_od.ft_valve_node_set_type() RETURNS TRIGGER AS $BODY$ BEGIN @@ -88,6 +90,23 @@ CREATE TRIGGER valve_node_set_type FOR EACH ROW EXECUTE PROCEDURE qwat_od.ft_valve_node_set_type(); COMMENT ON TRIGGER valve_node_set_type ON qwat_od.valve IS 'Trigger: set-type of node after inserting a valve (to get orientation).'; +*/ +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_set_orientation() RETURNS TRIGGER AS +$BODY$ + BEGIN + PERFORM qwat_od.fn_valve_set_orientation(NEW.id); + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.ft_valve_set_orientation() IS 'Trigger: set orientation after inserting a valve.'; + +CREATE TRIGGER valve_set_orientation + AFTER INSERT ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); +COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; + @@ -122,3 +141,38 @@ CREATE TRIGGER valve_handle_altitude_insert_trigger FOR EACH ROW EXECUTE PROCEDURE qwat_od.ft_valve_handle_altitude(); COMMENT ON TRIGGER valve_handle_altitude_insert_trigger ON qwat_od.valve IS 'Trigger: when updating, check if altitude or Z value of geometry changed and synchronize them.'; + + + +/* --------------------------------------------*/ +/* --- ADD VERTEX TO PIPE AT VALVE LOCATION ----*/ +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_add_pipe_vertex() + RETURNS trigger AS +$BODY$ + DECLARE + pipe_id integer; + BEGIN + -- add a vertex to the corresponding pipe if it intersects + -- when the valve is close enough to the pipe (< 1 micrometer) the valve is considered to intersect the pipe + -- it allows to deal with intersections that cannot be represented by floating point numbers + UPDATE qwat_od.pipe SET geometry = ST_Snap(geometry, NEW.geometry, 1e-6) WHERE ST_Distance(geometry, NEW.geometry) < 1e-6; + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; + +CREATE TRIGGER tr_valve_add_pipe_vertex_insert + AFTER INSERT + ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_add_pipe_vertex(); +COMMENT ON TRIGGER tr_valve_add_pipe_vertex_insert ON qwat_od.valve IS 'Trigger: updates auto fields after insert.'; + +CREATE TRIGGER tr_valve_add_pipe_vertex_update + AFTER UPDATE OF geometry + ON qwat_od.valve + FOR EACH ROW + WHEN (ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) + EXECUTE PROCEDURE qwat_od.ft_valve_add_pipe_vertex(); +COMMENT ON TRIGGER tr_valve_add_pipe_vertex_update ON qwat_od.valve IS 'Trigger: updates auto fields after geom update.'; + diff --git a/ordinary_data/valve/tr_valve_pipe.sql b/ordinary_data/valve/tr_valve_pipe.sql index c3fa9a68..06f3e610 100644 --- a/ordinary_data/valve/tr_valve_pipe.sql +++ b/ordinary_data/valve/tr_valve_pipe.sql @@ -21,6 +21,7 @@ $BODY$ ) AS valve_group ON pipe_dupp.id = valve_group.fk_pipe WHERE pipe.id = _pipe_id; + END; $BODY$ LANGUAGE plpgsql; @@ -75,3 +76,44 @@ CREATE TRIGGER tr_valve_update_trigger EXECUTE PROCEDURE qwat_od.ft_valve_update(); COMMENT ON TRIGGER tr_valve_update_trigger ON qwat_od.valve IS 'Trigger: when updating a valve, reevaluate old and new pipes.'; + + +/* ASSIGN PIPE TO VALVE */ +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_infos_insert() RETURNS TRIGGER AS +$BODY$ + BEGIN + NEW.fk_pipe := qwat_od.fn_pipe_get_id(NEW.geometry); + NEW.fk_district := qwat_od.fn_get_district(NEW.geometry); + NEW.fk_pressurezone := qwat_od.fn_get_pressurezone(NEW.geometry); + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.ft_valve_infos_insert() IS 'Trigger: when inserting a valve, assign pipe.'; +CREATE TRIGGER tr_valve_infos_insert_trigger + BEFORE INSERT ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_infos_insert(); +COMMENT ON TRIGGER tr_valve_infos_insert_trigger ON qwat_od.valve IS 'Trigger: when inserting a valve, assign pipe.'; + + + +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_infos_update() RETURNS TRIGGER AS +$BODY$ + BEGIN + if OLD.geometry <> NEW.geometry THEN + NEW.fk_pipe := qwat_od.fn_pipe_get_id(NEW.geometry); + NEW.fk_district := qwat_od.fn_get_district(NEW.geometry); + NEW.fk_pressurezone := qwat_od.fn_get_pressurezone(NEW.geometry); + END IF; + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.ft_valve_infos_insert() IS 'Trigger: when inserting a valve, assign pipe.'; +CREATE TRIGGER tr_valve_infos_update_trigger + BEFORE UPDATE ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_infos_insert(); +COMMENT ON TRIGGER tr_valve_infos_update_trigger ON qwat_od.valve IS 'Trigger: when inserting a valve, assign pipe.'; + diff --git a/tests/test_add_node.expected.sql b/tests/test_add_node.expected.sql index 3b72cd2f..892ff9ee 100644 --- a/tests/test_add_node.expected.sql +++ b/tests/test_add_node.expected.sql @@ -4,17 +4,18 @@ INSERT 0 1 INSERT 0 1 INSERT 0 1 INSERT 0 1 +INSERT 0 1 +INSERT 0 1 num_points_after_add1|3 INSERT 0 1 num_points_after_add2|4 -INSERT 0 1 -num_points_after_add3|5 new pipe close to the previous INSERT 0 1 INSERT 0 1 new pipe close to the previous - end INSERT 0 1 UPDATE 1 +DELETE 2 DELETE 0 DELETE 4 DELETE 1 diff --git a/tests/test_add_node.sql b/tests/test_add_node.sql index 1deac440..ff76696c 100644 --- a/tests/test_add_node.sql +++ b/tests/test_add_node.sql @@ -18,6 +18,7 @@ INSERT INTO qwat_od.pipe (id, fk_node_a, fk_node_b, 101, 101, 101, 1, 101, 101, 101, 101, st_setsrid('linestring(530000 138260 0,530050 138270 0)'::geometry, 21781)); +-- add a node to the pipe's start INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, pressure_static, pressure_dynamic, flow, @@ -27,12 +28,13 @@ INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_prov True, 9.5, 101, 101, 101, 101, 101, 101, 101, 12.3, 12.3, 12.3, '2016/01/01', 'Test', - st_setsrid('POINT(530025 138265 0)'::geometry,21781) + st_setsrid('POINT(530000 138260 0)'::geometry,21781) ); -- Now we have 3 vertices on the pipe -SELECT 'num_points_after_add1', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; +--SELECT 'num_points_after_add1', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; +-- add a node to the pipe's end INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, pressure_static, pressure_dynamic, flow, @@ -42,27 +44,37 @@ INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_prov True, 9.5, 101, 101, 101, 101, 101, 101, 101, 12.3, 12.3, 12.3, '2016/01/01', 'Test', - st_setsrid('POINT(530040 138268 0)'::geometry,21781) + st_setsrid('POINT(530050 138270 0)'::geometry,21781) ); -- Now we have 4 vertices on the pipe -SELECT 'num_points_after_add2', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; +--SELECT 'num_points_after_add2', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; + +-- Add a valve on the pipe +INSERT INTO qwat_od.valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, + fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_object_reference, + year, closed, fk_maintenance,altitude,schema_force_visible, + geometry) + VALUES (1, 1, 1, 1, 1, + 101, 101, 101, 101, 6108, 101, 101, + 2016, True, '{1,2}', 9.5, True, + st_setsrid('point(530025 138265 0)'::geometry,21781)); --- What if the intersection test fails but the node is close enough to the pipe ? -INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, - underground, altitude, fk_precisionalti, fk_precision, fk_object_reference, fk_model_sup, fk_model_inf, fk_material, fk_output, - pressure_static, pressure_dynamic, flow, - observation_date, observation_source, - geometry) - VALUES (2016, 1, 101, 101, - True, 9.5, 101, 101, 101, 101, 101, 101, 101, - 12.3, 12.3, 12.3, - '2016/01/01', 'Test', - st_setsrid('POINT(530025.2 138265.04 0)'::geometry,21781) - ); +-- Now we have 3 vertices on the pipe +SELECT 'num_points_after_add1', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; + +-- What if the intersection test fails but the valve is close enough to the pipe ? +INSERT INTO qwat_od.valve (id, fk_district, fk_pressurezone, fk_distributor, fk_pipe, + fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_object_reference, + year, closed, fk_maintenance,altitude,schema_force_visible, + geometry) + VALUES (2, 1, 1, 1, 1, + 101, 101, 101, 101, 6108, 101, 101, + 2016, True, '{1,2}', 9.5, True, + st_setsrid('point(530025.2 138265.04 0)'::geometry,21781)); -- Another point added -SELECT 'num_points_after_add3', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; +SELECT 'num_points_after_add2', st_numpoints(geometry) FROM qwat_od.pipe WHERE id = 1; -- insert two pipes with the second one starting very close to the end of the first (1e-8) -- this should not result in a recursive trigger call @@ -98,10 +110,10 @@ INSERT INTO qwat_od.pipe (id, fk_node_a, fk_node_b, UPDATE qwat_od.pipe SET geometry = st_setsrid('linestring(530000 148260 0, 530001 148260 0)'::geometry, 21781) where id=11; -- restore the initial state +DELETE FROM qwat_od.valve; DELETE FROM qwat_od.vw_element_hydrant; DELETE FROM qwat_od.pipe; DELETE FROM qwat_od.pressurezone; DELETE FROM qwat_od.consumptionzone; DELETE FROM qwat_od.district; DELETE FROM qwat_od.distributor; - diff --git a/tests/test_node_orientation.expected.sql b/tests/test_node_orientation.expected.sql index 333bda8f..594dee10 100644 --- a/tests/test_node_orientation.expected.sql +++ b/tests/test_node_orientation.expected.sql @@ -6,9 +6,9 @@ INSERT 0 1 INSERT 0 1 INSERT 0 1 INSERT 0 1 -45 -135 --106.582742061043 +59.3399934572086 +0 +-225 DELETE 0 DELETE 4 DELETE 0 diff --git a/tests/test_node_orientation.sql b/tests/test_node_orientation.sql index b639e6a1..def301e8 100644 --- a/tests/test_node_orientation.sql +++ b/tests/test_node_orientation.sql @@ -22,7 +22,7 @@ INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_prov True, 9.5, 101, 101, 101, 101, 101, 101, 101, 12.3, 12.3, 12.3, '2016/01/01', 'Test', - st_setsrid('POINT(559990 149990 0)'::geometry,21781) + st_setsrid('POINT(559980 149980 0)'::geometry,21781) ); INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, @@ -34,7 +34,7 @@ INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_prov True, 9.5, 101, 101, 101, 101, 101, 101, 101, 12.3, 12.3, 12.3, '2016/01/01', 'Test', - st_setsrid('POINT(559990 150010 0)'::geometry,21781) + st_setsrid('POINT(560000 150000 0)'::geometry,21781) ); INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_provider, @@ -46,7 +46,7 @@ INSERT INTO qwat_od.vw_element_hydrant (year, fk_distributor, fk_status, fk_prov True, 9.5, 101, 101, 101, 101, 101, 101, 101, 12.3, 12.3, 12.3, '2016/01/01', 'Test', - st_setsrid('POINT(559979.975263114 149979.915516003 0)'::geometry,21781) + st_setsrid('POINT(559980 150020 0)'::geometry,21781) ); -- The CALCULATED orientation diff --git a/tests/test_scalability.py b/tests/test_scalability.py index 4024da4d..e1604481 100644 --- a/tests/test_scalability.py +++ b/tests/test_scalability.py @@ -76,7 +76,9 @@ def _execute_statements(cur, conn, fileName, nb_iteration): 'id_cp': 1, 'id_node_a': 1, - 'id_node_b': 2 + 'id_node_b': 2, + + 'valve_id': 1 } # Var list to replace in SQL @@ -123,7 +125,8 @@ def _execute_statements(cur, conn, fileName, nb_iteration): 'DEL_Z1', 'DEL_X2', 'DEL_Y2', - 'DEL_Z2' + 'DEL_Z2', + 'VALVE_ID' ] for i in range(int(nb_iteration)): @@ -174,6 +177,7 @@ def _execute_statements(cur, conn, fileName, nb_iteration): p['id_node_a'] = i * OFFSET_ID p['id_node_b'] = i * OFFSET_ID p['installation_id'] = i * OFFSET_ID + p['valve_id'] = i * OFFSET_ID for statement in open(fileName).read().split(";;")[:-1]: if statement != '' and statement[:2] != '--': diff --git a/tests/test_scalability_multithread.py b/tests/test_scalability_multithread.py index bc98927e..a57066b5 100644 --- a/tests/test_scalability_multithread.py +++ b/tests/test_scalability_multithread.py @@ -12,16 +12,21 @@ import argparse import string import time +import datetime import psycopg2 import psycopg2.extras from subprocess import call TEST_SCRIPT = 'test_scenarii_scalability.model' +OUTPUT_STAT = 'scalability_stats.txt' OFFSET = 10 # offset in meter to create new objects OFFSET_ORIGIN = 2000 OFFSET_ID = 10 # offset for new ids NB_THREADS = 10 threads = [] +fileStats = None +startTime = None +endTime = None origin = {'x': 530000, 'y': 140000} @@ -143,12 +148,20 @@ def run(self): print "Starting " + self.name _execute_statements(self.cur, self.conn, self.name, self.threadID, self.origin, self.nbIterations) print "Exiting " + self.name + endTime = datetime.datetime.now().time() + fileStats = open(OUTPUT_STAT, 'a') + fileStats.write("Process {p} ended at {t}\n".format(t=endTime.isoformat(), p=self.threadID)) def test_scalability(pgService, nbIterations): + fileStats = open(OUTPUT_STAT, 'w') + fileStats.write("Nb thread: {nbthreads}\n".format(nbthreads=NB_THREADS)) + fileStats.write("Nb iterations: {nbiterations}\n".format(nbiterations=nbIterations)) + startTime = datetime.datetime.now().time() + fileStats.write("Process started at {t}\n".format(t=startTime.isoformat())) + # Create new threads for t in range(0, NB_THREADS): - #newThread = ScalabilityThread(t+1, "Thread-"+str(t), nbIterations, cur, conn) newThread = ScalabilityThread(t + 1, "Thread-" + str(t + 1), nbIterations, pgService) threads.append(newThread) @@ -157,6 +170,7 @@ def test_scalability(pgService, nbIterations): threads[t].start() + def _execute_statements(cur, conn, threadName, threadId, origin, nbIterations): count = 1 p = sqlParams.copy() diff --git a/tests/test_scenarii.expected.sql b/tests/test_scenarii.expected.sql index 9525254d..63f85dac 100644 --- a/tests/test_scenarii.expected.sql +++ b/tests/test_scenarii.expected.sql @@ -16,3 +16,15 @@ INSERT 0 1 INSERT 0 1 INSERT 0 1 INSERT 0 1 +DELETE 1 +DELETE 0 +DELETE 1 +DELETE 4 +DELETE 0 +DELETE 1 +DELETE 0 +DELETE 0 +DELETE 1 +DELETE 1 +DELETE 1 +DELETE 1 diff --git a/tests/test_scenarii.sql b/tests/test_scenarii.sql index 89780456..ef2e5355 100644 --- a/tests/test_scenarii.sql +++ b/tests/test_scenarii.sql @@ -110,7 +110,7 @@ INSERT INTO qwat_od.cover (identification, form_dimension, fk_cover_type, circul -- TODO -- deletes -/* + DELETE FROM qwat_dr.constructionpoint; DELETE FROM qwat_od.vw_element_part; DELETE FROM qwat_od.valve; @@ -119,6 +119,7 @@ DELETE FROM qwat_od.vw_element_hydrant; DELETE FROM qwat_od.cover; DELETE FROM qwat_od.vw_element_installation; DELETE FROM qwat_od.network_element; +DELETE FROM qwat_od.pressurezone; +DELETE FROM qwat_od.consumptionzone; DELETE FROM qwat_od.distributor; DELETE FROM qwat_od.leak; -*/ diff --git a/tests/test_valve_orientation.expected.sql b/tests/test_valve_orientation.expected.sql new file mode 100644 index 00000000..0e8d9e27 --- /dev/null +++ b/tests/test_valve_orientation.expected.sql @@ -0,0 +1,21 @@ +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +INSERT 0 1 +-135 +-135 +-45 +DELETE 3 +DELETE 4 +DELETE 0 +DELETE 1 +DELETE 1 +DELETE 1 +DELETE 1 diff --git a/tests/test_valve_orientation.sql b/tests/test_valve_orientation.sql new file mode 100644 index 00000000..dae4c930 --- /dev/null +++ b/tests/test_valve_orientation.sql @@ -0,0 +1,63 @@ +-- add a district +INSERT INTO qwat_od.district (id, name) VALUES (1, 'My district'); + +-- add a distributor +INSERT INTO qwat_od.distributor (id, name) VALUES (1, 'Demo Distributor'); + +-- add a consumption zone +INSERT INTO qwat_od.consumptionzone(id, name) VALUES (1, 'A consumption zone'); + +-- add a pressure zone +INSERT INTO qwat_od.pressurezone (id, name, fk_distributor, fk_consumptionzone) VALUES (1, 'A pressure zone', 1, 1); + +INSERT INTO qwat_od.pipe (geometry, fk_function, fk_material, fk_distributor, fk_status, fk_watertype, fk_installmethod, fk_precision, fk_bedding) +SELECT ST_SetSRID(ST_GeomFromText('LINESTRING (559980 149980 0, 559990 149990 0, 560000 150000 0)'), 21781), 4105, 101, 1, 1301, 1502, 101, 101, 101; + +INSERT INTO qwat_od.pipe (geometry, fk_function, fk_material, fk_distributor, fk_status, fk_watertype, fk_installmethod, fk_precision, fk_bedding) +SELECT ST_SetSRID(ST_GeomFromText('LINESTRING (560000 150000 0, 560010 150010 0, 560020 150020 0)'), 21781), 4105, 101, 1, 1301, 1502, 101, 101, 101; + +INSERT INTO qwat_od.pipe (geometry, fk_function, fk_material, fk_distributor, fk_status, fk_watertype, fk_installmethod, fk_precision, fk_bedding) +SELECT ST_SetSRID(ST_GeomFromText('LINESTRING (560000 150000 0, 559990 150010 0, 559980 150020 0)'), 21781), 4105, 101, 1, 1301, 1502, 101, 101, 101; + +INSERT INTO qwat_od.pipe (geometry, fk_function, fk_material, fk_distributor, fk_status, fk_watertype, fk_installmethod, fk_precision, fk_bedding) +SELECT ST_SetSRID(ST_GeomFromText('LINESTRING(559980 149980 0, 559979.975263114 149979.915516003 0, 559979.947476254 149979.823744764 0)'), 21781), 4105, 101, 1, 1301, 1502, 101, 101, 101; + + +INSERT INTO qwat_od.valve (id, fk_district, fk_pressurezone, fk_distributor, + fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_object_reference, + year, closed, fk_maintenance,altitude,schema_force_visible, + geometry) + VALUES (1, 1, 1, 1, + 101, 101, 101, 101, 6108, 101, 101, + 2016, True, '{1,2}', 9.5, True, + st_setsrid('point(559990 149990 0)'::geometry,21781)); + +INSERT INTO qwat_od.valve (id, fk_district, fk_pressurezone, fk_distributor, + fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_object_reference, + year, closed, fk_maintenance,altitude,schema_force_visible, + geometry) + VALUES (2, 1, 1, 1, + 101, 101, 101, 101, 6108, 101, 101, + 2016, True, '{1,2}', 9.5, True, + st_setsrid('point(560010 150010 0)'::geometry,21781)); + +INSERT INTO qwat_od.valve (id, fk_district, fk_pressurezone, fk_distributor, + fk_precision, fk_precisionalti, fk_status, fk_valve_type, fk_valve_function, fk_valve_actuation, fk_object_reference, + year, closed, fk_maintenance,altitude,schema_force_visible, + geometry) + VALUES (3, 1, 1, 1, + 101, 101, 101, 101, 6108, 101, 101, + 2016, True, '{1,2}', 9.5, True, + st_setsrid('point(559990 150010 0)'::geometry,21781)); + +-- The CALCULATED orientation +SELECT orientation AS qwat_calculated_orientation FROM qwat_od.valve order by id; + +-- restore the initial state +DELETE FROM qwat_od.valve; +DELETE FROM qwat_od.pipe; +DELETE FROM qwat_od.node; +DELETE FROM qwat_od.pressurezone; +DELETE FROM qwat_od.consumptionzone; +DELETE FROM qwat_od.district; +DELETE FROM qwat_od.distributor; \ No newline at end of file diff --git a/tests/tests.sh b/tests/tests.sh index 92e57cc6..daaa7d54 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -31,6 +31,7 @@ export PGOPTIONS="-c lc_messages=C -c client_min_messages=ERROR" TESTS="test_add_node.sql \ test_node_orientation.sql \ + test_valve_orientation.sql \ test_altitude.sql \ test_alternative_geometry.sql \ test_move_node_end_pipe.sql \ diff --git a/tests/tests_scalability_multithread.sh b/tests/tests_scalability_multithread.sh index 8aa2abd4..792b701e 100755 --- a/tests/tests_scalability_multithread.sh +++ b/tests/tests_scalability_multithread.sh @@ -2,7 +2,7 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" INIT_DB=1 -NB_ITERATIONS=10 +NB_ITERATIONS=100 while [[ $# > 0 ]]; do key="$1" diff --git a/update/delta/delta_0.1.1.sql b/update/delta/delta_0.1.1.sql index 872149d0..fdd8dbc9 100644 --- a/update/delta/delta_0.1.1.sql +++ b/update/delta/delta_0.1.1.sql @@ -45,7 +45,24 @@ ALTER TABLE qwat_od.valve ALTER COLUMN id serial; ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; +ALTER TABLE qwat_od.valve ADD COLUMN orientation float default null; SELECT qwat_sys.fn_enable_schemaview( 'valve' ); +-- Valve orientation +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_set_orientation() RETURNS TRIGGER AS +$BODY$ + BEGIN + PERFORM qwat_od.fn_valve_set_orientation(NEW.id); + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.ft_valve_set_orientation() IS 'Trigger: set orientation after inserting a valve.'; + +CREATE TRIGGER valve_set_orientation + AFTER INSERT ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); +COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; From 2ea18d776e978ef2ed940c67dac58d43adef3ea5 Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Thu, 15 Sep 2016 16:58:17 +0200 Subject: [PATCH 3/9] Trigger func simplifications for valves Code cleaning --- ordinary_data/functions/fn_node_create.sql | 11 ++---- ordinary_data/nodes/od_node.sql | 42 ---------------------- ordinary_data/valve/od_valve.sql | 6 ++++ ordinary_data/valve/tr_valve_pipe.sql | 30 ++++++---------- 4 files changed, 20 insertions(+), 69 deletions(-) diff --git a/ordinary_data/functions/fn_node_create.sql b/ordinary_data/functions/fn_node_create.sql index 54b8ba74..205f8dce 100644 --- a/ordinary_data/functions/fn_node_create.sql +++ b/ordinary_data/functions/fn_node_create.sql @@ -11,14 +11,9 @@ $BODY$ BEGIN SELECT id FROM qwat_od.node WHERE ST_Equals(ST_Force2d(_point), ST_Force2d(node.geometry)) IS TRUE LIMIT 1 INTO _node_id; IF _node_id IS NULL THEN - IF deactivate_node_add_pipe_vertex THEN - -- if we are called from a pipe creation, do not try to add a vertex on the pipe - --ALTER TABLE qwat_od.node DISABLE TRIGGER tr_node_add_pipe_vertex_insert; - END IF; - INSERT INTO qwat_od.node (geometry) VALUES (ST_Force3D(_point)) RETURNING id INTO _node_id; - IF deactivate_node_add_pipe_vertex THEN - --ALTER TABLE qwat_od.node ENABLE TRIGGER tr_node_add_pipe_vertex_insert; - END IF; + + INSERT INTO qwat_od.node (geometry) VALUES (ST_Force3D(_point)) RETURNING id INTO _node_id; + IF _node_id IS NULL THEN RAISE EXCEPTION 'Node is null although it should have been created'; END IF; diff --git a/ordinary_data/nodes/od_node.sql b/ordinary_data/nodes/od_node.sql index 4c85b33a..b274a88e 100644 --- a/ordinary_data/nodes/od_node.sql +++ b/ordinary_data/nodes/od_node.sql @@ -83,48 +83,6 @@ CREATE TRIGGER tr_node_geom_update COMMENT ON TRIGGER tr_node_geom_update ON qwat_od.node IS 'Trigger: updates auto fields after geom update.'; -/* --------------------------------------------*/ -/* --- ADD VERTEX TO PIPE AT NODE LOCATION ----*/ -/* -CREATE OR REPLACE FUNCTION qwat_od.ft_node_add_pipe_vertex() - RETURNS trigger AS -$BODY$ - DECLARE - pipe_id integer; - BEGIN - pipe_id := 0; - SELECT id INTO pipe_id FROM qwat_od.pipe WHERE fk_node_a = NEW.id OR fk_node_b = NEW.id FOR UPDATE; - -- add a vertex to the corresponding pipe if it intersects - -- when the node is close enough to the pipe (< 1 micrometer) the node is considered to intersect the pipe - -- it allows to deal with intersections that cannot be represented by floating point numbers - RAISE NOTICE '############# node id = %', NEW.id; - IF pipe_id is null THEN - RAISE NOTICE '------------------------------------ pipe id = %', pipe_id; - UPDATE qwat_od.pipe SET geometry = ST_Snap(geometry, NEW.geometry, 1e-6) WHERE ST_Distance(geometry, NEW.geometry) < 1e-6; - END IF; - RETURN NEW; - END; -$BODY$ -LANGUAGE plpgsql; - -CREATE TRIGGER tr_node_add_pipe_vertex_insert - AFTER INSERT - ON qwat_od.node - FOR EACH ROW - EXECUTE PROCEDURE qwat_od.ft_node_add_pipe_vertex(); -COMMENT ON TRIGGER tr_node_add_pipe_vertex_insert ON qwat_od.node IS 'Trigger: updates auto fields after insert.'; - -CREATE TRIGGER tr_node_add_pipe_vertex_update - AFTER UPDATE OF geometry - ON qwat_od.node - FOR EACH ROW - WHEN (ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) - EXECUTE PROCEDURE qwat_od.ft_node_add_pipe_vertex(); -COMMENT ON TRIGGER tr_node_add_pipe_vertex_update ON qwat_od.node IS 'Trigger: updates auto fields after geom update.'; -*/ - - - /* --------------------------------------------*/ /* -------- MOVED NODE TRIGGER ----------------*/ CREATE OR REPLACE FUNCTION qwat_od.ft_pipe_node_moved() RETURNS TRIGGER AS diff --git a/ordinary_data/valve/od_valve.sql b/ordinary_data/valve/od_valve.sql index b375af19..f5a59efd 100644 --- a/ordinary_data/valve/od_valve.sql +++ b/ordinary_data/valve/od_valve.sql @@ -107,6 +107,12 @@ CREATE TRIGGER valve_set_orientation EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; +CREATE TRIGGER valve_update_orientation + AFTER UPDATE OF geometry ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); +COMMENT ON TRIGGER valve_update_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; + diff --git a/ordinary_data/valve/tr_valve_pipe.sql b/ordinary_data/valve/tr_valve_pipe.sql index 06f3e610..ab127663 100644 --- a/ordinary_data/valve/tr_valve_pipe.sql +++ b/ordinary_data/valve/tr_valve_pipe.sql @@ -79,41 +79,33 @@ COMMENT ON TRIGGER tr_valve_update_trigger ON qwat_od.valve IS 'Trigger: when up /* ASSIGN PIPE TO VALVE */ -CREATE OR REPLACE FUNCTION qwat_od.ft_valve_infos_insert() RETURNS TRIGGER AS +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_geom() RETURNS TRIGGER AS $BODY$ BEGIN NEW.fk_pipe := qwat_od.fn_pipe_get_id(NEW.geometry); NEW.fk_district := qwat_od.fn_get_district(NEW.geometry); NEW.fk_pressurezone := qwat_od.fn_get_pressurezone(NEW.geometry); + + -- la gestion du champ altitude en combinaison avec la géométrie 3D (qui existe pour les noeuds) + + RETURN NEW; END; $BODY$ LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.ft_valve_infos_insert() IS 'Trigger: when inserting a valve, assign pipe.'; +COMMENT ON FUNCTION qwat_od.ft_valve_geom() IS 'Trigger: when inserting or updating a valve, assign pipe and geom infos.'; CREATE TRIGGER tr_valve_infos_insert_trigger BEFORE INSERT ON qwat_od.valve FOR EACH ROW - EXECUTE PROCEDURE qwat_od.ft_valve_infos_insert(); + EXECUTE PROCEDURE qwat_od.ft_valve_geom(); COMMENT ON TRIGGER tr_valve_infos_insert_trigger ON qwat_od.valve IS 'Trigger: when inserting a valve, assign pipe.'; - -CREATE OR REPLACE FUNCTION qwat_od.ft_valve_infos_update() RETURNS TRIGGER AS -$BODY$ - BEGIN - if OLD.geometry <> NEW.geometry THEN - NEW.fk_pipe := qwat_od.fn_pipe_get_id(NEW.geometry); - NEW.fk_district := qwat_od.fn_get_district(NEW.geometry); - NEW.fk_pressurezone := qwat_od.fn_get_pressurezone(NEW.geometry); - END IF; - RETURN NEW; - END; -$BODY$ -LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.ft_valve_infos_insert() IS 'Trigger: when inserting a valve, assign pipe.'; CREATE TRIGGER tr_valve_infos_update_trigger BEFORE UPDATE ON qwat_od.valve FOR EACH ROW - EXECUTE PROCEDURE qwat_od.ft_valve_infos_insert(); -COMMENT ON TRIGGER tr_valve_infos_update_trigger ON qwat_od.valve IS 'Trigger: when inserting a valve, assign pipe.'; + --WHEN (OLD.geometry <> NEW.geometry) + WHEN (NOT ST_Equals(OLD.geometry, NEW.geometry)) + EXECUTE PROCEDURE qwat_od.ft_valve_geom(); +COMMENT ON TRIGGER tr_valve_infos_update_trigger ON qwat_od.valve IS 'Trigger: when updating a valve, assign pipe.'; From 5344559e039a29e52825a0c6d7f2ed4f955e9032 Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Mon, 26 Sep 2016 10:34:06 +0200 Subject: [PATCH 4/9] Valve exit node adjustements Add good version delta file for valves Corrected SRID for delta Update delta file for valves --- ordinary_data/functions/fn_node_set_type.sql | 32 -- ordinary_data/nodes/od_network_element.sql | 2 +- .../valve/fn_valve_set_orientation.sql | 17 - ordinary_data/valve/od_valve.sql | 12 +- ordinary_data/valve/tr_valve_pipe.sql | 7 +- .../od_installation_inheritance.py | 3 +- update/delta/delta_1.1.0.sql | 398 ++++++++++++++++++ 7 files changed, 410 insertions(+), 61 deletions(-) create mode 100644 update/delta/delta_1.1.0.sql diff --git a/ordinary_data/functions/fn_node_set_type.sql b/ordinary_data/functions/fn_node_set_type.sql index 256e79a8..e6e41f65 100644 --- a/ordinary_data/functions/fn_node_set_type.sql +++ b/ordinary_data/functions/fn_node_set_type.sql @@ -50,38 +50,6 @@ $BODY$ RAISE NOTICE 'Delete node %' , _node_id; DELETE FROM qwat_od.node WHERE id = _node_id; -- delete on table level for safety (do not delete on the merge view) RETURN; - -- otherwise this means the node is node at the end of a pipe, it must be on a vertex - ELSE - -- calculate the orientation on that vertex - _pipe_geom := geometry - FROM qwat_od.pipe - WHERE ST_Intersects(pipe.geometry, _node_geom) - ORDER BY ( - _node_geom IN ( - ST_StartPoint(pipe.geometry), - ST_EndPoint(pipe.geometry)) - )::integer ASC -- prefer a pipe which doesn't end on the valve - LIMIT 1; - - IF _pipe_geom IS NULL THEN - RAISE NOTICE 'Network element of type % with ID % is not located on a pipe!' - , element_type FROM qwat_od.vw_qwat_network_element WHERE id = _node_id - , _node_id ; - ELSE - _lin_ref := ST_LineLocatePoint(_pipe_geom,_node_geom); -- shouldn't be 0 or 1 as it would mean that the node is a pipe end - - _sub_geom := ST_LineSubstring( _pipe_geom, 0, _lin_ref); - _orientation := pi()/2 - ST_Azimuth( ST_PointN(_sub_geom, ST_NumPoints(_sub_geom)-1), - ST_EndPoint(_sub_geom) ); - _sub_geom := ST_LineSubstring( _pipe_geom, _lin_ref, 1); - _orientation2 := pi()/2 - ST_Azimuth( ST_PointN(_sub_geom, 2), - ST_StartPoint(_sub_geom) ); - _orientation2 := pi() + _orientation2; -- reverse angle - -- RAISE NOTICE 'pipe 1 %', degrees( _orientation ); - -- RAISE NOTICE 'pipe 2 %', degrees( _orientation2 ); - _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); - -- RAISE NOTICE 'mean %', degrees( _orientation ); - END IF; END IF; ELSE _type := NULL::qwat_od.pipe_connection; diff --git a/ordinary_data/nodes/od_network_element.sql b/ordinary_data/nodes/od_network_element.sql index 5889809a..807fe376 100644 --- a/ordinary_data/nodes/od_network_element.sql +++ b/ordinary_data/nodes/od_network_element.sql @@ -9,7 +9,7 @@ CREATE TABLE qwat_od.network_element (); COMMENT ON TABLE qwat_od.network_element IS 'Tables for network network_elements. -Every network_element of the network (hydrants, valves, network_element, installations, etc.) inherit from network_element which itself inherits from node.'; +Every network_element of the network (hydrants, network_element, installations, etc.) inherit from network_element which itself inherits from node.'; /* COLUMNS */ ALTER TABLE qwat_od.network_element ADD COLUMN id integer NOT NULL REFERENCES qwat_od.node(id) PRIMARY KEY; diff --git a/ordinary_data/valve/fn_valve_set_orientation.sql b/ordinary_data/valve/fn_valve_set_orientation.sql index c519568e..cde6beb1 100644 --- a/ordinary_data/valve/fn_valve_set_orientation.sql +++ b/ordinary_data/valve/fn_valve_set_orientation.sql @@ -12,11 +12,8 @@ $BODY$ _pipeitem record; _pipe_id integer; _grouped record; - _year integer; - _material varchar(50); _diameter smallint; _looppos integer := 0; - _type qwat_od.pipe_connection; _orientation double precision := 0; _orientation2 double precision := 0; _node_geom geometry; @@ -54,9 +51,6 @@ $BODY$ ) LOOP IF _looppos=0 THEN -- first pipe - _type := 'pipe_end'::qwat_od.pipe_connection; - _year := _pipeitem.year; - _material := _pipeitem.material; _diameter := _pipeitem.diameter; _pipe_id := _pipeitem.id; _looppos := 1; @@ -64,17 +58,6 @@ $BODY$ -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); ELSE -- second pipe if exists - IF _material = _pipeitem.material AND _diameter = _pipeitem.diameter AND _year = _pipeitem.year THEN - _type := 'couple_same'::qwat_od.pipe_connection; - ELSIF _material = _pipeitem.material AND _diameter = _pipeitem.diameter THEN - _type := 'couple_year'::qwat_od.pipe_connection; - ELSIF _material = _pipeitem.material THEN - _type := 'couple_diameter'::qwat_od.pipe_connection; - ELSIF _diameter = _pipeitem.diameter THEN - _type := 'couple_material'::qwat_od.pipe_connection; - ELSE - _type := 'couple_other'; - END IF; _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); _orientation2 := pi() + _orientation2; -- reverse angle -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); diff --git a/ordinary_data/valve/od_valve.sql b/ordinary_data/valve/od_valve.sql index f5a59efd..a749a593 100644 --- a/ordinary_data/valve/od_valve.sql +++ b/ordinary_data/valve/od_valve.sql @@ -91,6 +91,7 @@ CREATE TRIGGER valve_node_set_type EXECUTE PROCEDURE qwat_od.ft_valve_node_set_type(); COMMENT ON TRIGGER valve_node_set_type ON qwat_od.valve IS 'Trigger: set-type of node after inserting a valve (to get orientation).'; */ +/* CREATE OR REPLACE FUNCTION qwat_od.ft_valve_set_orientation() RETURNS TRIGGER AS $BODY$ BEGIN @@ -100,20 +101,22 @@ $BODY$ $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.ft_valve_set_orientation() IS 'Trigger: set orientation after inserting a valve.'; - +*/ +/* CREATE TRIGGER valve_set_orientation AFTER INSERT ON qwat_od.valve FOR EACH ROW EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; - +*/ +/* CREATE TRIGGER valve_update_orientation AFTER UPDATE OF geometry ON qwat_od.valve FOR EACH ROW + WHEN ST_NotEquals(OLD.geometry, NEW.geometry) EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); COMMENT ON TRIGGER valve_update_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; - - +*/ /* HANDLE ALTITUDE TRIGGER */ @@ -162,6 +165,7 @@ $BODY$ -- when the valve is close enough to the pipe (< 1 micrometer) the valve is considered to intersect the pipe -- it allows to deal with intersections that cannot be represented by floating point numbers UPDATE qwat_od.pipe SET geometry = ST_Snap(geometry, NEW.geometry, 1e-6) WHERE ST_Distance(geometry, NEW.geometry) < 1e-6; + PERFORM qwat_od.fn_valve_set_orientation(NEW.id); RETURN NEW; END; $BODY$ diff --git a/ordinary_data/valve/tr_valve_pipe.sql b/ordinary_data/valve/tr_valve_pipe.sql index ab127663..c60adfdc 100644 --- a/ordinary_data/valve/tr_valve_pipe.sql +++ b/ordinary_data/valve/tr_valve_pipe.sql @@ -70,6 +70,7 @@ $BODY$ $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.ft_valve_update() IS 'Trigger: when updating a valve, reevaluate old and new pipes.'; + CREATE TRIGGER tr_valve_update_trigger AFTER UPDATE ON qwat_od.valve FOR EACH ROW @@ -85,15 +86,12 @@ $BODY$ NEW.fk_pipe := qwat_od.fn_pipe_get_id(NEW.geometry); NEW.fk_district := qwat_od.fn_get_district(NEW.geometry); NEW.fk_pressurezone := qwat_od.fn_get_pressurezone(NEW.geometry); - - -- la gestion du champ altitude en combinaison avec la géométrie 3D (qui existe pour les noeuds) - - RETURN NEW; END; $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.ft_valve_geom() IS 'Trigger: when inserting or updating a valve, assign pipe and geom infos.'; + CREATE TRIGGER tr_valve_infos_insert_trigger BEFORE INSERT ON qwat_od.valve FOR EACH ROW @@ -104,7 +102,6 @@ COMMENT ON TRIGGER tr_valve_infos_insert_trigger ON qwat_od.valve IS 'Trigger: w CREATE TRIGGER tr_valve_infos_update_trigger BEFORE UPDATE ON qwat_od.valve FOR EACH ROW - --WHEN (OLD.geometry <> NEW.geometry) WHEN (NOT ST_Equals(OLD.geometry, NEW.geometry)) EXECUTE PROCEDURE qwat_od.ft_valve_geom(); COMMENT ON TRIGGER tr_valve_infos_update_trigger ON qwat_od.valve IS 'Trigger: when updating a valve, assign pipe.'; diff --git a/ordinary_data/views/inheritance/od_installation_inheritance.py b/ordinary_data/views/inheritance/od_installation_inheritance.py index 44ddc4da..21841bec 100755 --- a/ordinary_data/views/inheritance/od_installation_inheritance.py +++ b/ordinary_data/views/inheritance/od_installation_inheritance.py @@ -18,8 +18,7 @@ table: qwat_od.installation pkey: id pkey_value: NEW.id -generate_child_views: True - +generate_child_views: False allow_type_change: false schema: qwat_od diff --git a/update/delta/delta_1.1.0.sql b/update/delta/delta_1.1.0.sql new file mode 100644 index 00000000..c72e16ba --- /dev/null +++ b/update/delta/delta_1.1.0.sql @@ -0,0 +1,398 @@ +ALTER TABLE qwat_od.valve ADD COLUMN fk_district integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_district FOREIGN KEY (fk_district) REFERENCES qwat_od.district(id) MATCH FULL; +CREATE INDEX fki_valve_fk_district ON qwat_od.valve(fk_district); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_pressurezone integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_pressurezone FOREIGN KEY (fk_pressurezone) REFERENCES qwat_od.pressurezone(id) MATCH FULL; +CREATE INDEX fki_valve_fk_pressurezone ON qwat_od.valve(fk_pressurezone); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_distributor integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_distributor FOREIGN KEY (fk_distributor) REFERENCES qwat_od.distributor(id) MATCH FULL; +CREATE INDEX fki_valve_fk_distributor ON qwat_od.valve(fk_distributor); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_precision integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precision FOREIGN KEY (fk_precision) REFERENCES qwat_vl.precision(id) MATCH FULL; +CREATE INDEX fki_valve_fk_precision ON qwat_od.valve(fk_precision); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_status integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_status FOREIGN KEY (fk_status) REFERENCES qwat_vl.status(id) MATCH FULL; +CREATE INDEX fki_valve_fk_status ON qwat_od.valve(fk_status); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_object_reference integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_object_reference FOREIGN KEY (fk_object_reference) REFERENCES qwat_vl.object_reference(id) MATCH FULL; +CREATE INDEX fki_valve_fk_object_reference ON qwat_od.valve(fk_object_reference); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_folder integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_folder FOREIGN KEY (fk_folder) REFERENCES qwat_od.folder(id) MATCH FULL; +CREATE INDEX fki_valve_fk_folder ON qwat_od.valve(fk_folder); + +ALTER TABLE qwat_od.valve ADD COLUMN fk_precisionalti integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precisionalti FOREIGN KEY (fk_precisionalti) REFERENCES qwat_vl.precisionalti(id) MATCH FULL; +CREATE INDEX fki_valve_fk_precisionalti ON qwat_od.valve(fk_precisionalti); + +ALTER TABLE qwat_od.valve ADD COLUMN geometry geometry('POINTZ',21781); +ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt1 geometry('POINTZ',21781); +ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt2 geometry('POINTZ',21781); +ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt1 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated +ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated + +CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); +CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); +CREATE INDEX valve_geoidx_alt2 ON qwat_od.valve USING GIST ( geometry_alt2 ); + +-- ALTER TABLE qwat_od.valve ALTER COLUMN id serial; +-- integer NOT NULL REFERENCES qwat_od.network_element(id) PRIMARY KEY; + +ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); +ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; +ALTER TABLE qwat_od.valve ADD COLUMN orientation float default null; + +SELECT qwat_sys.fn_enable_schemaview( 'valve' ); + +-- TODO We need to tranfert all the column data from node to valve +UPDATE qwat_od.valve SET fk_district = (SELECT fk_district FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_pressurezone = (SELECT fk_pressurezone FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_distributor = (SELECT fk_distributor FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_precision = (SELECT fk_precision FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_status = (SELECT fk_status FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_object_reference = (SELECT fk_object_reference FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_folder = (SELECT fk_folder FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_precisionalti = (SELECT fk_precisionalti FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry = (SELECT geometry FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry_alt1 = (SELECT geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry_alt2 = (SELECT geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET update_geometry_alt1 = (SELECT update_geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET update_geometry_alt2 = (SELECT update_geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET year = (SELECT year FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET altitude = (SELECT altitude FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET orientation = (SELECT orientation FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); + + +ALTER TABLE qwat_od.valve ALTER COLUMN fk_distributor SET NOT NULL; +ALTER TABLE qwat_od.valve ALTER COLUMN fk_precision SET NOT NULL; +ALTER TABLE qwat_od.valve ALTER COLUMN fk_status SET NOT NULL; +ALTER TABLE qwat_od.valve ALTER COLUMN geometry SET NOT NULL; + + +-- Valve orientation +CREATE OR REPLACE FUNCTION qwat_od.fn_valve_set_orientation(_valve_id integer) RETURNS void AS +$BODY$ + DECLARE + _pipeitem record; + _pipe_id integer; + _grouped record; + _diameter smallint; + _looppos integer := 0; + _orientation double precision := 0; + _orientation2 double precision := 0; + _node_geom geometry; + _pipe_geom geometry; + _sub_geom geometry; + _lin_ref float; + BEGIN + + -- get the geometry + _node_geom := geometry FROM qwat_od.valve WHERE id = _valve_id; + + -- count the active pipes associated to this valve + SELECT + COUNT(*) AS count, + bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible + INTO _grouped + FROM qwat_od.pipe + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id + WHERE (pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id)) + AND status.active IS TRUE; + + -- if not connected to any pipe, do nothing + IF _grouped.count <= 2 THEN + /* loop over them, and take the 2 first/last vertices + of the pipe to determine orientation (used for symbology) */ + FOR _pipeitem IN ( + SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, + ST_StartPoint(geometry) AS point_1, + ST_PointN(geometry,2) AS point_2 + FROM qwat_od.pipe + INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + WHERE pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id) AND status.active IS TRUE + ) LOOP + IF _looppos=0 THEN + -- first pipe + _diameter := _pipeitem.diameter; + _pipe_id := _pipeitem.id; + _looppos := 1; + _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); + ELSE + -- second pipe if exists + _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + _orientation2 := pi() + _orientation2; -- reverse angle + -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); + _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); + -- RAISE NOTICE 'mean: %', degrees(_orientation ); + -- reverse arrow according to diameter reduction + IF _pipeitem.diameter > _diameter THEN + _orientation := _orientation + pi(); + END IF; + END IF; + END LOOP; + END IF; + + -- update the valve table + UPDATE qwat_od.valve SET + orientation = degrees(_orientation) + --_pipe_schema_visible = _grouped.schema_visible + WHERE id = _valve_id; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation for a valve.'; + + +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_set_orientation() RETURNS TRIGGER AS +$BODY$ + BEGIN + PERFORM qwat_od.fn_valve_set_orientation(NEW.id); + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.ft_valve_set_orientation() IS 'Trigger: set orientation after inserting a valve.'; + +CREATE TRIGGER valve_set_orientation + AFTER INSERT ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); +COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; + + +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_add_pipe_vertex() + RETURNS trigger AS +$BODY$ + DECLARE + pipe_id integer; + BEGIN + -- add a vertex to the corresponding pipe if it intersects + -- when the valve is close enough to the pipe (< 1 micrometer) the valve is considered to intersect the pipe + -- it allows to deal with intersections that cannot be represented by floating point numbers + UPDATE qwat_od.pipe SET geometry = ST_Snap(geometry, NEW.geometry, 1e-6) WHERE ST_Distance(geometry, NEW.geometry) < 1e-6; + PERFORM qwat_od.fn_valve_set_orientation(NEW.id); + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; + +CREATE TRIGGER tr_valve_add_pipe_vertex_insert + AFTER INSERT + ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_add_pipe_vertex(); +COMMENT ON TRIGGER tr_valve_add_pipe_vertex_insert ON qwat_od.valve IS 'Trigger: updates auto fields after insert.'; + +CREATE TRIGGER tr_valve_add_pipe_vertex_update + AFTER UPDATE OF geometry + ON qwat_od.valve + FOR EACH ROW + WHEN (ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) + EXECUTE PROCEDURE qwat_od.ft_valve_add_pipe_vertex(); +COMMENT ON TRIGGER tr_valve_add_pipe_vertex_update ON qwat_od.valve IS 'Trigger: updates auto fields after geom update.'; + + +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_geom() RETURNS TRIGGER AS +$BODY$ + BEGIN + NEW.fk_pipe := qwat_od.fn_pipe_get_id(NEW.geometry); + NEW.fk_district := qwat_od.fn_get_district(NEW.geometry); + NEW.fk_pressurezone := qwat_od.fn_get_pressurezone(NEW.geometry); + RETURN NEW; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.ft_valve_geom() IS 'Trigger: when inserting or updating a valve, assign pipe and geom infos.'; + +CREATE TRIGGER tr_valve_infos_insert_trigger + BEFORE INSERT ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_geom(); +COMMENT ON TRIGGER tr_valve_infos_insert_trigger ON qwat_od.valve IS 'Trigger: when inserting a valve, assign pipe.'; + + +CREATE TRIGGER tr_valve_infos_update_trigger + BEFORE UPDATE ON qwat_od.valve + FOR EACH ROW + WHEN (NOT ST_Equals(OLD.geometry, NEW.geometry)) + EXECUTE PROCEDURE qwat_od.ft_valve_geom(); +COMMENT ON TRIGGER tr_valve_infos_update_trigger ON qwat_od.valve IS 'Trigger: when updating a valve, assign pipe.'; + + +-- =================================================== +CREATE OR REPLACE VIEW qwat_od.vw_search_view AS + /* ouvrages */SELECT + 'Ouvrages' as layer_name, + CASE + WHEN installation_type = 'source'::qwat_od.installation_type THEN 'Source ' || identification || ' ' || name + WHEN installation_type = 'treatment'::qwat_od.installation_type THEN 'Traitement ' || identification || ' ' || name + WHEN installation_type = 'tank'::qwat_od.installation_type THEN 'Réservoir ' || identification || ' ' || name + WHEN installation_type = 'pressurecontrol'::qwat_od.installation_type THEN 'Régulation de pression ' || identification || ' ' || name + WHEN installation_type = 'pump'::qwat_od.installation_type THEN 'Pompage ' || identification || ' ' || name + WHEN installation_type = 'chamber'::qwat_od.installation_type THEN 'Chambre ' || identification || ' ' || name + END AS search_text, + ST_Force2d(geometry) AS geometry + FROM qwat_od.vw_export_installation + WHERE status_active IS TRUE + UNION SELECT + 'Hydrantes' as layer_name, + district_name || ' '|| identification as search_text, + ST_Force2d(geometry) AS geometry + FROM qwat_od.vw_export_hydrant WHERE status_active IS TRUE + + UNION SELECT + 'Abonnés' as layer_name, + subscriber_type_value_fr || ' ' || coalesce(district_prefix||'_','') || identification || ' ' || district_name as search_text, + ST_Force2d(geometry) AS geometry + FROM qwat_od.vw_export_subscriber + + UNION SELECT + 'Compteur' as layer_name, + COALESCE(district_prefix||'_')||meter.identification AS search_text, + ST_Force2d(meter.geometry) AS geometry + FROM qwat_od.vw_export_meter meter; + + +CREATE OR REPLACE VIEW qwat_od.vw_valve_lines AS + SELECT valve.id, + ST_MakeLine(ST_Force2D(valve.handle_geometry), valve.geometry)::geometry(LineString, 21781) AS geometry + FROM qwat_od.valve + WHERE valve.handle_geometry IS NOT NULL AND valve.geometry IS NOT NULL; + +COMMENT ON VIEW qwat_od.vw_valve_lines IS +'Valves represented as lines. Each line is made from two points, the handle as starting point and the location on the pipe as ending point.'; + + + + +CREATE OR REPLACE FUNCTION qwat_od.fn_node_set_type(_node_id integer) RETURNS void AS +$BODY$ + DECLARE + _pipeitem record; + _pipe_id integer; + _grouped record; + _year integer; + _material varchar(50); + _diameter smallint; + _looppos integer := 0; + _type qwat_od.pipe_connection; + _orientation double precision := 0; + _orientation2 double precision := 0; + _node_geom geometry; + _pipe_geom geometry; + _sub_geom geometry; + _lin_ref float; + BEGIN + + -- get the geometry + _node_geom := geometry FROM qwat_od.node WHERE id = _node_id; + + -- count the active pipes associated to this node + SELECT + COUNT(pipe.id) AS count, + bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible + INTO _grouped + FROM qwat_od.pipe + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id + WHERE (fk_node_a = _node_id OR fk_node_b = _node_id) + AND status.active IS TRUE; + + -- if not connected to any pipe, delete the node if it is not something else (i.e. is not inherited) + IF _grouped.count = 0 THEN + -- check it is not associated to any pipe (including inactive ones) + IF _node_id NOT IN (SELECT fk_node_a FROM qwat_od.pipe UNION SELECT fk_node_b FROM qwat_od.pipe) THEN + -- if it is not something else + IF ( SELECT node_type = 'node'::qwat_od.node_type FROM qwat_od.vw_qwat_node WHERE id = _node_id) THEN + -- delete it + RAISE NOTICE 'Delete node %' , _node_id; + DELETE FROM qwat_od.node WHERE id = _node_id; -- delete on table level for safety (do not delete on the merge view) + RETURN; + END IF; + ELSE + _type := NULL::qwat_od.pipe_connection; + END IF; + -- if 1 or 2 pipes associated + ELSEIF _grouped.count <= 2 THEN + /* loop over them, and take the 2 first/last vertices + of the pipe to determine orientation (used for symbology) */ + FOR _pipeitem IN ( + SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, + ST_StartPoint(geometry) AS point_1, + ST_PointN(geometry,2) AS point_2 + FROM qwat_od.pipe + INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + WHERE fk_node_a = _node_id AND status.active IS TRUE + UNION ALL + SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, + ST_EndPoint(geometry) AS point_1, + ST_PointN(geometry,ST_NPoints(geometry)-1) AS point_2 + FROM qwat_od.pipe + INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + WHERE fk_node_b = _node_id AND status.active IS TRUE + ) LOOP + IF _looppos=0 THEN + -- first pipe + _type := 'pipe_end'::qwat_od.pipe_connection; + _year := _pipeitem.year; + _material := _pipeitem.material; + _diameter := _pipeitem.diameter; + _pipe_id := _pipeitem.id; + _looppos := 1; + _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); + ELSE + -- second pipe if exists + IF _material = _pipeitem.material AND _diameter = _pipeitem.diameter AND _year = _pipeitem.year THEN + _type := 'couple_same'::qwat_od.pipe_connection; + ELSIF _material = _pipeitem.material AND _diameter = _pipeitem.diameter THEN + _type := 'couple_year'::qwat_od.pipe_connection; + ELSIF _material = _pipeitem.material THEN + _type := 'couple_diameter'::qwat_od.pipe_connection; + ELSIF _diameter = _pipeitem.diameter THEN + _type := 'couple_material'::qwat_od.pipe_connection; + ELSE + _type := 'couple_other'; + END IF; + _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + _orientation2 := pi() + _orientation2; -- reverse angle + -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); + _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); + -- RAISE NOTICE 'mean: %', degrees(_orientation ); + -- reverse arrow according to diameter reduction + IF _pipeitem.diameter > _diameter THEN + _orientation := _orientation + pi(); + END IF; + END IF; + END LOOP; + -- more than 2 pipes connected, nothing to calculate + ELSEIF _grouped.count > 2 THEN + _type := 'T'::qwat_od.pipe_connection; + END IF; + + -- update the node table + UPDATE qwat_od.node SET + _pipe_node_type = _type, + _pipe_orientation = degrees(_orientation), + _pipe_schema_visible = _grouped.schema_visible + WHERE id = _node_id; + --RAISE NOTICE '% %' , _node_id , degrees(_orientation); + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation and type for a node. If three pipe arrives at the node: intersection. If one pipe: end. If two: depends on characteristics of pipe: year (is different), material (and year), diameter(and material/year)'; + + +--DROP VIEW qwat_od.vw_element_valve; + + From e367b75078619d0588a420b23e51e979bb2c0248 Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Mon, 26 Sep 2016 16:33:20 +0200 Subject: [PATCH 5/9] Temprary rebuild children for installation Missing fields for valve Rewrite views instead of create in conformity check Code cleaning & missing delta Drop unused trigger --- ordinary_data/functions/fn_node_create.sql | 23 +- ordinary_data/functions/fn_node_set_type.sql | 218 +++++++++--------- .../valve/fn_valve_set_orientation.sql | 139 ++++++----- ordinary_data/valve/od_valve.sql | 31 ++- ordinary_data/valve/tr_valve_pipe.sql | 9 +- ordinary_data/views/drop_views.sql | 1 + .../od_installation_inheritance.py | 2 +- update/delta/delta_1.1.0.sql | 116 +++++++--- 8 files changed, 306 insertions(+), 233 deletions(-) diff --git a/ordinary_data/functions/fn_node_create.sql b/ordinary_data/functions/fn_node_create.sql index 205f8dce..71f47348 100644 --- a/ordinary_data/functions/fn_node_create.sql +++ b/ordinary_data/functions/fn_node_create.sql @@ -6,22 +6,21 @@ CREATE OR REPLACE FUNCTION qwat_od.fn_node_create( _point geometry, deactivate_node_add_pipe_vertex boolean = FALSE ) RETURNS integer AS $BODY$ - DECLARE - _node_id integer; - BEGIN - SELECT id FROM qwat_od.node WHERE ST_Equals(ST_Force2d(_point), ST_Force2d(node.geometry)) IS TRUE LIMIT 1 INTO _node_id; - IF _node_id IS NULL THEN + DECLARE + _node_id integer; + BEGIN + SELECT id FROM qwat_od.node WHERE ST_Equals(ST_Force2d(_point), ST_Force2d(node.geometry)) IS TRUE LIMIT 1 INTO _node_id; + IF _node_id IS NULL THEN INSERT INTO qwat_od.node (geometry) VALUES (ST_Force3D(_point)) RETURNING id INTO _node_id; - IF _node_id IS NULL THEN - RAISE EXCEPTION 'Node is null although it should have been created'; - END IF; - END IF; - RETURN _node_id; - END; + IF _node_id IS NULL THEN + RAISE EXCEPTION 'Node is null although it should have been created'; + END IF; + END IF; + RETURN _node_id; + END; $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.fn_node_create(geometry, boolean) IS 'Returns the node for a given geometry (point). If node does not exist, create it.'; - diff --git a/ordinary_data/functions/fn_node_set_type.sql b/ordinary_data/functions/fn_node_set_type.sql index e6e41f65..16423ebb 100644 --- a/ordinary_data/functions/fn_node_set_type.sql +++ b/ordinary_data/functions/fn_node_set_type.sql @@ -9,119 +9,119 @@ /* node type: end, intersection, year, material, diameter */ CREATE OR REPLACE FUNCTION qwat_od.fn_node_set_type(_node_id integer) RETURNS void AS $BODY$ - DECLARE - _pipeitem record; - _pipe_id integer; - _grouped record; - _year integer; - _material varchar(50); - _diameter smallint; - _looppos integer := 0; - _type qwat_od.pipe_connection; - _orientation double precision := 0; - _orientation2 double precision := 0; - _node_geom geometry; - _pipe_geom geometry; - _sub_geom geometry; - _lin_ref float; - BEGIN + DECLARE + _pipeitem record; + _pipe_id integer; + _grouped record; + _year integer; + _material varchar(50); + _diameter smallint; + _looppos integer := 0; + _type qwat_od.pipe_connection; + _orientation double precision := 0; + _orientation2 double precision := 0; + _node_geom geometry; + _pipe_geom geometry; + _sub_geom geometry; + _lin_ref float; + BEGIN - -- get the geometry - _node_geom := geometry FROM qwat_od.node WHERE id = _node_id; + -- get the geometry + _node_geom := geometry FROM qwat_od.node WHERE id = _node_id; - -- count the functional pipes associated to this node - SELECT - COUNT(pipe.id) AS count, - bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible - INTO _grouped - FROM qwat_od.pipe - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id - WHERE (fk_node_a = _node_id OR fk_node_b = _node_id) - AND status.functional IS TRUE; + -- count the functional pipes associated to this node + SELECT + COUNT(pipe.id) AS count, + bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible + INTO _grouped + FROM qwat_od.pipe + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id + WHERE (fk_node_a = _node_id OR fk_node_b = _node_id) + AND status.functional IS TRUE; - -- if not connected to any pipe, delete the node if it is not something else (i.e. is not inherited) - IF _grouped.count = 0 THEN - -- check it is not associated to any pipe (including inactive ones) - IF _node_id NOT IN (SELECT fk_node_a FROM qwat_od.pipe UNION SELECT fk_node_b FROM qwat_od.pipe) THEN - -- if it is not something else - IF ( SELECT node_type = 'node'::qwat_od.node_type FROM qwat_od.vw_qwat_node WHERE id = _node_id) THEN - -- delete it - RAISE NOTICE 'Delete node %' , _node_id; - DELETE FROM qwat_od.node WHERE id = _node_id; -- delete on table level for safety (do not delete on the merge view) - RETURN; - END IF; - ELSE - _type := NULL::qwat_od.pipe_connection; - END IF; - -- if 1 or 2 pipes associated - ELSEIF _grouped.count <= 2 THEN - /* loop over them, and take the 2 first/last vertices - of the pipe to determine orientation (used for symbology) */ - FOR _pipeitem IN ( - SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, - ST_StartPoint(geometry) AS point_1, - ST_PointN(geometry,2) AS point_2 - FROM qwat_od.pipe - INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - WHERE fk_node_a = _node_id AND status.functional IS TRUE - UNION ALL - SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, - ST_EndPoint(geometry) AS point_1, - ST_PointN(geometry,ST_NPoints(geometry)-1) AS point_2 - FROM qwat_od.pipe - INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - WHERE fk_node_b = _node_id AND status.functional IS TRUE - ) LOOP - IF _looppos=0 THEN - -- first pipe - _type := 'pipe_end'::qwat_od.pipe_connection; - _year := _pipeitem.year; - _material := _pipeitem.material; - _diameter := _pipeitem.diameter; - _pipe_id := _pipeitem.id; - _looppos := 1; - _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); - ELSE - -- second pipe if exists - IF _material = _pipeitem.material AND _diameter = _pipeitem.diameter AND _year = _pipeitem.year THEN - _type := 'couple_same'::qwat_od.pipe_connection; - ELSIF _material = _pipeitem.material AND _diameter = _pipeitem.diameter THEN - _type := 'couple_year'::qwat_od.pipe_connection; - ELSIF _material = _pipeitem.material THEN - _type := 'couple_diameter'::qwat_od.pipe_connection; - ELSIF _diameter = _pipeitem.diameter THEN - _type := 'couple_material'::qwat_od.pipe_connection; - ELSE - _type := 'couple_other'; - END IF; - _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - _orientation2 := pi() + _orientation2; -- reverse angle - -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); - _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); - -- RAISE NOTICE 'mean: %', degrees(_orientation ); - -- reverse arrow according to diameter reduction - IF _pipeitem.diameter > _diameter THEN - _orientation := _orientation + pi(); - END IF; - END IF; - END LOOP; - -- more than 2 pipes connected, nothing to calculate - ELSEIF _grouped.count > 2 THEN - _type := 'T'::qwat_od.pipe_connection; - END IF; + -- if not connected to any pipe, delete the node if it is not something else (i.e. is not inherited) + IF _grouped.count = 0 THEN + -- check it is not associated to any pipe (including non functional ones) + IF _node_id NOT IN (SELECT fk_node_a FROM qwat_od.pipe UNION SELECT fk_node_b FROM qwat_od.pipe) THEN + -- if it is not something else + IF ( SELECT node_type = 'node'::qwat_od.node_type FROM qwat_od.vw_qwat_node WHERE id = _node_id) THEN + -- delete it + RAISE NOTICE 'Delete node %' , _node_id; + DELETE FROM qwat_od.node WHERE id = _node_id; -- delete on table level for safety (do not delete on the merge view) + RETURN; + END IF; + ELSE + _type := NULL::qwat_od.pipe_connection; + END IF; + -- if 1 or 2 pipes associated + ELSEIF _grouped.count <= 2 THEN + /* loop over them, and take the 2 first/last vertices + of the pipe to determine orientation (used for symbology) */ + FOR _pipeitem IN ( + SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, + ST_StartPoint(geometry) AS point_1, + ST_PointN(geometry,2) AS point_2 + FROM qwat_od.pipe + INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + WHERE fk_node_a = _node_id AND status.functional IS TRUE + UNION ALL + SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, + ST_EndPoint(geometry) AS point_1, + ST_PointN(geometry,ST_NPoints(geometry)-1) AS point_2 + FROM qwat_od.pipe + INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + WHERE fk_node_b = _node_id AND status.functional IS TRUE + ) LOOP + IF _looppos=0 THEN + -- first pipe + _type := 'pipe_end'::qwat_od.pipe_connection; + _year := _pipeitem.year; + _material := _pipeitem.material; + _diameter := _pipeitem.diameter; + _pipe_id := _pipeitem.id; + _looppos := 1; + _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); + ELSE + -- second pipe if exists + IF _material = _pipeitem.material AND _diameter = _pipeitem.diameter AND _year = _pipeitem.year THEN + _type := 'couple_same'::qwat_od.pipe_connection; + ELSIF _material = _pipeitem.material AND _diameter = _pipeitem.diameter THEN + _type := 'couple_year'::qwat_od.pipe_connection; + ELSIF _material = _pipeitem.material THEN + _type := 'couple_diameter'::qwat_od.pipe_connection; + ELSIF _diameter = _pipeitem.diameter THEN + _type := 'couple_material'::qwat_od.pipe_connection; + ELSE + _type := 'couple_other'; + END IF; + _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + _orientation2 := pi() + _orientation2; -- reverse angle + -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); + _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); + -- RAISE NOTICE 'mean: %', degrees(_orientation ); + -- reverse arrow according to diameter reduction + IF _pipeitem.diameter > _diameter THEN + _orientation := _orientation + pi(); + END IF; + END IF;+ + END LOOP; + -- more than 2 pipes connected, nothing to calculate + ELSEIF _grouped.count > 2 THEN + _type := 'T'::qwat_od.pipe_connection; + END IF; - -- update the node table - UPDATE qwat_od.node SET - _pipe_node_type = _type, - _pipe_orientation = degrees(_orientation), - _pipe_schema_visible = _grouped.schema_visible - WHERE id = _node_id; - --RAISE NOTICE '% %' , _node_id , degrees(_orientation); - END; + -- update the node table + UPDATE qwat_od.node SET + _pipe_node_type = _type, + _pipe_orientation = degrees(_orientation), + _pipe_schema_visible = _grouped.schema_visible + WHERE id = _node_id; + --RAISE NOTICE '% %' , _node_id , degrees(_orientation); + END; $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation and type for a node. If three pipe arrives at the node: intersection. If one pipe: end. If two: depends on characteristics of pipe: year (is different), material (and year), diameter(and material/year)'; diff --git a/ordinary_data/valve/fn_valve_set_orientation.sql b/ordinary_data/valve/fn_valve_set_orientation.sql index cde6beb1..981c39c5 100644 --- a/ordinary_data/valve/fn_valve_set_orientation.sql +++ b/ordinary_data/valve/fn_valve_set_orientation.sql @@ -1,85 +1,82 @@ /* - qWat - QGIS Water Module + qWat - QGIS Water Module - SQL file :: valve functions + SQL file :: valve functions */ /* define valve orientation type */ CREATE OR REPLACE FUNCTION qwat_od.fn_valve_set_orientation(_valve_id integer) RETURNS void AS $BODY$ - DECLARE - _pipeitem record; - _pipe_id integer; - _grouped record; - _diameter smallint; - _looppos integer := 0; - _orientation double precision := 0; - _orientation2 double precision := 0; - _node_geom geometry; - _pipe_geom geometry; - _sub_geom geometry; - _lin_ref float; - BEGIN + DECLARE + _pipeitem record; + _pipe_id integer; + _grouped record; + _diameter smallint; + _looppos integer := 0; + _orientation double precision := 0; + _orientation2 double precision := 0; + _node_geom geometry; + _pipe_geom geometry; + _sub_geom geometry; + _lin_ref float; + BEGIN - -- get the geometry - _node_geom := geometry FROM qwat_od.valve WHERE id = _valve_id; + -- get the geometry + _node_geom := geometry FROM qwat_od.valve WHERE id = _valve_id; - -- count the active pipes associated to this valve - SELECT - COUNT(*) AS count, - bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible - INTO _grouped - FROM qwat_od.pipe - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id - WHERE (pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id)) - AND status.active IS TRUE; + -- count the active pipes associated to this valve + SELECT + COUNT(*) AS count, + bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible + INTO _grouped + FROM qwat_od.pipe + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id + WHERE (pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id)) + AND status.active IS TRUE; - -- if not connected to any pipe, do nothing - IF _grouped.count <= 2 THEN - /* loop over them, and take the 2 first/last vertices - of the pipe to determine orientation (used for symbology) */ - FOR _pipeitem IN ( - SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, - ST_StartPoint(geometry) AS point_1, - ST_PointN(geometry,2) AS point_2 - FROM qwat_od.pipe - INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - WHERE pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id) AND status.active IS TRUE - ) LOOP - IF _looppos=0 THEN - -- first pipe - _diameter := _pipeitem.diameter; - _pipe_id := _pipeitem.id; - _looppos := 1; - _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); - ELSE - -- second pipe if exists - _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - _orientation2 := pi() + _orientation2; -- reverse angle - -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); - _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); - -- RAISE NOTICE 'mean: %', degrees(_orientation ); - -- reverse arrow according to diameter reduction - IF _pipeitem.diameter > _diameter THEN - _orientation := _orientation + pi(); - END IF; - END IF; - END LOOP; - END IF; + -- if not connected to any pipe, do nothing + IF _grouped.count <= 2 THEN + /* loop over them, and take the 2 first/last vertices + of the pipe to determine orientation (used for symbology) */ + FOR _pipeitem IN ( + SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, + ST_StartPoint(geometry) AS point_1, + ST_PointN(geometry,2) AS point_2 + FROM qwat_od.pipe + INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id + INNER JOIN qwat_vl.status ON pipe.fk_status = status.id + WHERE pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id) AND status.active IS TRUE + ) LOOP + IF _looppos=0 THEN + -- first pipe + _diameter := _pipeitem.diameter; + _pipe_id := _pipeitem.id; + _looppos := 1; + _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); + ELSE + -- second pipe if exists + _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); + _orientation2 := pi() + _orientation2; -- reverse angle + -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); + _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); + -- RAISE NOTICE 'mean: %', degrees(_orientation ); + -- reverse arrow according to diameter reduction + IF _pipeitem.diameter > _diameter THEN + _orientation := _orientation + pi(); + END IF; + END IF; + END LOOP; + END IF; - -- update the valve table - UPDATE qwat_od.valve SET - orientation = degrees(_orientation) - --_pipe_schema_visible = _grouped.schema_visible - WHERE id = _valve_id; - END; + -- update the valve table + UPDATE qwat_od.valve SET + orientation = degrees(_orientation) + --_pipe_schema_visible = _grouped.schema_visible + WHERE id = _valve_id; + END; $BODY$ LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation for a valve.'; - - - +COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation for a valve.'; \ No newline at end of file diff --git a/ordinary_data/valve/od_valve.sql b/ordinary_data/valve/od_valve.sql index a749a593..6f1f5cba 100644 --- a/ordinary_data/valve/od_valve.sql +++ b/ordinary_data/valve/od_valve.sql @@ -9,8 +9,6 @@ CREATE TABLE qwat_od.valve (id serial PRIMARY KEY); COMMENT ON TABLE qwat_od.valve IS 'Table for valve.'; /* columns */ -ALTER TABLE qwat_od.valve ADD COLUMN fk_district integer; -ALTER TABLE qwat_od.valve ADD COLUMN fk_pressurezone integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_valve_type integer not null; ALTER TABLE qwat_od.valve ADD COLUMN fk_valve_function integer not null; ALTER TABLE qwat_od.valve ADD COLUMN fk_valve_actuation integer not null; @@ -18,23 +16,34 @@ ALTER TABLE qwat_od.valve ADD COLUMN fk_pipe integer ; ALTER TABLE qwat_od.valve ADD COLUMN fk_handle_precision integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_handle_precisionalti integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_maintenance integer[]; --TODO should use n:m relations! +ALTER TABLE qwat_od.valve ADD COLUMN diameter_nominal varchar(10); +ALTER TABLE qwat_od.valve ADD COLUMN closed boolean default false; +ALTER TABLE qwat_od.valve ADD COLUMN networkseparation boolean default false; +ALTER TABLE qwat_od.valve ADD COLUMN handle_altitude decimal(10,3); +ALTER TABLE qwat_od.valve ADD COLUMN handle_geometry geometry(PointZ,:SRID); +ALTER TABLE qwat_od.valve ADD COLUMN fk_district integer; +ALTER TABLE qwat_od.valve ADD COLUMN fk_pressurezone integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_distributor integer not null; ALTER TABLE qwat_od.valve ADD COLUMN fk_precision integer not null; ALTER TABLE qwat_od.valve ADD COLUMN fk_precisionalti integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_status integer not null; ALTER TABLE qwat_od.valve ADD COLUMN fk_object_reference integer; ALTER TABLE qwat_od.valve ADD COLUMN fk_folder integer; -ALTER TABLE qwat_od.valve ADD COLUMN diameter_nominal varchar(10); -ALTER TABLE qwat_od.valve ADD COLUMN closed boolean default false; -ALTER TABLE qwat_od.valve ADD COLUMN networkseparation boolean default false; -ALTER TABLE qwat_od.valve ADD COLUMN handle_altitude decimal(10,3); -ALTER TABLE qwat_od.valve ADD COLUMN handle_geometry geometry(PointZ,:SRID); ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); +ALTER TABLE qwat_od.valve ADD COLUMN year_end smallint CHECK (year_end IS NULL OR year_end > 1800 AND year_end < 2100); ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; ALTER TABLE qwat_od.valve ADD COLUMN orientation float default null; +ALTER TABLE qwat_od.valve ADD COLUMN fk_locationtype integer[]; +ALTER TABLE qwat_od.valve ADD COLUMN identification varchar(50); +ALTER TABLE qwat_od.valve ADD COLUMN remark text; +ALTER TABLE qwat_od.valve ADD COLUMN fk_printmap integer[]; +ALTER TABLE qwat_od.valve ADD COLUMN _geometry_alt1_used boolean; +ALTER TABLE qwat_od.valve ADD COLUMN _geometry_alt2_used boolean; +ALTER TABLE qwat_od.valve ADD COLUMN _pipe_node_type qwat_od.pipe_connection default null; +ALTER TABLE qwat_od.valve ADD COLUMN _pipe_orientation float default 0; +ALTER TABLE qwat_od.valve ADD COLUMN _pipe_schema_visible boolean default false; +ALTER TABLE qwat_od.valve ADD COLUMN _printmaps text; -- list of printmap where it is included -/* Schema view */ -SELECT qwat_sys.fn_enable_schemaview( 'valve' ); /* constraints */ ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_type FOREIGN KEY (fk_valve_type) REFERENCES qwat_vl.valve_type(id) MATCH FULL; CREATE INDEX fki_valve_fk_type ON qwat_od.valve(fk_valve_type); @@ -68,6 +77,10 @@ ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt1 boolean default null; ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated +/* Schema view */ +SELECT qwat_sys.fn_enable_schemaview( 'valve' ); + + /* GEOM INDEXES */ CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); diff --git a/ordinary_data/valve/tr_valve_pipe.sql b/ordinary_data/valve/tr_valve_pipe.sql index c60adfdc..8896addf 100644 --- a/ordinary_data/valve/tr_valve_pipe.sql +++ b/ordinary_data/valve/tr_valve_pipe.sql @@ -21,7 +21,6 @@ $BODY$ ) AS valve_group ON pipe_dupp.id = valve_group.fk_pipe WHERE pipe.id = _pipe_id; - END; $BODY$ LANGUAGE plpgsql; @@ -31,10 +30,10 @@ LANGUAGE plpgsql; /* REASSIGN THE PIPE OF A VALVE WHEN THE PIPE MOVES OR IS DELETED */ CREATE OR REPLACE FUNCTION qwat_od.ft_valve_pipe_update() RETURNS TRIGGER AS $BODY$ - BEGIN - UPDATE qwat_od.valve SET fk_pipe = qwat_od.fn_pipe_get_id(geometry) WHERE fk_pipe = OLD.id OR ST_Distance(geometry, OLD.geometry) < 1e-4; - RETURN NULL; - END; + BEGIN + UPDATE qwat_od.valve SET fk_pipe = qwat_od.fn_pipe_get_id(geometry) WHERE fk_pipe = OLD.id OR ST_Distance(geometry, OLD.geometry) < 1e-4; + RETURN NULL; + END; $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.ft_valve_pipe_update() IS 'Trigger: when moving or deleting a pipe, reassign the pipe to all valves connected to the old pipe. Do an AFTER trigger since it will update valve after updating the node.'; diff --git a/ordinary_data/views/drop_views.sql b/ordinary_data/views/drop_views.sql index 9d6535e4..9f9ef90f 100644 --- a/ordinary_data/views/drop_views.sql +++ b/ordinary_data/views/drop_views.sql @@ -35,6 +35,7 @@ drop view if exists qwat_od.vw_element_meter; drop view if exists qwat_od.vw_element_part; drop view if exists qwat_od.vw_element_samplingpoint; drop view if exists qwat_od.vw_element_subscriber; +drop view if exists qwat_od.vw_element_valve; drop view if exists qwat_od.vw_installation_chamber; drop view if exists qwat_od.vw_installation_pressurecontrol; diff --git a/ordinary_data/views/inheritance/od_installation_inheritance.py b/ordinary_data/views/inheritance/od_installation_inheritance.py index 21841bec..841d56cc 100755 --- a/ordinary_data/views/inheritance/od_installation_inheritance.py +++ b/ordinary_data/views/inheritance/od_installation_inheritance.py @@ -18,7 +18,7 @@ table: qwat_od.installation pkey: id pkey_value: NEW.id -generate_child_views: False +generate_child_views: True allow_type_change: false schema: qwat_od diff --git a/update/delta/delta_1.1.0.sql b/update/delta/delta_1.1.0.sql index c72e16ba..33126f76 100644 --- a/update/delta/delta_1.1.0.sql +++ b/update/delta/delta_1.1.0.sql @@ -14,6 +14,10 @@ ALTER TABLE qwat_od.valve ADD COLUMN fk_precision integer; ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precision FOREIGN KEY (fk_precision) REFERENCES qwat_vl.precision(id) MATCH FULL; CREATE INDEX fki_valve_fk_precision ON qwat_od.valve(fk_precision); +ALTER TABLE qwat_od.valve ADD COLUMN fk_precisionalti integer; +ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precisionalti FOREIGN KEY (fk_precisionalti) REFERENCES qwat_vl.precisionalti(id) MATCH FULL; +CREATE INDEX fki_valve_fk_precisionalti ON qwat_od.valve(fk_precisionalti); + ALTER TABLE qwat_od.valve ADD COLUMN fk_status integer; ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_status FOREIGN KEY (fk_status) REFERENCES qwat_vl.status(id) MATCH FULL; CREATE INDEX fki_valve_fk_status ON qwat_od.valve(fk_status); @@ -26,9 +30,20 @@ ALTER TABLE qwat_od.valve ADD COLUMN fk_folder integer; ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_folder FOREIGN KEY (fk_folder) REFERENCES qwat_od.folder(id) MATCH FULL; CREATE INDEX fki_valve_fk_folder ON qwat_od.valve(fk_folder); -ALTER TABLE qwat_od.valve ADD COLUMN fk_precisionalti integer; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precisionalti FOREIGN KEY (fk_precisionalti) REFERENCES qwat_vl.precisionalti(id) MATCH FULL; -CREATE INDEX fki_valve_fk_precisionalti ON qwat_od.valve(fk_precisionalti); +ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); +ALTER TABLE qwat_od.valve ADD COLUMN year_end smallint CHECK (year_end IS NULL OR year_end > 1800 AND year_end < 2100); +ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; +ALTER TABLE qwat_od.valve ADD COLUMN orientation float default null; +ALTER TABLE qwat_od.valve ADD COLUMN fk_locationtype integer[]; +ALTER TABLE qwat_od.valve ADD COLUMN identification varchar(50); +ALTER TABLE qwat_od.valve ADD COLUMN remark text; +ALTER TABLE qwat_od.valve ADD COLUMN fk_printmap integer[]; +ALTER TABLE qwat_od.valve ADD COLUMN _geometry_alt1_used boolean; +ALTER TABLE qwat_od.valve ADD COLUMN _geometry_alt2_used boolean; +ALTER TABLE qwat_od.valve ADD COLUMN _pipe_node_type qwat_od.pipe_connection default null; +ALTER TABLE qwat_od.valve ADD COLUMN _pipe_orientation float default 0; +ALTER TABLE qwat_od.valve ADD COLUMN _pipe_schema_visible boolean default false; +ALTER TABLE qwat_od.valve ADD COLUMN _printmaps text; -- list of printmap where it is included ALTER TABLE qwat_od.valve ADD COLUMN geometry geometry('POINTZ',21781); ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt1 geometry('POINTZ',21781); @@ -36,36 +51,49 @@ ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt2 geometry('POINTZ',21781); ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt1 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated + + CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); CREATE INDEX valve_geoidx_alt2 ON qwat_od.valve USING GIST ( geometry_alt2 ); -- ALTER TABLE qwat_od.valve ALTER COLUMN id serial; -- integer NOT NULL REFERENCES qwat_od.network_element(id) PRIMARY KEY; +CREATE SEQUENCE qwat_od.valve_id_seq START 1; +SELECT setval('qwat_od.valve_id_seq', (select COALESCE(max(id), '0')+1 from qwat_od.valve)); +ALTER TABLE qwat_od.valve ALTER COLUMN id SET default nextval('qwat_od.valve_id_seq'); -ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); -ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; -ALTER TABLE qwat_od.valve ADD COLUMN orientation float default null; SELECT qwat_sys.fn_enable_schemaview( 'valve' ); -- TODO We need to tranfert all the column data from node to valve -UPDATE qwat_od.valve SET fk_district = (SELECT fk_district FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_pressurezone = (SELECT fk_pressurezone FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_district = (SELECT fk_district FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_pressurezone = (SELECT fk_pressurezone FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); UPDATE qwat_od.valve SET fk_distributor = (SELECT fk_distributor FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_precision = (SELECT fk_precision FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_status = (SELECT fk_status FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_object_reference = (SELECT fk_object_reference FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_folder = (SELECT fk_folder FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_precisionalti = (SELECT fk_precisionalti FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET geometry = (SELECT geometry FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET geometry_alt1 = (SELECT geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET geometry_alt2 = (SELECT geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET update_geometry_alt1 = (SELECT update_geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET update_geometry_alt2 = (SELECT update_geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET year = (SELECT year FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET altitude = (SELECT altitude FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET orientation = (SELECT orientation FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_precision = (SELECT fk_precision FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_status = (SELECT fk_status FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_object_reference = (SELECT fk_object_reference FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_folder = (SELECT fk_folder FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_precisionalti = (SELECT fk_precisionalti FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry = (SELECT geometry FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry_alt1 = (SELECT geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry_alt2 = (SELECT geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET update_geometry_alt1 = (SELECT update_geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET update_geometry_alt2 = (SELECT update_geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET year = (SELECT year FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET altitude = (SELECT altitude FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET orientation = (SELECT orientation FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_locationtype = (SELECT fk_locationtype FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET identification = (SELECT identification FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET remark = (SELECT remark FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET year_end = (SELECT year_end FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_printmap = (SELECT fk_printmap FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _geometry_alt1_used = (SELECT _geometry_alt1_used FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _geometry_alt2_used = (SELECT _geometry_alt2_used FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _pipe_node_type = (SELECT _pipe_node_type FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _pipe_orientation = (SELECT _pipe_orientation FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _pipe_schema_visible = (SELECT _pipe_schema_visible FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _printmaps = (SELECT _printmaps FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); ALTER TABLE qwat_od.valve ALTER COLUMN fk_distributor SET NOT NULL; @@ -73,6 +101,30 @@ ALTER TABLE qwat_od.valve ALTER COLUMN fk_precision SET NOT NULL; ALTER TABLE qwat_od.valve ALTER COLUMN fk_status SET NOT NULL; ALTER TABLE qwat_od.valve ALTER COLUMN geometry SET NOT NULL; +ALTER TABLE qwat_od.valve DROP CONSTRAINT valve_id_fkey; + + + +CREATE OR REPLACE FUNCTION qwat_od.fn_node_create( _point geometry, deactivate_node_add_pipe_vertex boolean = FALSE ) RETURNS integer AS +$BODY$ + DECLARE + _node_id integer; + BEGIN + SELECT id FROM qwat_od.node WHERE ST_Equals(ST_Force2d(_point), ST_Force2d(node.geometry)) IS TRUE LIMIT 1 INTO _node_id; + IF _node_id IS NULL THEN + + INSERT INTO qwat_od.node (geometry) VALUES (ST_Force3D(_point)) RETURNING id INTO _node_id; + + IF _node_id IS NULL THEN + RAISE EXCEPTION 'Node is null although it should have been created'; + END IF; + END IF; + RETURN _node_id; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.fn_node_create(geometry, boolean) IS 'Returns the node for a given geometry (point). If node does not exist, create it.'; + -- Valve orientation CREATE OR REPLACE FUNCTION qwat_od.fn_valve_set_orientation(_valve_id integer) RETURNS void AS @@ -150,7 +202,7 @@ $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation for a valve.'; - +/* CREATE OR REPLACE FUNCTION qwat_od.ft_valve_set_orientation() RETURNS TRIGGER AS $BODY$ BEGIN @@ -166,7 +218,7 @@ CREATE TRIGGER valve_set_orientation FOR EACH ROW EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; - +*/ CREATE OR REPLACE FUNCTION qwat_od.ft_valve_add_pipe_vertex() RETURNS trigger AS @@ -227,6 +279,16 @@ CREATE TRIGGER tr_valve_infos_update_trigger COMMENT ON TRIGGER tr_valve_infos_update_trigger ON qwat_od.valve IS 'Trigger: when updating a valve, assign pipe.'; +CREATE OR REPLACE FUNCTION qwat_od.ft_valve_pipe_update() RETURNS TRIGGER AS +$BODY$ + BEGIN + UPDATE qwat_od.valve SET fk_pipe = qwat_od.fn_pipe_get_id(geometry) WHERE fk_pipe = OLD.id OR ST_Distance(geometry, OLD.geometry) < 1e-4; + RETURN NULL; + END; +$BODY$ +LANGUAGE plpgsql; +COMMENT ON FUNCTION qwat_od.ft_valve_pipe_update() IS 'Trigger: when moving or deleting a pipe, reassign the pipe to all valves connected to the old pipe. Do an AFTER trigger since it will update valve after updating the node.'; + -- =================================================== CREATE OR REPLACE VIEW qwat_od.vw_search_view AS /* ouvrages */SELECT @@ -392,7 +454,9 @@ $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation and type for a node. If three pipe arrives at the node: intersection. If one pipe: end. If two: depends on characteristics of pipe: year (is different), material (and year), diameter(and material/year)'; +DROP TRIGGER tr_node_add_pipe_vertex_insert ON qwat_od.node; +DROP TRIGGER tr_node_add_pipe_vertex_update ON qwat_od.node; +DROP FUNCTION qwat_od.ft_node_add_pipe_vertex(); ---DROP VIEW qwat_od.vw_element_valve; - - +DROP TRIGGER valve_node_set_type ON qwat_od.valve; +DROP FUNCTION qwat_od.ft_valve_node_set_type(); From d6dd40b822ecec8eb778c38820fdc8914b684408 Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Tue, 27 Sep 2016 14:26:07 +0200 Subject: [PATCH 6/9] Recalculate valve orientation when pipe is moved Incorrect delta file Fixing od_install_inheritance file for walves Missing DO PERFORM instead of select --- ordinary_data/valve/od_valve.sql | 3 +- ordinary_data/valve/tr_valve_pipe.sql | 18 +++-- ordinary_data/views/export/search_view.sql | 8 ++- .../od_installation_inheritance.py | 1 + update/delta/delta_0.1.1.sql | 68 ------------------- update/delta/delta_1.1.0.sql | 38 ++++++++++- 6 files changed, 57 insertions(+), 79 deletions(-) delete mode 100644 update/delta/delta_0.1.1.sql diff --git a/ordinary_data/valve/od_valve.sql b/ordinary_data/valve/od_valve.sql index 6f1f5cba..c5559a94 100644 --- a/ordinary_data/valve/od_valve.sql +++ b/ordinary_data/valve/od_valve.sql @@ -78,8 +78,7 @@ ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; /* Schema view */ -SELECT qwat_sys.fn_enable_schemaview( 'valve' ); - +DO $$ BEGIN PERFORM qwat_sys.fn_enable_schemaview('valve'); END $$; /* GEOM INDEXES */ CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); diff --git a/ordinary_data/valve/tr_valve_pipe.sql b/ordinary_data/valve/tr_valve_pipe.sql index 8896addf..0ceb105e 100644 --- a/ordinary_data/valve/tr_valve_pipe.sql +++ b/ordinary_data/valve/tr_valve_pipe.sql @@ -27,16 +27,25 @@ LANGUAGE plpgsql; -/* REASSIGN THE PIPE OF A VALVE WHEN THE PIPE MOVES OR IS DELETED */ +/* REASSIGN THE PIPE OF A VALVE WHEN THE PIPE MOVES OR IS DELETED, AND RECALCULATE VALVE ORIENTATION */ CREATE OR REPLACE FUNCTION qwat_od.ft_valve_pipe_update() RETURNS TRIGGER AS $BODY$ + DECLARE + r record; BEGIN UPDATE qwat_od.valve SET fk_pipe = qwat_od.fn_pipe_get_id(geometry) WHERE fk_pipe = OLD.id OR ST_Distance(geometry, OLD.geometry) < 1e-4; + + -- Il faudrait un trigger sur un changement de géom sur les conduites qui appellent valve_set_orientation pour toutes les vannes avec fk_pipe = id. + FOR r IN SELECT id FROM qwat_od.valve WHERE fk_pipe = OLD.id + LOOP + PERFORM qwat_od.fn_valve_set_orientation(r.id); + END LOOP; + RETURN NULL; END; $BODY$ LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.ft_valve_pipe_update() IS 'Trigger: when moving or deleting a pipe, reassign the pipe to all valves connected to the old pipe. Do an AFTER trigger since it will update valve after updating the node.'; +COMMENT ON FUNCTION qwat_od.ft_valve_pipe_update() IS 'Trigger: when moving or deleting a pipe, reassign the pipe to all valves connected to the old pipe and recalculate valve orientation. Do an AFTER trigger since it will update valve after updating the node.'; /* WHEN THE PIPE MOVES */ CREATE TRIGGER tr_valve_pipe_update -- this will be fired for every node, although not every node is valve @@ -44,15 +53,14 @@ CREATE TRIGGER tr_valve_pipe_update FOR EACH ROW WHEN ( ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) EXECUTE PROCEDURE qwat_od.ft_valve_pipe_update(); -COMMENT ON TRIGGER tr_valve_pipe_update ON qwat_od.pipe IS 'Trigger: when moving a pipe, reassign the pipe to all valves connected to the old pipe. Do an AFTER trigger since it will update valve after updating the node.'; +COMMENT ON TRIGGER tr_valve_pipe_update ON qwat_od.pipe IS 'Trigger: when moving a pipe, reassign the pipe to all valves connected to the old pipe and recalculate valve orientation. Do an AFTER trigger since it will update valve after updating the node.'; /* WHEN THE PIPE IS DELETED */ CREATE TRIGGER tr_valve_pipe_delete -- this will be fired for every node, although not every node is valve AFTER DELETE ON qwat_od.pipe FOR EACH ROW EXECUTE PROCEDURE qwat_od.ft_valve_pipe_update(); -COMMENT ON TRIGGER tr_valve_pipe_delete ON qwat_od.pipe IS 'Trigger: when deleting a pipe, reassign the pipe to all valves connected to the old pipe. Do an AFTER trigger since it will update valve after updating the node.'; - +COMMENT ON TRIGGER tr_valve_pipe_delete ON qwat_od.pipe IS 'Trigger: when deleting a pipe, reassign the pipe to all valves connected to the old pipe and recalculate valve orientation. Do an AFTER trigger since it will update valve after updating the node.'; diff --git a/ordinary_data/views/export/search_view.sql b/ordinary_data/views/export/search_view.sql index ea2b7116..9658af01 100644 --- a/ordinary_data/views/export/search_view.sql +++ b/ordinary_data/views/export/search_view.sql @@ -31,4 +31,10 @@ CREATE OR REPLACE VIEW qwat_od.vw_search_view AS 'Compteur' as layer_name, COALESCE(district_prefix||'_')||meter.identification AS search_text, ST_Force2d(meter.geometry) AS geometry - FROM qwat_od.vw_export_meter meter; + FROM qwat_od.vw_export_meter meter + + UNION SELECT + 'Vannes' as layer_name, + valve_function_value_fr || ' ' || identification || ' ' || district_name as search_text, + ST_Force2d(geometry) AS geometry + FROM qwat_od.vw_export_valve WHERE identification IS NOT NULL; \ No newline at end of file diff --git a/ordinary_data/views/inheritance/od_installation_inheritance.py b/ordinary_data/views/inheritance/od_installation_inheritance.py index 841d56cc..44ddc4da 100755 --- a/ordinary_data/views/inheritance/od_installation_inheritance.py +++ b/ordinary_data/views/inheritance/od_installation_inheritance.py @@ -19,6 +19,7 @@ pkey: id pkey_value: NEW.id generate_child_views: True + allow_type_change: false schema: qwat_od diff --git a/update/delta/delta_0.1.1.sql b/update/delta/delta_0.1.1.sql deleted file mode 100644 index fdd8dbc9..00000000 --- a/update/delta/delta_0.1.1.sql +++ /dev/null @@ -1,68 +0,0 @@ -ALTER TABLE qwat_od.valve ADD COLUMN fk_district integer; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_district FOREIGN KEY (fk_district) REFERENCES qwat_od.district(id) MATCH FULL; -CREATE INDEX fki_valve_fk_district ON qwat_od.valve(fk_district); - -ALTER TABLE qwat_od.valve ADD COLUMN fk_pressurezone integer; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_pressurezone FOREIGN KEY (fk_pressurezone) REFERENCES qwat_od.pressurezone(id) MATCH FULL; -CREATE INDEX fki_valve_fk_pressurezone ON qwat_od.valve(fk_pressurezone); - -ALTER TABLE qwat_od.valve ADD COLUMN fk_distributor integer not null; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_distributor FOREIGN KEY (fk_distributor) REFERENCES qwat_od.distributor(id) MATCH FULL; -CREATE INDEX fki_valve_fk_distributor ON qwat_od.valve(fk_distributor); - -ALTER TABLE qwat_od.valve ADD COLUMN fk_precision integer not null; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precision FOREIGN KEY (fk_precision) REFERENCES qwat_vl.precision(id) MATCH FULL; -CREATE INDEX fki_valve_fk_precision ON qwat_od.valve(fk_precision); - -ALTER TABLE qwat_od.valve ADD COLUMN fk_status integer not null; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_status FOREIGN KEY (fk_status) REFERENCES qwat_vl.status(id) MATCH FULL; -CREATE INDEX fki_valve_fk_status ON qwat_od.valve(fk_status); - -ALTER TABLE qwat_od.valve ADD COLUMN fk_object_reference integer; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_object_reference FOREIGN KEY (fk_object_reference) REFERENCES qwat_vl.object_reference(id) MATCH FULL; -CREATE INDEX fki_valve_fk_object_reference ON qwat_od.valve(fk_object_reference); - -ALTER TABLE qwat_od.valve ADD COLUMN fk_folder integer; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_folder FOREIGN KEY (fk_folder) REFERENCES qwat_od.folder(id) MATCH FULL; -CREATE INDEX fki_valve_fk_folder ON qwat_od.valve(fk_folder); - -ALTER TABLE qwat_od.valve ADD COLUMN fk_precisionalti integer; -ALTER TABLE qwat_od.valve ADD CONSTRAINT valve_fk_precisionalti FOREIGN KEY (fk_precisionalti) REFERENCES qwat_vl.precisionalti(id) MATCH FULL; -CREATE INDEX fki_valve_fk_precisionalti ON qwat_od.valve(fk_precisionalti); - -ALTER TABLE qwat_od.valve ADD COLUMN geometry geometry('POINTZ',:SRID) NOT NULL; -ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt1 geometry('POINTZ',:SRID); -ALTER TABLE qwat_od.valve ADD COLUMN geometry_alt2 geometry('POINTZ',:SRID); -ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt1 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated -ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; -- used to determine if alternative geometries should be updated when main geometry is updated - -CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); -CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); -CREATE INDEX valve_geoidx_alt2 ON qwat_od.valve USING GIST ( geometry_alt2 ); - -ALTER TABLE qwat_od.valve ALTER COLUMN id serial; --- integer NOT NULL REFERENCES qwat_od.network_element(id) PRIMARY KEY; - -ALTER TABLE qwat_od.valve ADD COLUMN year smallint CHECK (year IS NULL OR year > 1800 AND year < 2100); -ALTER TABLE qwat_od.valve ADD COLUMN altitude decimal(10,3) default null; -ALTER TABLE qwat_od.valve ADD COLUMN orientation float default null; - -SELECT qwat_sys.fn_enable_schemaview( 'valve' ); - --- Valve orientation -CREATE OR REPLACE FUNCTION qwat_od.ft_valve_set_orientation() RETURNS TRIGGER AS -$BODY$ - BEGIN - PERFORM qwat_od.fn_valve_set_orientation(NEW.id); - RETURN NEW; - END; -$BODY$ -LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.ft_valve_set_orientation() IS 'Trigger: set orientation after inserting a valve.'; - -CREATE TRIGGER valve_set_orientation - AFTER INSERT ON qwat_od.valve - FOR EACH ROW - EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); -COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; - diff --git a/update/delta/delta_1.1.0.sql b/update/delta/delta_1.1.0.sql index 33126f76..efec2297 100644 --- a/update/delta/delta_1.1.0.sql +++ b/update/delta/delta_1.1.0.sql @@ -60,11 +60,12 @@ CREATE INDEX valve_geoidx_alt2 ON qwat_od.valve USING GIST ( geometry_alt2 ); -- ALTER TABLE qwat_od.valve ALTER COLUMN id serial; -- integer NOT NULL REFERENCES qwat_od.network_element(id) PRIMARY KEY; CREATE SEQUENCE qwat_od.valve_id_seq START 1; -SELECT setval('qwat_od.valve_id_seq', (select COALESCE(max(id), '0')+1 from qwat_od.valve)); +-- SELECT setval('qwat_od.valve_id_seq', (select COALESCE(max(id), '0')+1 from qwat_od.valve)); +DO $$ BEGIN PERFORM setval('qwat_od.valve_id_seq', (select COALESCE(max(id), '0')+1 from qwat_od.valve)); END $$; ALTER TABLE qwat_od.valve ALTER COLUMN id SET default nextval('qwat_od.valve_id_seq'); -SELECT qwat_sys.fn_enable_schemaview( 'valve' ); +DO $$ BEGIN PERFORM qwat_sys.fn_enable_schemaview('valve'); END $$; -- TODO We need to tranfert all the column data from node to valve UPDATE qwat_od.valve SET fk_district = (SELECT fk_district FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); @@ -279,15 +280,43 @@ CREATE TRIGGER tr_valve_infos_update_trigger COMMENT ON TRIGGER tr_valve_infos_update_trigger ON qwat_od.valve IS 'Trigger: when updating a valve, assign pipe.'; +/* REASSIGN THE PIPE OF A VALVE WHEN THE PIPE MOVES OR IS DELETED, AND RECALCULATE VALVE ORIENTATION */ CREATE OR REPLACE FUNCTION qwat_od.ft_valve_pipe_update() RETURNS TRIGGER AS $BODY$ + DECLARE + r record; BEGIN UPDATE qwat_od.valve SET fk_pipe = qwat_od.fn_pipe_get_id(geometry) WHERE fk_pipe = OLD.id OR ST_Distance(geometry, OLD.geometry) < 1e-4; + + -- Il faudrait un trigger sur un changement de géom sur les conduites qui appellent valve_set_orientation pour toutes les vannes avec fk_pipe = id. + FOR r IN SELECT id FROM qwat_od.valve WHERE fk_pipe = OLD.id + LOOP + PERFORM qwat_od.fn_valve_set_orientation(r.id); + END LOOP; + RETURN NULL; END; $BODY$ LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.ft_valve_pipe_update() IS 'Trigger: when moving or deleting a pipe, reassign the pipe to all valves connected to the old pipe. Do an AFTER trigger since it will update valve after updating the node.'; +COMMENT ON FUNCTION qwat_od.ft_valve_pipe_update() IS 'Trigger: when moving or deleting a pipe, reassign the pipe to all valves connected to the old pipe and recalculate valve orientation. Do an AFTER trigger since it will update valve after updating the node.'; +/* WHEN THE PIPE MOVES */ +DROP TRIGGER tr_valve_pipe_update ON qwat_od.pipe; +CREATE TRIGGER tr_valve_pipe_update + -- this will be fired for every node, although not every node is valve + AFTER UPDATE OF geometry ON qwat_od.pipe + FOR EACH ROW + WHEN ( ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) + EXECUTE PROCEDURE qwat_od.ft_valve_pipe_update(); +COMMENT ON TRIGGER tr_valve_pipe_update ON qwat_od.pipe IS 'Trigger: when moving a pipe, reassign the pipe to all valves connected to the old pipe and recalculate valve orientation. Do an AFTER trigger since it will update valve after updating the node.'; +/* WHEN THE PIPE IS DELETED */ +DROP TRIGGER tr_valve_pipe_delete ON qwat_od.pipe; +CREATE TRIGGER tr_valve_pipe_delete + -- this will be fired for every node, although not every node is valve + AFTER DELETE ON qwat_od.pipe + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_valve_pipe_update(); +COMMENT ON TRIGGER tr_valve_pipe_delete ON qwat_od.pipe IS 'Trigger: when deleting a pipe, reassign the pipe to all valves connected to the old pipe and recalculate valve orientation. Do an AFTER trigger since it will update valve after updating the node.'; + -- =================================================== CREATE OR REPLACE VIEW qwat_od.vw_search_view AS @@ -454,6 +483,9 @@ $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation and type for a node. If three pipe arrives at the node: intersection. If one pipe: end. If two: depends on characteristics of pipe: year (is different), material (and year), diameter(and material/year)'; + + + DROP TRIGGER tr_node_add_pipe_vertex_insert ON qwat_od.node; DROP TRIGGER tr_node_add_pipe_vertex_update ON qwat_od.node; DROP FUNCTION qwat_od.ft_node_add_pipe_vertex(); From a811681a409edf47b2c45595d07ddf99ac326f14 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Wed, 30 Nov 2016 07:46:00 +0100 Subject: [PATCH 7/9] add missing labeling fields to valve add missing alternative geometry handling functions are auto rewriteen in conformity test to simplify deltas --- init_qwat.sh | 2 +- ordinary_data/functions/fn_node_set_type.sql | 2 +- .../fn_valve_set_orientation.sql | 0 ordinary_data/valve/od_valve.sql | 27 +- update/delta/delta_1.1.0.sql | 303 ++++-------------- 5 files changed, 87 insertions(+), 247 deletions(-) rename ordinary_data/{valve => functions}/fn_valve_set_orientation.sql (100%) diff --git a/init_qwat.sh b/init_qwat.sh index 5e880ff3..2b5ef662 100755 --- a/init_qwat.sh +++ b/init_qwat.sh @@ -235,7 +235,7 @@ psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/samplingpoint/od_sampling psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/surveypoint/od_surveypoint.sql psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/leak/od_leak.sql -psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/valve/fn_valve_set_orientation.sql +psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/functions/fn_valve_set_orientation.sql psql -v ON_ERROR_STOP=1 -v SRID=$SRID -f ordinary_data/valve/tr_valve_pipe.sql diff --git a/ordinary_data/functions/fn_node_set_type.sql b/ordinary_data/functions/fn_node_set_type.sql index 16423ebb..019f4998 100644 --- a/ordinary_data/functions/fn_node_set_type.sql +++ b/ordinary_data/functions/fn_node_set_type.sql @@ -107,7 +107,7 @@ $BODY$ IF _pipeitem.diameter > _diameter THEN _orientation := _orientation + pi(); END IF; - END IF;+ + END IF; END LOOP; -- more than 2 pipes connected, nothing to calculate ELSEIF _grouped.count > 2 THEN diff --git a/ordinary_data/valve/fn_valve_set_orientation.sql b/ordinary_data/functions/fn_valve_set_orientation.sql similarity index 100% rename from ordinary_data/valve/fn_valve_set_orientation.sql rename to ordinary_data/functions/fn_valve_set_orientation.sql diff --git a/ordinary_data/valve/od_valve.sql b/ordinary_data/valve/od_valve.sql index c5559a94..27be4f7a 100644 --- a/ordinary_data/valve/od_valve.sql +++ b/ordinary_data/valve/od_valve.sql @@ -80,6 +80,9 @@ ALTER TABLE qwat_od.valve ADD COLUMN update_geometry_alt2 boolean default null; /* Schema view */ DO $$ BEGIN PERFORM qwat_sys.fn_enable_schemaview('valve'); END $$; +/* LABELS */ +DO $$ BEGIN PERFORM qwat_sys.fn_label_create_fields('valve'); END $$; + /* GEOM INDEXES */ CREATE INDEX valve_geoidx ON qwat_od.valve USING GIST ( geometry ); CREATE INDEX valve_geoidx_alt1 ON qwat_od.valve USING GIST ( geometry_alt1 ); @@ -130,8 +133,30 @@ CREATE TRIGGER valve_update_orientation COMMENT ON TRIGGER valve_update_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; */ +/* --------------------------------------------*/ +/* -------- ALTERNATIVE GEOM TRIGGER ----------*/ +CREATE TRIGGER tr_valve_altgeom_insert + BEFORE INSERT ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_geometry_alternative_main(); +COMMENT ON TRIGGER tr_valve_altgeom_insert ON qwat_od.valve IS 'Trigger: handle alternative geometries on insert'; + +CREATE TRIGGER tr_valve_altgeom_update + BEFORE UPDATE OF geometry ON qwat_od.valve + FOR EACH ROW + WHEN ( ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) + EXECUTE PROCEDURE qwat_od.ft_geometry_alternative_main(); +COMMENT ON TRIGGER tr_valve_altgeom_update ON qwat_od.valve IS 'Trigger: handle alternative geometries on update'; + +CREATE TRIGGER tr_valve_altgeom_alt + BEFORE UPDATE OF geometry_alt1, geometry_alt2 ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_geometry_alternative_aux(); +COMMENT ON TRIGGER tr_valve_altgeom_alt ON qwat_od.valve IS 'Trigger: when updating, check if alternative geometries are different to fill the boolean fields.'; -/* HANDLE ALTITUDE TRIGGER */ + +/* --------------------------------------------*/ +/* ------- HANDLE ALTITUDE TRIGGER ------------*/ CREATE OR REPLACE FUNCTION qwat_od.ft_valve_handle_altitude() RETURNS TRIGGER AS $BODY$ DECLARE diff --git a/update/delta/delta_1.1.0.sql b/update/delta/delta_1.1.0.sql index efec2297..9912fc65 100644 --- a/update/delta/delta_1.1.0.sql +++ b/update/delta/delta_1.1.0.sql @@ -66,35 +66,47 @@ ALTER TABLE qwat_od.valve ALTER COLUMN id SET default nextval('qwat_od.valve_id_ DO $$ BEGIN PERFORM qwat_sys.fn_enable_schemaview('valve'); END $$; - --- TODO We need to tranfert all the column data from node to valve -UPDATE qwat_od.valve SET fk_district = (SELECT fk_district FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_pressurezone = (SELECT fk_pressurezone FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_distributor = (SELECT fk_distributor FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_precision = (SELECT fk_precision FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_status = (SELECT fk_status FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_object_reference = (SELECT fk_object_reference FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_folder = (SELECT fk_folder FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_precisionalti = (SELECT fk_precisionalti FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET geometry = (SELECT geometry FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET geometry_alt1 = (SELECT geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET geometry_alt2 = (SELECT geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +DO $$ BEGIN PERFORM qwat_sys.fn_label_create_fields('valve'); END $$; + +-- We need to tranfert all the column data from node to valve +UPDATE qwat_od.valve SET fk_district = (SELECT fk_district FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_pressurezone = (SELECT fk_pressurezone FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_distributor = (SELECT fk_distributor FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_precision = (SELECT fk_precision FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_status = (SELECT fk_status FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_object_reference = (SELECT fk_object_reference FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_folder = (SELECT fk_folder FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_precisionalti = (SELECT fk_precisionalti FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry = (SELECT geometry FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry_alt1 = (SELECT geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET geometry_alt2 = (SELECT geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); UPDATE qwat_od.valve SET update_geometry_alt1 = (SELECT update_geometry_alt1 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); UPDATE qwat_od.valve SET update_geometry_alt2 = (SELECT update_geometry_alt2 FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET year = (SELECT year FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET altitude = (SELECT altitude FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET orientation = (SELECT orientation FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_locationtype = (SELECT fk_locationtype FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET identification = (SELECT identification FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET remark = (SELECT remark FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET year_end = (SELECT year_end FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET fk_printmap = (SELECT fk_printmap FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET _geometry_alt1_used = (SELECT _geometry_alt1_used FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET _geometry_alt2_used = (SELECT _geometry_alt2_used FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET _pipe_node_type = (SELECT _pipe_node_type FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET _pipe_orientation = (SELECT _pipe_orientation FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET year = (SELECT year FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET altitude = (SELECT altitude FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET orientation = (SELECT orientation FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_locationtype = (SELECT fk_locationtype FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET identification = (SELECT identification FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET remark = (SELECT remark FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET year_end = (SELECT year_end FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET fk_printmap = (SELECT fk_printmap FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _geometry_alt1_used = (SELECT _geometry_alt1_used FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _geometry_alt2_used = (SELECT _geometry_alt2_used FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _pipe_node_type = (SELECT _pipe_node_type FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _pipe_orientation = (SELECT _pipe_orientation FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); UPDATE qwat_od.valve SET _pipe_schema_visible = (SELECT _pipe_schema_visible FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); -UPDATE qwat_od.valve SET _printmaps = (SELECT _printmaps FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET _printmaps = (SELECT _printmaps FROM qwat_od.node WHERE qwat_od.node.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET schema_force_visible = (SELECT schema_force_visible FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_1_visible = (SELECT label_1_visible FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_1_x = (SELECT label_1_x FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_1_y = (SELECT label_1_y FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_1_rotation = (SELECT label_1_rotation FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_1_text = (SELECT label_1_text FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_2_visible = (SELECT label_2_visible FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_2_x = (SELECT label_2_x FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_2_y = (SELECT label_2_y FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_2_rotation = (SELECT label_2_rotation FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); +UPDATE qwat_od.valve SET label_2_text = (SELECT label_2_text FROM qwat_od.network_element WHERE qwat_od.network_element.id = qwat_od.valve.id); ALTER TABLE qwat_od.valve ALTER COLUMN fk_distributor SET NOT NULL; @@ -104,6 +116,27 @@ ALTER TABLE qwat_od.valve ALTER COLUMN geometry SET NOT NULL; ALTER TABLE qwat_od.valve DROP CONSTRAINT valve_id_fkey; +/* --------------------------------------------*/ +/* -------- ALTERNATIVE GEOM TRIGGER ----------*/ +CREATE TRIGGER tr_valve_altgeom_insert + BEFORE INSERT ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_geometry_alternative_main(); +COMMENT ON TRIGGER tr_valve_altgeom_insert ON qwat_od.valve IS 'Trigger: handle alternative geometries on insert'; + +CREATE TRIGGER tr_valve_altgeom_update + BEFORE UPDATE OF geometry ON qwat_od.valve + FOR EACH ROW + WHEN ( ST_Equals(ST_Force2d(NEW.geometry), ST_Force2d(OLD.geometry)) IS FALSE ) + EXECUTE PROCEDURE qwat_od.ft_geometry_alternative_main(); +COMMENT ON TRIGGER tr_valve_altgeom_update ON qwat_od.valve IS 'Trigger: handle alternative geometries on update'; + +CREATE TRIGGER tr_valve_altgeom_alt + BEFORE UPDATE OF geometry_alt1, geometry_alt2 ON qwat_od.valve + FOR EACH ROW + EXECUTE PROCEDURE qwat_od.ft_geometry_alternative_aux(); +COMMENT ON TRIGGER tr_valve_altgeom_alt ON qwat_od.valve IS 'Trigger: when updating, check if alternative geometries are different to fill the boolean fields.'; + CREATE OR REPLACE FUNCTION qwat_od.fn_node_create( _point geometry, deactivate_node_add_pipe_vertex boolean = FALSE ) RETURNS integer AS @@ -127,100 +160,6 @@ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.fn_node_create(geometry, boolean) IS 'Returns the node for a given geometry (point). If node does not exist, create it.'; --- Valve orientation -CREATE OR REPLACE FUNCTION qwat_od.fn_valve_set_orientation(_valve_id integer) RETURNS void AS -$BODY$ - DECLARE - _pipeitem record; - _pipe_id integer; - _grouped record; - _diameter smallint; - _looppos integer := 0; - _orientation double precision := 0; - _orientation2 double precision := 0; - _node_geom geometry; - _pipe_geom geometry; - _sub_geom geometry; - _lin_ref float; - BEGIN - - -- get the geometry - _node_geom := geometry FROM qwat_od.valve WHERE id = _valve_id; - - -- count the active pipes associated to this valve - SELECT - COUNT(*) AS count, - bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible - INTO _grouped - FROM qwat_od.pipe - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id - WHERE (pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id)) - AND status.active IS TRUE; - - -- if not connected to any pipe, do nothing - IF _grouped.count <= 2 THEN - /* loop over them, and take the 2 first/last vertices - of the pipe to determine orientation (used for symbology) */ - FOR _pipeitem IN ( - SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, - ST_StartPoint(geometry) AS point_1, - ST_PointN(geometry,2) AS point_2 - FROM qwat_od.pipe - INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - WHERE pipe.id = (SELECT fk_pipe FROM qwat_od.valve WHERE id = _valve_id) AND status.active IS TRUE - ) LOOP - IF _looppos=0 THEN - -- first pipe - _diameter := _pipeitem.diameter; - _pipe_id := _pipeitem.id; - _looppos := 1; - _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); - ELSE - -- second pipe if exists - _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - _orientation2 := pi() + _orientation2; -- reverse angle - -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); - _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); - -- RAISE NOTICE 'mean: %', degrees(_orientation ); - -- reverse arrow according to diameter reduction - IF _pipeitem.diameter > _diameter THEN - _orientation := _orientation + pi(); - END IF; - END IF; - END LOOP; - END IF; - - -- update the valve table - UPDATE qwat_od.valve SET - orientation = degrees(_orientation) - --_pipe_schema_visible = _grouped.schema_visible - WHERE id = _valve_id; - END; -$BODY$ -LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation for a valve.'; - -/* -CREATE OR REPLACE FUNCTION qwat_od.ft_valve_set_orientation() RETURNS TRIGGER AS -$BODY$ - BEGIN - PERFORM qwat_od.fn_valve_set_orientation(NEW.id); - RETURN NEW; - END; -$BODY$ -LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.ft_valve_set_orientation() IS 'Trigger: set orientation after inserting a valve.'; - -CREATE TRIGGER valve_set_orientation - AFTER INSERT ON qwat_od.valve - FOR EACH ROW - EXECUTE PROCEDURE qwat_od.ft_valve_set_orientation(); -COMMENT ON TRIGGER valve_set_orientation ON qwat_od.valve IS 'Trigger: set orientation after inserting a valve.'; -*/ - CREATE OR REPLACE FUNCTION qwat_od.ft_valve_add_pipe_vertex() RETURNS trigger AS $BODY$ @@ -362,130 +301,6 @@ COMMENT ON VIEW qwat_od.vw_valve_lines IS 'Valves represented as lines. Each line is made from two points, the handle as starting point and the location on the pipe as ending point.'; - - -CREATE OR REPLACE FUNCTION qwat_od.fn_node_set_type(_node_id integer) RETURNS void AS -$BODY$ - DECLARE - _pipeitem record; - _pipe_id integer; - _grouped record; - _year integer; - _material varchar(50); - _diameter smallint; - _looppos integer := 0; - _type qwat_od.pipe_connection; - _orientation double precision := 0; - _orientation2 double precision := 0; - _node_geom geometry; - _pipe_geom geometry; - _sub_geom geometry; - _lin_ref float; - BEGIN - - -- get the geometry - _node_geom := geometry FROM qwat_od.node WHERE id = _node_id; - - -- count the active pipes associated to this node - SELECT - COUNT(pipe.id) AS count, - bool_or(coalesce(schema_force_visible,pipe_function.schema_visible)) AS schema_visible - INTO _grouped - FROM qwat_od.pipe - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - INNER JOIN qwat_vl.pipe_function ON pipe.fk_function = pipe_function.id - WHERE (fk_node_a = _node_id OR fk_node_b = _node_id) - AND status.active IS TRUE; - - -- if not connected to any pipe, delete the node if it is not something else (i.e. is not inherited) - IF _grouped.count = 0 THEN - -- check it is not associated to any pipe (including inactive ones) - IF _node_id NOT IN (SELECT fk_node_a FROM qwat_od.pipe UNION SELECT fk_node_b FROM qwat_od.pipe) THEN - -- if it is not something else - IF ( SELECT node_type = 'node'::qwat_od.node_type FROM qwat_od.vw_qwat_node WHERE id = _node_id) THEN - -- delete it - RAISE NOTICE 'Delete node %' , _node_id; - DELETE FROM qwat_od.node WHERE id = _node_id; -- delete on table level for safety (do not delete on the merge view) - RETURN; - END IF; - ELSE - _type := NULL::qwat_od.pipe_connection; - END IF; - -- if 1 or 2 pipes associated - ELSEIF _grouped.count <= 2 THEN - /* loop over them, and take the 2 first/last vertices - of the pipe to determine orientation (used for symbology) */ - FOR _pipeitem IN ( - SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, - ST_StartPoint(geometry) AS point_1, - ST_PointN(geometry,2) AS point_2 - FROM qwat_od.pipe - INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - WHERE fk_node_a = _node_id AND status.active IS TRUE - UNION ALL - SELECT pipe.id, pipe.year, pipe_material.value_fr AS material, pipe_material.diameter_nominal AS diameter, - ST_EndPoint(geometry) AS point_1, - ST_PointN(geometry,ST_NPoints(geometry)-1) AS point_2 - FROM qwat_od.pipe - INNER JOIN qwat_vl.pipe_material ON pipe.fk_material = pipe_material.id - INNER JOIN qwat_vl.status ON pipe.fk_status = status.id - WHERE fk_node_b = _node_id AND status.active IS TRUE - ) LOOP - IF _looppos=0 THEN - -- first pipe - _type := 'pipe_end'::qwat_od.pipe_connection; - _year := _pipeitem.year; - _material := _pipeitem.material; - _diameter := _pipeitem.diameter; - _pipe_id := _pipeitem.id; - _looppos := 1; - _orientation := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - -- RAISE NOTICE 'pipe % %', _pipe_id, degrees( _orientation ); - ELSE - -- second pipe if exists - IF _material = _pipeitem.material AND _diameter = _pipeitem.diameter AND _year = _pipeitem.year THEN - _type := 'couple_same'::qwat_od.pipe_connection; - ELSIF _material = _pipeitem.material AND _diameter = _pipeitem.diameter THEN - _type := 'couple_year'::qwat_od.pipe_connection; - ELSIF _material = _pipeitem.material THEN - _type := 'couple_diameter'::qwat_od.pipe_connection; - ELSIF _diameter = _pipeitem.diameter THEN - _type := 'couple_material'::qwat_od.pipe_connection; - ELSE - _type := 'couple_other'; - END IF; - _orientation2 := pi()/2 - ST_Azimuth(_pipeitem.point_2,_pipeitem.point_1); - _orientation2 := pi() + _orientation2; -- reverse angle - -- RAISE NOTICE 'pipe % %', _pipeitem.id, degrees( _orientation2 ); - _orientation := ATAN2( (SIN(_orientation)+SIN(_orientation2))/2 , (COS(_orientation)+COS(_orientation2))/2 ); - -- RAISE NOTICE 'mean: %', degrees(_orientation ); - -- reverse arrow according to diameter reduction - IF _pipeitem.diameter > _diameter THEN - _orientation := _orientation + pi(); - END IF; - END IF; - END LOOP; - -- more than 2 pipes connected, nothing to calculate - ELSEIF _grouped.count > 2 THEN - _type := 'T'::qwat_od.pipe_connection; - END IF; - - -- update the node table - UPDATE qwat_od.node SET - _pipe_node_type = _type, - _pipe_orientation = degrees(_orientation), - _pipe_schema_visible = _grouped.schema_visible - WHERE id = _node_id; - --RAISE NOTICE '% %' , _node_id , degrees(_orientation); - END; -$BODY$ -LANGUAGE plpgsql; -COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation and type for a node. If three pipe arrives at the node: intersection. If one pipe: end. If two: depends on characteristics of pipe: year (is different), material (and year), diameter(and material/year)'; - - - - DROP TRIGGER tr_node_add_pipe_vertex_insert ON qwat_od.node; DROP TRIGGER tr_node_add_pipe_vertex_update ON qwat_od.node; DROP FUNCTION qwat_od.ft_node_add_pipe_vertex(); From bbdbe543563c69a2134043be4166dbde60b65ea2 Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Thu, 1 Dec 2016 18:23:10 +0100 Subject: [PATCH 8/9] Add DB num version checking in integration process Exit integration process if DB numbers does not match with deltas Testing integration process with two good delta, and the last one missing the DB number version Adding num version to delta Bug fixing: wrong DB on integration process to check number --- update/delta/delta_1.0.0_12092016.sql | 2 ++ update/delta/delta_1.0.1_06062016.sql | 1 + update/delta/delta_1.1.0.sql | 2 ++ update/verify_upgrade_db.sh | 21 ++++++++++++++++++--- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/update/delta/delta_1.0.0_12092016.sql b/update/delta/delta_1.0.0_12092016.sql index 252d5d2f..9f9a27c9 100644 --- a/update/delta/delta_1.0.0_12092016.sql +++ b/update/delta/delta_1.0.0_12092016.sql @@ -31,3 +31,5 @@ Replay a logged event. Arguments: pevent_id: The event_id of the event in qwat_sys.logged_actions to replay $body$; + +UPDATE qwat_sys.versions SET version = '1.0.0'; \ No newline at end of file diff --git a/update/delta/delta_1.0.1_06062016.sql b/update/delta/delta_1.0.1_06062016.sql index 259ce391..b707fd7b 100644 --- a/update/delta/delta_1.0.1_06062016.sql +++ b/update/delta/delta_1.0.1_06062016.sql @@ -160,3 +160,4 @@ $BODY$ LANGUAGE plpgsql; COMMENT ON FUNCTION qwat_od.fn_node_set_type(integer) IS 'Set the orientation and type for a node. If three pipe arrives at the node: intersection. If one pipe: end. If two: depends on characteristics of pipe: year (is different), material (and year), diameter(and material/year)'; +UPDATE qwat_sys.versions SET version = '1.0.1'; \ No newline at end of file diff --git a/update/delta/delta_1.1.0.sql b/update/delta/delta_1.1.0.sql index 9912fc65..90b2414c 100644 --- a/update/delta/delta_1.1.0.sql +++ b/update/delta/delta_1.1.0.sql @@ -307,3 +307,5 @@ DROP FUNCTION qwat_od.ft_node_add_pipe_vertex(); DROP TRIGGER valve_node_set_type ON qwat_od.valve; DROP FUNCTION qwat_od.ft_valve_node_set_type(); + +UPDATE qwat_sys.versions SET version = '1.1.0'; \ No newline at end of file diff --git a/update/verify_upgrade_db.sh b/update/verify_upgrade_db.sh index df7dcd44..d891d3c0 100755 --- a/update/verify_upgrade_db.sh +++ b/update/verify_upgrade_db.sh @@ -83,11 +83,26 @@ echo "Applying deltas on $TESTCONFORMDB:" for f in $DIR/delta/*.sql do CURRENT_DELTA=$(basename "$f") + CURRENT_DELTA_WITHOUT_EXT="${CURRENT_DELTA%.*}" #CURRENT_DELTA_NUM_VERSION=$(echo $CURRENT_DELTA| cut -d'_' -f 2) - CURRENT_DELTA_NUM_VERSION=$(echo $CURRENT_DELTA| cut -c 7) - if [[ $CURRENT_DELTA_NUM_VERSION > $SHORT_LATEST_TAG || $CURRENT_DELTA_NUM_VERSION = $SHORT_LATEST_TAG || $SHORT_LATEST_TAG = '' ]]; then - printf " Processing ${GREEN}$CURRENT_DELTA${NC}, num version = $CURRENT_DELTA_NUM_VERSION\n" + CURRENT_DELTA_NUM_VERSION=$(echo $CURRENT_DELTA_WITHOUT_EXT| cut -c 7) + CURRENT_DELTA_NUM_VERSION_FULL=$(echo $CURRENT_DELTA_WITHOUT_EXT| cut -d'_' -f 2) + if [[ $CURRENT_DELTA_NUM_VERSION > $SHORT_LATEST_TAG || $CURRENT_DELTA_NUM_VERSION == $SHORT_LATEST_TAG || $SHORT_LATEST_TAG == '' ]]; then + printf " Processing ${GREEN}$CURRENT_DELTA${NC}, num version = $CURRENT_DELTA_NUM_VERSION ($CURRENT_DELTA_NUM_VERSION_FULL)\n" /usr/bin/psql -v ON_ERROR_STOP=1 --host $HOST --port 5432 --username "$USER" --no-password -q -d "$TESTCONFORMDB" -f $f + + printf " Verifying num version conformity - " + # For each delta run on the DB, we have to check that the version number contained in the file name is the same that has been hardcoded in the DB + # note: delta files MUST include at their end: UPDATE qwat_sys.versions SET version = 'x.x.x'; + OUTPUT_NUM=`/usr/bin/psql -v ON_ERROR_STOP=1 --host $HOST --port 5432 --username "$USER" --no-password -q -d "$TESTCONFORMDB" -t -c "SELECT version FROM qwat_sys.versions;"` + OUTPUT_NUM="$(echo -e "${OUTPUT_NUM}" | tr -d '[:space:]')" + if [ "$OUTPUT_NUM" != "$CURRENT_DELTA_NUM_VERSION_FULL" ]; then + printf " Num in DB: ${GREEN}$OUTPUT_NUM${NC} - Num in file: ${RED}$CURRENT_DELTA_NUM_VERSION_FULL${NC} => ${RED}Numbers do NOT match !${NC}\n" + EXITCODE=1 + exit $EXITCODE + else + printf " Num in DB: ${GREEN}$OUTPUT_NUM${NC} - Num in file: ${RED}$CURRENT_DELTA_NUM_VERSION_FULL${NC} => ${GREEN}OK${NC}\n" + fi else printf " Bypassing ${RED}$CURRENT_DELTA${NC}, num version = $CURRENT_DELTA_NUM_VERSION\n" fi From 91cb45064935d3917f690d2c768cb13ca3d4a676 Mon Sep 17 00:00:00 2001 From: Sylvain Beorchia Date: Mon, 5 Dec 2016 14:11:50 +0100 Subject: [PATCH 9/9] Drop useless trigger function for valves Added proper drop before dropping function triggers for valves --- update/delta/delta_1.1.0.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/update/delta/delta_1.1.0.sql b/update/delta/delta_1.1.0.sql index 90b2414c..e762a2c8 100644 --- a/update/delta/delta_1.1.0.sql +++ b/update/delta/delta_1.1.0.sql @@ -308,4 +308,14 @@ DROP FUNCTION qwat_od.ft_node_add_pipe_vertex(); DROP TRIGGER valve_node_set_type ON qwat_od.valve; DROP FUNCTION qwat_od.ft_valve_node_set_type(); +-- We need to manually remove those 3 triggers functions, as they cannot be removed automatically by the rewrite views process +DROP VIEW qwat_od.vw_search_view; +DROP VIEW qwat_od.vw_export_valve; +DROp VIEW qwat_od.vw_valve_lines; +DROP VIEW qwat_od.vw_element_valve; +DROP FUNCTION qwat_od.ft_element_valve_delete(); +DROP FUNCTION qwat_od.ft_element_valve_insert(); +DROP FUNCTION qwat_od.ft_element_valve_update(); + + UPDATE qwat_sys.versions SET version = '1.1.0'; \ No newline at end of file