Thanks for all your replys.<br><br>I finally make this work. Using your suggestions, I create my own function that works fine. The function will return a line pointing from linestring1 to linestring2 at minimum distance points. Maybe there will be some improvements to my function, like considering different posibles situations (
e.g. parallel linestrings segments, etc.) and giving more suitable result (i.e. taking into consideration all the points at the minimum distance and not just one).<br>However this solution works fine for my needs right now (I'm open to any suggestion).
<br><br>Thanks all of you again.<br><br>Rodrigo.<br><br>Here is the code of my function:<br><br>CREATE OR REPLACE FUNCTION nearest_point(linestring1 geometry, linestring2 geometry) RETURNS geometry AS $BODY$<br>DECLARE<br>
mindistance float8;<br> i integer;<br> j integer;<br> segmentaux1 geometry;<br> segmentaux2 geometry;<br> segmentmindist1 geometry;<br> segmentmindist2 geometry;<br> point1 geometry;<br>BEGIN<br> mindistance := distance(linestring1,linestring2)+1;
<br> /* I set mindistance to distance between linstrings plus 1 so at least there will be<br> a case in wich the distance will be minor than this value */<br> FOR i IN 1 .. NumPoints(linestring1) - 1 LOOP<br> segmentaux1 :=MakeLine(PointN(linestring1, i), PointN(linestring1, i+1));
<br> FOR j IN 1 .. NumPoints(linestring2) - 1 LOOP<br> segmentaux2 :=MakeLine(PointN(linestring2, j), PointN(linestring2, j+1));<br> IF Distance(segmentaux1,segmentaux2) < mindistance THEN<br>
mindistance := Distance(segmentaux1,segmentaux2);<br> segmentmindist1:=segmentaux1;<br> segmentmindist2:=segmentaux2;<br> END IF;<br> END LOOP;
<br> END LOOP;<br> /* After this nested loops I have a pair of segments that are separated by the minimum<br> distance between linestrings.<br><br> Now I find the Start/EndPoint of this two segments that minimize the distance*/
<br><br> IF distance(StartPoint(segmentmindist1), segmentmindist2) = mindistance THEN<br> point1:=StartPoint(segmentmindist1);<br> END IF;<br> IF distance(EndPoint(segmentmindist1), segmentmindist2) = mindistance THEN
<br> point1:=EndPoint(segmentmindist1);<br> END IF;<br> IF distance(StartPoint(segmentmindist2), segmentmindist1) = mindistance THEN<br> point1:=StartPoint(segmentmindist2);<br> END IF;<br> IF distance(EndPoint(segmentmindist2), segmentmindist1) = mindistance THEN
<br> point1:=EndPoint(segmentmindist2);<br> END IF;<br><br> /* Once I find a point I create and return a line that goes from the nearest point of linestring1<br> to point1 to the nearest point of linestring2 to point1 so I can find then the coordinates of this points
<br> using the StartPoint/EndPoint function with the x() and y() functions.*/<br><br> RETURN makeline(line_interpolate_point(segmentmindist1, line_locate_point(segmentmindist1, point1)), line_interpolate_point(segmentmindist2, line_locate_point(segmentmindist2, point1)));
<br>END;<br>$BODY$ LANGUAGE plpgsql IMMUTABLE STRICT;