Search code examples
rterra

Meaning of terra::nearest() to_id column


I'm trying to use R's terra::nearest() function, but I cannot understand the output format and how to actually figure out which point was nearest! Here's a simple example:

> dest <- terra::vect(data.frame(x = c(0, 0, 5, 5), y = c(0, 5, 0, 5), attr = 1:4), geom = c("x", "y"))
> src <- terra::vect(data.frame(x = c(3, 1, 2), y = c(2, 3, 1)), geom = c("x", "y"))
> near_ret <- terra::nearest(src, dest)
> print(near_ret)
 class       : SpatVector 
 geometry    : points 
 dimensions  : 3, 7  (geometries, attributes)
 extent      : 0, 5, 0, 5  (xmin, xmax, ymin, ymax)
 coord. ref. :  
 names       : from_id from_x from_y to_id  to_x  to_y distance
 type        :   <num>  <num>  <num> <num> <num> <num>    <num>
 values      :       1      3      2     1     5     0    2.828
                     2      1      3     2     0     5    2.236
                     3      2      1     3     0     0    2.236

OK, so to_x, to_y and distance columns make sense to me and look correct. But to_id doesn't make any sense to me. I would have assumed that column would have indexes into dest, but it clearly does not because dest[1, ] is (0, 0) not (5, 0).

My end goal here is to figure out the value of attr for the dest point closest each src point. How can I find the dest point which is closest so that I can look up it's attr value?

(This is using terra version 1.3-4)


Solution

  • This bug was fixed in terra version 1.4-4