在没有安装 rgdal 的情况下解压缩和读取 R 中的形状文件 [英] Unzipping and reading shape file in R without rgdal installed

查看:72
本文介绍了在没有安装 rgdal 的情况下解压缩和读取 R 中的形状文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不依赖 rgdal 的情况下在 R 中解压缩并从网络中读取形状文件.我发现 fastshp 包的 read.shp 函数显然可以在没有在环境中安装 rgdal 的情况下完成此操作,但是,我无法实现.

I would like to unzip and read in a shape file from the web in R without relying on rgdal. I found the read.shp function of the fastshp package that can apparently accomplish this without rgdal installed in the environment, however, I'm having trouble implementing.

我想要一个可以解压缩然后读入形状文件的函数,类似于在这个SO 帖子,但用于 read.shp 功能.我尝试了以下但无济于事:

I would like a function that can unzip and then read in the shape file akin to what's found in this SO post but for the read.shp function. I tried the following but to no avail:

dlshape=function(shploc, format) {
  temp=tempfile()
  download.file(shploc, temp)
  unzip(temp)
  shp.data <- sapply(".", function(f) {
    f <- file.path(temp, f)
    return(read.shp(".", format))
  })
}

shp_object<-dlshape('https://www2.census.gov/geo/tiger/TIGER2017/COUNTY/tl_2017_us_county.zip', 'polygon')
 Error in read.shp(".", format) : unused argument (format) 

我还尝试了以下方法:

  dlshape=function(shploc) {
      temp=tempfile()
      download.file(shploc, temp)
      unzip(temp)
      shp.data <- sapply(".", function(f) {
        f <- file.path(temp, f)
        return(read.shp("."))
      })
    }

 shp_object<-dlshape('https://www2.census.gov/geo/tiger/TIGER2017/COUNTY/tl_2017_us_county.zip')

Error in file(shp.name, "rb") : cannot open the connection
In addition: Warning messages:
1: In file(shp.name, "rb") : 'raw = FALSE' but '.' is not a regular file
2: In file(shp.name, "rb") :
 Show Traceback
 Rerun with Debug
 Error in file(shp.name, "rb") : cannot open the connection

我怀疑这与在函数 read.shp() 中我给它提供文件夹名称而不是 .shp 名称(对于 readOGR 有效但不适用于 read.shp).非常感谢任何帮助.

I suspect it has to do with the fact that in the function read.shp() I'm feeding it the folder name and not the .shp name (for readOGR that works but not for read.shp). Any assistance is much appreciated.

推荐答案

您可以使用 utils 中的 unzip() 和 sf 中的 read_sf() 来解压然后加载你的形状文件.这是一个工作示例:

You can use unzip() from utils and read_sf() from sf to unzip and then load your shapefile. Here is a working example:

#create a couple temp files
temp <- tempfile()
temp2 <- tempfile()
#download the zip folder from the internet save to 'temp' 
download.file("https://www2.census.gov/geo/tiger/TIGER2017/COUNTY/tl_2017_us_county.zip",temp)
#unzip the contents in 'temp' and save unzipped content in 'temp2'
unzip(zipfile = temp, exdir = temp2)
#finds the filepath of the shapefile (.shp) file in the temp2 unzip folder
#the $ at the end of ".shp$" ensures you are not also finding files such as .shp.xml 
your_SHP_file<-list.files(temp2, pattern = ".shp$",full.names=TRUE)

#read the shapefile. Alternatively make an assignment, such as f<-sf::read_sf(your_SHP_file)
sf::read_sf(your_SHP_file)

这篇关于在没有安装 rgdal 的情况下解压缩和读取 R 中的形状文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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