Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in do.call(rbind, x) : second argument must be a list #2

Open
Ruslan161296 opened this issue Jan 4, 2024 · 0 comments
Open

Comments

@Ruslan161296
Copy link

Hello, awesome tutorial. I repeated all your steps and everything works. When I try to adapt the code to my zone of interest when executing block 8, I get an error: Error in do.call(rbind, x) : second argument must be a list. I couldn't figure out what the reason is, can you tell me? There were no problems with do.call in block 6

1. PACKAGES

libs <- c(
"terra",
"giscoR",
"sf",
"tidyverse",
"ggtern",
"elevatr",
"png",
"rayshader",
"magick"
)

installed_libraries <- libs %in% rownames(
installed.packages()
)

if(any(installed_libraries == F)){
install.packages(
libs[!installed_libraries]
)
}

invisible(
lapply(
libs, library, character.only = T
)
)

2. COUNTRY BORDERS

get_country_borders <- function() {
main_path <- getwd()
country_borders <- geodata::gadm(
country = "KAZ",
level = 1,
path = main_path
) |>
sf::st_as_sf()

return(country_borders)
}

country_borders <- get_country_borders()
unique(
country_borders$NAME_1
)

country_sf <- country_borders |>
dplyr::filter(
NAME_1 %in% c(
"South Kazakhstan"
)
) |>
sf::st_geometry()

plot(sf::st_geometry(
country_sf
))

#plot(sf::st_geometry(country_sf))

png("bih-borders.png")
plot(sf::st_geometry(country_sf))
dev.off()

3 DOWNLOAD ESRI LAND COVER TILES

urls <- c(
"https://lulctimeseries.blob.core.windows.net/lulctimeseriesv003/lc2022/42T_20220101-20230101.tif"

)

for(url in urls){
download.file(
url = url,
destfile = basename(url),
mode = "wb"
)
}

4 LOAD TILES

raster_files <- list.files(
path = getwd(),
pattern = "20230101.tif$",
full.names = T
)
print(raster_files)
crs <- "EPSG:4326"

for(raster in raster_files){
rasters <- terra::rast(raster)

country <- country_sf |>
    sf::st_transform(
        crs = terra::crs(
            rasters
        )
    )

land_cover <- terra::crop(
    rasters,
    terra::vect(
        country
    ),
    snap = "in",
    mask = T
) |>
terra::aggregate(
    fact = 5,
    fun = "modal"
) |>
terra::project(crs)

terra::writeRaster(
    land_cover,
    paste0(
        raster,
        "_bosnia",
        ".tif"
    )
)

}

5 LOAD VIRTUAL LAYER

r_list <- list.files(
path = getwd(),
pattern = "_bosnia",
full.names = T
)

land_cover_vrt <- terra::vrt(
r_list,
"bosnia_land_cover_vrt.vrt",
overwrite = T
)

6 FETCH ORIGINAL COLORS

ras <- terra::rast(
raster_files[[1]]
)

raster_color_table <- do.call(
data.frame,
terra::coltab(ras)
)

head(raster_color_table)

hex_code <- ggtern::rgb2hex(
r = raster_color_table[,2],
g = raster_color_table[,3],
b = raster_color_table[,4]
)

7 ASSIGN COLORS TO RASTER

cols <- hex_code[c(2,3,5,6,8,9,10,11,12)]
print(cols)
from <- c(1, 2, 4, 5, 7, 8, 9, 10, 11) # Adjust the indices based on your specific case
to <- t(col2rgb(cols))
print(to)
print(land_cover_vrt)

land_cover_vrt <- na.omit(land_cover_vrt)

land_cover_bosnia <- terra::subst(
land_cover_vrt,
from = from,
to = to,
names = cols
)

terra::plotRGB(land_cover_bosnia)

8 DIGITAL ELEVATION MODEL

elev <- elevatr::get_elev_raster(
locations = country_sf,
z = 9, clip = "locations"
)

crs_lambert <-
"+proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +datum=WGS84 +units=m +no_frfs"

land_cover_bosnia_resampled <- terra::resample(
x = land_cover_bosnia,
y = terra::rast(elev),
method = "near"
) |>
terra::project(crs_lambert)

terra::plotRGB(land_cover_bosnia_resampled)

img_file <- "land_cover_bosnia.png"

terra::writeRaster(
land_cover_bosnia_resampled,
img_file,
overwrite = T,
NAflag = 255
)

img <- png::readPNG(img_file)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant