[英]Piping OGR/GDAL in R
我必須閱讀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?
嘗試使用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))
請注意,您可以使用rgeos包進行裁剪,如下所示(我使用光柵使創建簡單的蒙版多邊形變得容易):
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沒問題,則值得探討。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.