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

add function to extract points from rasters #91

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Authors@R: c(
person("Stijn", "Van Hoey", role = c("aut", "cre"), email = "stijnvanhoey@gmail.com", comment = c(ORCID = "0000-0001-6413-3185")),
person("Willem", "Maetens", role = "ctb", email = "w.maetens@vmm.be"),
person("Peter", "Desmet", role = "ctb", email = "peter.desmet@inbo.be", comment = c(ORCID = "0000-0002-8442-8025")),
person(given = "Thierry", family = "Onkelinx", role = "ctb", email = "thierry.onkelinx@inbo.be", comment = c(ORCID = "0000-0001-8804-4216")),
person("Research Institute for Nature and Forest (INBO)", role = "cph", email = "info@inbo.be")
)
License: MIT + file LICENSE
Expand All @@ -20,10 +21,12 @@ BugReports: https://github.com/ropensci/wateRinfo/issues
Depends:
R (>= 2.10)
Imports:
assertthat,
dplyr,
httr,
jsonlite,
openssl,
purrr,
lubridate (>= 1.6.0),
rlang,
utils
Expand All @@ -36,4 +39,4 @@ Suggests:
LazyData: true
Encoding: UTF-8
VignetteBuilder: knitr
RoxygenNote: 6.1.1
RoxygenNote: 7.1.1
8 changes: 8 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ S3method(print,token)
S3method(print,waterinfo_api)
export(check_period_format)
export(expires.in)
export(get_raster_points)
export(get_stations)
export(get_timeseries_tsid)
export(get_token)
Expand All @@ -17,9 +18,14 @@ export(resolve_timeseriesgroupid)
export(show.token)
export(supported_frequencies)
export(supported_variables)
importFrom(assertthat,assert_that)
importFrom(assertthat,has_name)
importFrom(dplyr,"%>%")
importFrom(dplyr,bind_cols)
importFrom(dplyr,bind_rows)
importFrom(dplyr,filter)
importFrom(dplyr,filter_)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(httr,GET)
importFrom(httr,POST)
Expand All @@ -32,6 +38,8 @@ importFrom(jsonlite,fromJSON)
importFrom(lubridate,parse_date_time)
importFrom(lubridate,ymd_hms)
importFrom(openssl,base64_encode)
importFrom(purrr,map)
importFrom(purrr,map2)
importFrom(rlang,.data)
importFrom(rlang,quo)
importFrom(utils,read.csv)
Expand Down
25 changes: 12 additions & 13 deletions R/call_waterinfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ waterinfo_pro_base <- function() {
hic_base <- function() {
"https://www.waterinfo.be/tsmhic/KiWIS/KiWIS"
}
hydro_base <- function() {
"https://hydro.vmm.be/grid/kiwis/KiWIS"
}


#' http call to waterinfo.be
#'
Expand All @@ -26,20 +30,15 @@ hic_base <- function() {
#'
#' @importFrom httr GET http_type status_code http_error content add_headers
#' @importFrom jsonlite fromJSON
call_waterinfo <- function(query, base_url = "vmm", token = NULL) {

hic_base

call_waterinfo <- function(
query, base_url = c("vmm", "hic", "pro", "hydro"), token = NULL
) {
# check the base url, which depends of the query to execute
if (base_url == "vmm") {
base <- waterinfo_base()
} else if (base_url == "hic") {
base <- hic_base()
} else if (base_url == "pro") {
base <- waterinfo_pro_base()
} else {
stop("Base url should be vmm, hic or pro")
}
base_url <- match.arg(base_url)
base <- switch(
base_url, vmm = waterinfo_base(), hic = hic_base(),
pro = waterinfo_pro_base(), hydro = hydro_base()
)

if (is.null(token)) {
res <- GET(base, query = query)
Expand Down
47 changes: 47 additions & 0 deletions R/get_raster_points.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#' Get timeseries data for specific points on a raster
#' @inheritParams get_timeseries_tsid
#' @param points a matrix or data.frame with coordinates in decimal degrees
#' using the EPSG 4326 coordinate system.
#' It must have the colnames `lat` and `long`.
#' @export
#' @importFrom assertthat assert_that has_name
#' @importFrom dplyr bind_cols bind_rows mutate %>%
#' @importFrom lubridate ymd_hms
#' @importFrom purrr map map2
get_raster_points <- function(
ts_id = 911010, period = NULL, from = NULL, to = NULL, points,
datasource = 10, token = NULL
) {
# check and handle the date/period information
period_info <- parse_period(from, to, period)
assert_that(
inherits(points, "data.frame"), has_name(points, c("long", "lat"))
)

query_list <- list(
type = "queryServices", service = "kisters",
request = "getRasterToPointValues", ts_id = ts_id, format = "json",
datasource = datasource, raster_epsg = 4326, metadata = "True",
raster_x = paste(points$long, collapse = ","),
raster_y = paste(points$lat, collapse = ",")
)
time_series <- call_waterinfo(
query = c(query_list, period_info), base_url = "hydro", token = token
)
map(
seq_along(points$long),
function(i) {
cbind(points[i, c("long", "lat")], ts_id = ts_id)
}
) %>%
map(`rownames<-`, NULL) -> points_list

map(time_series$content$data, `colnames<-`, c("timestamp", "value")) %>%
map(data.frame) %>%
map2(points_list, bind_cols) %>%
bind_rows() %>%
mutate(
timestamp = ymd_hms(timestamp),
value = as.numeric(value)
)
}
6 changes: 4 additions & 2 deletions man/air_pressure.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/call_waterinfo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions man/get_raster_points.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/get_stations.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions man/get_timeseries_tsid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions man/get_token.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/get_variables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/liedekerke.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/wateRinfo-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading