Skip to content

Instantly share code, notes, and snippets.

@JosiahParry
Created July 3, 2024 18:28
Show Gist options
  • Save JosiahParry/cbbcf32d152e893570eb0fc4e6a9a4af to your computer and use it in GitHub Desktop.
Save JosiahParry/cbbcf32d152e893570eb0fc4e6a9a4af to your computer and use it in GitHub Desktop.
working with attachments in arcgislayers
# install the development version
# install.packages("pak")
# pak::pak("r-arcgis/arcgislayers")
library(pillar)

# for authorizing to your portal
library(arcgisutils)
#> 
#> Attaching package: 'arcgisutils'
#> The following object is masked from 'package:base':
#> 
#>     %||%
# for accessing feature service content
library(arcgislayers)

# authorize
set_arc_token(auth_user())

# URL to the Survey123 feature service with attachments
furl <- "https://services1.arcgis.com/hLJbHVT9ZrDIzK0I/arcgis/rest/services/v8_Wide_Area_Search_Form_Feature_Layer___a2fe9c/FeatureServer/0"

# create a reference to it in R
layer <- arc_open(furl)

# There are two parts to working with attachments:
# 1. Finding the attachments associated with a feature service
# 2. After finding the attachments, we can download them to our machine

# Query the attachments of a layer using query_layer_attachments()
# By default this returns metadata about every attachment in your
# feature service
att <- query_layer_attachments(layer)
att
#> # A data frame: 101 × 10
#>    parentGlobalId         parentObjectId    id globalId name  contentType   size
#>  * <chr>                           <int> <int> <chr>    <chr> <chr>        <int>
#>  1 51f34e57-003c-4cb5-b9…             13     2 1646cbf… b4ab… image/jpeg  353829
#>  2 ca558ac8-3377-43c0-bf…             14     3 71bbbee… 943c… image/jpeg  348549
#>  3 0f779c97-b83a-48d7-9c…             19     4 e9ba40a… 70ce… image/jpeg  368083
#>  4 91293e9d-c2fd-42c1-82…             21     5 20f702d… fa0e… image/jpeg  233991
#>  5 3b0f3d0e-564c-4b8f-bb…             22     6 a6d4f71… 3d1b… image/jpeg  206123
#>  6 d42dc23b-2cc6-475e-90…             27     7 ae11b34… 02d5… image/jpeg  401051
#>  7 73acf496-d80c-454e-b0…             34     8 5e47372… b994… image/jpeg  385913
#>  8 65face06-d4b0-47ec-b4…             35     9 70a150b… 38f8… image/jpeg  272721
#>  9 876b627c-4ae4-492f-8d…             36    10 a718d05… 96cf… image/jpeg  542690
#> 10 721dc5b7-5339-4eed-8b…             40    11 77c804d… 9d8b… image/jpeg  219489
#> # ℹ 91 more rows
#> # ℹ 3 more variables: keywords <chr>, url <chr>, exifInfo <lgl>
# We can limit this by providing a SQL where clause to the
# definition_expression argument.
# For example to find all of the attachments that have the
# followup_status field set to `needs_followup` we can provide the
# following definition_expression
query_layer_attachments(
  layer, "followup_status = 'needs_followup'"
)
#> # A data frame: 24 × 10
#>    parentGlobalId         parentObjectId    id globalId name  contentType   size
#>  * <chr>                           <int> <int> <chr>    <chr> <chr>        <int>
#>  1 498bb7fa-1b64-48b5-8c…             49    13 dd2afd9… imag… image/jpeg  159404
#>  2 0b45291f-ec76-4ee1-91…             53    15 146c89e… imag… image/jpeg  160810
#>  3 0b45291f-ec76-4ee1-91…             53    16 e929c75… imag… image/jpeg  184675
#>  4 0b45291f-ec76-4ee1-91…             53    17 54f1beb… imag… image/jpeg  168611
#>  5 edbe0c9f-f2a7-48b1-89…             69    23 482d4f6… imag… image/jpeg  247022
#>  6 e2d7ed7e-092a-4860-8d…             80    30 36fbdb2… imag… image/jpeg  202352
#>  7 5e059ee4-cbc7-4089-a8…             84    32 9e5cb7b… imag… image/jpeg  143612
#>  8 8823ae01-6799-4b74-ab…             86    33 2e807a4… imag… image/jpeg  186839
#>  9 612edf09-92cd-4e39-8b…             91    36 c8981b5… imag… image/jpeg  237814
#> 10 612edf09-92cd-4e39-8b…             91    37 1ff5060… imag… image/jpeg  183231
#> # ℹ 14 more rows
#> # ℹ 3 more variables: keywords <chr>, url <chr>, exifInfo <lgl>
# To find all of the attachments from after a point in time
query_layer_attachments(
  layer, "start_time >= '2023-01-01'"
)
#> # A data frame: 2 × 10
#>   parentGlobalId parentObjectId    id globalId name  contentType   size keywords
#> * <chr>                   <int> <int> <chr>    <chr> <chr>        <int> <chr>   
#> 1 4fd9351e-8137…            212   101 7f22182… e5d4… image/jpeg  249642 ""      
#> 2 90111945-ca59…            213   102 e658f84… 1cf9… image/jpeg  255001 ""      
#> # ℹ 2 more variables: url <chr>, exifInfo <lgl>
# To find attachments with a name that starts with `image0`
query_layer_attachments(
  layer,
  attachments_definition_expression = "att_name like 'image0%'"
)
#> # A data frame: 101 × 10
#>    parentGlobalId         parentObjectId    id globalId name  contentType   size
#>  * <chr>                           <int> <int> <chr>    <chr> <chr>        <int>
#>  1 51f34e57-003c-4cb5-b9…             13     2 1646cbf… b4ab… image/jpeg  353829
#>  2 ca558ac8-3377-43c0-bf…             14     3 71bbbee… 943c… image/jpeg  348549
#>  3 0f779c97-b83a-48d7-9c…             19     4 e9ba40a… 70ce… image/jpeg  368083
#>  4 91293e9d-c2fd-42c1-82…             21     5 20f702d… fa0e… image/jpeg  233991
#>  5 3b0f3d0e-564c-4b8f-bb…             22     6 a6d4f71… 3d1b… image/jpeg  206123
#>  6 d42dc23b-2cc6-475e-90…             27     7 ae11b34… 02d5… image/jpeg  401051
#>  7 73acf496-d80c-454e-b0…             34     8 5e47372… b994… image/jpeg  385913
#>  8 65face06-d4b0-47ec-b4…             35     9 70a150b… 38f8… image/jpeg  272721
#>  9 876b627c-4ae4-492f-8d…             36    10 a718d05… 96cf… image/jpeg  542690
#> 10 721dc5b7-5339-4eed-8b…             40    11 77c804d… 9d8b… image/jpeg  219489
#> # ℹ 91 more rows
#> # ℹ 3 more variables: keywords <chr>, url <chr>, exifInfo <lgl>
# Find attachments that contain "20221005"
query_layer_attachments(
  layer,
  attachments_definition_expression = "att_name like '%20221005%'"
)
#> # A data frame: 101 × 10
#>    parentGlobalId         parentObjectId    id globalId name  contentType   size
#>  * <chr>                           <int> <int> <chr>    <chr> <chr>        <int>
#>  1 51f34e57-003c-4cb5-b9…             13     2 1646cbf… b4ab… image/jpeg  353829
#>  2 ca558ac8-3377-43c0-bf…             14     3 71bbbee… 943c… image/jpeg  348549
#>  3 0f779c97-b83a-48d7-9c…             19     4 e9ba40a… 70ce… image/jpeg  368083
#>  4 91293e9d-c2fd-42c1-82…             21     5 20f702d… fa0e… image/jpeg  233991
#>  5 3b0f3d0e-564c-4b8f-bb…             22     6 a6d4f71… 3d1b… image/jpeg  206123
#>  6 d42dc23b-2cc6-475e-90…             27     7 ae11b34… 02d5… image/jpeg  401051
#>  7 73acf496-d80c-454e-b0…             34     8 5e47372… b994… image/jpeg  385913
#>  8 65face06-d4b0-47ec-b4…             35     9 70a150b… 38f8… image/jpeg  272721
#>  9 876b627c-4ae4-492f-8d…             36    10 a718d05… 96cf… image/jpeg  542690
#> 10 721dc5b7-5339-4eed-8b…             40    11 77c804d… 9d8b… image/jpeg  219489
#> # ℹ 91 more rows
#> # ℹ 3 more variables: keywords <chr>, url <chr>, exifInfo <lgl>
# to download the attachments:
# res <- download_attachments(
#   att,
#   "dev/field_images"
# )

Created on 2024-07-03 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment