View Domain Descriptions with notebooks / workforce / api for python

727
4
Jump to solution
05-04-2023 04:05 AM
RobynSnookCCB
New Contributor III

I am attempting to automat adding assignments to our workforce projects. I've got that done but where using field maps with a separate feature layer and I want to push that data into the description field of the workforce layer. However, some fields have coded domains. I am new to api for python and am unable to find a way to view domain descriptions vs the coded value. Thanks

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Once you have a domains dict, you can use the field name and attribute value to swap in the appropriate domain descriptions.

domains['fieldname'][feature['attributes']['fieldname']

 

jcarlson_0-1683208905240.png

Or if you use a dataframe, you can replace all the domains with their descriptions in one go:

jcarlson_1-1683209297861.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

With a feature service, you can use the properties to pull out domains for each field.

Once you have your service, you can use this one-liner to cobble together a dict of the layer's domains:

service = gis.content.get('some itemid').layers[0]

domains = {i['name']:{str(j['code']):j['name'] for (j) in i['domain']['codedValues']} for (i) in service.properties["fields"] if 'domain' in i and i['domain'] is not None}

 

jcarlson_0-1683207701427.png

 

- Josh Carlson
Kendall County GIS
0 Kudos
RobynSnookCCB
New Contributor III

Thanks, I found this in another forum, and it works for viewing the domains in notebooks. I'm looking to push the domain description value as text into a workforce field (description). Like how the attributes function works in a for loop below. But I just get the coded value back and can't find a way to get the domain description. 

feature.attributes['WV_Operation'] outputs a 0 or 1 and not 'Yes' or 'No'

I'm pretty new to this so perhaps I'm just missing how to do this?

service = gis.content.get('some itemid').layers[0]
assignments = []
for feature in service:
          
    assignments.append(
        workforce.Assignment(
            project,
            geometry=feature.geometry,
            location="Hydrant #" + feature.attributes['HydNum'] + " on " + feature.attributes["LocDesc"],
            description=feature.attributes["WV_Operation"],
            priority=1,
            work_order_id=feature.attributes["GlobalID"],
            assignment_type="Auxiliary Valve Issues",
            status="assigned",
            worker=Robyn
        )
    )
project.assignments.batch_add(assignments)

 

0 Kudos
jcarlson
MVP Esteemed Contributor

Once you have a domains dict, you can use the field name and attribute value to swap in the appropriate domain descriptions.

domains['fieldname'][feature['attributes']['fieldname']

 

jcarlson_0-1683208905240.png

Or if you use a dataframe, you can replace all the domains with their descriptions in one go:

jcarlson_1-1683209297861.png

 

- Josh Carlson
Kendall County GIS
0 Kudos
RobynSnookCCB
New Contributor III

Great thank you, it has worked! For anyone else I did need to tweak the exact code to: 

domains['Ownership'][feature.attributes['Ownership']]

0 Kudos