Postgresql触发器来计算多几何

时间:2018-04-19 14:57:03

标签: postgresql triggers postgis

基于以下解决方案Postgresql trigger to calculate total score 我正在尝试创建一个触发器,它将通过从cat_projects_geom表中添加多字符来更新cat_projects中现有记录的几何图形,其中name = name。 Cat projects_geom包含多个具有相同名称的多边形。

这是我的两个源表:第一个存储传入多边形的表。它可以是一个引用的多个记录...(例如:MSC17139),我想在第二个表中更新the_geom(Multygeom类型),因此一个引用有一个记录(ses second table)< / p>

-- ----------------------------
DROP TABLE IF EXISTS "public"."cat_projects_geom";
CREATE TABLE "public"."cat_projects_geom" (
"gid" int4 DEFAULT nextval('sq'::regclass) NOT NULL,
"Name" varchar(255) COLLATE "default",
"the_geom" "public"."geometry"
)
WITH (OIDS=FALSE)

;

-- ----------------------------
-- Records of cat_projects_geom
-- ----------------------------
INSERT INTO "public"."cat_projects_geom" VALUES ('1552', 'MSC17019', '0106000020E61000000100000001030000000100000005000000FAF3E49E5F1ABFBFF34D7165A6AF494094CA8C16EF1FBFBF33C95631ABAF4940BE4B1813F830BFBF63DE2F53AAAF494059688896202DBFBFBE05F062A5AF4940FAF3E49E5F1ABFBFF34D7165A6AF4940');
INSERT INTO "public"."cat_projects_geom" VALUES ('1553', 'MSC17138', '0106000020E6100000010000000103000000020000003E00000096E65B4E045ADBBF4E45A94D22BD4940FC50DE7D942EDBBFCA1995E71ABD4940BD1DAFE86A2EDBBF62BD16661ABD4940B01D34EE362EDBBF0B78C2BA18BD4940B01D34EE362EDBBFE119374618BD4940FDEB3F564151DBBF6BCABBEC13BD49408ABF4BF64848DBBF74746E0E0EBD4940E183A62EBA2DDBBF920D8AF311BD4940A6507799902DDBBF359BF18B11BD49409650FC9E5C2DDBBFC743F7850FBD494063EA895ED92DDBBFDF9685F70EBD4940AABFC6F07C48DBBF588D5C1F0BBD49408ABF4BF64848DBBFD5EF681006BD494055F21DB20647DBBFA8810E6B06BD49408325D24CFC46DBBF96DBABCC04BD4940DA8BBF87B346DBBFD737581E02BD49403125F06DC445DBBF9DBB343AFABC494021BEB96FD143DBBF2271B7B5F8BC49403ABDFFBCF940DBBFFDD15317F7BC49408D23EDF7B040DBBF9D5CC3B505BD494071BC3BFF893EDBBFD8EF681006BD494061BCC004563EDBBF20330862F6BC494039CAC5D19437DBBFC65C5282F6BC4940FCBBDE251E3DDBBFD8EF681006BD4940C3851A946933DBBFEE7ECBAE07BD4940BBD80BE81437DBBFD314B6DBFDBC49409D4FFF48EC2DDBBFC256272EFDBC49404F85BDBAFD31DBBF7BF67DFC07BD49402E1D5C1A972CDBBF3F30AFCB08BD49409D833F4AB62CDBBF25A004770ABD4940BB1CFF402B2BDBBFF4FBC3B70ABD4940D48E88A29520DBBF7974EDD5F4BC4940C4490EE8D617DBBF3C75F48810BD49402BE3B9C81B17DBBF5DCA473309BD4940E5E063843410DBBF443B45360ABD4940D31461E26512DBBFD6100A6D25BD49402D48866CF712DBBF9BB3630D2CBD4940DD485435FF14DBBF096598D52EBD494095AF234FEE15DBBF4CA471F831BD4940E1E2CDDE4B16DBBF7637CC9537BD49405E492C099F16DBBFB08293CC38BD4940B87C51933017DBBF8CA3332A3ABD494024B0761DC217DBBF4866D8843ABD49400A515978C82EDBBF6F0471F037BD4940AF2056978228DBBF03EA7A1443BD4940443385F51630DBBF8D8760E44DBD4940E35198306C31DBBF1A22F76739BD49404AEB4311B130DBBF5D45531533BD4940131C4AE8034FDBBFB8CF3D2053BD4940588C975B5348DBBF009C2B442DBD49405D1E7DB17230DBBFF1F182E830BD49401CEB4D1C4930DBBFD7770D612EBD49402959E3C05D48DBBFB6BA81F02ABD4940632C9CA15948DBBF16616FFE28BD49407B1EF8ABA630DBBF41AF155B2CBD49405D1E7DB17230DBBF41AF155B2CBD49400DEBD2211530DBBF9BB3630D2CBD4940D2B7A38CEB2FDBBF3CBACFA22ABD4940D2B7A38CEB2FDBBF2EB66B072ABD4940DEB71E871F30DBBF99B2B9B929BD49408FF26F785348DBBF97EB091626BD494096E65B4E045ADBBF4E45A94D22BD49400C0000002C8492188A2EDBBF2A70D9942EBD494054840308262FDBBF371B87DD35BD4940D0B0BFE0FD19DBBF6092ADB238BD4940151732218119DBBF6CB1E17E38BD49405CB062079218DBBF6F0471F037BD494063E3A5B2EB17DBBF0E69273B37BD494055E32AB8B717DBBF7608600436BD4940E67C47889817DBBFC7DD412534BD4940F47CC282CC17DBBF9E15D19633BD49401B4A75CCDA18DBBF4AA471F831BD4940294AF0C60E19DBBF3CAC8BDE31BD49402C8492188A2EDBBF2A70D9942EBD4940');
INSERT INTO "public"."cat_projects_geom" VALUES ('1554', 'MSC17139', '0106000020E6100000010000000103000000010000000F000000C690D435E905DBBFF07C11A163BD49403F908B72AD03DBBF5FC3F4084ABD49401B29698AEA00DBBF8DF53BF729BD49407D097B6AB0FFDABF5748CB371ABD4940FB2A30BE6D08DBBF5CFF61DA1BBD494002F8670D4809DBBFE44C0C2D22BD494033C627D8010FDBBF320FA89121BD49407A6083608611DBBF2DE1F0253EBD49404A2EE651D415DBBF5ACBB3D86DBD49407E611FF29515DBBF977F92F56EBD4940636129FD2D15DBBF6B60DAAA6FBD494037D5C2FD7414DBBFBAE34E4670BD494084FA6DF96E13DBBF999DED9370BD49400891B6142107DBBFF93E7DFE71BD4940C690D435E905DBBFF07C11A163BD4940');
INSERT INTO "public"."cat_projects_geom" VALUES ('1555', 'MSC17139', '0106000020E6100000010000000103000000010000001200000049C656B39FFADABF46E057D309BD4940EA5AA9BF30FBDABF9FD45DDE0BBD4940D1276614D7FBDABF1600A59812BD4940C727701F6FFBDABF002B85B814BD4940288ED8545AFBDABF14A4F34916BD4940368E534F8EFBDABFE68BB98318BD4940408E4944F6FBDABFB88417251BBD4940BDF4A76E49FCDABF343214281CBD4940D8F49D63B1FCDABFB8B444F71CBD4940D25B4F5CD8FEDABF5ECD2BF736BD4940038F03F7CDFEDABF1472829F37BD4940B75B596770FEDABF11FBB16E38BD49406A28AFD712FEDABFFAA422FD38BD49404F28B9E2AAFDDABF616CC75739BD4940B6C164C3EFFCDABF694B938B39BD49407F37D51FEDEEDABF0F4C0D523BBD494077F0D04307EBDABF7CC16AD10BBD494049C656B39FFADABF46E057D309BD4940');
INSERT INTO "public"."cat_projects_geom" VALUES ('1556', 'MSC17139', '0106000020E6100000010000000103000000020000003A0000007D097B6AB0FFDABF5748CB371ABD49401B29698AEA00DBBF8DF53BF729BD49403F908B72AD03DBBF5FC3F4084ABD4940C690D435E905DBBFF07C11A163BD49408691B6142107DBBFF73E7DFE71BD4940E6E9B0D3F205DBBFFBA4D21C72BD4940EFF650810405DBBF62F0C8D668BD4940275D48C75304DBBFF72F66346ABD49405EC33F0DA303DBBF04A5791D6BBD49409929BC58BE02DBBF1FC9CEC56BBD494023F6A0D9C401DBBF5D323E546CBD4940305C27307800DBBFB42DE2AE6CBD494066876C2D8DE0DABF1B71354971BD4940CD20180ED2DFDABF27C14F2F71BD494016870F5421DFDABFD9B09EE170BD494091D49F2D56DEDABF67BEE64370BD494092F584B5C5DDDABF383A068D6FBD49403820545062DDDABFBD1AC7C16EBD49405B538DF023DDDABF010D72196EBD49402086EEBC45DBDABFC458CC0B58BD494011EC7E1E91D9DABF6411ED1D44BD4940BE8591E3D9D9DABF9D965AB342BD4940791F1FA356DADABF6528EF2141BD494080EC56F230DBDABFC46642D13FBD494096B9093C3FDCDABFE0706EA73EBD4940D7B9EB1A77DDDABF7D2B4CCB3DBD49407F37D51FEDEEDABF0F4C0D523BBD4940B6C164C3EFFCDABF694B938B39BD49404F28B9E2AAFDDABF616CC75739BD49406A28AFD712FEDABFFAA422FD38BD4940B75B596770FEDABF11FBB16E38BD4940038F03F7CDFEDABF1472829F37BD4940D25B4F5CD8FEDABF5ECD2BF736BD4940D8F49D63B1FCDABFB8B444F71CBD4940BDF4A76E49FCDABF343214281CBD4940408E4944F6FBDABFB88417251BBD4940368E534F8EFBDABFE68BB98318BD4940288ED8545AFBDABF14A4F34916BD4940C727701F6FFBDABF002B85B814BD4940D1276614D7FBDABF1600A59812BD4940EA5AA9BF30FBDABF9FD45DDE0BBD494049C656B39FFADABF46E057D309BD49406DF44A95DDFADABF49E86FCB09BD4940C05A38D094FADABF42D65FD905BD4940914B17F45DFDDABFCE3A169303BD494034C3CE1D0703DBBF89CA59DD02BD49408EF6F3A79803DBBF71F2172309BD49408B29415E8A02DBBFDF2BF14909BD49408A8F4CBA0901DBBF028BB08A09BD4940AC2891B64AFFDABF6600AE8D0ABD494041C2328CF7FEDABFF0EC05360BBD49407BF5DC1B55FFDABF271A390410BD494080F5612121FFDABFBA9DE85411BD4940A3C1B791C3FEDABFA700A59812BD4940A95BDE6C3CFEDABF6501AF8E13BD49405C2834DDDEFDDABFB6D8CF6D15BD49400AC246A227FEDABF5FA54EEE19BD49407D097B6AB0FFDABF5748CB371ABD49401A0000004D7942F999FBDABF0F6EAB8A67BD4940CAC267390302DBBF16A099CD66BD49406729415E8A02DBBF5E8DF57266BD494083293753F202DBBF6E13581565BD4940E38F9F88DD02DBBF49978CE164BD4940DA299E37F603DBBFAF1AC1AD64BD494077C2855ACB00DBBF97A0E6EA40BD49404FF5D210BDFFDABF8F87CC0441BD4940A25BC04B74FFDABFA758AA2840BD4940A58E7EF101FFDABFDC157B593FBD4940DCF4753751FEDABF7B4FBC183FBD4940318E211896FDDABF7B4FBC183FBD4940F6263B7263F8DABFC8744FC43FBD4940DF20180ED2DFDABF785326E742BD494077ED77890CDFDABFFB9E6F9C43BD4940BC53EAC98FDEDABF1203E02A44BD49403FED8B9F3CDEDABF171A0FFA44BD49403FED8B9F3CDEDABFC2293EC945BD4940BC53EAC98FDEDABF521B36C849BD4940FEEDC04C48E1DABF76482B7269BD49404B216BDCA5E1DABFCD0C59416ABD494036EEAC3618E2DABFEA36AEE96ABD49409E214DBBDDE2DABF561345516BBD494097EE091084E3DABFE33752446BBD4940C327EB19A3FBDABF0842A8FA67BD49404D7942F999FBDABF0F6EAB8A67BD4940');
INSERT INTO "public"."cat_projects_geom" VALUES ('1557', 'MSC17139', '0106000020E6100000010000000103000000010000000E000000E4263B7263F8DABFC7744FC43FBD4940318E211896FDDABF7B4FBC183FBD4940DCF4753751FEDABF7B4FBC183FBD4940A58E7EF101FFDABFDC157B593FBD4940A25BC04B74FFDABFA758AA2840BD49404FF5D210BDFFDABF8F87CC0441BD494077C2855ACB00DBBF97A0E6EA40BD4940DA299E37F603DBBFAF1AC1AD64BD4940E38F9F88DD02DBBF49978CE164BD494083293753F202DBBF6E13581565BD49406729415E8A02DBBF5E8DF57266BD4940CAC267390302DBBF16A099CD66BD49404D7942F999FBDABF0F6EAB8A67BD4940E4263B7263F8DABFC7744FC43FBD4940');

