10. ロジックをSQLで表記
-- コアエリア(1ha以上の樹林を抽出)
Select * from hn_base
where vegetation = 'forest'
and area_m2 > 20000;
-- コアエリアからのバッファ
drop table if exists hn_shijyukara_cab;
create table hn_shijyukara_cab as
with hn_shijyukara_cab_tmp as
(select st_union(st_buffer(st_simplify(st_transform(the_geom,32654),10), 400)) buff from hn_shijyukara_ca)
select
st_transform(buff, 4326) the_geom,
st_transform(buff, 3857) the_geom_webmercator,
st_area(buff) area_m2
from hn_shijyukara_cab_tmp;
alter table hn_shijyukara_cab add cartodb_id serial;
select cdb_cartodbfytable('carto','hn_shijyukara_cab');
-- HQB
drop table if exists hn_shijyukara_hqb;
create table hn_shijyukara_hqb as
with hn_shijyukara_hqb_tmp2 as
(with hn_shijyukara_hqb_tmp as
(select cartodb_id, st_transform(st_buffer(st_simplify(st_transform(the_geom,32654),10), 200), 4326) the_geom
from hn_shijyukara_ca)
select cartodb_id, the_geom from hn_shijyukara_hqb_tmp
where hn_shijyukara_hqb_tmp.cartodb_id in
(select t1.cartodb_id from hn_shijyukara_hqb_tmp t1, hn_base t2
where st_intersects(t1.the_geom, t2.the_geom)
group by t1.cartodb_id
having sum(st_area(st_transform(t2.the_geom,32654))) > 40000
and max(st_area(st_transform(t2.the_geom,32654))) > 14500))
select st_union(the_geom) the_geom
from hn_shijyukara_hqb_tmp2;
alter table hn_shijyukara_hqb add the_geom_webmercator geometry;
update hn_shijyukara_hqb set the_geom_webmercator = st_transform(the_geom, 3857);
alter table hn_shijyukara_hqb add area_m2 float4;
update hn_shijyukara_hqb set area_m2 = st_area(st_transform(the_geom, 32654));
alter table hn_shijyukara_hqb add cartodb_id serial;
select cdb_cartodbfytable('carto','hn_shijyukara_hqb');
-- PSC
drop table if exists hn_shijyukara_psc;
create table hn_shijyukara_psc as
with hn_shijyukara_psc_tmp as
(select st_intersection(t1.the_geom, t2.the_geom) the_geom
from hn_base t1, hn_shijyukara_hqb t2
where t1.vegetation='forest' and t1.area_m2 < 10000
and st_intersects (t1.the_geom, t2.the_geom))
select the_geom, st_transform(the_geom, 3857) the_geom_webmercator, st_area(st_transform(the_geom, 32654)) area_m2
from hn_shijyukara_psc_tmp;
alter table hn_shijyukara_psc add cartodb_id serial;
select cdb_cartodbfytable('carto','hn_shijyukara_psc');
-- PSB
drop table if exists hn_shijyukara_psb;
create table hn_shijyukara_psb as
with hn_shijyukara_psb_tmp as
(select st_union(st_buffer(st_simplify(st_transform(the_geom, 32654), 10), 200)) buff from hn_shijyukara_psc)
select st_transform(buff, 4326) the_geom,
st_transform(buff, 3857) the_geom_webmercator, st_area(buff) area_m2
from hn_shijyukara_psb_tmp;
alter table hn_shijyukara_psb add cartodb_id serial;