glowabio / hydrographr

https://glowabio.github.io/hydrographr/
GNU General Public License v3.0
13 stars 1 forks source link

merge_tiles function error: "Error: [vect] Cannot open this file as a SpatVector: ..." #27

Closed ytorres-cambas closed 1 year ago

ytorres-cambas commented 1 year ago

Hi! I am trying to merge four tiles with the Danube basin and I am getting the error "Error: [vect] Cannot open this file as a SpatVector: ...". The function creates a .gpkg file anyways, and save it to disk, but the file has errors , that is why it cannot be imported into R (read = T) or into QGIS.

This is the code I used:

if (!require(remotes)) install.packages("remotes", dependencies = T) remotes::install_github("glowabio/hydrographr") library(hydrographr)

Download tiles into a folder

my_directory <- getwd() download_tiles(variable = "basin", file_format = "gpkg", tile_id = c("h18v02", "h18v04", "h20v02", "h20v04"), download_dir = my_directory)

Define folder containing only the tiles, which should me merged

tiles_folder <- paste0(my_directory, "/r.watershed/basin_tiles20d")

Define output folder

output_folder <- paste0(my_directory, "/merged_tiles")

Create output folder if it doesn't exist

if(!dir.exists(output_folder)) dir.create(output_folder)

Merge tiles

merged_tiles <- merge_tiles(tile_dir = tiles_folder, tile_names = c("basin_h18v02.gpkg", "basin_h18v04.gpkg", "basin_h20v02.gpkg", "basin_h20v04.gpkg"), out_dir = output_folder, file_name = "basin_merged.gpkg", read = TRUE)

afrogri37 commented 1 year ago

Hi! You need to set name="ID" in merge_tiles(). This is the attribute table column name of the basin columns in the basin .gpkg files. The default value for name is set to "stream", but this corresponds to the stream segment .gpkg files, which is not recognized in your case. Hope this solves the issue!

ytorres-cambas commented 1 year ago

Thanks! It works now. I would suggest to insert a few lines of code in the function merge_tiles() to check that the value of "name" given by the user actually corresponds to a column name in the attribute table of the .gpkg tiles.