-- ----------------------------
-- Alter Sequences Owned By 
-- ----------------------------

-- ----------------------------
-- Triggers structure for table cat_projects_geom
-- ----------------------------
CREATE TRIGGER "hftr" AFTER INSERT OR UPDATE ON "public"."cat_projects_geom"
FOR EACH ROW
EXECUTE PROCEDURE "add_multipart"();

-- ----------------------------
-- Primary Key structure for table cat_projects_geom
-- ----------------------------
ALTER TABLE "public"."cat_projects_geom" ADD PRIMARY KEY ("gid");



second table:

-- ----------------------------
-- Table structure for cat_projects
-- ----------------------------
DROP TABLE IF EXISTS "public"."cat_projects";
CREATE TABLE "public"."cat_projects" (
"gid" int4 DEFAULT nextval('sq'::regclass) NOT NULL,
"Name" varchar(255) COLLATE "default",
"Project Name" varchar(255) COLLATE "default",
"Service Code" varchar(255) COLLATE "default",
"Project Manager" varchar(255) COLLATE "default",
"Assistant Project Manager" varchar(255) COLLATE "default",
"Project_Owner" varchar(255) COLLATE "default",
"Total CAD estimated time" numeric(255,2),
"Total CAD spent time" numeric(255,2),
"Total CAD remaining time" numeric(255,2),
"the_geom" "public"."geometry",
"Site_estimated" numeric(255,2),
"Measurments" numeric(255,2),
"Geom_type" varchar(255) COLLATE "default",
"Site_Spent_time" numeric(255,2),
"Site_remaining_time" numeric(255,2)
)
WITH (OIDS=FALSE)

