Noch zu Trigger: Auf den hab ich heir verzichtet, weil er nicht nötig ist. In meiner Umgebung hab ich einen gebaut der mir beim Anlegen oder Ändern von Stammdaten bestimmte Felder z.B. tk25 oder die Gemeinde füllen soll oder die IDs hochzählt. Da ich die Geometrie in der Stammdatentabelle mitführe wird auch getestet ob sich die Koordinaten oder Geometrie geändert haben und das entsprechende nachgeführt. Da hier ein View genutzt wird ist das nicht notwendig.
Hier der Trigger zur Ansicht (das Aktualisieren von tk25 funktioniert hier im Moment noch nicht so wie es soll):
CREATE OR REPLACE FUNCTION sep3.sync_geometry()
RETURNS trigger AS
$BODY$
declare
srid integer := 25832;
begin
--check operation
if TG_OP = 'INSERT' then
raise notice 'insert';
--check if names set
if new.shortname is null then
new.shortname := coalesce(new.longname, 'NEU');
end if;
if new.longname is null then
new.longname := new.bver||'-'||coalesce(new.shortname, 'NEU');
end if;
--check keys
raise notice 'prj: %s, loc: %s, rec: %s', new.prjid, new.locid, new.recid;
if new.prjid is null then
new.prjid := 1;
end if;
if new.locid is null then
select coalesce(max(locid), 0) + 1 into new.locid from sep3.s3stamm where prjid = new.prjid;
end if;
if new.recid is null then
select coalesce(max(recid), 0) + 1 into new.recid from sep3.s3stamm where prjid = new.prjid and locid = new.locid;
end if;
raise notice 'prj: %s, loc: %s, rec: %s', new.prjid, new.locid, new.recid;
-- check if a geometry can be made
if new.geom is null and -500 = any(array[new.xcoord, new.ycoord, new.ksys]) is not null then
new.geom := st_transform(st_setsrid(st_makepoint(new.xcoord, new.ycoord), new.ksys), srid);
elsif new.geom is not null and st_srid(new.geom) > 0 then
new.xcoord := st_x(new.geom);
new.ycoord := st_y(new.geom);
new.ksys := st_srid(new.geom);
new.geom := st_transform(new.geom, srid);
else
raise notice 'no geometry possible!';
return null;
end if;
else
raise notice 'update';
if (new.shortname <> old.shortname or new.bver <> old.bver) and old.longname is null then
new.longname := new.bver||'-'||coalesce(new.shortname, 'NEU');
end if;
if not new.geom = old.geom then
raise notice 'geometry altered';
new.xcoord := st_x(st_transform(new.geom, new.ksys));
new.ycoord := st_y(st_transform(new.geom, new.ksys));
elsif new.xcoord <> old.xcoord
or new.ycoord <> old.ycoord
or new.zcoordb <> old.zcoordb
or new.ksys <> old.ksys then
raise notice 'coords altered';
new.geom := st_transform(st_setsrid(st_makepoint(new.xcoord, new.ycoord),new.ksys), srid);
end if;
end if;
-- get the tk
-- select dtknr
-- into new.tk25
-- from public.tk_tiles t1
-- where t1.m_id = '25'
-- and t1.type = 'TK'
-- and new.geom @ st_transform(t1.geom, srid);
return new;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;