Mox*_*Mox 0 for-loop r shapefile
我想将目录中的所有 shapefile 读入全局环境但是,当我在工作目录中获取文件列表时,该列表包含 .shp 和 .shp.xml 文件,后者是我不想要的。我的草稿代码读取 .shp 和 .shp.xml 文件。我怎样才能阻止它这样做?
草案代码如下:
库(地图工具)
# get all files with the .shp extension from working directory
setwd("N:/Dropbox/_BonesFirst/139_Transit_Metros_Points_Subset_by_R")
shps <- dir(getwd(), "*.shp")
# the assign function will take the string representing shp
# and turn it into a variable which holds the spatial points data
for (shp in shps) {
cat("now loading", shp, "...", '\n\r')
assign(shp, readOGR(shp))
}
Run Code Online (Sandbox Code Playgroud)
编辑:问题似乎出在 readShapePoints 中。readOGR(来自 rgdal)或 shapefile(来自 raster)效果更好。
获取所有文件:
# replace with your folder name:
dir <- "c:/files/shpfiles"
ff <- list.files(dir, pattern="\\.shp$", full.names=TRUE)
Run Code Online (Sandbox Code Playgroud)
现在阅读它们。最简单的与raster::shapefile. 不要使用 readShapefile (已过时且不完整)
library(raster)
# first file
shapefile(ff[1])
# all of them into a list
x <- lapply(ff, shapefile)
Run Code Online (Sandbox Code Playgroud)
如今,您可以使用“terra”并执行以下操作
library(terra)
v <- vect( lapply(ff, vect) )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5377 次 |
| 最近记录: |