[R-sig-Geo] Finding distances between points and contour lines
Barry Rowlingson
b.rowlingson at lancaster.ac.uk
Fri Jul 11 09:44:09 CEST 2014
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:
> Hi all,
>
> I have two sets of data; a dataframe of lon/lat pairs which correspond to
> storm track locations, and a gridded rainfall dataset. I've taken contours
> of the rainfall dataset at a single level and saved them as a
> SpatialLinesDataFrame. I'm attempting to determine the distance between each
> of the storm track coordinates and each of the contours. Below is an example
> of what I have so far, but I can't figure out the next step:
>
>
>
> For each entry in track.pts I want to return a distance to the nearest point
> of each entry in contourSL (or in contours, for that matter). Can anyone
> recommend a way to do this?
> Thanks very much!
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Finding-distances-between-points-and-contour-lines-tp7586733.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
More information about the R-sig-Geo
mailing list