pg,gp創建分區表、計算經緯度距離,轉換漂移經緯度

------------------------------------------------------------------------------------------------------

1.計算經緯度距離

CREATE OR REPLACE FUNCTION "dts_vehicle_resource"."getdistance"("i_latbegin" float8, "i_lngbegin" float8, "i_latend" float8, "i_lngend" float8)
  RETURNS "pg_catalog"."float8" AS $BODY$

--author  :
--created :
--purpose :calc the distance between diff lat and lng

declare

v_distance real;
v_earth_radius real;
v_radlatbegin real;
v_radlatend real;
v_radlatdiff real;
v_radlngdiff real;

BEGIN  

    --地球半徑
    v_earth_radius:=6378.137;
     
    v_radlatbegin := i_latbegin * pi()/180.0;
    v_radlatend   := i_latend * pi()/180.0;
    v_radlatdiff  := v_radlatbegin - v_radlatend;
    v_radlngdiff  := i_lngbegin * pi()/180.0 - i_lngend * pi()/180.0;

    v_distance := 2 * asin(sqrt(power(sin(v_radlatdiff / 2), 2) + cos(v_radlatbegin) * cos(v_radlatend) * power(sin(v_radlngdiff/2),2)));
    v_distance := v_distance * v_earth_radius*1000;
      
    RETURN v_distance; 
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

------------------------------------------------------------------------------------------------------

2.將漂移的經緯度轉爲真正的經緯度

CREATE OR REPLACE FUNCTION "dts_figure_resource"."transform_lat_lon"("lat" float8, "lon" float8)
  RETURNS "pg_catalog"."varchar" AS $BODY$

--author  :
--created :
--purpose : 將漂移的經緯度轉爲真正的經緯度

declare
a float8;
ee float8;
radLat float8;
dLon  float8;
dLat float8;
magic float8;
sqrtMagic float8;
lat_lon VARCHAR;
BEGIN  
 
    -- 公共部分
        --  a: 衛星橢球座標投影到平面地圖座標系的投影因子。
           a = 6378245.0; 
            -- ee: 橢球的偏心率。
        ee = 0.00669342162296594323; 
                radLat = lat / 180.0 * PI();
        magic = sin(radLat);
        magic = 1 - ee * magic * magic;
        sqrtMagic =sqrt(magic);    
    -- 轉換緯度
    
         --dLat = this.transformLat(lon - 105.0, lat - 35.0);
                dLat = -100.0 + 2.0 * (lon - 105.0)+ 3.0 * (lat - 35.0) + 0.2 * (lat - 35.0) * (lat - 35.0) + 0.1 * (lon - 105.0) * (lat - 35.0)+ 0.2 * sqrt(abs(lon - 105.0));
                dLat =dLat+(20.0 * sin(6.0 *(lon - 105.0)* PI()) + 20.0 * sin(2.0 *(lon - 105.0) * PI())) * 2.0 / 3.0;
                dLat =dLat+(20.0 * sin((lat - 35.0) * PI()) + 40.0 * sin((lat - 35.0) / 3.0 * PI())) * 2.0 / 3.0;
                dLat =dLat+ (160.0 * sin((lat - 35.0) / 12.0 * PI()) + 320 * sin((lat - 35.0) * PI() / 30.0)) * 2.0 / 3.0;
   
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * PI());
       dLat= lat + dLat;
                raise notice '%',dLat;
                -- 轉換經度
                --double dLon = this.transformLon(lon - 105.0, lat - 35.0);
                 dLon = 300.0 + (lon - 105.0)+ 2.0 * (lat - 35.0) + 0.1 *(lon - 105.0) * (lon - 105.0) + 0.1 * (lon - 105.0) * (lat - 35.0)+ 0.1 * sqrt(abs((lon - 105.0)));
        dLon =dLon +(20.0 * sin(6.0 * (lon - 105.0) * PI()) + 20.0 * sin(2.0 * (lon - 105.0) * PI())) * 2.0 / 3.0;
        dLon =dLon + (20.0 * sin((lon - 105.0)* PI()) + 40.0 * sin((lon - 105.0) / 3.0 * PI())) * 2.0 / 3.0;
        dLon =dLon + (150.0 * sin((lon - 105.0) / 12.0 * PI()) + 300.0 * sin((lon - 105.0) / 30.0 * PI())) * 2.0 / 3.0;
         
                dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * PI());
        dLon = lon + dLon;
                    raise notice '%',dLon;
      -- 返回
            lat_lon =round(dLat::numeric,6)::varchar||','||round(dLon::numeric,6)::varchar;
      
    RETURN lat_lon; 
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

------------------------------------------------------------------------------------------------------

3.創建分區表

