使用缓冲函数从栅格中提取xy坐标

时间:2022-09-21 14:56:33

I have some xy coordinates as a SpatialPoints (points) object and have used them to extract temperature values at these locations from a RasterLayer (raster):

我有一些xy坐标作为SpatialPoints(points)对象,并使用它们从RasterLayer(栅格)中提取这些位置的温度值:

extract = extract(raster, points)

However several of the points are falling outside of the raster layer (i.e. not plotting on land) and I want to use the buffer argument to expand the radius around each point by 10000m -

然而,有几个点落在栅格层之外(即没有在陆地上绘图),我想使用缓冲参数将每个点周围的半径扩展10000米 -

extract2 = extract(raster, points, method="simple",buffer=10000, cellnumbers=TRUE)

This produces a "list" object i.e.- 

head(extract2)

[[1]]
  cell  value 
591332    165 

[[2]]
  cell  value 
475809     NA 

[[3]]
  cell  value 
534127     NA 

[[4]]
  cell  value 
534127     NA 

[[5]]
  cell  value 
534127     NA 

[[6]]
  cell  value 
534127     NA 

but I would like to create a dataframe where I have the raster values at the point locations (either NA or a temperature value) and the cell numbers so I can access the original xy coordinates for the cells of interest in the raster layer. How can I do this?

但我想创建一个数据框,其中我在点位置(NA或温度值)和单元格编号具有栅格值,因此我可以访问栅格图层中感兴趣的单元格的原始xy坐标。我怎样才能做到这一点?

1 个解决方案

#1


Please provide a reproducible example when you ask a question. This is very easy to do as you can build of the examples in ?extract or other help pages.

当您提出问题时,请提供可重现的示例。这很容易,因为您可以在“提取”或其他帮助页面中构建示例。

# example data
r <- raster(ncol=36, nrow=18, crs='+proj=utm +zone=14 +datum=WGS84')
r[] <- 1:ncell(r)
xy <- cbind(x=-50, y=seq(-80, 80, by=20))

# extract
e <- extract(r, xy, buffer=10)
ee <- t(data.frame(e))
rownames(ee) <- NULL

data.frame(xy, ee)

The above works in many cases, but not around edges or with lon/lat data as there might be a varying number of cells. In such cases you might do:

上述情况在很多情况下都有效,但不是在边缘或lon / lat数据周围,因为可能存在不同数量的单元格。在这种情况下,您可以:

 e <- extract(r, xy, buffer=10)
 m <- max(sapply(e, length))
 x <- rep(NA, m)
 ee <- t(sapply(e, function(y) {x[1:length(y)] <- y; x}))
 data.frame(xy, ee)

#1


Please provide a reproducible example when you ask a question. This is very easy to do as you can build of the examples in ?extract or other help pages.

当您提出问题时,请提供可重现的示例。这很容易,因为您可以在“提取”或其他帮助页面中构建示例。

# example data
r <- raster(ncol=36, nrow=18, crs='+proj=utm +zone=14 +datum=WGS84')
r[] <- 1:ncell(r)
xy <- cbind(x=-50, y=seq(-80, 80, by=20))

# extract
e <- extract(r, xy, buffer=10)
ee <- t(data.frame(e))
rownames(ee) <- NULL

data.frame(xy, ee)

The above works in many cases, but not around edges or with lon/lat data as there might be a varying number of cells. In such cases you might do:

上述情况在很多情况下都有效,但不是在边缘或lon / lat数据周围,因为可能存在不同数量的单元格。在这种情况下,您可以:

 e <- extract(r, xy, buffer=10)
 m <- max(sapply(e, length))
 x <- rep(NA, m)
 ee <- t(sapply(e, function(y) {x[1:length(y)] <- y; x}))
 data.frame(xy, ee)