# Re: [R] Idetntifying nearest topographic contours to data points

From: Rolf Turner <rolf.turner_at_xtra.co.nz>
Date: Sat, 16 Apr 2011 13:54:32 +1200

On 16/04/11 04:18, sam.e wrote:
> Hi there,
> I have two data sets, one of locations at different elevations (x,y,z) and
> the other of points that make up topographic contours (also x,y,z). I have
> used:
>
> result<-apply(distppll(data2,cbind(topocon[-nrow(topocon),],topocon[-1,])),1,min)
>
> where 'data2' are my measurement coordinates (x, y) and 'topocon' are my
> topographic contours (x,y). While this gives me an output of the distance
> from each data point to the nearest topographic contour point, I would also
> like to output the 'z' value of the nearest topographic point to each data
>
> Ideally what I would like to output is a matrix which has:
> data elevation (data2\$z) distance to nearest contour elevation of
> nearest contour (topocon\$z)
>
> Any help would be much appreciated.

I presume you are using the distppll() function from the "spatstat" package.

If so I believe that you can get at what you want by setting the "mintype" argument of this function equal to 2:

distppll(whatever,mintype=2)

This will cause disppll() to return the index of the line segment which is nearest
to each point. You can then, with a modicum of effort, extract the associated
altitude (i.e. z) value.

This leaves the somewhat vexing issue of which *end* of the segment to use to determine z. Presumably there will be at least a slight difference between
the altitudes of the endpoints, and disppll() just gives the distance from the
point to the segment; it doesn't --- as it stands --- tell you which endpoint of
the segment is nearest to the point in question. (It may of course be the case that the point on the segment which is nearest to the point in question is *not* an endpoint of the segment.)

Presuming that you are satisfied with obtaining the endpoint of each segment which is nearest each of your locations and then obtaining the altitude of that
endpoint, may I suggest that you

• convert the collection of segments to a point pattern, using the endpoints() function
• use crossdist() to obtain the distances from each location (having represented the collections of locations as a point pattern) to each endpoint
• use which.min() applied to the rows of the resulting matrix of distances to find the index of the endpoint which achieves the minimum, whence you can work out the associated altitude.

It might be convenient to make the associated altitude values into the "marks"
component of the respective point patterns.

HTH.      cheers,

Rolf Turner

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 16 Apr 2011 - 01:58:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 16 Apr 2011 - 23:30:31 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.