# [postgis-users] Nearest point on an linestring

Martel, Christian Christian.Martel at drdc-rddc.gc.ca
Wed Dec 22 08:05:56 PST 2004

```Create a pl/pgsql function. Here is a draft algorithm (go back to your math
books for the exact equations).

- Find the nearest point of the line to P1
- Find both point before and after that point. You have now two segments to
analyse (line1 and line2).
- For each segment :
- calculate the slope s1
- calculate the perpendicular of that slope (s2)
- calculate the equation of the line (line3) with slope s2 that
crosses P1
- find the intersection between line3 and the current segment
- test if the intersection is on the segment. If not, calculate and
test for the other segment.
- If the intersection is still not on the segment, then the nearest point IS
the point found in step 1.

HTH

Christian

-----Original Message-----
From: skoehler at buschjena.de [mailto:skoehler at buschjena.de]
Sent: Tuesday, December 21, 2004 10:03
To: PostGIS Users Discussion
Subject: Antwort: Re: [postgis-users] Nearest point on an linestring

but I search for an new point on a linestring.

*L1
|
|
x        *P1
|
|
*
In this example I search the coordinates for point x. The * are the points

from the linestring and the point.
Any idea?

bye
Steffen

postgis-users-bounces at postgis.refractions.net schrieb am 21.12.2004
09:53:09:

> create a plp/pgsql function using numpoints() and pointn() and find the
> minimum distance for each point from the linestring to your query
point...
>
> numpoints() help you to know the number of points in the lienstring and
> pointn() extract the n° point of the lienstring
> ----------------------------------------------------------------
> TECHER Jean David
> Responsable Informatique 01MAP
> e-mail: davidtecher at yahoo.fr
> Bureau: 04 67 45 60 27
> Portable: 06 85 37 36 75
> site perso : http://techer.pascal.free.fr/postgis/
> K-S:"The greatest trick the devil pulled off was convincing people he
didn't
> exist"
> ------------------------------------------------------------
> ----- Original Message -----
> From: <skoehler at buschjena.de>
> To: <postgis-users at postgis.refractions.net>
> Sent: Tuesday, December 21, 2004 8:57 AM
> Subject: [postgis-users] Nearest point on an linestring
>
>
> > Hello,
> >
> > with the distance function I can compute the distance between an
> > linestring and an point. But how can I compute the nearest point on
the
> > linestring to my query point?
> > I think the distance function needs this point also and so the
information
> >
> > is in the system, how can I use it?
> >
> > Thanks
> > Steffen
> > _______________________________________________
> > postgis-users mailing list
> > postgis-users at postgis.refractions.net
> > http://postgis.refractions.net/mailman/listinfo/postgis-users
> >
>
>
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users

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

```