【PostGIS】实时坐标-电子围栏-判断-(参考遥想公瑾当年postgres+socket.io+nodejs实时地图应用实践)

CREATE OR REPLACE FUNCTION "public"."process_t_gps"()
  RETURNS "pg_catalog"."trigger" AS $BODY$
    DECLARE
        rec record;
                ewith record;
    BEGIN
        IF (TG_OP = 'DELETE') THEN
            --插入的GPS都是4326的经纬度,我们将在3857的谷歌底图上显示数据,发送转换后的3857出去
            --select TG_OP TG_OP,OLD.id,ST_AsGeojson(ST_AsText(ST_Transform(OLD.geom,3857))) geom,gid as null into rec; 
            --perform pg_notify('gps',row_to_json(rec)::text);
                        
                        select TG_OP TG_OP,OLD.id,ST_AsGeojson(ST_AsText(OLD.geom)) geom into rec;
            perform pg_notify('gps',row_to_json(rec)::text);
                        
 
                        --SELECT gid,ST_AsGeojson(ST_AsText(s.geom)) as epg,OLD.id,ST_AsGeojson(ST_AsText(NEW.geom)) as gps FROM eploygon s where st_within( OLD.geom , s.geom) into ewith;
                        --raise notice 'data:%',row_to_json(ewith)::text;
                        --perform pg_notify('gpswith',row_to_json(ewith)::text);
                     
                        
            RETURN OLD;
        ELSIF (TG_OP = 'UPDATE') THEN 
                
                
                     SELECT gid,ST_AsGeojson(ST_AsText(s.geom)) as epg,NEW.id,ST_AsGeojson(ST_AsText(NEW.geom)) as gps FROM eploygon s where st_within( NEW.geom , s.geom) into ewith;
                     --raise notice 'data:%',row_to_json(ewith)::text;
                     --perform pg_notify('gpswith',row_to_json(ewith)::text);
                     
                     
           select TG_OP TG_OP,NEW.id,ST_AsGeojson(ST_AsText(NEW.geom)) geom,null as gid into rec;
                     rec.gid = ewith.gid;
                     raise notice 'data:%',row_to_json(rec)::text;
           perform pg_notify('gps',row_to_json(rec)::text);  
                        
            RETURN NEW;
        ELSIF (TG_OP = 'INSERT') THEN
                
                     SELECT gid,ST_AsGeojson(ST_AsText(s.geom)) as epg,NEW.id,ST_AsGeojson(ST_AsText(NEW.geom)) as gps FROM eploygon s where st_within( NEW.geom , s.geom) into ewith;
                     --raise notice 'data:%',row_to_json(ewith)::text;
                     --raise notice 'data:%',ewith.gid;
                     --perform pg_notify('gpswith',row_to_json(ewith)::text);
                     
            select TG_OP TG_OP,NEW.id,ST_AsGeojson(ST_AsText(NEW.geom)) geom ,null as gid into rec;
                        rec.gid = ewith.gid;
                        raise notice 'data:%',row_to_json(rec)::text;
            perform pg_notify('gps',row_to_json(rec)::text);
                         
            RETURN NEW;
        END IF;
        RETURN NULL;
    END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

参考来源:https://www.jianshu.com/p/4c075a69ca13?mType=Group

 备注:pg触发的事件名称需要小写,否则不会发送,如:perform pg_notify('gps',row_to_json(rec)::text);  

博客地址: http://www.cnblogs.com/defineconst/
博客版权: 本文以学习、研究和分享为主,欢迎转载和各类爬虫,但必须在文章页面明显位置给出原文链接。 如果文中有不妥或者错误的地方还望高手的您指出,以免误人子弟。如果您有更好的建议,不如留言一起讨论,共同进步! 再次感谢您耐心的读完本篇文章。
原文地址:https://www.cnblogs.com/defineconst/p/15056647.html