英文:
Calculate distance between point and nearest raster cell edge in R
问题
I'll provide the translation of the code portion you provided:
我试图计算点要素与最近的30 x 30米栅格单元边缘之间的距离。我手头的数据是按季度聚合的,为期五年(2016-2020),因此我需要为每一年的每个季度分别进行这个计算。栅格单元中的值并不重要,我只需要每个点与给定季度和年份的最近栅格单元之间的矢量距离。
示例数据
library(raster)
设置研究区域的范围
ext <- extent(401000, 402000, 4405000, 4406000)
创建带有投放位置的点文件
set.seed(123) # 为可重现性设置随机种子
years <- c(2016, 2017, 2018, 2019, 2020)
quarters <- c("Q1", "Q2", "Q3", "Q4")
coords <- expand.grid(year = years, quarter = quarters,
x = runif(4, 401000, 402000),
y = runif(4, 4405000, 4406000))
points <- st_as_sf(coords, coords = c("x", "y"), crs = st_crs(3310))
points$year <- as.factor(points$year)
points$quarter <- as.factor(points$quarter)
创建具有生物量估算的栅格文件
raster <- raster(ext, res = 30)
values(raster) <- NA
non_empty_cells <- sum(runif(ncell(raster)) > 0.98)
if (non_empty_cells > 0) {
values(raster)[sample(which(is.na(values(raster))), non_empty_cells)] <- runif(non_empty_cells, 0, 10)
}
绘制数据
plot(raster, col = gray.colors(10, start = 0.8, end = 0))
plot(points, add = TRUE, col="red")
Please note that the code is in R, and the comments within the code have been translated into Chinese.
英文:
I'm trying to calculate the distance in meters between a point feature and the edge of the nearest 30 x 30 meter raster cell. The data I have are aggregated by quarters for five years (2016-2020), so I need to do this calculation iteratively for each quarter within each year. The value in the cell does not matter, I just need the vector distance between each point and it's closest raster cell for a given quarter and year.
Sample data
library(raster)
# set the extent of the study area
ext <- extent(401000, 402000, 4405000, 4406000)
# create a point file with drop locations
set.seed(123) # set a random seed for reproducibility
years <- c(2016, 2017, 2018, 2019, 2020)
quarters <- c("Q1", "Q2", "Q3", "Q4")
coords <- expand.grid(year = years, quarter = quarters,
x = runif(4, 401000, 402000),
y = runif(4, 4405000, 4406000))
points <- st_as_sf(coords, coords = c("x", "y"), crs = st_crs(3310))
points$year <- as.factor(points$year)
points$quarter <- as.factor(points$quarter)
# create a raster file with biomass estimates
raster <- raster(ext, res = 30)
values(raster) <- NA
non_empty_cells <- sum(runif(ncell(raster)) > 0.98)
if (non_empty_cells > 0) {
values(raster)[sample(which(is.na(values(raster))), non_empty_cells)] <- runif(non_empty_cells, 0, 10)
}
# plot the data
plot(raster, col = gray.colors(10, start = 0.8, end = 0))
plot(points, add = TRUE, col="red")
答案1
得分: 1
I am assuming that you want the distance to the border of the nearest cell with a value (that is not NA). If your raster is not too large you could do:
示例数据
library(terra)
set.seed(123)
raster <- rast(ext=c(401000, 402000, 4405000, 4406000), res = 30, crs="local")
cells <- sample(ncell(raster), 20)
raster[cells] <- runif(length(cells))
years <- c(2016, 2017, 2018, 2019)
quarters <- c("Q1", "Q2", "Q3", "Q4")
points <- data.frame(year = years, quarter = quarters,
x = runif(4, 401000, 402000), y = runif(4, 4405000, 4406000))
pts <- vect(points, geom=c("x", "y"), crs=crs(raster))
解决方案
lns <- as.lines(as.polygons(raster, dissolve=FALSE))
#nearest(pts, lns) |> values()
# from_id from_x from_y to_id distance
#1 1 401127.5 4405665 7 136.23261
#2 2 401753.3 4405095 19 27.12752
#3 3 401895.0 4405384 10 74.71168
#4 4 401374.5 4405274 14 57.06026
If the distances are large relative to the resolution, you could also do
x <- distance(raster>0)
extract(x, pts)
# ID lyr.1
#1 1 169.70563
#2 2 42.42641
#3 3 108.16653
#4 4 84.85281
That computes the distance from cell center (that the points fall in) to cell center (of the nearest cell).
英文:
I am assuming that you want the distance to the border of the nearest cell with a value (that is not NA). If your raster is not too large you could do:
Example data
library(terra)
set.seed(123)
raster <- rast(ext=c(401000, 402000, 4405000, 4406000), res = 30, crs="local")
cells <- sample(ncell(raster), 20)
raster[cells] <- runif(length(cells))
years <- c(2016, 2017, 2018, 2019)
quarters <- c("Q1", "Q2", "Q3", "Q4")
points <- data.frame(year = years, quarter = quarters,
x = runif(4, 401000, 402000), y = runif(4, 4405000, 4406000))
pts <- vect(points, geom=c("x", "y"), crs=crs(raster))
Solution
lns <- as.lines(as.polygons(raster, dissolve=FALSE))
#nearest(pts, lns) |> values()
# from_id from_x from_y to_id distance
#1 1 401127.5 4405665 7 136.23261
#2 2 401753.3 4405095 19 27.12752
#3 3 401895.0 4405384 10 74.71168
#4 4 401374.5 4405274 14 57.06026
If the distance are large relatively to the resolution, you could also do
x <- distance(raster>0)
extract(x, pts)
# ID lyr.1
#1 1 169.70563
#2 2 42.42641
#3 3 108.16653
#4 4 84.85281
That computes the distance from cell center (that the points fall in) to cell center (of the nearest cell).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论