Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between sf::st_buffer and terra::buffer on R: diameter and radius

Tags:

r

buffer

terra

r-sf

I'm getting confused with the usage of st_buffer from package sf and buffer from terra package on R.

I'm creating buffers around points and when using sf::st_buffer I understand I need to use the radius of the buffer in the argument dist BUT when using terra::buffer I need to specify the diameter of the buffer in the argument width.

Is this correct?

I´ve checked here for sf::st_buffer, argument dist: https://rdrr.io/r/stats/dist.html

and here for terra::buffer, argument width: https://rdrr.io/cran/terra/man/width.html

like image 803
CMai Avatar asked Sep 18 '25 19:09

CMai


1 Answers

A buffer is a distance from an object. In that general sense it is neither a radius nor a diameter.

When you create a buffer around a point, the buffer is a circle, and the buffer distance (width) is equivalent to the radius of that circle.

This does not apply for buffers around lines, polygons or raster cells.

Some illustrative code with terra

library(terra)
v <- vect(cbind(0,0), crs="+proj=utm +zone=1")
b <- buffer(v, 1)
ext(b)
#SpatExtent : -1, 1, -1, 1 (xmin, xmax, ymin, ymax)

v <- vect(cbind(0,0), crs="+proj=longlat")
# 1 degree at equator is ~ 111 km
b <- buffer(v, 111000)
round(ext(b), 2)
#SpatExtent : -1, 1, -1, 1 (xmin, xmax, ymin, ymax)

The below shows buffers for longitude/latitude points at different latitudes. And these buffers are circles, all with the same radius, even though that is hard to see with the distortion from flattening the earth.

v <- vect(cbind(0, seq(0, 80, 15)), crs="+proj=longlat")
b <- buffer(v, 1100000)
plot(b, asp=1, border=rainbow(8), lwd=2)
points(v)

enter image description here

like image 126
Robert Hijmans Avatar answered Sep 20 '25 09:09

Robert Hijmans