[postgis-users] Obtaining nearest points between two linestrings

Christian Heine christian at geosci.usyd.edu.au
Thu Dec 7 22:27:01 PST 2006


Rodrigo,

> I was trying to find a way to obtain the coordinates of the nearest  
> points between two linestring. I play some time with the distance,  
> line_locate_point, line_interpolate_point and other functions but I  
> didn't find a way to get the information I want. I think that what  
> I'm trying to find is a function like line_locate_point that obtain  
> the point on one linestring nearest to another linestring.
>
> I know that there may be lot of cases in wich the "nearest point"  
> could be more than one (e.g . parallel segments, two linestring  
> with two sections of parallel segments at the minimum distance,  
> etc), but at least I want to get one (any) of this points.
>
> I also try a workaround creating a buffer around one of the  
> linestring using the distance between the linestrings as width,  
> finding the intersection between the buffer and the other  
> linestring and obtaining the centroid of the result, but I couldn't  
> make this works.

below an old message by Micheal Fuhr in reply to a simliar problem I  
had finding the _longest_ distance between two points in a polygon.  
With some tinkering you should be able to tackle your problem.

HTH,
Christian




Date:	Wednesday, 1 February 2006 12:57:21 PM Australia/Sydney
From:	Michael Fuhr
Subject:	Re: [postgis-users] Longest axis of a polygon
To:	PostGIS Users Discussion
Reply-To:	PostGIS Users Discussion

On Tue, Jan 31, 2006 at 05:01:13PM +1100, Christian Heine wrote:
 > > I'm not familiar with solving this problem so maybe this isn't
 > > correct or what you need, but could you look for the pair of points
 > > on the polygon's (simplified) exterior ring or convex hull  
separated
 > > by the most distance?  It would be easy to iterate through the
 > > points in a function.
 >
 > Thanks for the suggestion!  I looked at convexhull() and this seems
 > to be quite ok for my purposes -
 > How do I loop over a set of points in PostGIS/SQL?

Here's a function that I've only minimally tested.  As I said, I'm
not familiar with solving this particular problem so corrections
or improvements are welcome.  One possible problem is that for
certain geometries the function might return a linestring that lies
entirely outside the geometry, except for the endpoints; a crescent
comes to mind.

CREATE FUNCTION longestaxis(geom geometry) RETURNS geometry AS $$
DECLARE
     hull    geometry := convexhull(geom);
     ering   geometry;
     len     double precision;
     maxlen  double precision := -1;
     maxi    integer;
     maxj    integer;
BEGIN
     IF geometrytype(hull) IN ('POINT', 'LINESTRING') THEN
         RETURN hull;
     END IF;

     ering := exteriorring(hull);

     FOR i IN 1 .. npoints(ering) - 2 LOOP
         FOR j IN i + 1 .. npoints(ering) - 1 LOOP
             len := distance(pointn(ering, i), pointn(ering, j));
             IF len > maxlen THEN
                 maxlen := len;
                 maxi := i;
                 maxj := j;
             END IF;
         END LOOP;
     END LOOP;

     RETURN makeline(pointn(ering, maxi), pointn(ering, maxj));
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

-- 
Michael Fuhr
_______________________________________________
postgis-users mailing list
postgis-users at postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users







More information about the postgis-users mailing list