简体   繁体   English

如何在 R 中使用 3x3 窗口对栅格执行聚焦操作(平均值)? 我有纬度/经度值

[英]How to perform focal operation (mean) on raster using 3x3 window in R? I have lat/long values

I have raster and lat/long values, I want to perform focal operation on these points using 3x3 window/kernel.我有栅格和纬度/经度值,我想使用 3x3 窗口/内核对这些点执行焦点操作。 I am new to R.我是 R 的新手。

Here is a workflow to compute the mean of a raster in 3x3 zones centered at defined lat/lon coordinates.这是一个工作流程,用于计算以定义的纬度/经度坐标为中心的 3x3 区域中的栅格平均值。

Rasterize the points and dilate the resulting raster to create the 3x3 zones栅格化点并扩大生成的栅格以创建 3x3 区域

library(mmand)
library(raster)

# rasterize points based on lat/lon coordinates
z <- rasterize(pts[,2:3], r, field = pts$id)

# dilate z using a 3x3 box
kern <- shapeKernel(c(3,3), type="box")
z[,] <- dilate(as.matrix(z), kern)

plot(z)

在此处输入图片说明

Compute the mean of r values in each zone计算每个区域中 r 值的平均值

# raster::zonal function (zonal statistics) is used 
# -Inf correspond to NA values and should not be taken into account
# you can change "mean" by the stats you would like to compute
zonal(r, z, fun = "mean")

#     zone     mean
#[1,] -Inf 5.563607
#[2,]    1 5.000000
#[3,]    2 3.444444
#[4,]    3 5.222222

Sample data样本数据

library(raster)
set.seed(1)
# Generate raster of random values
r <- raster(crs = CRS("+proj=robin +datum=WGS84"), resolution = c(10, 10))
r[] <- round(runif(ncell(r), 1, 10))
# Generate data frame with lat/lon coordinates
pts <- data.frame(id = 1:3, lon = c(-100, 40, 120), lat = c(-45, 5, 35))

plot(r)
points(pts$lon, pts$lat, pch = 20, cex = 2)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R 焦点函数:计算栅格单元和邻域之间差异的平方并找到 3x3 窗口的平均值 - R focal function: Calculate square of difference between raster cell and neighborhood and find the mean value for a 3x3 window 如何使用焦点(R raster/terra)通过 IDW 填补 NA 空白? - How to fill NA gaps by IDW using focal (R raster/terra)? 如何在 R 的光栅图像中获取中心像素的纬度/经度? - How to get lat/long of the center pixel in a raster image in R? R 中的栅格:使用提取()操作使用像素值 - raster in R: Operation with extract() using pixel values R焦点(栅格)-条件滤镜(仅在窗口中心为值1时运行) - R focal (raster)- conditional filter (only run if window center is value 1) 如何使用 r 将多面体的几何图形转换为 lat 和 long - How do I convert the geometry of multipolygons to lat and long using r 使用 R 创建分类栅格值的直方图? (或者,使用经纬度值创建 data.table) - Use R to create a historgram of categorical raster values? (or, create data table with lat/long values) 如何用R中的预处理值制作3x3矩阵? - How to make a 3x3 matrix with the pre-processed values in R? raster :: focal返回不正确的值 - raster::focal returns incorrect values R focus(栅格软件包):如何将过滤器应用于背景数据的子集? - R focal (raster package): how to apply filter to subsets of background data?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM