【问题标题】:How can I clip large shapefiles in program R without the computer crashing?如何在程序 R 中剪辑大型 shapefile 而不会导致计算机崩溃?
【发布时间】:2015-02-13 05:30:14
【问题描述】:

我想在程序 R 中剪辑一个大的 shapefile (67MB),并从其中大约 5% 导出一个小得多的栅格。加载后,shapefile 具有 221388 个特征和 5 个字段 - 并扩展至 746 MB。

我的困难在于尝试将文件剪辑到可行的大小 - 程序在几分钟后崩溃。我已经尝试过裁剪(来自光栅)和 gIntersection(来自 rgeos)但没有成功。我有 8GB 的​​ RAM - 显然存在内存问题。

我猜可能有解决办法。我知道那里有一些大内存包 - 但它们中的任何一个都可以帮助我的这种情况吗?我当前的代码如下:

# dataset can be found at 
# http://data.fao.org/map?entryId=271096b2-8a12-4050-9ff2-27c0fec16c8f

# location of files
ogrListLayers("C:/Users/Me/Documents/PNG Glob")

# import shapefile
ogrDrivers()[10,]

# shapefiles
Glob<-readOGR("C:/Users/Me/Documents/PNG Glob", layer="png_gc_adg_1")

# assign projection
Glob@proj4string<- CRS("+proj=longlat")

#object size
object.size(Glob) 

# clipping
crop(Glob, extent(c(144,146,-7,-5)))

【问题讨论】:

  • this question 的答案可能会对您有所帮助。但我不确定在 Windows 上的可行性。
  • 谢谢帕斯卡 - 我现在正在调查这个。

标签: r gis ram raster shapefile


【解决方案1】:

正如@Pascal 所建议的,GDAL 的ogr2ogr 对此很有用。您可以使用system 从 R 中调用它,如下所示(包括在 Windows 上),尽管这假设(1)您有一个有效的 GDAL 安装并且(2)GDAL 二进制文件的路径存在于您的 PATH 环境变量中:

  1. 下载并解压 PNG shapefile:

    download.file('http://www.fao.org/geonetwork/srv/en/resources.get?id=37162&fname=png_gc_adg.zip&access=private', 
                  f <- tempfile(fileext='.zip'))
    unzip(f, exdir=tempdir())
    
  2. 从 R 调用 ogr2ogrsystem 以剪辑 PNG shapefile 并将生成的 .shp 保存到工作目录:

    system(sprintf('ogr2ogr -clipsrc 144 -7 146 -5 png_clip.shp %s', 
                   file.path(tempdir(), 'png_gc_adg_1.shp')))
    

    在我的系统上,这大约需要 70 秒,而内存使用量的增加似乎没有超过 100MB。 (我确实收到了很多 Warning 1: Value 138717513240 of field AREA of feature 0 not successfully written. Possibly due to too larger number with respect to field width 之类的警告 - 不知道那是什么意思。)

  3. 加载剪切的shapefile:

    library(rgdal)
    p <- readOGR('.', 'png_clip')
    plot(p)
    

【讨论】:

  • 谢谢!这看起来是要走的路。我目前正在尝试安装 GDAL,但有一些安装问题......一旦它启动并运行,我会试一试。
  • @NathanWhitmore,我害怕在 Windows 操作系统上出现这些问题。
  • @Pascal - 这可能很痛苦,但它可能的。我正在使用 Windows :)
  • @jbaums 很高兴听到。因此,OP有希望:)
  • @jbaums 终于让它运行起来了——谢谢。经过一番困惑后,我设法使用 OSGeo4W 让 ogr2ogr 运行起来。获取正确路径的 R 代码在这里:gis.stackexchange.com/questions/79981/…
猜你喜欢
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多