Copying Domains between layers

1829
4
Jump to solution
03-17-2021 12:20 PM
AlfredBaldenweck
MVP Regular Contributor

Is there an easy way to copy attribute domains between features?

A lot of my data requires the same starting information (e.g. State, County), and it's a pain to have to manually type them for every single feature, especially when there are so many potential options.

Thanks!

 

0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

I didn't realize at the time (despite copious googling) that an easier way is to use the Domain tools in ArcPro and go from there.

Thank you for your response.

View solution in original post

4 Replies
jcarlson
MVP Esteemed Contributor

"Easy" depends on how comfortable you are with directly editing a service's JSON and using Python.

To do this, you'll need to access the managers submodule of the arcgis.features module, and specifically the FeatureLayerManager class. Here's what it might look like:

from arcgis import GIS

gis = GIS('portal-url', 'user', 'pw')

src_layer = gis.content.get('itemID').layers[0]
src_props = src_layer.manager.properties

 

The object returned by properties will be a dict akin to the JSON you see in the REST directory. If you look through it, you'll see a section that looks like this:

 

{
  ...
  "fields": [
    ...
    {
      "name": "the-field-you-want",
      ...
      "domain": {
        "type": "codedValue",
        "name": "booleanExample",
        "codedValues": [
          {
            "name": "Yes",
            "code": 1
          },
          {
            "name": "No",
            "code": 0
          }
        ]
      },
    ...
    ],
  ...
}

 

 So what we'll do is copy the contents of the domain key out. Then we can use another manager function, update_definition, to update the other layers. Here's the rest of the Python:

domain_dict = src_props['fields'][index-of-the-field-you-want]['domain']

dest_lyrs = [list, of, layers]

update_dict = {
    "fields": [
        "name": "destination-field-name",
        "domain": domain_dict
    ]
}

for lyr in dest_lyrs:
    lyr.manager.update_definition(update_dict)

 

Now, the field you're updating might change layer to layer, so perhaps this can't be done in iteration. That'll depend on your situation. You could probably roll this all up into a custom function, too.

- Josh Carlson
Kendall County GIS
0 Kudos
AlfredBaldenweck
MVP Regular Contributor

I didn't realize at the time (despite copious googling) that an easier way is to use the Domain tools in ArcPro and go from there.

Thank you for your response.

jcarlson
MVP Esteemed Contributor

Oh, nice! That's very good to know. I was stuck on finding an AGOL answer to an AGOL problem, but it's important to remember Pro for some of the more technical operations, even for hosted layers.

PS - You should elaborate a bit and accept your own answer, so that the next person searching for the same problem can find the solution that much quicker.

- Josh Carlson
Kendall County GIS
RobertAnderson3
MVP Regular Contributor

Glad you found an answer @AlfredBaldenweck and I agree with @jcarlson that elaborating more on your own answer and solution would be awesome, as I'm trying to figure out how to do this now myself haha

0 Kudos