使用st_crop裁剪stars对象至美国本土shp文件时出现边界不重叠错误
问题:stars对象裁剪至美国本土Shapefile时出现边界框不重叠警告
我正在处理一个包含PRISM温度数据的stars对象,存储了各经纬度坐标的每日温度值,尝试将其裁剪至美国本土的shapefile时,始终遇到问题。
操作步骤与代码
# 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]
# 确认坐标系为NAD83 st_crs(tempdata) # 读取shapefile 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
裁剪尝试与警告
# 裁剪至shapefile范围 prism_cropped <- st_crop(tempdata, shapefile)
Warning message:
In st_crop.stars(onedaytemp, shapefile) :
st_crop: bounding boxes of x and y do not overlap
两者坐标系均为NAD83,但数据无法正确识别/重叠,需要解决方法。
解决方案
1. 统一CRS的具体参数
NAD83存在多个子类型(如NAD83(2011)、NAD83(NSRS2007)),表面标注一致但实际参数可能有差异。先确认详细CRS信息:
# 查看stars对象的完整CRS定义 st_crs(tempdata)$wkt # 查看shapefile的完整CRS定义 st_crs(shapefile)$wkt
若不一致,将shapefile转换为与tempdata完全匹配的CRS:
shapefile <- st_transform(shapefile, st_crs(tempdata))
2. 缩小Shapefile的处理范围
你的Shapefile边界覆盖全球,而tempdata仅包含美国本土区域,过大的边界可能导致st_crop识别异常。先提取美国本土的Shapefile子集:
# 手动构造美国本土的边界框(匹配tempdata的覆盖范围) us_bbox <- st_bbox(c(xmin = -125, xmax = -66.5, ymin = 24, ymax = 50), crs = st_crs(tempdata)) us_bbox_poly <- st_as_sfc(us_bbox) # 裁剪Shapefile到美国本土范围 us_shape <- st_crop(shapefile, us_bbox_poly) # 再用裁剪后的Shapefile处理stars对象 prism_cropped <- st_crop(tempdata, us_shape)
3. 验证维度顺序
检查stars对象的x/y维度是否与Shapefile的坐标顺序一致:
# 查看stars对象的维度顺序 dim(tempdata) # 确认Shapefile的坐标顺序(st_coordinates输出第一列为x,第二列为y) head(st_coordinates(shapefile))
若维度颠倒,用st_transpose调整:
tempdata <- st_transpose(tempdata)
4. 借助terra包中转处理
如果stars原生裁剪持续异常,可通过terra包的SpatRaster中转:
# 将stars对象转换为SpatRaster temp_rast <- rast(tempdata) # 裁剪到Shapefile(确保CRS已匹配) cropped_rast <- crop(temp_rast, vect(shapefile)) # 转回stars对象 prism_cropped <- st_as_stars(cropped_rast)
内容的提问来源于stack exchange,提问作者ss2025
相关产品推荐
相关产品推荐

