Can any one guide me on How to get attribute domain based on the subtype value using arcpy.

1545
4
Jump to solution
08-08-2017 02:10 AM
SurajJha1
New Contributor III

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

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

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

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

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 codeCodeDescription
830Municipal Government FacilityMunicipal Government Facility
820CampgroundCampground
820MuseumMuseum
830Municipal Government FacilityMunicipal Government Facility
820ParkPark
730School: High SchoolSchool: High School
730School: ElementarySchool: Elementary
730College / UniversityCollege / University
720Shopping Mall / ComplexShopping Mall / Complex
820CemeteryCemetery
820ParkPark
730SchoolSchool
820ParkPark
820ParkPark
810Railroad StationRailroad Station
820CemeteryCemetery

etc...

This example is not the best, since the coded domain codes correspond to the descriptions, but it should work. 

XanderBakker
Esri Esteemed Contributor

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

SurajJha1
New Contributor III

Thank you very much for the Answer, You support solved my problem.

0 Kudos
XanderBakker
Esri Esteemed Contributor

You're welcome, I'm glad it worked for you.

0 Kudos