Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect behavior in distance() #1462

Open
dfriend21 opened this issue Mar 19, 2024 · 0 comments
Open

Incorrect behavior in distance() #1462

dfriend21 opened this issue Mar 19, 2024 · 0 comments

Comments

@dfriend21
Copy link
Contributor

I've found some incorrect behavior when using distance() with a SpatRaster and a SpatVector. In particular, when finding a raster representing the distance from lines or polygons, the distance is only calculated from the vertices (plots 1 and 3, below). In the case of polygons, the entire area covered by the polygon is correctly represented as having distance 0, but the distance for areas outside the polygons uses the vertices only (plot 1). When rasterize = TRUE, the distance is calculated as I would expect - the distance from the entire line or polygon, not just the vertices (plots 2 and 4).

I see the following sentence in the documentation for distance: "For lines and polygons this is done after rasterization; and only the overlapping areas of the vector and raster are considered (for now)." Based on this, I'm assuming that rasterization is supposed to always happen for lines and polygons, but this isn't the case right now.

library(terra)
#> terra 1.7.73

v <- vect(system.file("ex/lux.shp", package = "terra"))
r <- rast(system.file("ex/elev.tif", package = "terra"))

v_simp <- simplifyGeom(v)
plot(v, border = "gray", main = "original and simplified polys")
polys(v_simp, border = "red")

# find the distance from the simplified polygons
d2 <- distance(r, v_simp, progress = FALSE)
d2b <- distance(r, v_simp, rasterize = TRUE, progress = FALSE)

# repeat for lines
v_simp_l <- as.lines(v_simp)
d3 <- distance(r, v_simp_l, progress = FALSE)
d3b <- distance(r, v_simp_l, rasterize = TRUE, progress = FALSE)

par(mfrow = c(2,2))
plot(d2, main = "1: polygons, rasterize = FALSE")
plot(d2b, main = "2: polygons, rasterize = TRUE")
plot(d3, main = "3: lines, rasterize = FALSE")
plot(d3b, main = "4: lines, rasterize = TRUE")

Created on 2024-03-19 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant