Skip to content
Prev 21309 / 29559 Next

Finding distances between points and contour lines

Couple of clarifications:

 * I think you sent an HTML email which stripped out your example. Its
visible here:

http://r-sig-geo.2731867.n2.nabble.com/Finding-distances-between-points-and-contour-lines-tp7586733.html

but you shouldn't send HTML emails to the mailing list.

 * Do you want the minimal distance between the *vertices* of the
lines or the minimal distance between the lines themselves?

 For the former its just a question of extracting the vertex
coordinates from the spatial data structure and doing a distance
calculation between all pairs from each item. If you've transformed
your data to a regular cartesian coordinate system then Pythagoras
will help you.

 For the latter just turn your track into a SpatialLines object and
use gDistance from package rgeos:

trackSL = SpatialLines(LinesList=list(Lines(Line(coords=track.pts),1)))
gDistance(trackSL,contourSL)
[1] 0.02220655

this will return zero if the track defined by the points crosses the
contour, whereas the vertex distance method will return zero only if a
vertex is exactly on the contour.

# fiddle the track to cross the contour:

 track.pts[1,1]=.1

# rebuild the lines:

 trackSL = SpatialLines(LinesList=list(Lines(Line(coords=track.pts),1)))

# plot and check:

 lines(trackSL)

# distance should be zero:

 gDistance(trackSL,contourSL)
 # returns [1] 0
On Thu, Jul 10, 2014 at 10:23 PM, rhodes.uor <r.rhodes at pgr.reading.ac.uk> wrote: