如何从栅格中提取xy坐标,其中最高值位于多边形内?

N'y*_*'ya 3 r geospatial r-raster

给定的是栅格以及SpatialPolygonsDataframe.为了在多边形区域内检索栅格的最高值,可以使用raster :: extract.它工作正常.

如何在多边形区域内另外获取所提取的栅格最高值的坐标?

# create raster
r <- raster(ncol=36, nrow=18)
r[] <- runif(ncell(r))
# create SpatialPolygons from GridTopology
grd <- GridTopology(c(-150, -50), c(40, 40), c(8, 3))
Spol <- as(grd, "SpatialPolygons")
# create SpatialPolygonsDataFrame
centroids <- coordinates(Spol)
x <- centroids[,1]
y <- centroids[,2]
SPDF <- SpatialPolygonsDataFrame(Spol, data=data.frame(x=x, y=y, row.names=row.names(Spol)))
# extract max value of raster for each SpatialPolygon
ext <- raster::extract(r, SPDF, fun=max)
Run Code Online (Sandbox Code Playgroud)

*示例代码取自R-documentation

Rob*_*ans 5

您可以使用cellnumbers=TRUE参数in extract,然后使用a sapply来获取单元格编号:

ext <- raster::extract(r, SPDF, cellnumbers=TRUE)
v <- t(sapply(ext, function(i) i[which.max(i[,2]), ] ))

#      cell     value
# [1,]  185 0.9303460
# [2,]  188 0.9821190
# [3,]  154 0.9926290
# [4,]  232 0.8907819
# [5,]  234 0.9998510
Run Code Online (Sandbox Code Playgroud)

要获得坐标:

xyFromCell(r, v[,1])

#         x   y
# [1,] -135  35
# [2,] -105  35
# [3,]  -85  45
# [4,]  -25  25
# [5,]   -5  25
Run Code Online (Sandbox Code Playgroud)