# [R-sig-Geo] shortest distance from points to polylines

Kamran Safi ksafi at orn.mpg.de
Wed May 20 12:41:08 CEST 2009

```Hi Pilar,

I had to do the same recently and find the shortest distance from a
point to a line. The solution I chose was to point sample the line and
then find the nearest point from the sample. You can work with
spsample() and spDistN1().
Here's the bit that did it for me (it can be written shorter):

tempPoints <- spsample(<Line>, n=samplesize, type="regular")  # sample
it at an interval
tempPoints<- as.matrix(as.data.frame(tempPoints))   #get their coordinates
dists <- as.data.frame(cbind(Longitude=tempPoints[,1],
Latitude=tempPoints[,2], distance=spDistsN1(as.matrix(tempPoints),
c(<POF_Long>, <POF_Lat>), longlat=FALSE)))  #calculate the distances of
the point of interest from the sample points on the segment
dists <- dists[order(dists\$distance),]  #sort by distance
P1 <- dists[1,c("Longitude", "Latitude")] # get the closest point on the
segment to the watch site

hth

Kami

Pilar Tugores Ferra wrote:
> Hi list!
> I need computing shortest distance from points to a polyline layer (coast line).
> I've been searching all the morning a function in R that can do this but couldn't find anything!
> Is there any R function that do this? Or can anybody give me some hint about how it could be done?
> Kind regards!
> Pilar
>
>
> Mª Pilar Tugores Ferrà
> PhD Student
> Instituto Español de Oceanografía
> Centro Oceanográfico de Baleares
> Muelle de Poniente s/n
> 07015 Palma de Mallorca
> Baleares, España
> Telf.: (34) 971 401561
>
> La información contenida en este e-mail y sus ficheros adjuntos es totalmente confidencial y no debería ser usado si no fuera usted alguno de los destinatarios. Si ha recibido este e-mail por error, por favor avise al remitente y bórrelo de su buzón o de cualquier otro medio de almacenamiento.   This email is confidential and should not be used by anyone who is not the original intended  recipient. If you have received this e-mail in  error please inform the sender and delete it from  your mailbox or any other storage mechanism.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3275 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20090520/0f88894f/attachment.bin>
```

More information about the R-sig-Geo mailing list