[R] Idetntifying nearest topographic contours to data points

Rolf Turner rolf.turner at xtra.co.nz
Sat Apr 16 03:54:32 CEST 2011


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
> point. Does anyone know how I could go about this?
>
> 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



More information about the R-help mailing list