<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div></div><div><span><br></span></div><br><div class="yui_3_7_2_150_1386664103448_60" style="font-family: times new roman, new york, times, serif; font-size: 12pt;"><div class="yui_3_7_2_150_1386664103448_64" style="font-family: times new roman, new york, times, serif; font-size: 12pt;">Hi,<br><br>I
 have a dataset with start & end points that we are represtnting as 
linestrings. Many records do not have endpoints - so we are takimg the 
median bearling for comparable startpoints & creating an arbitrary 
endpoint.<br><br>While it is easy to get a bearing for a known endpoint 
with ST_Azimuth(), is there a simple way in Postgis to get the point at a
 defined distance & bearing? I've perused the docs & nothing 
jumped out as an obvious solution.</div></div><div><br></div><div><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">Thanks,</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;">  Brent Wood<br></div>  <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <hr size="1">  <font face="Arial" size="2"> <b><span style="font-weight:bold;">From:</span></b> Simon Greener <simon@spatialdbadvisor.com><br> <b><span style="font-weight:
 bold;">To:</span></b> PostGIS Users Discussion <postgis-users@lists.osgeo.org> <br> <b><span style="font-weight: bold;">Sent:</span></b> Thursday, April 11, 2013 6:57 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [postgis-users] Finding closet intersect point along direction of line<br> </font> </div> <div class="y_msg_container"><br><div id="yiv2930287765">
<style type="text/css">#yiv2930287765 body {font-family:'Times New Roman';font-size:13px;}</style>

<div><div>Adrian,</div><div><br>That's excellent.</div><div><br></div><div>regards</div><div>Simon</div><div><br></div><div>On Thu, 11 Apr 2013 16:51:14 +1000, <adrian.kitchingman@dse.vic.gov.au> wrote:<br></div><br><blockquote style="margin:0 0 0.80ex;border-left:#0000FF 2px solid;padding-left:1ex;"><font face="sans-serif" size="2">Thanks for the code Simon. After a few
tweaks I got it working perfectly in PostGIS2.</font><br><br><font face="sans-serif" size="2">Here's the updated code.</font><br><br><font face="sans-serif" size="2">Adrian</font><br><br><font face="sans-serif" size="2">CREATE OR REPLACE FUNCTION ST_Extend(eje_
geometry, bound_ geometry)</font><br><font face="sans-serif" size="2">RETURNS geometry</font><br><font face="sans-serif" size="2">AS $$</font><br><font face="sans-serif" size="2">-- Use Case: I need to "extend"
a linestring in another one. The new linestring must be</font><br><font face="sans-serif" size="2">-- obtained such as their new extremes
are the intersection of it with a</font><br><font face="sans-serif" size="2">-- polygon that contains it. The original
linestring is composed of only a</font><br><font face="sans-serif" size="2">-- linear segment.</font><br><font face="sans-serif" size="2">-- version: alfa , by Julio A. Galindo,
April 17, 2007: juliogalindoq at gmail.com</font><br><font face="sans-serif" size="2">DECLARE</font><br><font face="sans-serif" size="2">      b_ geometry = st_boundary(bound_);</font><br><font face="sans-serif" size="2">      dist float;</font><br><font face="sans-serif" size="2">      max_dist float
= 0;</font><br><font face="sans-serif" size="2">      n_points int;</font><br><font face="sans-serif" size="2">      pto_1 geometry;</font><br><font face="sans-serif" size="2">      pto_2 geometry;</font><br><font face="sans-serif" size="2">      first_pto geometry;</font><br><font face="sans-serif" size="2">      last_pto geometry;</font><br><font face="sans-serif" size="2">      u_1 float;</font><br><font face="sans-serif" size="2">      u_2 float;</font><br><font face="sans-serif" size="2">      norm float;</font><br><font face="sans-serif" size="2">      result text = 'LINESTRING(';</font><br><font face="sans-serif" size="2">BEGIN</font><br><font face="sans-serif" size="2">      IF  st_GeometryType(eje_)
  NOT LIKE 'ST_LineString' OR</font><br><font face="sans-serif" size="2">           st_GeometryType(bound_)
