通过相当讨厌的大型空间多边形数据框剪切表面多边形

Clipping surface polygon by a rather nasty large Spatial polygon data frame

我遇到了一个棘手的问题。

我正在尝试使用 sp 包执行一个简单的多边形剪辑,使用函数 st_difference(st_union(x),st_union(y))(或其中的变体)或 st_intersection 函数,以效果最佳者为准。

虽然这对于两个表面多边形来说很容易,但我需要将它剪辑到一个可怕的大下载 Large SpatialPolygonsDataFrame,它只是英国的 shapefile,下载自: https://gadm.org/download_country_v3.html

shapefile如下(绘制于leaflet):

    > str(uk)
    Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
      ..@ data       :'data.frame': 1 obs. of  70 variables:
      .. ..$ ID_0      : Factor w/ 1 level "242": 1
      .. ..$ ISO       : Factor w/ 1 level "GBR": 1
      .. ..$ NAME_0    : Factor w/ 1 level "United Kingdom": 1
    # .....etc.
    #
    > str(box)
    sfc_POLYGON of length 1; first list element: List of 1
     $ : num [1:5, 1:2] -7.237 0.126 0.126 -7.237 -7.237 ...
     - attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"

我想将 box(蓝色)剪辑到 uk,原因是在英国(以及法国)渲染 shapefile 花费的时间太长在传单中。

这样大概可以吗?

sf::st_intersection(UK, box)

完整代码

library(sf)

UK <- readRDS("./gadm36_GBR_0_sf.rds")

#create box since it was not provided in question
box <- c("POLYGON((-7.237 48,0 48,0 52,-7.237 52, -7.237 48))") %>% 
  st_as_sfc(crs = "+proj=longlat +datum=WGS84")

mapview::mapview(list(UK,box))

mapview::mapview( st_intersection(UK, box) )

更新

如果要用英国的shapefile切框,用st_difference()

mapview::mapview( st_difference (box, UK) )