【问题标题】:Piping OGR/GDAL in R在 R 中管道 OGR/GDAL
【发布时间】:2013-09-13 21:18:23
【问题描述】:

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

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

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

第 2 步:我使用 rgdal 将其读入 R:

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

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

【问题讨论】:

  • Here's an example 了解如何使用rgeos::gIntersection 裁剪多边形。 (这与下面@mdsumner 的回答基本相同,但带有可重现的示例和纯图片。)

标签: r gis gdal


【解决方案1】:

尝试使用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))

【讨论】:

  • 工作就像一个魅力。非常感谢,SlowLearner。 (PS:如果这样慢,不知道你提速后会是什么样子。)
  • 嗯,这都是相对的,不是吗 - 与 Stack Overflow 的 R 元老们相比,我们还有很长的路要走......
  • 至少对我来说是这样。
【解决方案2】:

请注意,您可以使用 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 不成问题,可能值得探索。

【讨论】:

  • 您好 mdsumner... 问题是文件太大而无法直接在 R 中加载。这就是我分两步执行此操作的原因。 SlowLearner 的解决方案效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 2019-08-28
  • 1970-01-01
相关资源
最近更新 更多