runzipterra

R read zip-file, gpkg-file with terra vect()


I have two questions on applying vect of the package terra with files from an opendata-source.

  1. I would like to read the shp-file with vect from the directory "Amphibien_LV95" within the zip-file "data" without downloading and unzipping the file locally.
link <- https://data.geo.admin.ch/ch.bafu.bundesinventare-amphibien/data.zip

Unfortunately my attempts with unz and unzip in combination with vect() were not successful.

  1. I would like to read a gpkg-File from an http-address (in special the layer=ElectricityProductionPlant)
link <- "https://data.geo.admin.ch/ch.bfe.elektrizitaetsproduktionsanlagen/gpkg/2056/ch.bfe.elektrizitaetsproduktionsanlagen.gpkg"

data <- vect(link)

With vect() I get the error:

Error: [vect] cannot read this geometry type: Unknown (any)

If I read the link with st_read and then transform it with vect it works. Why?

a <- st_read(link)
data <- vect(a)

Solution

  • VSI handlers can also be chained, so we could use zip + curl for the 1st:

    library(terra)
    #> terra 1.7.83
    zip_link <- "https://data.geo.admin.ch/ch.bafu.bundesinventare-amphibien/data.zip"
    (vsi <- paste("/vsizip", "/vsicurl", zip_link, "Amphibien_LV95", sep = "/" ))
    #> [1] "/vsizip//vsicurl/https://data.geo.admin.ch/ch.bafu.bundesinventare-amphibien/data.zip/Amphibien_LV95"
    vector_layers(vsi)
    #> [1] "am_A3"              "amphibLaichgebiet"  "amphibWanderobjekt"
    vect(vsi, layer = "amphibLaichgebiet")[,1:4]
    #>  class       : SpatVector 
    #>  geometry    : polygons 
    #>  dimensions  : 1448, 4  (geometries, attributes)
    #>  extent      : 2486326, 2830988, 1075278, 1291233  (xmin, xmax, ymin, ymax)
    #>  source      : Amphibien_LV95 (amphibLaichgebiet)
    #>  coord. ref. : CH1903+ / LV95 (EPSG:2056) 
    #>  names       : ObjNummer           Name                RefObjBlat DesignatTy
    #>  type        :     <chr>          <chr>                     <chr>      <chr>
    #>  values      :       AG3 Zurlindeninsel https://data.geo.admin.c~        NDA
    #>                     AG19       Halbmond https://data.geo.admin.c~        NDA
    #>                     AG19       Halbmond https://data.geo.admin.c~        NDA