1

我有一个光栅和一些点。我想根据一些一般条件捕捉最接近栅格的点。

library(raster)
##create a diagonal matrix
xy = diag(1, 100, 100)
# Turn the matrix into a raster
rast <- raster(xy)
# Give it lat/lon coords 
extent(rast) <- c(-180,180,-90,90)
# ... and assign a projection
projection(rast) <- CRS("+proj=longlat +datum=WGS84")

##create two points just for reference
lonlat <- data.frame(x = c(50,130), y = c(75,-50))
coordinates(lonlat)<-~x+y
crs(lonlat)<- CRS("+proj=longlat +datum=WGS84")
plot(rast)
plot(lonlat,add=T)

结果是

在此处输入图像描述

现在我想捕捉绿色对角线上的点(+)。在这里,我提供了一个对角矩阵来简化它,但它可以是任何形状(例如像河流一样弯曲的形状)。

我发现了一些仅将最近的栅格网格捕捉到点的方法。

##snap raster grid closest to point
Idx = sapply(lonlat$x,function(i) which.min(abs(unique(rasterToPoints(rast, spatial = TRUE)@coords[,1])-i)))
Idy = sapply(lonlat$y,function(i) which.min(abs(unique(rasterToPoints(rast, spatial = TRUE)@coords[,2])-i)))

我基本上想要两件事(a)根据一些简单的条件( rast == 1 )捕捉最近的点。(b) 根据一些搜索半径捕捉点(假设邻近一些点)。

4

0 回答 0