[postgis-users] st_union() - a dumb question

Kevin Neufeld kneufeld at refractions.net
Thu Oct 25 13:02:58 PDT 2007


Here are a few ways to union/merge lines together.

CREATE TABLE tmp (geom geometry);
INSERT INTO tmp VALUES ('LINESTRING(0 0, 1 0, 2 0, 5 0)'::geometry);
INSERT INTO tmp VALUES ('LINESTRING(2 0, 5 0, 6 0, 7 0)'::geometry);
INSERT INTO tmp VALUES ('LINESTRING(1 0, 2 0, 5 0, 6 0)'::geometry);

SELECT AsText(ST_Union(geom)) FROM tmp;
                               astext                              
--------------------------------------------------------------------
 MULTILINESTRING((1 0,2 0),(2 0,5 0),(5 0,6 0),(6 0,7 0),(0 0,1 0))
(1 row)

SELECT AsText(ST_Collect(geom)) FROM tmp;
                                 astext                                
------------------------------------------------------------------------
 MULTILINESTRING((0 0,1 0,2 0,5 0),(2 0,5 0,6 0,7 0),(1 0,2 0,5 0,6 0))
(1 row)

SELECT AsText(ST_Union(geom)) FROM (SELECT ST_Collect(geom) as geom FROM 
tmp) AS foo;
                                 astext                                
------------------------------------------------------------------------
 MULTILINESTRING((0 0,1 0,2 0,5 0),(2 0,5 0,6 0,7 0),(1 0,2 0,5 0,6 0))
(1 row)

SELECT AsText(ST_Union(geom, ST_StartPoint(geom))) FROM (SELECT 
ST_Collect(geom) as geom FROM tmp) AS foo;
                               astext                              
--------------------------------------------------------------------
 MULTILINESTRING((0 0,1 0),(1 0,2 0),(2 0,5 0),(5 0,6 0),(6 0,7 0))
(1 row)

SELECT AsText(ST_LineMerge(geom)) FROM (SELECT ST_Union(geom) AS geom 
FROM tmp) AS foo;
               astext               
-------------------------------------
 LINESTRING(0 0,1 0,2 0,5 0,6 0,7 0)
(1 row)


So, to answer your question, yes, you should expect to see a 
multilinestring of 5 lines.
If you want to sew these lines together, use LineMerge(...)

Hope this helps,

-------------
Kevin Neufeld
Software Developer
Refractions Research Inc.
300-1207 Douglas St.
Victoria, B.C., V8W 2E7

Phone: (250) 383-3022
Email: kneufeld at refractions.net



Chris Hermansen wrote:
> Good people, I have a dumb question.  Yes it's really a dumb question.
>
> To accompany the dumb question, I have a dumb illustration that will
> definitely look really dumb unless viewed with a fixed-width font.
>
> Let's say I have three somewhat coincident lines.  Specifically, let's
> say they're precisely colinear, and "from the side", they look like this:
>
> A --------------------------------------
> B         --------------------------------------------------
> C    ----------------------------------------------
>
> What I'm wondering is, if I st_union() them together, should I expect as
> a result a linestring composed of five shorter segments that looks like
> this?  Or alternatively, maybe a bunch of short lines, or maybe a
> multilinestring?
>
> U ---+----+----------------------------+----------+---------
>
> In other words, with "nodes" where there were endpoints of the input lines?
>
>   



More information about the postgis-users mailing list