Hi anhtin<br><br>To find the nearest polyline from a table to a point you can do this:<br><br>SELECT * FROM mainroad ORDER BY Distance(the_geom,PointFromText('POINT(517651 2121421)')) LIMIT 1<br><br>To get the coordinates of the nearest point of that line to your point then you must do:
<br><br>SELECT *, line_interpolate_point(the_geom,line_locate_point(the_geom,PointFromText('POINT(517651 2121421)'))) FROM mainroad ORDER BY Distance(the_geom,PointFromText('POINT(517651 2121421)')) LIMIT 1
<br><br>So with the last query you can get all the data (*) of the nearest linestring and the point of the that linestring that is nearest to the given Point.<br><br>The way it works is, first the line_locate_point function get the linstring and the point and gives you a value 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. Then the line_interpolate_point function will take the linestring and the value between 0 and 1 and return a Point geometry with the location of the nearest point on the linestring. If you want to get the X and Y coordinates of that point you can do also X(geometry), Y(geometry):
<br><br>SELECT *, X(line_interpolate_point(the_geom,line_locate_point(the_geom,PointFromText('POINT(517651 2121421)')))),<br>Y(line_interpolate_point(the_geom,line_locate_point(the_geom,PointFromText('POINT(517651 2121421)')))) FROM mainroad ORDER BY Distance(the_geom,PointFromText('POINT(517651 2121421)')) LIMIT 1
<br><br>Rodrigo.<br><br><div><span class="gmail_quote">On 6/16/07, <b class="gmail_sendername">anhtin</b> <<a href="mailto:anhtin@gmail.com">anhtin@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>hi all<br>I am developing a web application. i am using Mapserver and Postgis<br>I have a question below<br>if i want to click one point on map to select the nearest point on the<br>nearest polyline from the point i clicked. How can i do?
<br>Notes: The point I clicked is converted to X,Y coordinate (295149 2315499)<br><br>could you show me the sql command in postgis.<br>1. How can i select the nearest polyline on the click point.<br>2. How can i select the the nearest point on the selected nearest polyline.
<br>:computer-user:<br>:rules:<br><br>Somebody suggest that I should search the polyline nearest on the click<br>point by the script below<br><br>SELECT * FROM mainroad WHERE GeomFromText('POINT(517651 2121421)', 42102) &&
<br>the_geom<br> AND distance(the_geom,<br>GeomFromText('POINT(517651 2121421)', 42102)) < 200000<br><br>however, sometime it has no result because the distance from point to<br>polyline is larger than 200000.
<br>--<br>View this message in context: <a href="http://www.nabble.com/Select-Point-near-Polyline-Postgis-tf3931432.html#a11150683">http://www.nabble.com/Select-Point-near-Polyline-Postgis-tf3931432.html#a11150683</a><br>
Sent from the PostGIS - User mailing list archive at <a href="http://Nabble.com">Nabble.com</a>.<br><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">http://postgis.refractions.net/mailman/listinfo/postgis-users</a><br></blockquote></div><br>