2

我必须在 R 中读取一个巨大的 ESRI shapefile 的一小部分。我分两步执行此操作:

第 1 步:我使用ogr2ogr将 shapefile 剪辑到我的边界框:

ogr2ogr -clipsrc xMin yMin xMax yMax outfile.shp infile.shp

第2步:我将其读入R rgdal

df = readOGR(dsn="/path", layer="outfile")

问题是我必须为多个文件执行此操作,并且很难跟踪OGR生成每个单独文件的操作。有没有办法ogr2ogr在 R 中进行管道传输,以便即时完成第 1 步?

4

2 回答 2

5

尝试使用system通话。没有代码和数据很难说,但是如果您有多个要从 shapefile 剪辑的边界框,您应该能够创建要处理的 shapefile 列表(如果您有多个 shapefile)或要处理的坐标。

work.dir <- "C:/Program Files (x86)/FWTools2.4.7/bin" # use your FWTools location
setwd(work.dir)
out.shape.file <- "foo2.shp"
in.shape.file <- "foo1.shp"
x.min <- 100
y.min <- 100
x.max <- 200
y.max <- 200
system(paste("ogr2ogr -clipsrc", x.min, y.min, x.max, y.max, 
      out.shape.file, in.shape.file))
于 2013-09-10T09:40:09.680 回答
1

请注意,您可以使用 rgeos 包进行剪辑,如下所示(我使用 raster 来轻松创建简单的蒙版多边形):

library(rgeos)
library(raster)
library(rgdal)
## as per your example
df = readOGR(dsn="/path", layer="outfile")
## create a simple polygon layer and intersect
res <- gIntersection(df, as(extent(x.min, x.max, ymin, y.max), "SpatialPolygons"), byid = TRUE)

这可能无法完全按原样工作,但如果阅读整个 shapefile 不成问题,可能值得探索。

于 2013-09-10T10:39:27.007 回答