[postgis-users] Closing polylines

Nicolas Ribot nicolas.ribot at gmail.com
Thu May 17 05:37:08 PDT 2012


Sorry, the correct version handling all cases. Seems to work when there are
gaps between segments and when segments are touching each other:

create or replace function st_makeShortestLine(g1 geometry, g2 geometry)
returns geometry as $$

with dist as (
select  st_distance(st_endPoint($1), st_endPoint($2)) as dee,
        st_distance(st_endPoint($1), st_startPoint($2)) as des,
        st_distance(st_startPoint($1), st_endPoint($2)) as dse,
        st_distance(st_startPoint($1), st_startPoint($2)) as dss
) select case when st_distance(st_startPoint($1), st_startPoint($2)) = 0
then st_makeline(st_reverse($1), $2)
      when st_distance(st_startPoint($1), st_endPoint($2)) = 0 then
st_makeline($2, $1)
      when st_distance(st_endPoint($1), st_endPoint($2)) = 0 then
st_makeline($1, st_reverse($2))
      when st_distance(st_endPoint($1), st_startPoint($2)) = 0 then
st_makeline($1, $2)
      when dee < des and dee < dse and dee < dss then st_makeline($1,
st_reverse($2))
      when des < dee and des < dse and des < dss then st_makeline($1, $2)
      when dss < dee and dss < dse and dss < des then
st_makeline(st_reverse($1), $2)
      else st_makeline($2, $1) end
from dist;
$$ language SQL;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20120517/65dc0556/attachment.html>


More information about the postgis-users mailing list