HI, Can any one guide me on How to get attribute domain based on the subtype value using arcpy. In my feature class different domains have been defined on a field based on subtype please guide me how i can get the domain name on this field if i know the subtype code/description
Solved! Go to Solution.
Hi jha.surajkumar ,
If you like one-liners you could use this:
arcpy.da.ListSubtypes(fc)[code]['FieldValues'][fld_name][1].codedValues[dom_value]
Or something like this:
import arcpy
def main():
import os
ws = r'C:\Esri\Curso\Aguas\WaterUtilityNetworkEditing\localgovernment.gdb'
fc = r'C:\Esri\Curso\Aguas\WaterUtilityNetworkEditing\localgovernment.gdb\ReferenceData\FacilitySite'
fld_subtype = arcpy.Describe(fc).subtypeFieldName
fld_fcode = 'FCODE'
description = GetDescriptionForSubTypeCodeAndCodedValueShort(fc, 820, fld_fcode, "Museum")
print "description:", description
def GetDescriptionForSubTypeCodeAndCodedValueShort(fc, code, fld_name, dom_value):
# fc = featureclass that contains the subtypes
# code = the code of the subtype in the subtype field
# fld_name = the field name that contains the values
# dom_value = the value to search in the code value domain
try:
return arcpy.da.ListSubtypes(fc)[code]['FieldValues'][fld_name][1].codedValues[dom_value]
except:
return None
Just to show you how "deep" the coded domains are stored inside the subtype object
Have a look at this example of a subtype (field SUBTYPE) and multiple domains for a field called FCODE:
and...
Have a look at the following code:
import arcpy
def main():
import os
ws = r'C:\Esri\Curso\Aguas\WaterUtilityNetworkEditing\localgovernment.gdb'
fc = r'C:\Esri\Curso\Aguas\WaterUtilityNetworkEditing\localgovernment.gdb\ReferenceData\FacilitySite'
fld_subtype = arcpy.Describe(fc).subtypeFieldName
fld_fcode = 'FCODE'
flds = (fld_subtype, fld_fcode)
print "Subtype code\tCode\tDescription"
with arcpy.da.SearchCursor(fc, flds) as curs:
for row in curs:
subtype_code = row[0]
value = row[1]
description = GetDescriptionForSubTypeCodeAndCodedValue(fc, subtype_code, fld_fcode, value)
print "\t".join([str(subtype_code), str(value), description])
def GetDescriptionForSubTypeCodeAndCodedValue(fc, code, fld_name, dom_value):
description = None
dct_subtypes = arcpy.da.ListSubtypes(fc)
if code in dct_subtypes:
dct_subtype = dct_subtypes[code]
dct_fieldvalues = dct_subtype['FieldValues']
if fld_name in dct_fieldvalues:
subtype_info = dct_fieldvalues[fld_name]
domain_name = subtype_info[0]
domain = subtype_info[1]
if domain.domainType == 'CodedValue':
coded_values = domain.codedValues
if dom_value in coded_values:
description = coded_values[dom_value]
return description
if __name__ == '__main__':
main()
This yields the following list:
Subtype code | Code | Description |
830 | Municipal Government Facility | Municipal Government Facility |
820 | Campground | Campground |
820 | Museum | Museum |
830 | Municipal Government Facility | Municipal Government Facility |
820 | Park | Park |
730 | School: High School | School: High School |
730 | School: Elementary | School: Elementary |
730 | College / University | College / University |
720 | Shopping Mall / Complex | Shopping Mall / Complex |
820 | Cemetery | Cemetery |
820 | Park | Park |
730 | School | School |
820 | Park | Park |
820 | Park | Park |
810 | Railroad Station | Railroad Station |
820 | Cemetery | Cemetery |
etc...
This example is not the best, since the coded domain codes correspond to the descriptions, but it should work.
Hi jha.surajkumar ,
If you like one-liners you could use this:
arcpy.da.ListSubtypes(fc)[code]['FieldValues'][fld_name][1].codedValues[dom_value]
Or something like this:
import arcpy
def main():
import os
ws = r'C:\Esri\Curso\Aguas\WaterUtilityNetworkEditing\localgovernment.gdb'
fc = r'C:\Esri\Curso\Aguas\WaterUtilityNetworkEditing\localgovernment.gdb\ReferenceData\FacilitySite'
fld_subtype = arcpy.Describe(fc).subtypeFieldName
fld_fcode = 'FCODE'
description = GetDescriptionForSubTypeCodeAndCodedValueShort(fc, 820, fld_fcode, "Museum")
print "description:", description
def GetDescriptionForSubTypeCodeAndCodedValueShort(fc, code, fld_name, dom_value):
# fc = featureclass that contains the subtypes
# code = the code of the subtype in the subtype field
# fld_name = the field name that contains the values
# dom_value = the value to search in the code value domain
try:
return arcpy.da.ListSubtypes(fc)[code]['FieldValues'][fld_name][1].codedValues[dom_value]
except:
return None
Just to show you how "deep" the coded domains are stored inside the subtype object
Thank you very much for the Answer, You support solved my problem.
You're welcome, I'm glad it worked for you.