Search code examples
rgisterra

Is there any equivalent `terra` for sp::remove.duplicates()?


Is there any workaround in terra for the sp::remove.duplicates() function? When using the sp function, the zero argument is very convenient to remove duplicated points within a certain distance.

pts <- data.frame(
  longitude = c(-70.1234, -70.1240, -70.1300, -70.1400, -70.1250, -68),
  latitude = c(40.6789, 40.6780, 40.6760, 40.6750, 40.6770, 39)
)


unduplicated <- sp::remove.duplicates(pts_sp, zero = 2)

unduplicated
#class       : SpatialPoints 
#features    : 2 
#extent      : -70.1234, -68, 39, 40.6789  (xmin, xmax, ymin, ymax)
#crs         : NA 

There are some approaches using sf (see final comments on https://github.com/r-spatial/sf/issues/669), but I was not able to find a straightforward way for terra SpatVectors.

Thanks in advance!


Solution

  • You can use round and unique.

     v <- vect(pts, c("longitude", "latitude"))
     round(v, 1) |> unique()
     #class       : SpatVector 
     #geometry    : points 
     #dimensions  : 2, 0  (geometries, attributes)
     #extent      : -70.1, -68, 39, 40.7  (xmin, xmax, ymin, ymax)
     #coord. ref. :