| 1 | This 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 | |
| 3 | Example Usage: |
| 4 | {{{ |
| 5 | --Example: Geocode on a forward range from TIGER line file (Assuming integer range fields) |
| 6 | select |
| 7 | st_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' |
| 12 | from tl_edges te |
| 13 | join tl_addr ta on te.tlid = ta.tlid |
| 14 | where fullname = '3rd St' and 12710 between fromhn::int and tohn::int and zip = '92399' |
| 15 | and (12710 % 2) = (fromhn::int % 2) |
| 16 | and fromhn::int < tohn::int; |
| 17 | }}} |
| 18 | |
| 19 | Function Definition: |
| 20 | {{{ |
| 21 | create or replace function st_line_interpolate_point(m_geom geometry, m_percent double precision, m_offset double precision) |
| 22 | returns 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 | |
| 31 | declare m_seg geometry; |
| 32 | declare p1 geometry; |
| 33 | declare p2 geometry; |
| 34 | |
| 35 | BEGIN |
| 36 | |
| 37 | m_seg := st_linesubstring(m_geom,0, m_percent); |
| 38 | |
| 39 | /* Grab the 2 points of the matching line segment */ |
| 40 | p1:=st_pointn(m_seg,st_npoints(m_seg)-1); |
| 41 | p2:=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 */ |
| 46 | return 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 | |
| 52 | END; |
| 53 | |
| 54 | $BODY$ |
| 55 | language plpgsql; |
| 56 | }}} |