坐标混淆st_crop

Coordinate confusion with st_crop

我想裁剪 sf 对象。当我在 ggplot 中绘制对象时,我估计了我希望裁剪的区域。但是,当我使用 st_crop 执行裁剪时,tibble 有零行。这是为什么?

here 下载 alberta_border.RDS

library("sf")
library("ggplot2")

alberta <- readRDS("alberta_border.RDS")

ggplot() +
  geom_sf(data = alberta) +
  coord_sf()


alberta_crop <- st_crop(alberta, c(xmin = -118, xmax = -112, ymin = 50, ymax = 56))
alberta_crop

看来你打错了crs。你可以告诉alberta shapefile的crs:

st_crs(alberta)
#Coordinate Reference System:
#EPSG: 26911 
#proj4string: "+proj=utm +zone=11 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs"

您的输入文件使用的是 EPSG 26911UTM zone 11N,而您输入的坐标 (c(xmin = -118, xmax = -112, ymin = 50, ymax = 56)) 看起来是 lat/lng 或 EPSG 4326.

因此我们应该在裁剪之前将原始数据集转换为lat/lng:

library(dplyr)
library(sf)
library(ggplot2)

alberta <- alberta %>%
  st_transform(4326)

alberta_crop <- st_crop(alberta, c(xmin = -118, xmax = -112, ymin = 50, ymax = 56))


ggplot() + 
  geom_sf(data = alberta, aes(fill = 'alberta')) + 
  geom_sf(data = alberta_crop, aes(fill = 'cropped')) +
  theme_minimal()