Skip to content

Commit

Permalink
add get_layer_estimates()
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Dec 7, 2023
1 parent 636a644 commit 908ea26
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(create_feature_server)
export(delete_features)
export(get_all_layers)
export(get_layer)
export(get_layer_estimates)
export(get_layers)
export(list_fields)
export(list_items)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# arcgislayers 0.1.0 (unreleased)

- Add `get_layer_estimates()` to retrieve estimate info such as the number of features and the extent of the layer
- Add `truncate_layer()` to support truncate and append workflow
- Add support for opening `MapServers` <https://github.com/R-ArcGIS/arcgislayers/pull/83>
- `arc_open()` with a layer that does not support `Query` sets the `n` attribute to`NA` <https://github.com/R-ArcGIS/arcgislayers/pull/83>
Expand Down
69 changes: 69 additions & 0 deletions R/get-estimates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#' Get Estimates
#'
#' @inheritParams arc_select
#'
#' @references [ArcGIS REST Doc](https://developers.arcgis.com/rest/services-reference/enterprise/get-estimates-feature-service-layer-.htm)
#' @examples
#' furl <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Counties_Generalized_Boundaries/FeatureServer/0"
#'
#' county_fl <- arc_open(furl)
#' get_layer_estimates(county_fl)
#' @export
#' @returns
#' A named list containing all estimate info. If `extent` is present,
#' it is available as an object of class `bbox`.
get_layer_estimates <- function(x, token = Sys.getenv("ARCGIS_TOKEN")) {

# check if its a supported layer
obj_check_layer(x)

# check if `infoInEstimates` is null
if (is.null(x[["infoInEstimates"]])) {
cli::cli_abort(
"{.var {rlang::caller_arg(x)}} does not have estimates."
)
}

b_req <- httr2::request(x[["url"]])


est_req <- httr2::req_url_path_append(
b_req,
"getEstimates"
)

# token bug :[
if (token != "") {
resp <-
httr2::req_url_query(est_req, f = "json") |>
httr2::req_auth_bearer_token(token) |>
httr2::req_perform()
} else {
resp <-
httr2::req_url_query(est_req, f = "json") |>
httr2::req_perform()
}

# process json string
res_raw <- httr2::resp_body_string(resp) |>
RcppSimdJson::fparse()

# process extent if present
ext <- res_raw[["extent"]]

if (!is.null(ext)) {
crs <- sf::st_crs(ext[["spatialReference"]][[1]])
bbox <- sf::st_bbox(
unlist(ext[c("xmin", "ymin", "xmax", "ymax")]),
crs = crs
)
res_raw[["extent"]] <- bbox
}

# return the rest
res_raw
}




30 changes: 30 additions & 0 deletions man/get_layer_estimates.Rd

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

0 comments on commit 908ea26

Please sign in to comment.