小编Erv*_*van的帖子

在大型数据集上加速 st_crop(sf 包)的方法

我需要从不同的 shapefile 中提取大约 4 个 1 公顷的 mio 网格单元的信息。目前,我在所有单元格的 for 循环中的每一层上使用 st_crop,但这将永远运行。我想加快使用“data.table”(DT)排序方式按坐标裁剪 shapefile 的过程。让我们考虑下面的示例,我正在寻找感兴趣区域中多边形边缘的范围:

require(sf)
require(data.table)
require(ggplot2)
require(tidyverse)

# load shapefile
nc = st_read(system.file("shape/nc.shp", package="sf"))


# Define a bounding-box that mimic a mowing-window or area of interest
bb <- st_bbox(c(xmin= -79, xmax=-78,ymin= 34.5, ymax= 35.5))


# Commute 'nc' into some sort of data.table object for fast subsetting, in preserving object's integrity (i.e. same id to all points of a given polygon)
nobs <- mapview::npts(nc,by_feature=T)
NC <- data.table::data.table(id=rep(1:nrow(nc),nobs),st_coordinates(nc)[,1:2])
head(NC)

# Compare …
Run Code Online (Sandbox Code Playgroud)

r spatial data.table r-sf

2
推荐指数
1
解决办法
690
查看次数

标签 统计

data.table ×1

r ×1

r-sf ×1

spatial ×1