Skip to content

Commit a2fbd49

Browse files
committed
update attachments function
1 parent e895c1e commit a2fbd49

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

R/attachments.R

+40-4
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,16 @@ query_layer_attachments <- function(
7575

7676
req <- httr2::req_body_form(
7777
b_req,
78-
objectIds = object_ids,
79-
globalIds = global_ids,
78+
# TODO test these why aren't these working?!!
79+
objectIds = paste(object_ids, collapse = ","),
80+
# TODO create a test for this
81+
globalIds = paste(global_ids, collapse = ","),
8082
attachmentTypes = attachment_types,
83+
# TODO document common examples:
84+
# filter based on
85+
# - date
86+
# - common prefix
87+
# - numeric value
8188
definitionExpression = definition_expression,
8289
keywords = keywords,
8390
returnUrl = TRUE,
@@ -96,6 +103,11 @@ query_layer_attachments <- function(
96103
#' @param x the attachmentGroups column from the query results
97104
#' @noRd
98105
unnest_attachment_groups <- function(x) {
106+
if (is.null(x[["attachmentInfos"]])) {
107+
cli::cli_alert_info("No attachments found.")
108+
return(NULL)
109+
}
110+
99111
n_elem <- vapply(x[["attachmentInfos"]], nrow, integer(1))
100112
res <- cbind(
101113
x[rep.int(1:nrow(x), n_elem), c("parentGlobalId", "parentObjectId")],
@@ -128,6 +140,7 @@ download_attachments <- function(
128140
attachments,
129141
out_dir,
130142
...,
143+
overwrite = FALSE,
131144
.progress = TRUE,
132145
token = arc_token()) {
133146
# check that the input is a data frame with the appropriate types
@@ -195,9 +208,32 @@ download_attachments <- function(
195208

196209
# create the output path names
197210
out_fps <- file.path(out_dir, attachments[["name"]])
211+
already_exist <- file.exists(out_fps)
212+
213+
# required fields
214+
urls <- attachments[["url"]]
215+
content_types <- attachments[["contentType"]]
216+
217+
if (!overwrite && any(already_exist)) {
218+
# Files with the same name found.
219+
cli::cli_inform(
220+
c(
221+
"Files with the same name found in {.path {out_dir}}",
222+
"i" = "Existing files: {.file {out_fps[already_exist]}}",
223+
">" = "set {.arg overwrite = TRUE} to overwrite these files"
224+
)
225+
)
226+
# subset to the files that dont already exist
227+
out_fps <- out_fps[!already_exist]
228+
urls <- urls[!already_exist]
229+
content_types <- content_types[!already_exist]
230+
}
231+
# TODO check if files exist, if they do provide a message
232+
# saying we're skipping downloading.
233+
# To download use `overwrite = TRUE`
198234

199235
# create the requests
200-
attachment_reqs <- lapply(attachments[["url"]], arc_base_req, token = token)
236+
attachment_reqs <- lapply(urls, arc_base_req, token = token)
201237

202238
# perform the requests
203239
resps <- httr2::req_perform_parallel(
@@ -207,7 +243,7 @@ download_attachments <- function(
207243
)
208244

209245

210-
Map(.download_attachment, resps, attachments[["contentType"]], out_fps)
246+
Map(.download_attachment, resps, content_types, out_fps)
211247
}
212248

213249
.download_attachment <- function(.resp, .content_type, .fp) {

dev/attachments-demo.R

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# install the development version
2+
# install.packages("pak")
3+
# pak::pak("r-arcgis/arcgislayers")
4+
5+
# for authorizing to your portal
6+
library(arcgisutils)
7+
8+
# for accessing feature service content
9+
library(arcgislayers)
10+
11+
# authorize
12+
set_arc_token(auth_user())
13+
14+
# URL to the Survey123 feature service with attachments
15+
furl <- "https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/v8_Wide_Area_Search_Form_Feature_Layer___a2fe9c/FeatureServer/0"
16+
17+
# create a reference to it in R
18+
layer <- arc_open(furl)
19+
20+
# There are two parts to working with attachments:
21+
# 1. Finding the attachments associated with a feature service
22+
# 2. After finding the attachments, we can download them to our machine
23+
24+
# Query the attachments of a layer using query_layer_attachments()
25+
# By default this returns metadata about every attachment in your
26+
# feature service
27+
att <- query_layer_attachments(layer)
28+
att
29+
30+
# We can limit this by providing a SQL where clause to the
31+
# definition_expression argument.
32+
# For example to find all of the attachments that have the
33+
# followup_status field set to `needs_followup` we can provide the
34+
# following definition_expression
35+
query_layer_attachments(
36+
layer, "followup_status = 'needs_followup'"
37+
)
38+
39+
# To find all of the attachmetns from after a point in time
40+
query_layer_attachments(
41+
layer, "start_time >= '2023-01-01'"
42+
)
43+
44+
# to download the attachments:
45+
res <- download_attachments(
46+
att,
47+
"dev/field_images"
48+
)

0 commit comments

Comments
 (0)