将R目录中的所有shapefile读入内存 [英] Read in all shapefiles in a directory in R into memory

查看:240
本文介绍了将R目录中的所有shapefile读入内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将目录中的所有shapefile读取到全局环境中.但是,当我在工作目录中获得文件列表时,该列表同时包含.shp和.shp.xml文件,而我会这样做不想.我的草稿代码同时读取.shp和.shp.xml文件.如何防止它这样做?

I would like to read all shapefiles in a directory into the global environment However, when I get a list of files in my working directory, the list includes both .shp and .shp.xml files, the latter of which I do not want. My draft code reads both .shp and .shp.xml files. How can I prevent it from doing so?

草稿代码如下:

库(maptools)

library(maptools)

# 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)) 
                  }

问题似乎在readShapePoints中. readOGR(来自rgdal)或shapefile(来自栅格)可以更好地工作.

Problems seems to be in the readShapePoints. Either readOGR (from rgdal) or shapefile (from raster) work better.

推荐答案

获取所有文件:

# replace with your folder name:
dir <- "c:/files/shpfiles"
ff <- list.files(dir, pattern="\\.shp$", full.names=TRUE)

现在阅读它们. raster::shapefile最简单.不要使用readShapefile(过时和不完整)

Now read them. Easiest with raster::shapefile. Do not use readShapefile (obsolete and incomplete)

library(raster)
# first file
shapefile(ff[1])

# all of them into a list
x <- lapply(ff, shapefile)

这篇关于将R目录中的所有shapefile读入内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