[postgis-users] Re: Obtaining nearest points between two linestrings

Rodrigo Martín LÓPEZ GREGORIO rodrigomlg at yahoo.com.ar
Mon Dec 11 06:58:43 PST 2006


Thanks for all your replys.

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).
However this solution works fine for my needs right now (I'm open to any
suggestion).

Thanks all of you again.

Rodrigo.

Here is the code of my function:

CREATE OR REPLACE FUNCTION nearest_point(linestring1 geometry, linestring2
geometry) RETURNS geometry AS $BODY$
DECLARE
    mindistance float8;
    i integer;
    j integer;
    segmentaux1 geometry;
    segmentaux2 geometry;
    segmentmindist1 geometry;
    segmentmindist2 geometry;
    point1 geometry;
BEGIN
    mindistance := distance(linestring1,linestring2)+1;
    /* I set mindistance to distance between linstrings plus 1 so at least
there will be
    a case in wich the distance will be minor than this value */
    FOR i IN 1 .. NumPoints(linestring1) - 1 LOOP
        segmentaux1 :=MakeLine(PointN(linestring1, i), PointN(linestring1,
i+1));
        FOR j IN 1 .. NumPoints(linestring2) - 1 LOOP
            segmentaux2 :=MakeLine(PointN(linestring2, j),
PointN(linestring2, j+1));
            IF Distance(segmentaux1,segmentaux2) < mindistance THEN
                       mindistance := Distance(segmentaux1,segmentaux2);
                       segmentmindist1:=segmentaux1;
                       segmentmindist2:=segmentaux2;
            END IF;
        END LOOP;
    END LOOP;
    /* After this nested loops I have a pair of segments that are separated
by the minimum
    distance between linestrings.

    Now I find the Start/EndPoint of this two segments that minimize the
distance*/

    IF distance(StartPoint(segmentmindist1), segmentmindist2) = mindistance
THEN
        point1:=StartPoint(segmentmindist1);
    END IF;
    IF distance(EndPoint(segmentmindist1), segmentmindist2) = mindistance
THEN
        point1:=EndPoint(segmentmindist1);
    END IF;
    IF distance(StartPoint(segmentmindist2), segmentmindist1) = mindistance
THEN
        point1:=StartPoint(segmentmindist2);
    END IF;
    IF distance(EndPoint(segmentmindist2), segmentmindist1) = mindistance
THEN
        point1:=EndPoint(segmentmindist2);
    END IF;

    /* Once I find a point I create and return a line that goes from the
nearest point of linestring1
    to point1 to the nearest point of linestring2 to point1 so I can find
then the coordinates of this points
    using the StartPoint/EndPoint function with the x() and y() functions.*/

     RETURN makeline(line_interpolate_point(segmentmindist1,
line_locate_point(segmentmindist1, point1)),
line_interpolate_point(segmentmindist2, line_locate_point(segmentmindist2,
point1)));
END;
$BODY$ LANGUAGE plpgsql IMMUTABLE STRICT;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20061211/9459ee8d/attachment.html>


More information about the postgis-users mailing list