Copying Domains between layers

405
3
03-17-2021 12:20 PM
Labels (2)
AlfredBaldenweck
Occasional Contributor III

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
3 Replies
jcarlson
MVP Notable 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
Occasional Contributor III

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 Notable 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
0 Kudos