我想用sf::st_crop()或sf::st_intersection()将一个sf对象一个接一个地裁剪。但是,得到的裁剪对象与它被裁剪的对象不匹配(参见下面的reprex)。相反,看起来裁剪发生在左上角和右下角。如何让它真正裁剪到poly1的轮廓?
library(sf); library(rnaturalearth)
# polygon to clip by
poly1 <- matrix(c(-113, 23.5,
-113, -23.5,
-34, -23.5,
-34, 23.5,
-113, 23.5),
byrow=T, ncol=2) %>%
list() %>%
st_polygon() %>%
st_sfc(., crs = "epsg:4326")
countries <- ne_countries(continent = c("South America", "North America"),
returnclass = "sf") %>%
st_transform(., crs="epsg:4326")
plot(countries$geometry)
plot(poly1, add=T, border = "red")
cr1 <- st_crop(countries, poly1)
plot(cr1$geometry, add=T, col="red")
# intersection does the same
cr2 <- st_intersection(countries, poly1)
plot(cr2, add=T, col="green")

编辑:事实证明,如果我恢复到以前的R版本(它也附加了不同的包版本),这个错误就会消失。
出现问题的会话信息:
> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rnaturalearth_0.1.0 sf_1.0-0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.7 magrittr_2.0.1 units_0.7-2 tidyselect_1.1.1 lattice_0.20-44 R6_2.5.1 rlang_0.4.11
[8] fansi_0.5.0 s2_1.0.6 dplyr_1.0.7 wk_0.4.1 tools_4.1.0 grid_4.1.0 KernSmooth_2.23-20
[15] utf8_1.2.2 e1071_1.7-7 DBI_1.1.1 rgeos_0.5-5 ellipsis_0.3.2 class_7.3-19 assertthat_0.2.1
[22] tibble_3.1.4 lifecycle_1.0.0 crayon_1.4.1 purrr_0.3.4 vctrs_0.3.8 glue_1.4.2 sp_1.4-5
[29] proxy_0.4-26 compiler_4.1.0 pillar_1.6.2 generics_0.1.0 classInt_0.4-3 pkgconfig_2.0.3会话信息不存在的地方:
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8.1 x64 (build 9600)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rnaturalearth_0.1.0 sf_0.9-7
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 pillar_1.4.6 compiler_3.6.3 plyr_1.8.6 class_7.3-15 tools_3.6.3 lifecycle_0.2.0
[8] tibble_3.0.4 gtable_0.3.0 lattice_0.20-41 pkgconfig_2.0.3 rlang_0.4.10 DBI_1.1.1 rstudioapi_0.11
[15] yaml_2.2.1 e1071_1.7-4 dplyr_1.0.0 stringr_1.4.0 rgeos_0.5-2 generics_0.1.0 vctrs_0.3.4
[22] classInt_0.4-3 grid_3.6.3 tidyselect_1.1.0 glue_1.4.2 ggnewscale_0.4.3 R6_2.4.1 sp_1.4-5
[29] purrr_0.3.4 reshape2_1.4.4 ggplot2_3.3.2.9000 magrittr_2.0.1 scales_1.1.1 ellipsis_0.3.1 units_0.6-7
[36] rsconnect_0.8.16 colorspace_1.4-1 KernSmooth_2.23-16 stringi_1.5.3 munsell_0.5.0 crayon_1.3.4发布于 2021-09-17 19:15:27
在转向使用s2之后,这成了sf_1.0-0的一个问题。通过在脚本开头运行sf_use_s2(FALSE)可以修复此问题。
https://stackoverflow.com/questions/69227019
复制相似问题