我正在处理一个包含来自 PRISM 温度数据的恒星对象。星星对象包含每个纬度和经度坐标的每日温度值。但是,我正在尝试裁剪到美国本土的形状文件,并继续遇到一些问题。 在此处链接 .rds 数据集和 .shp 形状文件
这是我迄今为止尝试过的:
#Load packages
library(stars)
library(dplyr)
library(raster)
library(terra)
library(tidyverse)
#Read in temperature file
tempdata <- readRDS("tempdata.rds")
tempdata 具有以下描述/属性:
stars object with 2 dimensions and 1 attribute
attribute(s):
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
PRISM_tmin_stable_4kmD2_202... -9.449 9.476 16.621 15.18067 20.438 30.148 390874
dimension(s):
from to offset delta refsys x/y
x 1 1405 -125 0.04167 NAD83 [x]
y 1 621 49.94 -0.04167 NAD83 [y]
#Confirm coordinate reference system- it is NAD83.
st_crs(tempdata)
#Read in shape file
shapefile <- st_read("shapefile.shp")
shapefile 具有以下属性:
sing driver `ESRI Shapefile'
Simple feature collection with 33791 features and 9 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -176.6967 ymin: -14.37378 xmax: 145.8305 ymax: 71.34132
Geodetic CRS: NAD83
然后,我尝试将较大的棱镜文件裁剪为形状文件:
#Crop to shape file
prism_cropped <- st_crop(tempdata, shapefile)
但是,这会返回以下错误:
警告信息: 在 st_crop.stars(onedaytemp, shapefile) 中: st_crop:x 和 y 的边界框不重叠
此时我不知道该怎么办。星星对象中肯定存在时空数据,但由于某种原因,尽管具有相同的坐标参考系统,但它似乎无法正确读取/与形状文件重叠,而且我不知道如何修复它。
看起来只需使用
stars
包将 spatRaster
对象转换为 terra::
即可。这段代码对我有用。请注意,由于 shapefile 有超过 30000 个多边形,我只从 shapefile 中取出前五个多边形,这样就不会永远绘制。希望这有帮助。
library(stars)
library(dplyr)
library(raster)
library(terra)
library(tidyverse)
library(tmap)
#Read in temperature file
tempdata <- readRDS("PRISM_tmin_y20200701.rds")
shapefile <- st_read("tl_2020_us_zcta520.shp")
temprast<-rast(tempdata)
croppedTemp<-crop(temprast, shapefile)
tmap_mode("plot")
tm_shape(croppedTemp)+
tm_raster(palette="viridis")+
tm_shape(shapefile[1:5,])+
tm_polygons(col=NA, border.col="red")