diff --git a/NAMESPACE b/NAMESPACE index f735f55..899625d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/NEWS.md b/NEWS.md index 6fb9e04..a31e8ab 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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` - `arc_open()` with a layer that does not support `Query` sets the `n` attribute to`NA` diff --git a/R/get-estimates.R b/R/get-estimates.R new file mode 100644 index 0000000..fcd5c0e --- /dev/null +++ b/R/get-estimates.R @@ -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 +} + + + + diff --git a/man/get_layer_estimates.Rd b/man/get_layer_estimates.Rd new file mode 100644 index 0000000..afb5aa0 --- /dev/null +++ b/man/get_layer_estimates.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get-estimates.R +\name{get_layer_estimates} +\alias{get_layer_estimates} +\title{Get Estimates} +\usage{ +get_layer_estimates(x, token = Sys.getenv("ARCGIS_TOKEN")) +} +\arguments{ +\item{x}{A \code{FeatureLayer} or \code{Table} class object created with \code{\link[=arc_open]{arc_open()}}.} + +\item{token}{your authorization token. By default checks the environment +variable \code{ARCGIS_TOKEN}. Set your token using \code{arcgisutils::set_auth_token()}.} +} +\value{ +A named list containing all estimate info. If \code{extent} is present, +it is available as an object of class \code{bbox}. +} +\description{ +Get Estimates +} +\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) +} +\references{ +\href{https://developers.arcgis.com/rest/services-reference/enterprise/get-estimates-feature-service-layer-.htm}{ArcGIS REST Doc} +}