You are on page 1of 2

PostGIS 1.5.

1 Manual
245 / 315

7.10.2 ST_Line_Locate_Point
Name
ST_Line_Locate_Point Returns a float between 0 and 1 representing the location of the closest point on LineString to the given
Point, as a fraction of total 2d line length.

Synopsis
float ST_Line_Locate_Point(geometry a_linestring, geometry a_point);

Description
Returns a float between 0 and 1 representing the location of the closest point on LineString to the given Point, as a fraction of
total 2d line length.
You can use the returned location to extract a Point (ST_Line_Interpolate_Point) or a substring (ST_Line_Substring).
This is useful for approximating numbers of addresses
Availability: 1.1.0

Examples
--Rough approximation of finding the street number of a point along the street
--Note the whole foo thing is just to generate dummy data that looks
--like house centroids and street
--We use ST_DWithin to exclude
--houses too far away from the street to be considered on the street
SELECT ST_AsText(house_loc) As as_text_house_loc,
startstreet_num +
CAST( (endstreet_num - startstreet_num)
* ST_Line_Locate_Point(street_line, house_loc) As integer) As street_num
FROM
(SELECT ST_GeomFromText(LINESTRING(1 2, 3 4)) As street_line,
ST_MakePoint(x*1.01,y*1.03) As house_loc, 10 As startstreet_num,
20 As endstreet_num
FROM generate_series(1,3) x CROSS JOIN generate_series(2,4) As y)
As foo
WHERE ST_DWithin(street_line, house_loc, 0.2);
as_text_house_loc | street_num
-------------------+-----------POINT(1.01 2.06) |
10
POINT(2.02 3.09) |
15
POINT(3.03 4.12) |
20
--find closest point on a line to a point or other geometry
SELECT ST_AsText(ST_Line_Interpolate_Point(foo.the_line, ST_Line_Locate_Point(foo.the_line , ST_GeomFromText(POINT(4 3)))))
FROM (SELECT ST_GeomFromText(LINESTRING(1 2, 4 5, 6 7)) As the_line) As foo;
st_astext
---------------POINT(3 4)

PostGIS 1.5.1 Manual


246 / 315

See Also
ST_DWithin, ST_Length2D, ST_Line_Interpolate_Point, ST_Line_Substring

7.10.3 ST_Line_Substring
Name
ST_Line_Substring Return a linestring being a substring of the input one starting and ending at the given fractions of total 2d
length. Second and third arguments are float8 values between 0 and 1.

Synopsis
geometry ST_Line_Substring(geometry a_linestring, float startfraction, float endfraction);

Description
Return a linestring being a substring of the input one starting and ending at the given fractions of total 2d length. Second and
third arguments are float8 values between 0 and 1. This only works with LINESTRINGs. To use with contiguous MULTILINESTRINGs use in conjunction with ST_LineMerge.
If start and end have the same value this is equivalent to ST_Line_Interpolate_Point.
See ST_Line_Locate_Point for computing the line location nearest to a Point.
Note
Since release 1.1.1 this function also interpolates M and Z values (when present), while prior releases set them to
unspecified values.

Availability: 1.1.0 , Z and M supported added in 1.1.1


This function supports 3d and will not drop the z-index.

Examples

A linestring seen with 1/3 midrange overlaid (0.333, 0.666)

You might also like