奇怪的 sf::st_crop() 行为
Odd sf::st_crop() behaviour
我想用一个 sf
对象裁剪另一个对象,我可以使用 sf::st_crop()
或 sf::st_intersection()
来实现。但是,生成的裁剪对象与裁剪对象不匹配(请参见下面的 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
在转为使用 s2
后,sf_1.0-0
出现了问题。它由脚本开头的 运行 sf_use_s2(FALSE)
修复。
我想用一个 sf
对象裁剪另一个对象,我可以使用 sf::st_crop()
或 sf::st_intersection()
来实现。但是,生成的裁剪对象与裁剪对象不匹配(请参见下面的 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
在转为使用 s2
后,sf_1.0-0
出现了问题。它由脚本开头的 运行 sf_use_s2(FALSE)
修复。