簡體   English   中英

使用list.files讀取許多形狀文件,然后將它們合並為一個大文件

[英]using list.files to read many shape files and then merge them into one big file

我在一個目錄中有超過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)

這是一種略有不同的方法。 如果您已經知道文件的位置及其文件名,則不需要使用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將是一個具有各個shapefile中所有功能的sf對象。 然后,您可以使用st_write將其寫入所選格式的單個文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM