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

Fix #191 and #188 #192

Merged
merged 4 commits into from
Jun 24, 2024
Merged
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Add `pull_field_aliases()` utility function
- `arc_select()` now uses `arcgisutils::rbind_results()` for faster row-binding if `{collapse}`, `{data.table}`, `{vctrs}` are installed (#175)
- Preserve order of `fields` column names for `arc_select()` (fixes minor bug with `arc_read` handling of `col_names`) (#185)
- Set CRS for a FeatureLayer or ImageServer using `"wkid"` or `"wkt"` value if `"latestWkid"` is missing. (#188)
- Fix issue with `arc_select()` when layer can't support pagination. (#191)

# arcgislayers 0.2.0

Expand Down
86 changes: 67 additions & 19 deletions R/arc-select.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ collect_layer <- function(
page_size = NULL,
...,
error_call = rlang::caller_env()) {

# 1. Make base request
# 2. Identify necessary query parameters
# 3. Figure out offsets and update query parameters
Expand Down Expand Up @@ -203,28 +202,22 @@ collect_layer <- function(
# Offsets -----------------------------------------------------------------

# count the number of features in a query
n_feats <- count_results(req, query_params, n_max = n_max, error_call = error_call)
n_feats <- count_results(
req = req,
query = query_params,
n_max = n_max,
error_call = error_call
)

# create a list of record counts based on number of features, page size and max records
record_offsets <- set_record_offsets(
all_resps <- get_query_resps(
x = x,
req = req,
n_feats = n_feats,
page_size = page_size,
max_records = x[["maxRecordCount"]],
query_params = query_params,
error_call = error_call
)

# create a list of requests from the offset and page sizes
all_requests <- mapply(
add_offset,
.offset = record_offsets[["offsets"]],
.page_size = record_offsets[["counts"]],
MoreArgs = list(.req = req, .params = query_params),
SIMPLIFY = FALSE
)

# make all requests and store responses in list
all_resps <- httr2::req_perform_parallel(all_requests, on_error = "continue")

# identify any errors
# TODO: determine how to handle errors
# has_error <- vapply(all_resps, function(x) inherits(x, "error"), logical(1))
Expand Down Expand Up @@ -264,6 +257,62 @@ collect_layer <- function(
res
}

#' Get query responses with handling for layers that don't support pagination
#' @noRd
get_query_resps <- function(
req,
x,
n_feats,
page_size = NULL,
query_params = list(),
error_call = rlang::caller_env()) {
# If pagination is not supported, we create one query and return the results
# in a list with a warning. This way the maximum number of results is returned
# but the user is also informed that they will not get tha maximum number of
# records. Otherwise, we continue and utilize the pagination
if (isFALSE(x[["advancedQueryCapabilities"]][["supportsPagination"]])) {
if (n_feats > x[["maxRecordCount"]]) {
cli::cli_warn(
c(
"{class(x)} {.val {x[['name']]}} does not support pagination and
complete results can't be returned.",
"i" = "{n_feats} features are selected by the query and the maximum
is {x[['maxRecordCount']]} records."
)
)
}

req <- httr2::req_body_form(
httr2::req_url_path_append(req, "query"),
!!!query_params
)

resp <- httr2::req_perform(req, error_call = error_call)

return(list(resp))
}

# create a list of record counts based on number of features, page size and max records
record_offsets <- set_record_offsets(
n_feats = n_feats,
page_size = page_size,
max_records = x[["maxRecordCount"]],
error_call = error_call
)

# create a list of requests from the offset and page sizes
all_requests <- mapply(
add_offset,
.offset = record_offsets[["offsets"]],
.page_size = record_offsets[["counts"]],
MoreArgs = list(.req = req, .params = query_params),
SIMPLIFY = FALSE
)

# make all requests and store responses in list
httr2::req_perform_parallel(all_requests, on_error = "continue")
}


# utility -----------------------------------------------------------------

Expand Down Expand Up @@ -425,8 +474,7 @@ count_results <- function(req, query, n_max = Inf, error_call = rlang::caller_en
validate_results_count <- function(
n_results = NULL,
n_max = Inf,
error_call = rlang::caller_env()
) {
error_call = rlang::caller_env()) {
if (is.null(n_results)) {
cli::cli_abort(
c(
Expand Down
30 changes: 27 additions & 3 deletions R/sf-methods.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@

# st_crs method for feature layer
st_crs.FeatureLayer <- function(obj, ...) {
sf::st_crs(obj[["extent"]][["spatialReference"]][["latestWkid"]])
arc_sr_as_crs(obj)
}

st_crs.ImageServer <- function(obj, ...) {
sf::st_crs(obj[["extent"]][["spatialReference"]][["latestWkid"]])
arc_sr_as_crs(obj)
}

#' Get a Coordinate Reference System from a FeatureLayer or ImageServer spatial
#' reference
#'
#' [arc_sr_as_crs()] converts a FeatureLayer or ImageServer into a coordinate
#' reference system (CRS) using [sf::st_crs()].
#'
#' @param x A FeatureLayer or ImageServer or another object with a named
#' `"spatialReference"` element that can be converted to a coordinate
#' reference system with [sf::st_crs()].
#' @seealso [arcgisutils::validate_crs()]
#' @returns An object of class crs of length 2.
#' @noRd
arc_sr_as_crs <- function(x) {
if (rlang::has_name(x, "extent")) {
spatialReference <- x[["extent"]][["spatialReference"]]
} else {
spatialReference <- x[["spatialReference"]] %||% list()
}

x_crs <- spatialReference[["latestWkid"]] %||%
spatialReference[["wkid"]] %||%
spatialReference[["wkt"]]

sf::st_crs(x_crs)
}

# Implement `st_transform()`
Loading