Well. Sorry for the delay. Here I'm with a posible solution.<br><br>I downloaded your data file and take a look at it. When I take a look I figure out that there is no way line_locate_point and line_interpolate_point work with MULTILINESTRING so the first solution I though was join your MULTILINESTRINGS in just one LINESTRING but looking at the complexity of your data I though that this was not a good idea, speccialy cause the diferents linestring in a MULTILINESTRING were not geographically continuous so joining all in one linestring was not an alternative. So I write a stored function (preety simple by the way) that get what you need. The function get two parameters... the first is a multilinestring, the second, your point. The function loops over all linestrings in the multilinestring, get the nearest one and then with that linestring calculates the location of the nearest point of the linestring to your point. To get this function working run this script and it will create your function (take a look at the end of the function and chanche the user name if necesary; mine was postgres):
<br><br><div style="margin-left: 40px;">-- Function: multiline_locate_point(amultils geometry,apoint geometry)<br><br>-- DROP FUNCTION multiline_locate_point(amultils geometry,apoint geometry);<br><br>CREATE OR REPLACE FUNCTION multiline_locate_point(amultils geometry,apoint geometry)
<br>  RETURNS geometry AS<br>$BODY$<br>DECLARE<br>    mindistance float8;<br>    nearestlinestring geometry;<br>    nearestpoint geometry;<br>    i integer;<br><br>BEGIN<br>    mindistance := (distance(apoint,amultils)+100);
<br>    FOR i IN 1 .. NumGeometries(amultils) LOOP<br>        if distance(apoint,GeometryN(amultils,i)) < mindistance THEN<br>            mindistance:=distance(apoint,GeometryN(amultils,i));<br>            nearestlinestring:=GeometryN(amultils,i);
<br>        END IF;<br>    END LOOP;<br>    nearestpoint:=line_interpolate_point(nearestlinestring,line_locate_point(nearestlinestring,apoint));<br>    RETURN nearestpoint;<br>END;<br>$BODY$<br>  LANGUAGE 'plpgsql' IMMUTABLE STRICT;
<br>ALTER FUNCTION multiline_locate_point(amultils geometry,apoint geometry) OWNER TO postgres;<br></div><br>Once the function is stored and available for use, you can make the query like:<br><br><div style="margin-left: 40px;">
SELECT *, multiline_locate_point(the_geom,PointFromText('POINT(517651 2121421)', 42102)) <br>FROM mainroad ORDER BY Distance(the_geom,PointFromText('POINT(517651 2121421)', 42102)) LIMIT 1 <br></div><br>However, the query speed is a little bit slow if you do the query in that way. You can use the next query instead wich first gets the nearest multilinestring to your point and then call my function with that geometry instead of calling the function for all geometries:
<br><br>SELECT *, multiline_locate_point(the_geom,PointFromText('POINT(517651 2121421)', 42102)) FROM<br>(SELECT *, Distance(the_geom,PointFromText('POINT(517651 2121421)', 42102)) as dist <br>FROM mainroad ORDER BY Distance(the_geom,PointFromText('POINT(517651 2121421)', 42102)) LIMIT 1 ) as foo
<br><br>On my PC the second query takes 1/6 of the time that tooks the first one so the speed is much better at least here ;)<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 Rodrigo
<br>this  my column the_geom is type "MULTILINESTRING"<br>and all geometry data on this table  is type = "MULTILINESTRING"<br>when i run this script:<br>SELECT * ,<br>line_interpolate_point(the_geom,line_locate_point(the_geom,PointFromText('POINT(517651
<br>2121421)', 42102)))<br>FROM mainroad WHERE (GeometryType(the_geom) = 'LINESTRING')<br> ORDER BY Distance(the_geom,PointFromText('POINT(517651 2121421)', 42102))<br>LIMIT 1<br><br>it not error, however it has not result because geometry data is type =
<br>"MULTILINESTRING"<br><br>but when i add a condition is:<br>WHERE (GeometryType(the_geom) = 'LINESTRING') or (GeometryType(the_geom) =<br>'MULTILINESTRING')<br> it have exception error:<br><br>
ERROR: line_locate_point: 1st arg isnt a line<br>SQL state: XX000<br><br>I think this script error because the condition (GeometryType(the_geom) =<br>'MULTILINESTRING')<br><br>How could i do? Because my all geometry data is MULTILINESTRING  type.
<br>Could i convert my geometry data to orther data type ("LINESTRING").<br>But i think it not good if i convered to orther type.<br><br>this is my Data MainRoad. Can u try to use it. And u could show me the good
<br>a way :).<br><a href="http://www.nabble.com/file/p11151446/data%2Btype%2BMainRoad.rar">http://www.nabble.com/file/p11151446/data%2Btype%2BMainRoad.rar</a><br>data+type+MainRoad.rar<br><br><br><br><br><br><br><br>Rodrigo Martín LÓPEZ GREGORIO-3 wrote:
<br>><br>> I think the problem obviously is that some geometries in your table are<br>> not<br>> linestrings or multilinestrings. You can check it doing:<br>><br>> SELECT GeometryType(the_geom) FROM mainroad WHERE GeometryType(the_geom)
<br>> !=<br>> 'LINESTRING'<br>><br>> Then you will find wich Geometries are not linestring and those are the<br>> ones<br>> that are giving you some problems. You must take a look at that geometries
<br>> and choose what to do with them.<br>><br>> As a temporary sollution I think you can add a condition to your query<br>> that<br>> only takes into account for the result the geometries that are LINESTRING.
<br>> So your query will look something like this:<br>><br>> SELECT * ,<br>> line_interpolate_point(the_geom,line_locate_point(the_geom,PointFromText('POINT(5176512121421)',<br>> 42102))) FROM<br>> mainroad WHERE GeometryType(the_geom) = 'LINESTRING' ORDER BY
<br>> Distance(the_geom,PointFromText('POINT(517651 2121421)', 42102)) LIMIT 1<br>><br>> Maybe you can also consider the 'MULTILINESTRING' geometries; the<br>> line_interpolate_point should work also with that type of geometry. So the
<br>> condition will be:<br>><br>> WHERE (GeometryType(the_geom) = 'LINESTRING') or (GeometryType(the_geom) =<br>> 'MULTILINESTRING')<br>><br>> I never used any SRID in my querys but I don't think that can be a problem
<br>> at all.<br>><br>> Rodrigo.<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>><br>><br><br>--<br>View this message in context: <a href="http://www.nabble.com/Select-Point-near-Polyline-Postgis-tf3931432.html#a11151446">
http://www.nabble.com/Select-Point-near-Polyline-Postgis-tf3931432.html#a11151446</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>