Skip to content

Instantly share code, notes, and snippets.

@caiohamamura
Forked from pramsey/segments.sql
Last active July 16, 2016 20:11
Show Gist options
  • Select an option

  • Save caiohamamura/d53c77887df137159d800701adc7cdf2 to your computer and use it in GitHub Desktop.

Select an option

Save caiohamamura/d53c77887df137159d800701adc7cdf2 to your computer and use it in GitHub Desktop.
Convert a Linestring into a set of two-point segments
CREATE TABLE lines (
gid integer primary key,
geom geometry(Linestring, 4326)
);
INSERT INTO lines VALUES (1, 'SRID=4326;LINESTRING(1 1, 2 2, 3 3, 4 4)');
INSERT INTO lines VALUES (2, 'SRID=4326;LINESTRING(0 1, 0 2, 0 3, 0 4)');
SELECT ST_AsText((ST_Dump(ST_Split(lines.geom, points.geom))).geom)
FROM
(
SELECT ST_Union(pt) geom FROM (
SELECT gid, geom, (ST_DumpPoints(geom)).geom AS pt FROM lines) a
) points,
lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment