<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:st1="urn:schemas-microsoft-com:office:smarttags" xmlns="http://www.w3.org/TR/REC-html40">

<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<meta name=Generator content="Microsoft Word 11 (filtered medium)">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><o:SmartTagType
 namespaceuri="urn:schemas-microsoft-com:office:smarttags" name="country-region"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="City"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="place"/>
<o:SmartTagType namespaceuri="urn:schemas-microsoft-com:office:smarttags"
 name="PersonName"/>
<!--[if !mso]>
<style>
st1\:*{behavior:url(#default#ieooui) }
</style>
<![endif]-->
<style>
<!--
 /* Font Definitions */
 @font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
 /* Style Definitions */
 p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman";}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {color:blue;
        text-decoration:underline;}
p
        {mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman";}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:Arial;
        color:navy;}
@page Section1
        {size:8.5in 11.0in;
        margin:1.0in 1.25in 1.0in 1.25in;}
div.Section1
        {page:Section1;}
-->
</style>

</head>

<body lang=EN-US link=blue vlink=blue>

<div class=Section1>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'>Julio, THANKS!<o:p></o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<p class=MsoNormal><font size=2 color=navy face=Arial><span style='font-size:
10.0pt;font-family:Arial;color:navy'><o:p> </o:p></span></font></p>

<div>

<div class=MsoNormal align=center style='text-align:center'><font size=3
face="Times New Roman"><span style='font-size:12.0pt'>

<hr size=3 width="100%" align=center tabindex=-1>

</span></font></div>

<p class=MsoNormal><b><font size=2 face=Tahoma><span style='font-size:10.0pt;
font-family:Tahoma;font-weight:bold'>From:</span></font></b><font size=2
face=Tahoma><span style='font-size:10.0pt;font-family:Tahoma'> <st1:PersonName
w:st="on">postgis-users-bounces@postgis.refractions.net</st1:PersonName>
[mailto:<st1:PersonName w:st="on">postgis-users-bounces@postgis.refractions.net</st1:PersonName>]
<b><span style='font-weight:bold'>On Behalf Of </span></b>Julio Galindo<br>
<b><span style='font-weight:bold'>Sent:</span></b> Tuesday, April 17, 2007 1:53
AM<br>
<b><span style='font-weight:bold'>To:</span></b> PostGIS Users Discussion<br>
<b><span style='font-weight:bold'>Subject:</span></b> Re: [postgis-users]
Extending Linestrings</span></font><o:p></o:p></p>

</div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><o:p> </o:p></span></font></p>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>Hi Leticia,<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>I create the following function for your problem, please test it
carefully, I only made two test (at the end):<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>CREATE OR REPLACE FUNCTION ExtendLine(eje_ geometry, bound_ geometry)
RETURNS geometry AS $$<br>
-- version: alfa , by Julio A. Galindo, April 17, 2007: <a
href="mailto:juliogalindoq@gmail.com">juliogalindoq@gmail.com </a><br>
 DECLARE<br>
     b_ geometry = boundary(bound_);<br>
     dist float;<br>
     max_dist float = 0;<br>
     n_points int;<br>
     pto_1 geometry;<br>
     pto_2 geometry;<br>
     first_pto geometry;<br>
     last_pto geometry; <br>
     u_1 float;<br>
     u_2 float;<br>
     norm float;<br>
     result text = 'LINESTRING(';<br>
 BEGIN<br>
     IF GeometryType(eje_) NOT LIKE 'LINESTRING'<br>
  OR GeometryType(bound_) NOT LIKE 'POLYGON' THEN RETURN NULL; END
IF; <br>
     -- First Search how far is the boundary: (worst case)<br>
     pto_1 := StartPoint(eje_);<br>
     pto_2 := EndPoint(eje_);<br>
     FOR i IN 0..NumPoints(b_)-1 LOOP<br>
  dist := distance(PointN(b_,i),pto_1);<br>
  IF dist > max_dist THEN max_dist := dist; END IF; <br>
  dist := distance(PointN(b_,i),pto_2);<br>
  IF dist > max_dist THEN max_dist := dist; END IF;<br>
     END <st1:place w:st="on">LOOP</st1:place>;<br>
     -- Now extent the linestring:<br>
     pto_2 := PointN(eje_,2);<br>
     u_1 := X(pto_2)-X(pto_1); <br>
     u_2 := Y(pto_2)-Y(pto_1);<br>
     norm := sqrt(u_1^2 + u_2^2);<br>
     first_pto :=
MakePoint(X(pto_1)-u_1/norm*dist,Y(pto_1)-u_2/norm*dist);<br>
     n_points := nPoints(eje_);<br>
     IF n_points > 2 THEN<br>
  pto_1 := PointN(eje_,n_points-1);<br>
  pto_2 := PointN(eje_,n_points);<br>
  u_1 := X(pto_2)-X(pto_1);<br>
  u_2 := Y(pto_2)-Y(pto_1);<br>
  norm := sqrt(u_1^2 + u_2^2);<br>
     END IF;<br>
     last_pto :=
MakePoint(X(pto_2)+u_1/norm*dist,Y(pto_2)+u_2/norm*dist); <br>
     result := result || X(first_pto) || ' ' ||
Y(first_pto) || ',';<br>
     FOR i IN 1..NumPoints(eje_) LOOP<br>
  result := result || X(PointN(eje_,i)) || ' ' || Y(PointN(eje_,i))
|| ','; <br>
     END LOOP;<br>
     result := result || X(last_pto) || ' ' || Y(last_pto)
|| ')';<br>
     -- Find the final Linestring:<br>
     b_ :=
intersection(GeomFromText(result,SRID(eje_)),bound_);<br>
     RETURN b_; <br>
 END $$<br>
 LANGUAGE plpgsql<br>
 STABLE<br>
 RETURNS NULL ON NULL INPUT;<br>
 <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>Test 1:<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>select asText(extendLine(geomFromText('LINESTRING(5 5, 12
12)',-1),GeomFromText('POLYGON((0 0,15 0,15 20,0 20,0 0))',-1)));<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>            
astext             
<br>
---------------------------------<br>
 LINESTRING(0 0,5 5,12 12,15 15)<br>
 <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>The solution is good, because the line is at 45 degress <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>And the same function can work with a general linestring (many points):<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>select asText(extendLine(geomFromText('LINESTRING(5 5, 12 12, 13
12)',-1),GeomFromText('POLYGON((0 0,15 0,15 20,0 20,0 0))',-1)));<br>
               
astext                
<br>
--------------------------------------- <br>
 LINESTRING(0 0,5 5,12 12,13 12,15 12)<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>Let me know if you improve this simple function.<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>Regards,<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'> <o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>Julio A. Galindo Q.<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'>Topogen Ltda.<o:p></o:p></span></font></p>

</div>

<div>

<p class=MsoNormal><st1:place w:st="on"><st1:City w:st="on"><font size=3
  face="Times New Roman"><span style='font-size:12.0pt'>La Paz</span></font></st1:City>,
 <st1:country-region w:st="on">Bolivia</st1:country-region></st1:place><br>
<br>
 <o:p></o:p></p>

</div>

<div>

<p class=MsoNormal><span class=gmailquote><font size=3 face="Times New Roman"><span
style='font-size:12.0pt'>On 4/16/07, <b><span style='font-weight:bold'>Leticia</span></b>
<<a href="mailto:lgomez@itba.edu.ar">lgomez@itba.edu.ar</a>> wrote:</span></font></span>
<o:p></o:p></p>

<div vlink=purple link=blue>

<div>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>Hi community,</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>I need to "extend"
a linestring in another one. The new linestring must be obtained such as their
new extremes are the intersection of it with a polygon that contains it. The
original linestring is composed of only a linear segment. </span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'> </span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>Is there a simple method to
do this?</span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'> </span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'> </span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>-------------</span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>|    
      |</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>      |    
|    |</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>      |    
|    |</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>     
|           |</span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>-------------</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>I need the other one:</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>-------|-----</span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>|    
|    |</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>      |    
|    |</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>      |    
|    |</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>      |    
|    |</span></font><o:p></o:p></p>

<p style='text-indent:.5in'><font size=2 face="Courier New"><span
style='font-size:10.0pt;font-family:"Courier New"'>-------|-----</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>The original linestring is not always vertical u horizontal.</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>Thanks in advance,</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'>Leticia</span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face="Courier New"><span style='font-size:10.0pt;font-family:
"Courier New"'> </span></font><o:p></o:p></p>

<p><font size=2 face=Arial><span style='font-size:10.0pt;font-family:Arial'> </span></font><o:p></o:p></p>

</div>

</div>

<p class=MsoNormal style='margin-bottom:12.0pt'><font size=3
face="Times New Roman"><span style='font-size:12.0pt'><br>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a><br>
<a href="http://postgis.refractions.net/mailman/listinfo/postgis-users"
target="_blank">http://postgis.refractions.net/mailman/listinfo/postgis-users </a><o:p></o:p></span></font></p>

</div>

<p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size:
12.0pt'><o:p> </o:p></span></font></p>

</div>

</body>

</html>