简体   繁体   English

在R中管道OGR / GDAL

[英]Piping OGR/GDAL in R

I have to read in a small portion of a huge ESRI shapefile in R. I am doing this in two steps: 我必须阅读R中巨大的ESRI shapefile的一小部分。我分两个步骤进行操作:

Step 1: I use ogr2ogr to clip the shapefile to my bounding box: 步骤1:我使用ogr2ogr将shapefile剪辑到边界框:

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

Step 2: I read this into R with rgdal : 步骤2:我用rgdal读入R:

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

The problem is that I have to do this for multiple files, and it's hard to keep track of the OGR operations which generated each of these separate files. 问题是我必须对多个文件执行此操作,并且很难跟踪生成每个单独文件的OGR操作。 Is there any way to pipe ogr2ogr in R, so that step 1 is done on the fly? 有什么方法可以在R中通过管道ogr2ogr ,以便动态进行步骤1?

Try using the system call. 尝试使用system调用。 Hard to tell without code and data, but you should be able to create a list of either shapefiles to process (if you have multiple shapefiles) or coordinates to process if you have multiple bounding boxes you want to clip from the shapefile. 没有代码和数据很难说,但是如果要从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))

Note that you can clip with the rgeos package, something like this (I use raster to make creating a simple mask polygon easy): 请注意,您可以使用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)

That might not work exactly as is, but could be worth exploring if reading the entire shapefile is not a problem. 这可能无法完全正常工作,但是如果读取整个shapefile没问题,则值得探讨。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM