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

distance function: allow return of raster values as well as distances #1243

Closed
SimonDedman opened this issue Jul 23, 2023 · 2 comments
Closed

Comments

@SimonDedman
Copy link

Per this thread and the comments on the answer, it would be great to be able to specify if you also want to return the values of the nearest pixels in the target raster, rather than just the distance to those pixels.

Indeed in my current case, IDK that I need the distance at all, I just need points which are within 200m of the raster to get the nearest raster pixel's value appended - I don't subsequently care how far the donor pixel was (so long as it was <200m).

Not sure whether this would be a staggering amount of work, or conversely, once the code is at the point of having calculated the nearest pixel, it'd simply be a case of using that index to grab the required value(s).

Cheers for considering this regardless.

@ratnanil
Copy link

ratnanil commented Jul 3, 2024

That would be a life savoir... Are there any plans to implement something along these lines?

@rhijmans
Copy link
Member

rhijmans commented Jan 25, 2025

I am sorry that it took me so long, but here is a first attempt:

library(terra)
r <- rast(ncols=40, nrows=40, crs="+proj=utm +zone=1 +datum=WGS84")
r[4, 5:15] <- 5:15
r[36, 25:35] <- 25:35
d <- distance(r, values=TRUE)
plot(d)

Image

And

library(terra)
r <- rast(ncols=40, nrows=40, crs="lonlat")
r[4, 5:15] <- 5:15
r[36, 25:35] <- 25:35
d <- distance(r, values=TRUE)
plot(d)

Image

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

3 participants