Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.