无法解压缩文件

问题描述 投票:0回答:1

我是 R 新手,安装 Rbase 4.3,使用运输流量 (https://datacatalog.worldbank.org/search/dataset/0037580/Global-Shipping-Traffic-Density) 和夜间灯光 (https:// eogdata.mines.edu/nighttime_light/annual/),但我在运行时遇到一些错误:

> # STEP 2: download, unzip and load traffic data
> 
> url <- "https://datacatalogfiles.worldbank.org/ddh-published/0037580/DR0045406/shipdensity_global.zip"
> destfile <- basename(url)
> 
> options(timeout = 999)
> 
> download.file(
+   url = url,
+   destfile = destfile,
+   mode = "wb"
+ )
trying URL 'https://datacatalogfiles.worldbank.org/ddh-published/0037580/DR0045406/shipdensity_global.zip'
Content type 'application/zip' length 534907254 bytes (510.1 MB)
downloaded 510.1 MB

Warning message:
In download.file(url = url, destfile = destfile, mode = "wb") :
  the 'wininet' method is deprecated for http:// and https:// URLs
> 
> source("https://raw.githubusercontent.com/milos-agathon/shipping-traffic-maps/main/R/decompress_file.r")
> 
> decompress_file(
+   directory = getwd(),
+   file = destfile
+ )
Error in system2("unzip", args = c("-o", file), stdout = TRUE) : 
  '"unzip"' not found
> 
> rastfile <- gsub(
+   ".zip",
+   ".tif",
+   destfile
+ )
> 
> global_traffic <- terra::rast(rastfile)
Error: [rast] file does not exist: shipdensity_global.tif
In addition: Warning message:
shipdensity_global.tif: No such file or directory (GDAL error 4) 
> 
> # STEP 3: Select the area of interest and crop
> 
> xmin <- -11.557617
> ymin <- 47.591346
> xmax <- 8.305664
> ymax <- 55.453941
> 
> bounding_box <- sf::st_sfc(
+   sf::st_polygon(
+     list(
+       cbind(
+         c(xmin, xmax, xmax, xmin, xmin),
+         c(ymin, ymin, ymax, ymax, ymin)
+       )
+     )
+   ),
+   crs = 4326
+ )
> 
> shipping_traffic <- terra::crop(
+   x = global_traffic,
+   y = bounding_box,
+   snap = "in"
+ )
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'crop': object 'global_traffic' not found
> 
> terra::plot(shipping_traffic)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'plot': object 'shipping_traffic' not found
> 
> shipping_traffic_clean <- terra::ifel(
+   shipping_traffic == 0,
+   NA,
+   shipping_traffic
+ )
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'test' in selecting a method for function 'ifel': object 'shipping_traffic' not found
> 
> # STEP 4: Get nightlight data
> 
> u <- "https://eogdata.mines.edu/nighttime_light/annual/v22/2022/VNL_v22_npp-j01_2022_global_vcmslcfg_c202303062300.average_masked.dat.tif.gz"
> filename <- basename(u)
> 
> download.file(
+   url = u,
+   destfile = filename,
+   mode = "wb"
+ )
trying URL 'https://eogdata.mines.edu/nighttime_light/annual/v22/2022/VNL_v22_npp-j01_2022_global_vcmslcfg_c202303062300.average_masked.dat.tif.gz'
Content type 'application/x-gzip' length 309708241 bytes (295.4 MB)
downloaded 295.4 MB

Warning message:
In download.file(url = u, destfile = filename, mode = "wb") :
  the 'wininet' method is deprecated for http:// and https:// URLs
> 
> path_to_nightlight <- list.files(
+   path = getwd(),
+   pattern = filename,
+   full.names = TRUE
+ )
> 
> nightlight <- terra::rast(
+   paste0(
+     "/vsigzip/",
+     path_to_nightlight
+   )
+ )
> 
> nightlight_region <- terra::crop(
+   x = nightlight,
+   y = bounding_box,
+   snap = "in"
+ )
> 
> nightlight_resampled <- terra::resample(
+   x = nightlight_region,
+   y = shipping_traffic_clean,
+   method = "bilinear"
+ )
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'y' in selecting a method for function 'resample': object 'shipping_traffic_clean' not found
> 
> terra::plot(nightlight_resampled)
Error in h(simpleError(msg, call)) : 
  error in evaluating the argument 'x' in selecting a method for function 'plot': object 'nightlight_resampled' not found
> 

我希望有人可以帮助解决 R 脚本中的错误。谢谢。

r download unzip
1个回答
0
投票

这是对您的代码的重写,直至绘图。

url <- "https://datacatalogfiles.worldbank.org/ddh-published/0037580/DR0045406/shipdensity_global.zip"
zipfile <- basename(url)
if (!file.exists(zipfile)) {
    options(timeout = 999)
    download.file(url, zipfile, mode = "wb")
    unzip(zipfile)
}

library(terra)
global_traffic <- gsub(".zip$", ".tif", zipfile)  |> terra::rast()
bbox <- terra::ext(-11.557617, 8.305664, 47.591346, 55.453941)
shipping_traffic <- terra::crop(global_traffic, bbox)

terra::plot(shipping_traffic)
shipping_traffic_clean <- terra::subst(shipping_traffic, 0, NA)

u <- "https://eogdata.mines.edu/nighttime_light/annual/v22/2022/VNL_v22_npp-j01_2022_global_vcmslcfg_c202303062300.average_masked.dat.tif.gz"
filename <- basename(u)
if (!file.exists(filename)) {
    download.file(u, filename, "wb")
}

nightlight <- terra::rast(paste0( "/vsigzip/", filename))
nightlight_region <- terra::crop(x = nightlight, y= bbox)

nightlight_resampled <- terra::resample(nightlight_region, shipping_traffic_clean)
terra::plot(nightlight_resampled)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.