Buffer of a polygon does not fully contain a buffer around the centroid of this polygon
Closed this issue · 2 comments
OS: Ubuntu 22.04
R: 4.4.0
Terra: 1.7.78
terra::gdal(lib="all"): gdal: "3.4.1" proj: "8.2.1" geos: "3.10.2"
Dear Robert, I am using the terra package in R to get a buffer around polygons. However, I have noticed that terra::buffer()
is not behaving as expected. In particular, for some polygons and buffer widths the buffered polygon does not fully contain a buffer with the same width of its centroid. Is there a bug or am I doing something wrong? I provide reproducible code below. A picture of the nonintuitive area difference can be obtained from running the code below as well. Thanks for your help!
library(terra)
# Define the vertices of the triangle (right-angled at point (0, 0))
vertices <- rbind(
c(-93.746, 46.99), # Vertex 1 (right angle)
c(-93.736, 46.99), # Vertex 2 (height of 10 units)
c(-93.746, 46.9886), # Vertex 3 (base of 5 units)
c(-93.746, 46.99) # Close the polygon by returning to Vertex 1
)
# Create the triangle polygon - EPSG:4269 is lat/lon
triangle_polygon <- vect(vertices, type = "polygons", crs = "EPSG:4269")
# Plot the rectangular triangle
plot(triangle_polygon)
# Get centroids
centroid <- terra::centroids(triangle_polygon)
# Buffer centroid (5000m)
centroid_buffer <- terra::buffer(centroid, 5000)
# Buffer polygon (5000m)
buffer <- terra::buffer(triangle_polygon, 5000)
# Do buffered centroid minus buffered polygon
diff <- terra::erase(centroid_buffer, buffer)
# The picture shows area contained in the buffered centroid, but not in the buffered polygon
plot(diff)
There was a reply in my stackoverflow post about this that solved the issue. Apparently terra was struggling with geographic coordinate system (GCS) spheroid coordinates. To get buffer working correctly I should be using the appropriate UTM Zone CRS. My suggestion would be to add a note on that to the buffer command instructions, because it was not clear to me and may not be to other users as well.
Thank you, this has now been fixed.