Skip to content

Commit 28eaf2d

Browse files
authored
Merge pull request #213 from R-ArcGIS/rasterfn
Add raster function support for `arc_raster()`
2 parents 63cf157 + 638c1d3 commit 28eaf2d

File tree

7 files changed

+127
-2
lines changed

7 files changed

+127
-2
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
## New features
88

9+
- `arc_raster()` gains an argument `raster_fn` which takes a character scalar and performs a raster function server side before returning results
10+
- `list_service_raster_fns()` is a new helper function to list available raster functions for an `ImageServer`
11+
912
## Breaking changes
1013

1114
# arcgislayers 0.3.0

R/arc-raster.R

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#' @param bbox_crs the CRS of the values passed to `xmin`, `xmax`, `ymin`, and `ymax`.
1313
#' If not specified, uses the CRS of `x`.
1414
#' @param format default `"tiff"`. Must be one of "jpgpng", "png", "png8", "png24", "jpg", "bmp", "gif", "tiff", "png32", "bip", "bsq", "lerc".
15+
#' @param ... additional key value pairs to be passed to [`httr2::req_body_form()`].
16+
#' @param raster_fn a scalar string with the name of the service raster function. See [`list_service_raster_fns()`] for available raster functions.
1517
#' @param width default `NULL`. Cannot exceed `x[["maxImageWidth"]]`.
1618
#' @param height default `NULL`. Cannot exceed `x[["maxImageHeight"]]`.
1719
#' @param token default `arc_token()` authorization token.
@@ -29,8 +31,8 @@
2931
#' arc_raster(
3032
#' landsat,
3133
#' xmin = -71,
32-
#' ymin = 43,
3334
#' xmax = -67,
35+
#' ymin = 43,
3436
#' ymax = 47.5,
3537
#' bbox_crs = 4326,
3638
#' width = 100,
@@ -54,7 +56,22 @@ arc_raster <- function(
5456
width = NULL,
5557
height = NULL,
5658
format = "tiff",
59+
...,
60+
raster_fn = NULL,
5761
token = arc_token()) {
62+
check_string(raster_fn, allow_null = TRUE)
63+
if (!is.null(raster_fn)) {
64+
if (!raster_fn %in% list_service_raster_fns(x)[["name"]]) {
65+
cli::cli_abort(
66+
c(
67+
"{.arg raster_fn} value of {.val {raster_fn}} is not known",
68+
i = "Use {.fn list_service_raster_fns} to see available raster functions"
69+
)
70+
)
71+
} else {
72+
raster_fn <- jsonify::to_json(list(rasterFunction = raster_fn), unbox = TRUE)
73+
}
74+
}
5875
# validate and extract CRS object
5976
out_sr <- validate_crs(crs)[["spatialReference"]][["wkid"]]
6077

@@ -74,6 +91,8 @@ arc_raster <- function(
7491
format = format,
7592
size = paste0(c(width, height), collapse = ","),
7693
outSR = out_sr,
94+
...,
95+
renderingRule = raster_fn,
7796
f = "json"
7897
)
7998

R/arc-select.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@ validate_page_size <- function(
623623
# Protocol Buffer helpers ------------------------------------------------
624624

625625
supports_pbf <- function(x, arg = rlang::caller_arg(x), call = rlang::caller_call()) {
626+
check_inherits_any(
627+
x,
628+
class = c("FeatureLayer", "Table", "ImageServer"),
629+
arg = arg,
630+
call = call
631+
)
626632
# verify that x is an layer
627633
# FIXME: This check makes arc_select error on ImageServer inputs
628634
check_inherits_any(

R/raster-fns.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#' List Available Raster Funcitons
2+
#'
3+
#' This function returns the `rasterFunctionInfos` field of the `ImageServer`'s metadata
4+
#' as a `data.frame`. If the field does not exist then an error is emitted.
5+
#'
6+
#' @inheritParams arcgisutils::infer_esri_type
7+
#' @param x an `ImageServer`.
8+
#' @returns a data.frame of the available raster functions.
9+
#' @examples
10+
#' # example code
11+
#'
12+
list_service_raster_fns <- function(x, arg = rlang::caller_arg(x), call = rlang::caller_call()) {
13+
check_inherits_any(x, "ImageServer")
14+
15+
if (!x$allowRasterFunction) {
16+
cli::cli_abort("{.arg arg} does not support raster functions")
17+
}
18+
data_frame(x$rasterFunctionInfos)
19+
}

man/arc_raster.Rd

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/list_service_raster_fns.Rd

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-raster-fns.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
test_that("use raster functions", {
2+
furl <- "https://di-usfsdata.img.arcgis.com/arcgis/rest/services/FIA_BIGMAP_2018_Species_Aboveground_Biomass/ImageServer"
3+
4+
x <- arc_open(furl)
5+
expect_no_error({
6+
suppressWarnings({
7+
balsams <- arc_raster(
8+
x,
9+
xmin = -71,
10+
xmax = -67,
11+
ymin = 43,
12+
ymax = 47.5,
13+
bbox_crs = 4326,
14+
width = 100,
15+
height = 100,
16+
raster_fn = "SPCD_0012_Abies_balsamea"
17+
)
18+
})
19+
})
20+
})
21+
22+
test_that("list service raster functions", {
23+
furl <- "https://di-usfsdata.img.arcgis.com/arcgis/rest/services/FIA_BIGMAP_2018_Species_Aboveground_Biomass/ImageServer"
24+
25+
x <- arc_open(furl)
26+
raster_fns <- list_service_raster_fns(x)
27+
expect_identical(names(raster_fns), names(raster_fns))
28+
expect_s3_class(raster_fns, "data.frame")
29+
expect_s3_class(raster_fns, "tbl")
30+
})

0 commit comments

Comments
 (0)