[postgis-users] why doesn't linemerge() work?

Andrew Ukrainec aukrainec at accipiterradar.com
Fri Oct 19 07:20:19 PDT 2012


Thanks Nicolas.   After some further browsing and thinking I arrived
on the solution.

First, I tried this:

ST_LineMerge( ST_Collect(ST_AddPoint(G1,ST_StartPoint(G2)),G2)) );

By appending the first point of G2 LINESTRING to the end of G1 LINESTRING,
the ST_LineMerge now works.  Except, it only works up to and including 3D. 
I'm using the 4th dimesion M to store a timestamp with my XYZ points,
and this gets stripped off by ST_LineMerge and returns 3D.

Then I tried:

ST_MakeLine(G1,G2)

and it works!  Very embarassing.  The key is this comment in
ST_MakeLine documentation:

"Availability: 2.0.0 - Support for linestring input elements was introduced"

This explains why older posts on the net didn't show this obvious solution.
Probably would be helpful to add a simple example to the docs with
two linestrings as input to ST_MakeLine to emphasize the functionality.

Nicolas Ribot wrote:
> Hi Andrew,
>
> This (long) thread may give you some hints on how to order and then
> merge lines together:
> "Closing polylines", 11 May 2012.
>
> Nicolas
>
> On 19 October 2012 00:27, Andrew Ukrainec <aukrainec at accipiterradar.com> wrote:
>   
>> I've been trying to append a new LINESTRING to an existing
>> LINESTRING in a table, and this seems to be an impossible
>> task.  I've searched the net for solutions, and tried all
>> possible obvious approaches:
>>
>> ST_LineMerge(ST_Collect(geom1,geom2))
>> ST_LineMerge(ST_Union(geom1,geom2))
>> ...
>>
>> I wrote a function to use the aggregate version of the call
>>
>> ST_LineMerge(ST_Collect(geom))
>>
>> I keep getting a MULTILINESTRING() at the output rather
>> than a single LINESTRING().
>>
>> If I run this example adapted from an example on
>> the OSGeo site:
>>
>> SELECT ST_AsText(ST_LineMerge(ST_Collect(the_geom)))
>>   FROM (SELECT 'LINESTRING(0 0, 0 1)'::geometry the_geom
>>   UNION ALL
>>   SELECT 'LINESTRING(1 0, 1 1)'::geometry the_geom
>>   UNION ALL
>>   SELECT 'LINESTRING(0 0, 1 0)'::geometry the_geom
>>   UNION ALL
>>   SELECT 'LINESTRING(1 1, 0 1)'::geometry the_geom) as a;
>>
>> I get a single LINESTRING that I hoped for,
>>
>>   LINESTRING(0 0, 1 0, 1 1, 0 1, 0 0)
>>
>> but the answer is completely counter intuitive to me.
>> I expected the individual LINESTRINGs to be simply appended
>> in order.  If I adjust the data in the first LINESTRING so
>> that '0 1' point is now '1 0':
>>
>> SELECT ST_AsText(ST_LineMerge(ST_Collect(the_geom)))
>>   FROM (SELECT 'LINESTRING(0 0, 1 0)'::geometry the_geom
>>   UNION ALL
>>   SELECT 'LINESTRING(1 0, 1 1)'::geometry the_geom
>>   UNION ALL
>>   SELECT 'LINESTRING(0 0,1 0)'::geometry the_geom
>>   UNION ALL
>>   SELECT 'LINESTRING(1 1, 0 1)'::geometry the_geom) as a;
>>
>> then again the operation fails and I get MULTILINESTRING()
>> at the output.
>>
>>   MULTILINESTRING((1 0,1 1,0 1),(1 0,0 0,1 0))
>>
>> The PostGIS documentation only contains the mysterious
>> statement in the Example section:
>>
>> "--If can't be merged - original MULTILINESTRING is returned"
>>
>> I can accept that LINEMERGE() does not work the way I imagined
>> it would, but then there is no explanation of how it does work
>> in the documentation.  In the case that one wants to glue
>> two LINESTRING() together into a single LINESTRING() preserving
>> the order of the points, can anyone suggest how this can
>> be done efficiently?
>>
>> Thanks in advance.
>>
>> _______________________________________________
>> postgis-users mailing list
>> postgis-users at postgis.refractions.net
>> http://postgis.refractions.net/mailman/listinfo/postgis-users
>>     
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
>
>   


-- 
Dr. Andrew Ukrainec
Principal Engineer                  e-mail: aukrainec at accipiterradar.com

Accipiter Radar Technologies Inc.          http://www.accipiterradar.com
Niagara, Ontario             Tel: (905) 228-6888      Fax: (905)892-2249
Orchard Park, New York       Tel: (716) 508-4432      Fax: (888)393-6421

NOTICE: This e-mail transmission and its attachments are intended solely for
the recipients and may contain information that is proprietary or provided as
commercial-in-confidence.  Should you receive this e-mail or its attachments
and are not an intended recipient, you are hereby requested to notify the
sender and to delete and not forward the received information to any other
party.




More information about the postgis-users mailing list