Calculate distance between point and nearest raster cell edge in R

huangapple go评论62阅读模式
英文:

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 &lt;- extent(401000, 402000, 4405000, 4406000)

# create a point file with drop locations
set.seed(123) # set a random seed for reproducibility
years &lt;- c(2016, 2017, 2018, 2019, 2020)
quarters &lt;- c(&quot;Q1&quot;, &quot;Q2&quot;, &quot;Q3&quot;, &quot;Q4&quot;)
coords &lt;- expand.grid(year = years, quarter = quarters, 
                      x = runif(4, 401000, 402000), 
                      y = runif(4, 4405000, 4406000))
points &lt;- st_as_sf(coords, coords = c(&quot;x&quot;, &quot;y&quot;), crs = st_crs(3310))
points$year &lt;- as.factor(points$year)
points$quarter &lt;- as.factor(points$quarter)

# create a raster file with biomass estimates
raster &lt;- raster(ext, res = 30)
values(raster) &lt;- NA
non_empty_cells &lt;- sum(runif(ncell(raster)) &gt; 0.98)
if (non_empty_cells &gt; 0) {
  values(raster)[sample(which(is.na(values(raster))), non_empty_cells)] &lt;- 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=&quot;red&quot;)

答案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 &lt;- rast(ext=c(401000, 402000, 4405000, 4406000), res = 30, crs=&quot;local&quot;)
cells &lt;- sample(ncell(raster), 20)
raster[cells] &lt;- runif(length(cells))
years &lt;- c(2016, 2017, 2018, 2019)
quarters &lt;- c(&quot;Q1&quot;, &quot;Q2&quot;, &quot;Q3&quot;, &quot;Q4&quot;)
points &lt;- data.frame(year = years, quarter = quarters, 
          x = runif(4, 401000, 402000), y = runif(4, 4405000, 4406000))
pts &lt;- vect(points, geom=c(&quot;x&quot;, &quot;y&quot;), crs=crs(raster))

解决方案

lns &lt;- as.lines(as.polygons(raster, dissolve=FALSE))
#nearest(pts, lns) |&gt; 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 &lt;- distance(raster&gt;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 &lt;- rast(ext=c(401000, 402000, 4405000, 4406000), res = 30, crs=&quot;local&quot;)
cells &lt;- sample(ncell(raster), 20)
raster[cells] &lt;- runif(length(cells))
years &lt;- c(2016, 2017, 2018, 2019)
quarters &lt;- c(&quot;Q1&quot;, &quot;Q2&quot;, &quot;Q3&quot;, &quot;Q4&quot;)
points &lt;- data.frame(year = years, quarter = quarters, 
          x = runif(4, 401000, 402000), y = runif(4, 4405000, 4406000))
pts &lt;- vect(points, geom=c(&quot;x&quot;, &quot;y&quot;), crs=crs(raster))

Solution

lns &lt;- as.lines(as.polygons(raster, dissolve=FALSE))
#nearest(pts, lns) |&gt; 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 &lt;- distance(raster&gt;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).

huangapple
  • 本文由 发表于 2023年4月11日 07:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981464.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定