- Notifications
You must be signed in to change notification settings - Fork9
Description
I'm trying to useencode_field_values on my feature layer to replace coded values with the domain labels.
When I use
url <- "https://services1.arcgis.com/myurl..."feature_layer_arc <- arc_open(url)feature_layer_data <- arc_select(feature_layer_arc)feature_layer_labelled <- encode_field_values(feature_layer_data, feature_layer_arc, field = NULL)I get an error,
Error in .data[[col]][!miss_val] <- col_replace : replacement has length zeroHowever, if I examine the data, and pick out the four fields that have domains and use them in the fields argument, no error is obtained:
feature_layer_labelled <- encode_field_values(feature_layer_data, feature_layer_arc, field = c("Bander", "Writer", "Cloud_cover", "Precipitation"))I've attached a text file that can copied and pasted into R to reproduce. I tried to take out my personal information especially from the Bander and Writer fields. If you copy and paste the text in the file into R, you'll get the feature_layer_arc object and the feature_layer_data object.
github_arcgislayers_encode_field_names.txt
Interestingly, before I found the 'encode_field_values' function, I had ChatGPT help me write a function to replace values with their domain labels. Of course, I'd prefer to use yours!!... but this works to pull out my values:
library(tidyverse)# Step 1: Get field metadatafields_info <- list_fields(feature_layer_arc)# Step 2: Identify codedValue domainscoded_fields <- fields_info %>% filter(map_lgl(domain, ~ !is.null(.x) && .x$type == "codedValue")) %>% mutate(field_name = name) # Fix: use 'name' not 'field_name'# Step 3: Create lookup tables for each coded domain fielddomain_lookups <- map(coded_fields$field_name, function(field) { domain <- fields_info %>% filter(name == field) %>% pull(domain) %>% .[[1]] lookup <- domain$codedValues tibble(!!field := lookup$code, !!paste0(field, "_label") := lookup$name)}) %>% set_names(coded_fields$field_name)# Step 4: Get your AGOL feature layer datadata <- feature_layer_data# Step 5: Join domain labels into your datadata_labeled <- reduce(names(domain_lookups), function(d, field) { left_join(d, domain_lookups[[field]], by = field)}, .init = data)# Optional Step 6: Replace code fields with labels (if desired)data_cleaned <- data_labeled %>% select(-all_of(names(domain_lookups))) %>% rename_with(~ str_remove(., "_label$"), ends_with("_label"))