问题描述
我正在尝试以米为单位设置栅格像元大小,而不是以度为单位,但我的语法不正确。第一个代码块使用 UTM 投影,因此应以米为单位设置栅格大小。
rm(list=ls())
library("raster")
library("sf")
harvest <- sample(1:1000,100,replace = TRUE)
lon_1 <- sample(seq(140,141,0.25),replace = TRUE)
lat_1 <- sample(seq(37.5,38.5,replace = TRUE)
lon_2 <- sample(seq(130,131,replace = TRUE)
lat_2 <- sample(seq(32.5,33.5,replace = TRUE)
sample.df <- data.frame("lon"=c(lon_1,lon_2),"lat"=c(lat_1,lat_2),"harvest"=harvest)
sample_sf_1 <- st_as_sf(sample.df,coords = c("lon","lat"),crs = "+proj=utm +zone=54N +ellps=wgs84 +datum=wgs84 +units=m +no_defs")
sample_raster_1 <- raster(crs = crs(sample_sf_1),vals = 0,resolution = c(0.05,0.05),ext = extent(c(128,143,30,42)))
crs(sample_raster_1)
sample_rasterize_1 <- rasterize(sample_sf_1,sample_raster_1,sample_sf_1$harvest,fun=sum)
sample_rasterize_1.df <- raster::as.data.frame(sample_rasterize_1,xy=TRUE)
sample_rasterize_1.df <- sample_rasterize_1.df[!is.na(sample_rasterize_1.df$layer),]
但结果与未投影的栅格相同,除了 NA 单元格
sample_sf_2 <- st_as_sf(sample.df,crs = 4326)
sample_raster_2 <- raster(crs = crs(sample_sf_2),42)))
crs(sample_raster_2)
sample_rasterize_2 <- rasterize(sample_sf_2,sample_raster_2,sample_sf_2$harvest,fun=sum)
sample_rasterize_2.df <- raster::as.data.frame(sample_rasterize_2,xy=TRUE)
sample_rasterize_2.df <- sample_rasterize_2.df[!is.na(sample_rasterize_2.df$layer),]
sample_rasterize_1.df==sample_rasterize_2.df
我错过了什么?两个栅格的像元尺寸和图层值不应该不同吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)