I am new to R and install Rbase 4.3, using shipping traffic (https://datacatalog.worldbank.org/search/dataset/0037580/Global-Shipping-Traffic-Density) and nighttime light(https://eogdata.mines.edu/nighttime_light/annual/), but I have some error while running it:
> # 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
After that, I want to do
> 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
>
3
You are getting this error
Error in system2("unzip", args = c("-o", file), stdout = TRUE) :
'"unzip"' not found
And because you cannot unzip the downloaded file, the file needed in the remainder of your script is not available. So nothing works.
The download and unzip bit works for me like this:
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")
}
tiffile <- gsub(".zip$", ".tif", zipfile)
if (!file.exists(tiffile)) unzip(zipfile)
Here is a rewrite of the remainder of your code:
library(terra)
global_traffic <- terra::rast(tiffile)
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(log(nightlight_resampled))
2