在R中计算点到最近栅格单元边缘的距离
问题需求
需要计算点要素与最近30×30米栅格单元边缘之间的米制距离。数据按2016-2020年的季度聚合,需逐年逐季度迭代执行该计算。栅格单元的值无关紧要,仅需获取指定季度和年份下每个点到最近栅格单元的矢量距离。
示例数据
library(raster) library(sf) # 补充sf包依赖,用于创建矢量点要素 # 设置研究区范围 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")
内容的提问来源于stack exchange,提问作者Joshua Smith
相关产品推荐
相关产品推荐

