건물과 학구 데이터 처리 저장 과정

13588 단어 데이터베이스
CREATE OR REPLACE FUNCTION buildingareaprocess()
RETURNS integer AS 
$BODY$

DECLARE 
r RECORD;
num int4 := 0;
sql varchar;
create_sql varchar;

BEGIN
	--       
    select UpdateGeometrySRID('public', 'qu_area', 'the_geom', 4326) ;
	--       、  、     schema          
	ALTER TABLE
    geo_data.public.buildingarea ADD COLUMN xiaoxue VARCHAR(250);
	ALTER TABLE
    geo_data.public.buildingarea ADD COLUMN zhongxue VARCHAR(250);
	ALTER TABLE
    geo_data.public.buildingarea ADD COLUMN qu VARCHAR(40);
	
	--    
	--IF to_regclass('geo_data.public.index_fid') IS NULL THEN
		--CREATE INDEX index_fid ON geo_data.public.buildingarea (fid);
	--END IF;
	CREATE INDEX IF NOT EXISTS index_buildingfid ON geo_data.public.buildingarea (fid);
	
	--       
	drop table if exists primary_tmp;
	drop table if exists middle_tmp;
	drop table if exists district_tmp;

	
	create table primary_tmp as
	select b.fid,array_to_string(group_concat(xx."NAME"),',') xiaoxue from buildingarea b,xx_area xx  where ST_Contains(xx.the_geom,b.the_geom) group by b.fid ;
	
	create table middle_tmp as
	select b.fid,array_to_string(group_concat(zx."NAME"),',') zhongxue from buildingarea b,cz_area zx where ST_Contains(zx.the_geom,b.the_geom) group by b.fid ;

	create table district_tmp as
	select b.fid,q."name" qu from buildingarea b,qu_area q where ST_Contains(q.the_geom,b.the_geom) ;

	--         
	CREATE INDEX IF NOT EXISTS index_primary_tmp ON primary_tmp (fid);
	CREATE INDEX IF NOT EXISTS index_middle_tmp ON middle_tmp (fid);
	CREATE INDEX IF NOT EXISTS index_district_tmp ON district_tmp (fid);

	--  
	update buildingarea b set xiaoxue = (select t.xiaoxue from primary_tmp t where b.fid=t.fid);
	update buildingarea b set zhongxue = (select t.zhongxue from middle_tmp t where b.fid=t.fid);
	update buildingarea b set qu = (select t.qu from district_tmp t where b.fid=t.fid);

	--     
	CREATE INDEX IF NOT EXISTS index_buildingqu ON geo_data.public.buildingarea (qu);

	--   buildingarea          
	sql := 'select distinct "name" qu from qu_area;';
	FOR r IN EXECUTE sql LOOP
		create_sql := 'create table geo_data.public.qu_'||num||' as select * from buildingarea where qu = '''||r.qu||'''';
		EXECUTE create_sql;
		num := num + 1;
	END LOOP;
	return num;

END;
$BODY$
LANGUAGE plpgsql;



  :
postgresql        ,         

CREATE AGGREGATE group_concat(anyelement) ( 
sfunc = array_append, --        ,   append     
stype = anyarray, --           
initcond = '{}' --        
);

좋은 웹페이지 즐겨찾기