CREATE OR REPLACE FUNCTION "dts_figure_resource"."create_partition_tb_by_subpartition1"("cur_month" varchar)
  RETURNS "pg_catalog"."int8" AS $BODY$
    --  按月創建分區表 按日分區 按小時創建子分區,帶默認分區。
    -- 作者:餘偉
    --sql: SELECT "dts_figure_resource"."create_partition_tb"('201912')
    DECLARE
    cur_month1 text;
    tablename_cur_month text;
    ts int8;
    first_day text;
    end_day text;
    year_month text;
    create_sql text;
    result1 record;
    day_cnt int4;
    first_hour text;
    end_hour text;
    begin
    cur_month1:=substring(cur_month,1,4)||'-'||substring(cur_month,5, 2);
        tablename_cur_month:=substring(cur_month,1,4)||'_'||substring(cur_month,5, 2);
SELECT EXTRACT(epoch FROM TIMESTAMPTZ(cur_month1||'-01 00:00:00')) INTO ts;
SELECT to_char(to_TIMESTAMP(round(ts)),'yyyy-MM-dd')  INTO first_day;
select EXTRACT(DAY from date_trunc('month', first_day::date)- interval '-1 month'- interval '1 day') INTO day_cnt;
SELECT to_char(to_TIMESTAMP(round(ts+3600*24*day_cnt)),'yyyy-MM-dd')  INTO end_day;
first_hour=0;
end_hour=24;
--   raise notice '%',first_day;
--       raise notice '%',end_day;
    -- 創建分區表語句
 create_sql:='create table viid_facestatic.face_archive_'||tablename_cur_month||'(like viid_facestatic.face_archive_hour)
DISTRIBUTED BY(face_id) partition by range (cur_date) subpartition by list(cur_hour) 
subpartition template
(
        subpartition ph_0 VALUES(''00''),subpartition ph_1 VALUES(''01''),subpartition ph_2 VALUES(''02''),
            subpartition ph_3 VALUES(''03''),subpartition ph_4 VALUES(''04''),subpartition ph_5 VALUES(''05''),
            subpartition ph_6 VALUES(''06''),subpartition ph_7 VALUES(''07''),subpartition ph_8 VALUES(''08''),
              subpartition ph_9 VALUES(''09''),subpartition ph_10 VALUES(''10''),subpartition ph_11 VALUES(''11''),
              subpartition ph_12 VALUES(''12''),subpartition ph_13 VALUES(''13''),subpartition ph_14 VALUES(''14''),
                subpartition ph_15 VALUES(''15''),subpartition ph_16 VALUES(''16''),subpartition ph_17 VALUES(''17''),
                subpartition ph_18 VALUES(''18''),subpartition ph_19 VALUES(''19''),subpartition ph_20 VALUES(''20''),
                subpartition ph_21 VALUES(''21''),subpartition ph_22 VALUES(''22''),subpartition ph_23 VALUES(''23''),
                default subpartition default_p
)
( partition pn_ start ('''||first_day||'''::date) end ('''||end_day||'''::date) every (''1 day''::interval), 
    default partition default_p);';
EXECUTE create_sql;

  raise notice '%',create_sql;
    
RETURN 11; 
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

------------------------------------------------------------------------------------------------------

4.創建分區表

CREATE OR REPLACE FUNCTION "dts_figure_resource"."create_partition_tb"("cur_month" varchar)
  RETURNS "pg_catalog"."int8" AS $BODY$
    --  按月創建分區表 按日分區。
    -- 作者:餘偉
    --sql: SELECT "dts_figure_resource"."create_partition_tb"('201912')
    DECLARE
    cur_month1 text;
    tablename_cur_month text;
    ts int8;
    first_day text;
    end_day text;
    year_month text;
    create_sql text;
    result1 record;
    day_cnt int4;
    
    begin
    cur_month1:=substring(cur_month,1,4)||'-'||substring(cur_month,5, 2);
    tablename_cur_month:=substring(cur_month,1,4)||'_'||substring(cur_month,5, 2);
SELECT EXTRACT(epoch FROM TIMESTAMPTZ(cur_month1||'-01 00:00:00')) INTO ts;
SELECT to_char(to_TIMESTAMP(round(ts)),'yyyy-MM-dd')  INTO first_day;
select EXTRACT(DAY from date_trunc('month', first_day::date)- interval '-1 month'- interval '1 day') INTO day_cnt;
SELECT to_char(to_TIMESTAMP(round(ts+3600*24*day_cnt)),'yyyy-MM-dd')  INTO end_day;

    -- 創建分區表語句
 create_sql:='create table viid_facestatic.face_archive_'||tablename_cur_month||'(like viid_facestatic.face_archive)
DISTRIBUTED BY(face_id) partition by range (cur_date)
( partition pn_ start ('''||first_day||'''::date) end ('''||end_day||'''::date) every (''1 day''::interval), 
    default partition default_p);';
EXECUTE create_sql;

  raise notice '%',create_sql;
    
RETURN 11; 
end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100

------------------------------------------------------------------------------------------------------

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章