ビューを使用できますが、トリガーを使用して、元のポイントテーブルを変更するときにバッファーテーブルを自動的に更新することもできます。バッファの計算はCPUを集中的に使用するタスクであるため、テーブルを表示するたびにバッファを再生成したくない場合、これは本当に便利です。
これを実装する完全なコードサンプルは次のとおりです。ポイントテーブルと、ポイントテーブルの変更に基づいて自動的に更新されるpoint_bufferテーブル。
QGISでテストできます。両方のテーブルを開き、ポイントテーブルで編集モードに入ります。ポイントを移動するか、またはbuffer_distanceの値を変更すると、保存するたびにバッファーレイヤーが更新されます。
楽しい :)
drop table if exists point;
create table point (
gid serial primary key
, point_name varchar
, buffer_distance double precision
, the_geom geometry
);
drop table if exists point_buffer;
create table point_buffer (
gid serial primary key
, point_gid integer
, the_geom geometry
);
select populate_geometry_columns();
insert into
point (point_name, buffer_distance, the_geom)
select
'point ' || n::varchar as point_name
, random() * 100 + min_buf as buffer_distance
, st_setsrid(st_point(random() * 10000 + x0, random() * 10000 + y0), 2154) as the_geom
from
generate_series(1, 1000) as n
, (values (10)) as foox(x0)
, (values (10)) as fooy(y0)
, (values (10)) as buf(min_buf);
-- insert values into point_buffer
insert into
point_buffer (point_gid, the_geom)
select
gid as point_gid
, st_buffer(the_geom, buffer_distance)
from
point;
-- update all point_buffer
update
point_buffer as pb
set
the_geom = st_buffer(p.the_geom, p.buffer_distance)
from
point as p
where
p.gid = pb.point_gid;
-- add trigger to automate insert / delete / update
create or replace function update_point_buffer() returns trigger as
$$
begin
-- delete
IF (TG_OP = 'DELETE') THEN
delete from point_buffer as pb where point_gid = OLD.gid;
return OLD;
-- insert
ELSIF (TG_OP = 'INSERT') THEN
insert into
point_buffer (point_gid, the_geom)
select
NEW.gid as point_gid
, st_buffer(NEW.the_geom, NEW.buffer_distance);
return NEW;
-- update
else
update
point_buffer as pb
set
the_geom = st_buffer(NEW.the_geom, NEW.buffer_distance)
where
pb.gid = NEW.gid;
return NEW;
END IF;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_point_point_buffer ON point;
CREATE TRIGGER trg_point_point_buffer AFTER DELETE OR INSERT OR UPDATE ON point
FOR EACH ROW EXECUTE PROCEDURE update_point_buffer();
/* use it */
-- insert
insert into
point (point_name, buffer_distance, the_geom)
select
'added point to test trigger' as point_name
, random() * 100 + min_buf as buffer_distance
, st_setsrid(st_point(random() * 10000 + x0, random() * 10000 + y0), 2154) as the_geom
from
(values (10)) as foox(x0)
, (values (10)) as fooy(y0)
, (values (10)) as buf(min_buf);
select
st_astext(pb.the_geom)
, *
from
point_buffer as pb
join
point as p
on
p.gid = pb.point_gid
where
p.point_name = 'added point to test trigger';
-- update
update
point as p
set
the_geom = st_setsrid(st_point(0, 0), 2154)
, buffer_distance = 1
where
p.point_name = 'added point to test trigger';
-- check point_buffer
select
st_astext(pb.the_geom)
, *
from
point_buffer as pb
join
point as p
on
p.gid = pb.point_gid
where
p.point_name = 'added point to test trigger';
-- delete
delete from
point as p
where
p.point_name = 'added point to test trigger';
-- check point_buffer
select
*
from
point_buffer as pb
where
point_gid = 1001;