Skip to content

Instantly share code, notes, and snippets.

@leonmvd
Created January 8, 2019 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmvd/7ef1f30ede970d86102019d746ca1f84 to your computer and use it in GitHub Desktop.
Save leonmvd/7ef1f30ede970d86102019d746ca1f84 to your computer and use it in GitHub Desktop.
Rectangle splitter for Postgis
-- blades part adapted from "https://gis.stackexchange.com/questions/104439/how-to-extend-a-straight-line-in-postgis"
with medialaxis_multi as(
select
polygons.id,
st_union(ST_ApproximateMedialAxis(geom)) as geom
from
polygons
group by
polygons.id
),
medialaxis_single as(
select
id,
(st_dump(geom)).geom as geom
from
medialaxis_multi
),
blades as (
select
id,
st_makeline(st_translate(a, sin(az1) * len, cos(az1) * len),st_translate(b,sin(az2) * len, cos(az2) * len)) as geom
from (
select
id,
a,
b,
st_azimuth(a,b) as az1, st_azimuth(b, a) as az2, st_distance(a,b) + 1 as len
from (
select
id,
st_startpoint(geom) as a,
st_endpoint(geom) as b
from
medialaxis_single
) as sub
) as sub2
)
select
blades.id,
(st_dump(st_split(polygons.geom,blades.geom))).geom
from
polygons,
blades
where
polygons.id = blades.id
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment