Skip to content

Instantly share code, notes, and snippets.

@tkardi
Last active May 3, 2022 13:15
Show Gist options
  • Save tkardi/c295bbdcc2c7e21edb365fd3852894e9 to your computer and use it in GitHub Desktop.
Save tkardi/c295bbdcc2c7e21edb365fd3852894e9 to your computer and use it in GitHub Desktop.
tc
/* https://twitter.com/tkardi/status/1521366132371902465 */
drop table if exists tc;
create table tc as
select gid, path,
st_lineinterpolatepoint(
st_scale(
st_rotate(
st_exteriorring(
st_buffer(
st_setsrid(st_point(0,0),4326)::geography,
st_distance(st_setsrid(st_point(0,90),4326)::geography, geom::geography) / pi()
)::geometry(polygon, 4326)
),
radians(90)
), 1.70, 1.70 --3, 1.5 /* don't distort here, let mercator do it instead ! */
),
st_azimuth(st_setsrid(st_point(0, 90),4326)::geography,geom::geography) / (2*pi())
) as geom
from (
select gid, (st_dumppoints(geom)).*
from ne_10m_admin_0_countries
) f
where st_y(geom) >-85
--and gid = 176 /* antarctica */
;
/* outer bounds for antarctica, gid = 176*/
insert into tc (
gid, path, geom
)
select
gid, array[0, path[2], path[3]] as path, geom
from (
select
176 as gid,
(st_dumppoints(st_multi(st_envelope(st_makeline(array[st_setsrid(st_point(-180,-85),4326),st_setsrid(st_point(180,85),4326)]))))).*
) g
;
alter table tc add column oid serial;
alter table tc add constraint pk__tc primary key (oid);
create index sidx__tc on tc using gist (geom);
drop table if exists tc_area;
create table tc_area as
select
gid, (st_dump(st_buildarea(st_node(st_collect(l))))).geom as geom
from (
select
gid, path[1] as part, path[2] as ring,
st_makeline(array_agg(geom order by path[3])) as l
from tc
group by
gid, path[1], path[2]
) g
group by gid;
alter table tc_area add column oid serial;
alter table tc_area add constraint pk__tc_area primary key (oid);
create index sidx__tc_area on tc_area using gist (geom);
drop table if exists tc_area_continents;
create table tc_area_continents as
select (st_dump(st_union(geom))).geom
from tc_area;
/* https://twitter.com/tkardi/status/1520405854180503554 */
drop table if exists tc;
create table tc as
select row_number() over()::int as oid, gid, path,
st_project(
st_setsrid(st_point(-30,0),4326)::geography,
st_distance(st_setsrid(st_point(0,90),4326)::geography, geom::geography),
st_azimuth(st_setsrid(st_point(0,90),4326)::geography,geom::geography) - radians(125)
)::geometry(point, 4326) as geom
from (
select gid, (st_dumppoints(geom)).*
from ne_10m_admin_0_countries ) f
where st_y(geom) >-88
;
alter table tc add constraint pk__tc primary key (oid);
create index sidx__tc on tc using gist (geom);
--select * from tc limit 10
drop table if exists tc_area;
create table tc_area as
select
gid, (st_dump(st_buildarea(st_node(st_collect(l))))).geom as geom
from (
select
gid, path[1] as part, path[2] as ring,
st_makeline(array_agg(geom order by path[3])) as l
from tc
group by
gid, path[1], path[2]
) g
group by gid;
alter table tc_area add column oid serial;
alter table tc_area add constraint pk__tc_area primary key (oid);
create index sidx__tc_area on tc_area using gist (geom);
drop table if exists tc_area_continents;
create table tc_area_continents as
select (st_dump(st_union(geom))).geom
from tc_area;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment