Skip to content

Instantly share code, notes, and snippets.

@FxKu
Last active June 7, 2019 20:46
Show Gist options
  • Save FxKu/f3aec4492d9459b67669cc22ac59fc20 to your computer and use it in GitHub Desktop.
Save FxKu/f3aec4492d9459b67669cc22ac59fc20 to your computer and use it in GitHub Desktop.
Grouping intersecting lines
-- TIL: Create groups of intersecting lines (split by type)
-- first, create some example intersecting and non-intersecting lines
WITH lines(id, geom, type) AS (
VALUES (1, 'LINESTRING(1 1,3 1)'::geometry, 'type_A'),
(2, 'LINESTRING(2 1,2 2)'::geometry, 'type_A'),
(3, 'LINESTRING(3 2,4 2)'::geometry, 'type_A'),
(4, 'LINESTRING(3 1,3 2)'::geometry, 'type_B'),
(5, 'LINESTRING(3 2,3 3)'::geometry, 'type_B')
)
SELECT type, ST_AsText(
ST_CollectionHomogenize( -- convert to single and multi-linestrings
unnest(ST_ClusterIntersecting(geom)) -- create collections, put intersecting geoms together
)
)
FROM lines
GROUP BY type; -- don't aggregate geoms of a different type
-- thanks Claas Leiner for asking: https://lists.fossgis.de/pipermail/fossgis-talk-liste/2019-June/010055.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment