【问题标题】:R - read in a list of files from a list of zip archives without unzipping themR - 从 zip 存档列表中读取文件列表而不解压缩它们
【发布时间】:2023-02-05 20:59:10
【问题描述】:

我正在尝试从 zip 档案列表中读取 shapefile 列表,而不实际解压缩档案。是的,我知道档案将在后台解压缩,但我想避免在 Windows 资源管理器中看到解压缩的文件。

这个例子可以完全重现,你需要从this Github repository 下载所有文件,并将你的工作目录设置为你下载它们的文件夹。

我也想用 tidyverse 风格来做,用管道而不保存中间对象。

我目前尝试运行的代码是这样的:

library(tidyverse)
library(magrittr)
library(sf)

list.files() %>% 
  map(unzip, list = T) %>% 
  map(filter, grepl(".shp$", Name)) %>% 
  map(~ .x %$% Name) %>% 
  map2(.x = ., .y = list.files(), .f = ~st_read(unzip(zipfile = .y, files = .x)))

但是,那是行不通的。为什么?

编辑:为了使示例更简洁,我想您也可以从上述存储库中下载两个文件。

【问题讨论】:

    标签: r list zip shapefile


    【解决方案1】:

    您可以定义一个小函数来下载 zip 文件、解压缩它、将形状文件读入内存、删除临时文件,然后只返回 sf 对象。

    例如:

    read_online_zip_sf <- function(url) {
      dir.create("~/zipdir")
      f <- tempfile(tmpdir = "~/zipdir", fileext = ".zip")
      download.file(url, f)
      files <- unzip(f, list = TRUE)
      unzip(f, files = files$Name, exdir = "~/zipdir/files")
      obj <- sf::st_read("~/zipdir/files")
      unlink("~/zipdir", recursive = TRUE)
      return(obj)
    }
    

    现在使用链接存储库中的一个 zip 文件进行测试,我们得到:

    url <- paste0("https://github.com/generalpiston/geojson-us-city-boundaries/",
                  "raw/master/shapes/tl_2019_01_place.zip")
    
    read_online_zip_sf(url)
    #> Reading layer `tl_2019_01_place' from data source 
    #>   `C:UsersAdministratorDocumentszipdiriles' using driver `ESRI Shapefile'
    #> Simple feature collection with 586 features and 16 fields
    #> Geometry type: MULTIPOLYGON
    #> Dimension:     XY
    #> Bounding box:  xmin: -88.4442 ymin: 30.19825 xmax: -84.96303 ymax: 34.99807
    #> Geodetic CRS:  NAD83
    #> Simple feature collection with 586 features and 16 fields
    #> Geometry type: MULTIPOLYGON
    #> Dimension:     XY
    #> Bounding box:  xmin: -88.4442 ymin: 30.19825 xmax: -84.96303 ymax: 34.99807
    #> Geodetic CRS:  NAD83
    #> First 10 features:
    #>    STATEFP PLACEFP  PLACENS   GEOID        NAME         NAMELSAD LSAD CLASSFP
    #> 1       01   05932 02405250 0105932       Berry       Berry town   43      C1
    #> 2       01   25840 02403599 0125840     Fayette     Fayette city   25      C1
    #> 3       01   32536 02406632 0132536      Gu-Win      Gu-Win town   43      C1
    #> 4       01   02908 02403122 0102908    Ashville    Ashville city   25      C1
    #> 5       01   46696 02406094 0146696    Margaret    Margaret town   43      C1
    #> 6       01   56400 02407036 0156400   Odenville   Odenville town   43      C1
    #> 7       01   43648 02406027 0143648 Littleville Littleville town   43      C1
    #> 8       01   63216 02407169 0163216     Ragland     Ragland town   43      C1
    #> 9       01   27616 02403645 0127616  Fort Payne  Fort Payne city   25      C1
    #> 10      01   74400 02406707 0174400    Sylvania    Sylvania town   43      C1
    #>    PCICBSA PCINECTA MTFCC FUNCSTAT     ALAND AWATER    INTPTLAT     INTPTLON
    #> 1        N        N G4110        A  27884733  15029 +33.6667018 -087.6093110
    #> 2        N        N G4110        A  22143483 212108 +33.6942153 -087.8311690
    #> 3        N        N G4110        A   5031111      0 +33.9443303 -087.8703764
    #> 4        N        N G4110        A  49762931 487106 +33.8351967 -086.2700148
    #> 5        N        N G4110        A  25438710  39224 +33.6728398 -086.4639420
    #> 6        N        N G4110        A  36752190 241048 +33.7008754 -086.4233712
    #> 7        N        N G4110        A  12877594 116619 +34.5951318 -087.6707387
    #> 8        N        N G4110        A  43481620 128940 +33.7483254 -086.1359268
    #> 9        Y        N G4110        A 145527316 924064 +34.4573293 -085.6894469
    #> 10       N        N G4110        A  22841332 459682 +34.5620432 -085.8047205
    #>                          geometry
    #> 1  MULTIPOLYGON (((-87.6391 33...
    #> 2  MULTIPOLYGON (((-87.85507 3...
    #> 3  MULTIPOLYGON (((-87.88578 3...
    #> 4  MULTIPOLYGON (((-86.30442 3...
    #> 5  MULTIPOLYGON (((-86.46153 3...
    #> 6  MULTIPOLYGON (((-86.38406 3...
    #> 7  MULTIPOLYGON (((-87.68859 3...
    #> 8  MULTIPOLYGON (((-86.18473 3...
    #> 9  MULTIPOLYGON (((-85.74184 3...
    #> 10 MULTIPOLYGON (((-85.85684 3...
    

    创建于 2023-02-05 reprex v2.0.2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-08
      • 2015-12-26
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多