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

Add support for ArcGIS services using {arcgislayers} #110

Open
elipousson opened this issue Oct 29, 2024 · 5 comments
Open

Add support for ArcGIS services using {arcgislayers} #110

elipousson opened this issue Oct 29, 2024 · 5 comments
Labels
new package suggestion of a new spatial package to support

Comments

@elipousson
Copy link

I put together a couple of prototypes based on tarchetypes::tar_file_read() to support working with ArcGIS services using {arcgislayers}.

tar_arc_select() may be the better of the two since it could potentially use the service metadata (returned by arcgislayers::arc_open()) to determine when the target is stale and needs to be re-run. I'm new to developing targets functions so feedback on improvements are welcome!

tar_arc_select <- function(name,
                           url,
                           ...,
                           token = arcgisutils::arc_token(),
                           tidy_eval = targets::tar_option_get("tidy_eval"),
                           packages = targets::tar_option_get("packages"),
                           library = targets::tar_option_get("library"),
                           format = targets::tar_option_get("format"),
                           repository = targets::tar_option_get("repository"),
                           error = targets::tar_option_get("error"),
                           memory = targets::tar_option_get("memory"),
                           garbage_collection = targets::tar_option_get("garbage_collection"),
                           deployment = targets::tar_option_get("deployment"),
                           priority = targets::tar_option_get("priority"),
                           resources = targets::tar_option_get("resources"),
                           storage = targets::tar_option_get("storage"),
                           retrieval = targets::tar_option_get("retrieval"),
                           cue = targets::tar_option_get("cue")) {
  name <- targets::tar_deparse_language(substitute(name))

  envir <- targets::tar_option_get("envir")

  name_url <- paste0(name, "_url")

  name_layer <- paste0(name, "_layer")

  sym_url <- rlang::sym(name_url)

  url <- targets::tar_tidy_eval(
    expr = as.expression(substitute(url)),
    envir = targets::tar_option_get("envir"), tidy_eval = tidy_eval
  )

  layer <- targets::tar_tidy_eval(
    expr = as.expression(substitute(arcgislayers::arc_open(url = !!.x, token = token))),
    envir = list(.x = sym_url), tidy_eval = TRUE
  )

  select <- targets::tar_tidy_eval(
    expr = as.expression(substitute(arcgislayers::arc_select(x = !!.x, ..., token = token))),
    envir = list(.x = layer), tidy_eval = TRUE
  )

  list(
    targets::tar_target_raw(
      name = name_url,
      command = url,
      packages = packages, library = library, format = format,
      repository = repository, error = error, memory = memory,
      garbage_collection = garbage_collection, deployment = deployment,
      priority = priority, storage = storage, retrieval = retrieval,
      cue = cue
    ),
    targets::tar_target_raw(
      name = name_layer,
      command = layer,
      packages = packages, library = library, format = format,
      repository = repository, error = error, memory = memory,
      garbage_collection = garbage_collection, deployment = deployment,
      priority = priority, storage = storage, retrieval = retrieval,
      cue = cue
    ),
    targets::tar_target_raw(
      name = name,
      command = select,
      packages = packages, library = library,
      format = format, repository = repository, error = error,
      memory = memory, garbage_collection = garbage_collection,
      deployment = deployment, priority = priority, resources = resources,
      storage = storage, retrieval = retrieval, cue = cue
    )
  )
}

tar_arc_read <- function(name,
                         url,
                         ...,
                         token = arcgisutils::arc_token(),
                         tidy_eval = targets::tar_option_get("tidy_eval"),
                         packages = targets::tar_option_get("packages"),
                         library = targets::tar_option_get("library"),
                         format = targets::tar_option_get("format"),
                         repository = targets::tar_option_get("repository"),
                         error = targets::tar_option_get("error"),
                         memory = targets::tar_option_get("memory"),
                         garbage_collection = targets::tar_option_get("garbage_collection"),
                         deployment = targets::tar_option_get("deployment"),
                         priority = targets::tar_option_get("priority"),
                         resources = targets::tar_option_get("resources"),
                         storage = targets::tar_option_get("storage"),
                         retrieval = targets::tar_option_get("retrieval"),
                         cue = targets::tar_option_get("cue")) {
  name <- targets::tar_deparse_language(substitute(name))

  envir <- targets::tar_option_get("envir")

  name_url <- paste0(name, "_url")

  sym_url <- rlang::sym(name_url)

  url <- targets::tar_tidy_eval(
    expr = as.expression(substitute(url)),
    envir = targets::tar_option_get("envir"), tidy_eval = tidy_eval
  )

  res <- targets::tar_tidy_eval(
    expr = as.expression(substitute(arcgislayers::arc_read(url = !!.x, ..., token = token))),
    envir = list(.x = sym_url), tidy_eval = TRUE
  )

  list(
    targets::tar_target_raw(
      name = name_url,
      command = url,
      packages = packages, library = library, format = format,
      repository = repository, error = error, memory = memory,
      garbage_collection = garbage_collection, deployment = deployment,
      priority = priority, storage = storage, retrieval = retrieval,
      cue = cue
    ),
    targets::tar_target_raw(
      name = name,
      command = res,
      packages = packages, library = library,
      format = format, repository = repository, error = error,
      memory = memory, garbage_collection = garbage_collection,
      deployment = deployment, priority = priority, resources = resources,
      storage = storage, retrieval = retrieval, cue = cue
    )
  )
}
@Aariq
Copy link
Collaborator

Aariq commented Oct 29, 2024

This is a great idea and I think geotargets would be a nice place for this target factory. However, I don't currently have ArcGIS installed, so I suspect that's going to make it difficult to test and explore. I think I have access through my institution, so will definitely keep this in mind for the future.

@Aariq Aariq added the new package suggestion of a new spatial package to support label Oct 29, 2024
@elipousson
Copy link
Author

Great. Let me know if you have a chance to try it out and I'll share more here if I have more ideas for the implementation.

Since you mentioned needing to have ArcGIS installed, I wanted to clarify that you may be thinking of the arcgisbinding package that requires a local ArcGIS Pro installation. The arcgislayers package allows access to the ArcGIS REST API for both free and paid web services (mostly FeatureServers but also ImageServers, Tables, etc.).

@Aariq
Copy link
Collaborator

Aariq commented Oct 30, 2024

I was thinking of the arcgisbinding package. Thanks for the clarification!

@njtierney
Copy link
Owner

Just wanted to echo what @Aariq said - geotargets would be a nice place for this.

@elipousson do you happen to have any examples on hand to demonstrate the use of this target factory that don't require ArcGIS to be installed?

@elipousson
Copy link
Author

I can put together a standalone repo with a reprex later this week but I'm using {arcgislayers} quite a bit in this basemap plotting project I put together earlier this year. The helper function doesn't take advantage of targets in the way that tar_arc_read() does but you can still see it here: https://github.com/elipousson/tigris-basemap/blob/main/R/functions.R#L36-L74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new package suggestion of a new spatial package to support
Projects
None yet
Development

No branches or pull requests

3 participants