/
get_tile_id.R
56 lines (45 loc) · 1.9 KB
/
get_tile_id.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#' @title Get the Hydrography90m 20°x20° tile ID
#'
#' @description Identifies the 20°x20° tile IDs of the Hydrography90m
#' data in which the input points are located. The IDs can then be used to
#' download the data using \code{\link{download_tiles()}}. The input is a data
#' frame with point coordinates. For orientation, please also see the tiles at
#' the \url{https://hydrography.org/hydrography90m/hydrography90m_layers}
#'
#' @param data a data.frame or data.table that contains the columns regarding
#' the longitude / latitude coordinates in WGS84.
#' @param lon character. The name of the column with the longitude coordinates.
#' @param lat character. The name of the column with the latitude coordinates.
#' @importFrom data.table fread
#' @export
#'
#' @author Afroditi Grigoropoulou
#'
#' @examples
#' # Download test data into the temporary R folder
#' # or define a different directory
#' my_directory <- tempdir()
#' download_test_data(my_directory)
#'
#' # Load species occurrence data
#' species_occurrence <- read.table(paste0(my_directory,
#' "/hydrography90m_test_data",
#' "/spdata_1264942.txt"),
#' header = TRUE)
#'
#' # Get the tile ID
#' get_tile_id(data = species_occurrence,
#' lon = "longitude", lat = "latitude")
get_tile_id <- function(data, lon, lat) {
reg_un <- get_regional_unit_id(data, lon, lat)
lookup_file <- paste0(tempdir(), "/lookup_tile_regunit.txt")
if (!file.exists(lookup_file)) {
download.file("https://drive.google.com/uc?export=download&id=1deKhOEjGgvUXPwivYyH99hgHlJV7OgUv&confirm=t",
destfile = lookup_file,
quiet = FALSE)
}
lookup <- fread(lookup_file)
# Find the tile ID in the lookup table based on the regional unit id
tile_id <- sort(unique(lookup[lookup$reg_unit %in% reg_un, tile]))
tile_id
}