In this vignette I’ll explain how to use the CopernicusDEM R package based on a use case of the Movebank animal tracking data. I picked animal tracking data because there is an abundance in the Movebank archive from all over the world. In this specific vignette I’ll use data of Wolves from the northeastern Alberta and Caribou from the British Columbia (see the reference papers at the end of the vignette for more information).
The following wrapped code snippet creates the leaflet and tmap maps of this vignette, and it does the following:
= c(system.file('vignette_data/Alberta_Wolves.csv', package = "CopernicusDEM"),
files system.file('vignette_data/Mountain_caribou.csv', package = "CopernicusDEM"))
= tmap_data = list()
leafgl_data
for (FILE in files) {
cat(glue::glue("Processing of the '{basename(FILE)}' file ..."), '\n')
= data.table::fread(FILE, header = TRUE, stringsAsFactors = FALSE)
dtbl = c('location-long', 'location-lat', 'timestamp', 'individual-local-identifier',
cols 'individual-taxon-canonical-name')
= dtbl[, ..cols]
dtbl_subs colnames(dtbl_subs) = c('longitude', 'latitude', 'timestamp', 'individual_local_identifier',
'individual-taxon-canonical-name')
unique(dtbl_subs$`individual-taxon-canonical-name`)]] = dtbl_subs
leafgl_data[[
= sf::st_as_sf(dtbl_subs, coords = c("longitude", "latitude"), crs = 4326)
dtbl_subs_sf
= fitbitViz::extend_AOI_buffer(dat_gps_tcx = dtbl_subs_sf,
sf_rst_ext buffer_in_meters = 250,
CRS = 4326,
verbose = TRUE)
#................................................................
# Download the Copernicus DEM 30m elevation data because it has
# a better resolution, it takes a bit longer to download because
# the .tif file size is bigger
#...............................................................
= tempdir()
dem_dir
= CopernicusDEM::aoi_geom_save_tif_matches(sf_or_file = sf_rst_ext$sfc_obj,
dem30 dir_save_tifs = dem_dir,
resolution = 30,
crs_value = 4326,
threads = parallel::detectCores(),
verbose = TRUE)
= list.files(dem_dir, pattern = '.tif', full.names = TRUE)
TIF
if (length(TIF) > 1) {
#....................................................
# create a .VRT file if I have more than 1 .tif files
#....................................................
= file.path(dem_dir, 'VRT_mosaic_FILE.vrt')
file_out
= CopernicusDEM::create_VRT_from_dir(dir_tifs = dem_dir,
vrt_dem30 output_path_VRT = file_out,
verbose = TRUE)
}
if (length(TIF) == 1) {
#..................................................
# if I have a single .tif file keep the first index
#..................................................
= TIF[1]
file_out
}
= fitbitViz::crop_DEM(tif_or_vrt_dem_file = file_out,
raysh_rst sf_buffer_obj = sf_rst_ext$sfc_obj,
CRS = 4326,
digits = 6,
verbose = TRUE)
# convert to character to receive the correct labels in the 'tmap' object
$individual_local_identifier = as.character(dtbl_subs_sf$individual_local_identifier)
dtbl_subs_sf
# open with interactive viewer
::tmap_mode("view")
tmap
= tmap::tm_shape(shp = dtbl_subs_sf) +
map_coords ::tm_dots(col = 'individual_local_identifier')
tmap
= map_coords + tmap::tm_shape(shp = raysh_rst, is.master = FALSE, name = 'Elevation') +
map_coords ::tm_raster(alpha = 0.65, legend.reverse = TRUE)
tmap
unique(dtbl_subs$`individual-taxon-canonical-name`)]] = map_coords
tmap_data[[ }
Now, based on the saved data.tables we can create first the leaflet map to view the data of both animals in the same map,
#.....................................
# create the 'leafGl' of both datasets
#.....................................
= rbind(leafgl_data$`Canis lupus`, leafgl_data$`Rangifer tarandus`)
dtbl_all
# see the number of observations for each animal
table(dtbl_all$`individual-taxon-canonical-name`)
# create an 'sf' object of both data.tables
= sf::st_as_sf(dtbl_all, coords = c("longitude", "latitude"), crs = 4326)
dat_gps_tcx
= leaflet::leaflet()
lft = leaflet::addProviderTiles(map = lft, provider = leaflet::providers$OpenTopoMap)
lft
= leafgl::addGlPoints(map = lft,
lft data = dat_gps_tcx,
opacity = 1.0,
fillColor = 'individual-taxon-canonical-name',
popup = 'individual-taxon-canonical-name')
lft