【问题标题】:using list.files to read many shape files and then merge them into one big file使用 list.files 读取许多形状文件,然后将它们合并为一个大文件
【发布时间】:2019-06-14 23:41:08
【问题描述】:

我在一个目录中有超过 1000 个形状文件,我只想选择其中的 10 个名称我已经知道的文件,如下所示:

15TVN44102267_Polygons.shp, 15TVN44102275_Polygons.shp
15TVN44102282_Polygons.shp, 15TVN44102290_Polygons.shp
15TVN44102297_Polygons.shp, 15TVN44102305_Polygons.shp
15TVN44102312_Polygons.shp, 15TVN44102320_Polygons.shp
15TVN44102327_Polygons.shp, 15TVN44102335_Polygons.shp

首先我想使用 list.files 命令只读取这些形状文件,然后将它们合并到一个大文件中。我尝试了以下命令,但失败了。我将感谢社区提供的任何帮助。

setwd('D/LiDAR/CHM_tree_objects')
files <- list.files(pattern="15TVN44102267_Polygons|
15TVN44102275_Polygons|    15TVN44102282_Polygons|
15TVN44102290_Polygons|    15TVN44102297_Polygons|
15TVN44102305_Polygons|    15TVN44102312_Polygons|
15TVN44102320_Polygons|    15TVN44102327_Polygons|
15TVN44102335_Polygons|    15TVN44102342_Polygons|
15TVN44102350_Polygons|    15TVN44102357_Polygons",
recursive = TRUE, full.names = TRUE)

【问题讨论】:

    标签: r


    【解决方案1】:

    这里有一个稍微不同的方法。如果您已经知道文件的位置及其文件名,则无需使用list.files

    library(sf)
    
    baseDir <- '/temp/r/'
    filenames <- c('Denisonia-maculata.shp', 'Denisonia-devisi.shp')
    filepaths <- paste(baseDir, filenames, sep='')
    
    # Read each shapefile and return a list of sf objects
    listOfShp <- lapply(filepaths, st_read)
    
    # Look to make sure they're all in the same CRS
    unique(sapply(listOfShp, crs))
    
    # Combine the list of sf objects into a single object
    combinedShp <- do.call(what = sf:::rbind.sf, args=listOfShp)
    

    combinedShp 将成为一个 sf 对象,它具有您各个 shapefile 中的所有功能。然后,您可以使用 st_write 以您选择的格式将其写入单个文件。

    【讨论】:

    • 嗨斯图尔特,太棒了!谢谢你的帮助。您的代码可以正常工作以获得组合的Shp(即组合的 sf 对象)。但是,当我尝试使用 st_write(combinedShp, "combinedshp.shp") 将其写入单个 shapefile 时,我收到以下错误:GDAL 错误 6:shapefile 中不支持“3D 多边形”的几何类型。关于正在发生的事情以及如何纠正它的任何线索?再次感谢。
    • 仅供参考,当我使用 st_write(combinedShp, "combinedShp.gpkg") 时它可以工作。但是 st_write(combinedShp, "combinedShp.shp") 不起作用
    • 很高兴你能成功。您的原始 shapefile 是 3D 多边形吗?
    • 是的,多边形是使用 FUSION 软件生成的 LiDAR 衍生树对象(即单个树冠边界)。谢谢。
    • 嗨斯图尔特,我在尝试使用以下 R 脚本将数千个形状文件组合成一个大形状文件时收到错误“错误:参数有不同的 crs”。你能帮我纠正一下吗?感谢 library(sf) filepaths
    猜你喜欢
    • 1970-01-01
    • 2020-01-20
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2017-02-15
    • 2021-12-05
    相关资源
    最近更新 更多