-
Notifications
You must be signed in to change notification settings - Fork 23
Description
When equidistant = TRUE, the margin_factor argument is treated differently depending on the dimensions of the geographic extent used as the frame bounding box.
I discovered this when testing a dataset with margin_factor = 0.5, which was producing a terra [crop] extents do not overlap error for certain datasets. The error ultimately stemmed from these lines in .equidistant():
if (ax.dist[1] < ax.dist[2]) {
x.devi <- (ax.diff[1] / ax.dist[1]) * ((ax.dist[2] - ax.dist[1]) * margin_factor) / 2
y.devi <- ((ax.diff[2] / ax.dist[2]) * (ax.dist[2] * margin_factor)) - ax.diff[2]
} else {
x.devi <- ((ax.diff[1] / ax.dist[1]) * ax.dist[1]) - ax.diff[1]
y.devi <- ((ax.diff[2] / ax.dist[2]) * (ax.dist[1] - ax.dist[2]) / 2)
}In the case that the bounding box distance in the x dimension is less than the distance in the y dimension, margin_factor is used to calculate the expansion needed to make the bounding box equidistant. When margin_factor = 0.5,
y.devi <- ((ax.diff[2] / ax.dist[2]) * (ax.dist[2] * margin_factor)) - ax.diff[2]reduces to
y.devi <- -0.5 * ax.diff[2]This causes this line
sf::st_bbox(c(..., ext.ll[2] - y.devi, ext.ll[4] + y.devi))to collapse the y-dimension of the new bounding box to 0, because we add half the y-distance to the lower y limit (of the original bbox) and subtract half the y-distance from the upper y limit.
Of course, this behavior only happens if the x-distance is less than the y-distance in the initial input bbox. Otherwise, the error doesn't occur. It also only happens if equidistant = TRUE.
This seems to raise the question of what the intended behavior of the margin_factor argument is when equidistant = TRUE. Enforcing margin_factor > 1 would prevent the terra error, but the behavior of the argument would still change depending on the input bbox dimensions, even for valid values.