NOT LIKE 'ST_Polygon' THEN</font><br><font face="sans-serif" size="2">         RETURN
NULL;</font><br><font face="sans-serif" size="2">      END IF;</font><br><font face="sans-serif" size="2">      -- First Search
how far is the boundary: (worst case)</font><br><font face="sans-serif" size="2">      pto_1 := st_StartPoint(eje_);</font><br><font face="sans-serif" size="2">      pto_2 := st_EndPoint(eje_);</font><br><font face="sans-serif" size="2">      FOR i IN 1..st_NumPoints(b_)-1
LOOP</font><br><font face="sans-serif" size="2">         dist
:= st_distance(st_PointN(b_,i),pto_1);</font><br><font face="sans-serif" size="2">         IF
dist > max_dist THEN max_dist := dist; --END IF;</font><br><font face="sans-serif" size="2">           
max_dist := dist;</font><br><font face="sans-serif" size="2">         END
IF;</font><br><font face="sans-serif" size="2">         dist
:= st_distance(st_PointN(b_,i),pto_2);</font><br><font face="sans-serif" size="2">         IF
dist > max_dist THEN max_dist := dist; --END IF;</font><br><font face="sans-serif" size="2">           
max_dist := dist;</font><br><font face="sans-serif" size="2">         END
IF;</font><br><font face="sans-serif" size="2">      END LOOP;</font><br><font face="sans-serif" size="2">      -- Now extent the
linestring:</font><br><font face="sans-serif" size="2">      pto_2 := st_PointN(eje_,2);</font><br><font face="sans-serif" size="2">      u_1 := st_X(pto_2)-st_X(pto_1);</font><br><font face="sans-serif" size="2">      u_2 := st_Y(pto_2)-st_Y(pto_1);</font><br><font face="sans-serif" size="2">      norm := sqrt(u_1^2
+ u_2^2);</font><br><font face="sans-serif" size="2">      first_pto := st_MakePoint(st_X(pto_1)-u_1/norm*dist,st_Y(pto_1)-u_2/norm*dist);</font><br><font face="sans-serif" size="2">      n_points := st_nPoints(eje_);</font><br><font face="sans-serif" size="2">      IF n_points >
2 THEN</font><br><font face="sans-serif" size="2">         pto_1
:= st_PointN(eje_,n_points-1);</font><br><font face="sans-serif" size="2">         pto_2
:= st_PointN(eje_,n_points);</font><br><font face="sans-serif" size="2">         u_1
  := st_X(pto_2)-st_X(pto_1);</font><br><font face="sans-serif" size="2">         u_2
  := st_Y(pto_2)-st_Y(pto_1);</font><br><font face="sans-serif" size="2">         norm
 := sqrt(u_1^2 + u_2^2);</font><br><font face="sans-serif" size="2">      END IF;</font><br><font face="sans-serif" size="2">      last_pto := st_MakePoint(st_X(pto_2)+u_1/norm*dist,st_Y(pto_2)+u_2/norm*dist);</font><br><font face="sans-serif" size="2">      result   :=
result || st_X(first_pto) || ' ' || st_Y(first_pto) || ',';</font><br><font face="sans-serif" size="2">      FOR i IN 1..st_NumPoints(eje_)
LOOP</font><br><font face="sans-serif" size="2">          result
:= result || st_X(st_PointN(eje_,i)) || ' ' || st_Y(st_PointN(eje_,i))
|| ',';</font><br><font face="sans-serif" size="2">      END LOOP;</font><br><font face="sans-serif" size="2">      result := result
|| st_X(last_pto) || ' ' || st_Y(last_pto) || ')';</font><br><font face="sans-serif" size="2">      -- Find the final
Linestring:</font><br><font face="sans-serif" size="2">      b_ := st_intersection(st_GeomFromText(result,st_SRID(eje_)),bound_);</font><br><font face="sans-serif" size="2">      RETURN b_;</font><br><font face="sans-serif" size="2">  END $$</font><br><font face="sans-serif" size="2">  LANGUAGE plpgsql</font><br><font face="sans-serif" size="2">  STABLE</font><br><font face="sans-serif" size="2">  RETURNS NULL ON NULL INPUT;</font><br><font size="3"><br></font><font face="Verdana" size="1"><br></font><br>

<div>Notice:<br>This email and any attachments may contain information that is 
personal, confidential,<br>legally privileged and/or copyright. No part of it 
should be reproduced, adapted or communicated without the prior written consent 
of the copyright owner. </div>
<div>It is the responsibility of the recipient to check for and remove 
viruses.</div>
<div>If you have received this email in error, please notify the sender by return 
email, delete it from your system and destroy any copies. You are not authorised 
to use, communicate or rely on the information contained in this email.</div>
<div>Please consider the environment before printing this email.</div>
</blockquote><br><br><br><div id="yiv2930287765M2Signature"><div>-- </div><div>Holder of "2011 Oracle Spatial Excellence Award for Education and Research."<br>SpatialDB Advice and Design, Solutions Architecture and Programming,<br>Oracle Database 10g Administrator Certified Associate; Oracle Database 10g SQL Certified Professional<br>Oracle Spatial, SQL Server, PostGIS, MySQL, ArcSDE, Manifold GIS, FME, Radius Topology and Studio Specialist.<br>39 Cliff View Drive, Allens Rivulet, 7150, Tasmania, Australia.<br>Website: www.spatialdbadvisor.com<br>  Email: simon@spatialdbadvisor.com<br>  Voice: +61 362 396397<br>Mobile: +61 418 396391<br>Skype: sggreener<br>Longitude: 147.20515 (147° 12' 18" E)<br>Latitude: -43.01530 (43° 00' 55" S)<br>GeoHash: r22em9r98wg<br>NAC:W80CK 7SWP3</div></div></div></div><br>_______________________________________________<br>postgis-users mailing list<br><a ymailto="mailto:postgis-users@lists.osgeo.org"
 href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br><a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><br><br><br></div> </div> </div>  </div></body></html>