gis
Database
Tables
(current)
Columns
Constraints
Relationships
Orphan Tables
Anomalies
Routines
trg_avant_modification_ouvrage
Parameters
Name
Type
Mode
Definition
DECLARE organisme text; table_canalisation text; BEGIN -- Calcul de l'identifiant si besoin IF NEW.idouvrage IS NULL OR trim(NEW.idouvrage) = '' OR trim(NEW.idouvrage) = 'INCONNU' THEN NEW.idouvrage := raepa.generate_oid(TG_TABLE_NAME::text)::character varying; END IF; -- Calcul de la géométrie à partir de X et Y IF ( TG_OP = 'UPDATE' AND NEW.x IS NOT NULL AND NEW.y IS NOT NULL AND ( NEW.x != OLD.x OR NEW.y != OLD.y ) ) OR ( TG_OP = 'INSERT' AND NEW.x IS NOT NULL AND NEW.y IS NOT NULL ) THEN NEW.geom := ST_SetSRID(ST_Makepoint(NEW.x, NEW.y), 2154); RAISE NOTICE 'ouvrage % - X ou Y changé -> changement geom vers POINT(% %)', NEW.idouvrage, NEW.x, NEW.y; END IF; IF ( TG_OP = 'UPDATE' AND NOT ST_Equals(NEW.geom, OLD.geom) ) OR TG_OP = 'INSERT' THEN -- Calcul des X et Y si besoin NEW.x := ST_X(NEW.geom)::numeric(10,3); NEW.y := ST_Y(NEW.geom)::numeric(10,3); -- Calcul des canalisation amont et aval table_canalisation = 'raepa.raepa_canalass_l'; IF TG_TABLE_NAME = 'raepa_ouvraep_p' THEN table_canalisation = 'raepa.raepa_canalaep_l'; END IF; -- idcanamont EXECUTE format( ' SELECT COALESCE(string_agg(c.idcana, '','' ORDER BY idcana), ''INCONNU'') FROM %s AS c WHERE ST_DWithin(ST_EndPoint(c.geom), ''%s''::geometry, 0.05) ', table_canalisation, NEW.geom ) INTO NEW.idcanamont; -- idcanaval EXECUTE format( ' SELECT COALESCE(string_agg(c.idcana, '','' ORDER BY idcana), ''INCONNU'') FROM %s AS c WHERE ST_DWithin(ST_StartPoint(c.geom), ''%s''::geometry, 0.05) ', table_canalisation, NEW.geom ) INTO NEW.idcanaval; RAISE NOTICE 'ouvrage % - idcanamont changé à %', NEW.idouvrage, NEW.idcanamont; RAISE NOTICE 'ouvrage % - idcanaval changé à %', NEW.idouvrage, NEW.idcanaval; END IF; -- Métadonnées IF (NEW.mouvrage IS NULL) OR (NEW.gexploit IS NULL) OR (NEW.sourmaj IS NULL) THEN -- Récupération du gestionnaire SELECT INTO organisme s.nom FROM raepa.sys_organisme_gestionnaire s WHERE actif IS TRUE ORDER BY id DESC LIMIT 1; END IF; IF NEW.mouvrage IS NULL THEN NEW.mouvrage := organisme; END IF; IF NEW.gexploit IS NULL THEN NEW.gexploit := organisme; END IF; NEW.datemaj := now(); IF NEW.sourmaj IS NULL THEN NEW.sourmaj := organisme; END IF; RETURN NEW; END;