Handling Subtypes in Python

7070
4
Jump to solution
01-21-2015 06:39 PM
benberman
Occasional Contributor

Hi everyone,

 

I am looking to return the subtype domains associated with a subtype field. I was able to explore arcpy.da.ListSubtypes however this function seems to return a dictionary and I seem to be getting lost thereafter. Is there a quick way to isolate only the subtype domains from the dictionary?

 

Thanks!

1 Solution

Accepted Solutions
ChristianWells
Esri Regular Contributor

There is a good sample on the resource center that will list all the fields, their default value, and any domain assigned.

import arcpy

fc = <FeatureClass>
subtypes = arcpy.da.ListSubtypes(fc)

for stcode, stdict in subtypes.iteritems():
    print('Code: {0}'.format(stcode))
    for stkey in stdict.iterkeys():
        if stkey == 'FieldValues':
            print('Fields:')
            fields = stdict[stkey]
            for field, fieldvals in fields.iteritems():
                print(' --Field name: {0}'.format(field))
                print(' --Field default value: {0}'.format(fieldvals[0]))
                if not fieldvals[1] is None:
                    print(' --Domain name: {0}'.format(fieldvals[1].name))
        else:
            print('{0}: {1}'.format(stkey, stdict[stkey]))

However, if you are only interested in a list of all the domains used within the subtype, we can just list the domains using a similar code:

import arcpy

fc = <FeatureClass>
subtypes = arcpy.da.ListSubtypes(fc)

for stcode, stdict in subtypes.iteritems():
    print('Code: {0}'.format(stcode))
    for stkey in stdict.iterkeys():
        if stkey == 'FieldValues':
            print('Fields with Domains:')
            fields = stdict[stkey]
            for field, fieldvals in fields.iteritems():
                if not fieldvals[1] is None:
                    print(' --Field name: {0}'.format(field))
                    print(' --Domain name: {0}\n'.format(fieldvals[1].name))
        else:
            print('{0}: {1}'.format(stkey, stdict[stkey]))

Output:

Code: 1
Default: True
Name: Metal Pipe
SubtypeField: SubTypeCD
Fields:
 --Field name: InletWallThickness
 --Domain name: fcNumericWallThickness


 --Field name: Material
 --Domain name: fcMetalMaterial


 --Field name: PipeDistributor
 --Domain name: fcPipeDistributor

View solution in original post

4 Replies
ChristianWells
Esri Regular Contributor

There is a good sample on the resource center that will list all the fields, their default value, and any domain assigned.

import arcpy

fc = <FeatureClass>
subtypes = arcpy.da.ListSubtypes(fc)

for stcode, stdict in subtypes.iteritems():
    print('Code: {0}'.format(stcode))
    for stkey in stdict.iterkeys():
        if stkey == 'FieldValues':
            print('Fields:')
            fields = stdict[stkey]
            for field, fieldvals in fields.iteritems():
                print(' --Field name: {0}'.format(field))
                print(' --Field default value: {0}'.format(fieldvals[0]))
                if not fieldvals[1] is None:
                    print(' --Domain name: {0}'.format(fieldvals[1].name))
        else:
            print('{0}: {1}'.format(stkey, stdict[stkey]))

However, if you are only interested in a list of all the domains used within the subtype, we can just list the domains using a similar code:

import arcpy

fc = <FeatureClass>
subtypes = arcpy.da.ListSubtypes(fc)

for stcode, stdict in subtypes.iteritems():
    print('Code: {0}'.format(stcode))
    for stkey in stdict.iterkeys():
        if stkey == 'FieldValues':
            print('Fields with Domains:')
            fields = stdict[stkey]
            for field, fieldvals in fields.iteritems():
                if not fieldvals[1] is None:
                    print(' --Field name: {0}'.format(field))
                    print(' --Domain name: {0}\n'.format(fieldvals[1].name))
        else:
            print('{0}: {1}'.format(stkey, stdict[stkey]))

Output:

Code: 1
Default: True
Name: Metal Pipe
SubtypeField: SubTypeCD
Fields:
 --Field name: InletWallThickness
 --Domain name: fcNumericWallThickness


 --Field name: Material
 --Domain name: fcMetalMaterial


 --Field name: PipeDistributor
 --Domain name: fcPipeDistributor
benberman
Occasional Contributor

thank you!

0 Kudos
KhalidMahmood
New Contributor II

Hi, hoping that you are in good health. I need your support to review below code. Using ESRI ArcGIS Desktop version 10.4.1, a file Geodatabase having line feature class "Trench" with two tables "T_DUCT" and "T_SUB_DUCT".

Requirement: It is required to create sub type records of "T_DUCT" inside T_SUB_DUCT (as ROWS). Is there anything need to update in below code?

I have attached the code file.

Regards.

Khalid Mahmood

Riyadh, Saudi Arabia

kmc786pak@gmail.com

0 Kudos
gallgher55
New Contributor II

The logic of this code is still sound, users will just need to replace instances of .iteritems(), iterkeys(), with .items() and .keys() for ArcPro/Python3 processing.

0 Kudos