【发布时间】:2023-01-29 08:33:08
【问题描述】:
我有一个来自 entire world 的 shapefile,我使用 as Spatial() 函数转换它以与 sp 包兼容。
set.seed(27)
shp <- sf::st_read("earth_gadm.shp")
shape <- as_Spatial(shp)
因为我没有在任何特定区域工作,所以我将“+proj=longlat +ellps=WGS84 +datum=WGS84”crs 分配给了我的 shpfile。
crs <- "+proj=longlat +ellps=WGS84 +datum=WGS84"
proj4string(shape) = crs
在 Matt Strimas-Mackei workflow 之后,我使用 spsample() 和 HexPoints2SpatialPolygons() 基于我的形状对象创建了一个六边形网格,然后我将网格和多边形相交。
size <- 2.5 #2.5 degrees as i am working with a latlong projection (correct?)
hex_points <- spsample(shape, type = "hexagonal", cellsize = size)
hex_grid <- HexPoints2SpatialPolygons(hex_points, dx = size)
shape.grid <- gIntersection(shape, hex_grid, byid = T)
我在我的新 shapefile 上绘制了一些点并将它们覆盖到我的 shape.grid 对象上。
library(rgbif)
gbif_data <- occ_data(scientificName = 'Lestes sponsa',
hasCoordinate = TRUE, limit = 60)
gbif_data <- gbif_data$data
coords <- gbif_data[ , c("decimalLongitude", "decimalLatitude")]
coords$decimalLatitude <- as.numeric(coords$decimalLatitude)
coords$decimalLongitude <- as.numeric(coords$decimalLongitude)
coordinates(coords) <- ~decimalLongitude + decimalLatitude
coords <- data.frame(x = coords$decimalLongitude, y = coords$decimalLatitude)
coords <- SpatialPointsDataFrame(coords= coords, data = gbif_data)
proj4string(coords) = crs
x11()
plot(shape.grid, col = "grey50", bg = "light blue", axes = TRUE, cex = 20)
points(coords, col = 'blue', pch=20, cex = 0.75)
overlaid <- over(shape.grid, coords, returnList = T)
overlaid <- data.frame(matrix(unlist(overlaid), nrow=60,
byrow=TRUE),stringsAsFactors=FALSE)
现在我正在尝试从绘制有点的网格单元格中提取平均生物气候变量。我还有 19 个 .bil 栅格,我是从Wordclim 下载的。我在考虑使用这些栅格来提取生物气候变量。但是,我停留在这一步。
我试过了:
bioclim_data <- extract(x=stackrasters, c(overlaid$decimalLongitude, overlaid$decimalLatitude))
但是,我不确定我是否从网格单元格中提取平均值,除此之外,上面的命令行仅返回 NA 值。
【问题讨论】:
标签: r geospatial raster spatial