如何在 R 中将一个非常大的 OpenStreetMap 文件分成较小的文件而不 运行 内存不足?

How do I divide a very large OpenStreetMap file into smaller files in R without running out of memory?

我目前正在寻找不大于墨西哥城市规模(最大约 3 度 longitude/latitude 宽)的地图文件。但是,在尝试这样做时,我一直 运行 陷入内存问题(至少)。 OSM XML 对象的文件大小为 1.9 GB,供参考。

library(osmar)
get.map.for.municipality<-function(province,municipality){
  base.map.filename = 'OpenStreetMap/mexico-latest.osm'
  #bounds.list is a list that contains the boundaries
  bounds = bounds.list[[paste0(province,'*',municipality)]]
  my.bbox = corner_bbox(bounds[1],bounds[2],bounds[3],bounds[4])
  my.map.source = osmsource_file(base.map.filename)
  my.map = get_osm(my.bbox,my.map.source)
  return(my.map)
}

我运行在一个循环中执行此操作,但它甚至无法通过第一个循环。当我尝试 运行 时,我的电脑死机了,我只能用我的 phone 截屏。内存在几分钟内稳步下降,然后迅速上升,我还没来得及反应,我的电脑就死机了。

执行此操作的更好方法是什么?我预计必须 运行 这个循环大约 100-150 次,所以任何在内存方面更有效的方法都会有所帮助。我不想从 API 服务下载较小的文件。 如果有必要,我愿意使用另一种编程语言(最好是 Python 或 C++),但我更愿意将其保留在 R 中。

我建议不要为此使用 R。

这项工作有更好的工具。从命令行或使用 DBMS.

拆分、过滤内容的多种方法

以下是从 OSM Wiki 中提取的一些备选方案 http://wiki.openstreetmap.org:

Filter your osm files using osmfilter: "osmfilter is used to filter OpenStreetMap data files for specific tags. You can define different kinds of filters to get OSM objects (i.e. nodes, ways, relations), including their dependent objects, e.g. nodes of ways, ways of relations, relations of other relations."

使用 osmconvert 基于多边形或边界进行裁剪:http://wiki.openstreetmap.org/wiki/Osmconvert#Applying_Geographical_Borders

您可以为 osmfilter 和 osmconvert 编写 bash 脚本,但我建议使用 DBMS。只需使用 osm2pgsql 导入 PostGIS,并将您的 R 代码与任何 Postgresql 驱动程序连接。这将优化您的 read/write 操作。