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
Solved! Go to Solution.
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']
Or if you use a dataframe, you can replace all the domains with their descriptions in one go:
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}
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)
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']
Or if you use a dataframe, you can replace all the domains with their descriptions in one go:
Great thank you, it has worked! For anyone else I did need to tweak the exact code to:
domains['Ownership'][feature.attributes['Ownership']]