Changes between Initial Version and Version 1 of UsersWikiExamplesInterpolateWithOffset


Ignore:
Timestamp:
10/26/09 17:20:54 (15 years ago)
Author:
veggivore
Comment:

New example

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiExamplesInterpolateWithOffset

    v1 v1  
     1This code creates another st_line_interpolate_point with a 3rd argument for offset of the point to the left or right side of the line segment. Useful for Geocoding.
     2
     3Example Usage:
     4{{{
     5--Example: Geocode on a forward range from TIGER line file (Assuming integer range fields)
     6select
     7st_line_interpolate_point(te.the_geom,
     8        ((12710 - fromhn::float) / (tohn::float - fromhn::float)),
     9        case when side = 'R' then .0001 else -.0001 end
     10),
     11'12710 3rd St, 92399'
     12from tl_edges te
     13join tl_addr ta on te.tlid = ta.tlid
     14where fullname = '3rd St' and 12710 between fromhn::int and tohn::int and zip = '92399'
     15and (12710 % 2) = (fromhn::int % 2)
     16and fromhn::int < tohn::int;
     17}}}
     18
     19Function Definition:
     20{{{
     21create or replace function st_line_interpolate_point(m_geom geometry, m_percent double precision, m_offset double precision)
     22returns geometry as
     23$BODY$
     24
     25/*
     26        Interpolate a point across a line and then offset to the left ( negative ) or right by m_offset distance
     27        m_percent 0 to 1
     28
     29*/
     30
     31declare m_seg geometry;
     32declare p1 geometry;
     33declare p2 geometry;
     34
     35BEGIN
     36
     37m_seg :=  st_linesubstring(m_geom,0, m_percent);
     38
     39/* Grab the 2 points of the matching line segment */
     40p1:=st_pointn(m_seg,st_npoints(m_seg)-1);
     41p2:=st_endpoint(m_seg);
     42
     43/* get the delta of the line segment and offset the interpolated point */
     44/* Rotate deltas by 90 degrees (invert dx/dy ) */
     45/* spheroid projections will need the offset to vary based on lat/lon */
     46return st_translate(
     47        p2,
     48        ((st_y(p2) - st_y(p1)) / st_distance(p1,p2)) * m_offset,
     49        ((st_x(p2) - st_x(p1)) / st_distance(p1,p2)) * (-m_offset)
     50);
     51
     52END;
     53
     54$BODY$
     55language plpgsql;
     56}}}