;

-- ----------------------------
-- Records of cat_projects
-- ----------------------------
INSERT INTO "public"."cat_projects" VALUES ('52', 'MSC17139', 'T2 / T3 Car Park', 'UUD-TS-GI', 'abc', 'abc', 'abc', '1012.00', '709.83', '302.17', null, null, null, null, null, null);


-- ----------------------------
-- Alter Sequences Owned By 
-- ----------------------------


-- ----------------------------
-- Uniques structure for table cat_projects
-- ----------------------------
ALTER TABLE "public"."cat_projects" ADD UNIQUE ("gid");

-- ----------------------------
-- Primary Key structure for table cat_projects
-- ----------------------------
ALTER TABLE "public"."cat_projects" ADD PRIMARY KEY ("gid");

因此应将触发器添加到cat_projects_geom中。我对吗?每次向此表添加新几何体时,触发器都应重新收集所有几何图形,并将多几何图形结果(包括新多边形)添加到 cat_projects.the_geom field。

BEGIN
            UPDATE cat_projects 
                SET the_geom = (SELECT ST_CollectionExtract(ST_Collect(the_geom), 3)
                                      FROM cat_projects_geom
                                      WHERE "Name" = NEW."Name"
                                                                    GROUP BY "Name")
                 WHERE "Name" = NEW."Name";

            RETURN NEW;
        END;

我将此触发器功能设置为后触发器,但是当我向cat_projects_geom添加新多边形时,cat_projects中没有发生任何事情。我期望用新的多边形更新几何体字段(可以说包含2个多边形)(所以它们一起应该包含3 ..

0 个答案:

没有答案