Monday, 9 October 2017

python - Transfer subtype Descriptions form long integer value into a string at new diffrent field


I need to convert entire geodatebase into shp files.One of the layers have 30 subtypes. I want and maintain the subtype data. Is there a way to Automatically populate the name of the subtype (Transfer subtype Descriptions)in to a new string field in order to not do it manually and not lussing the subtype data ?



Answer



I am not aware of any GP tool that would do that. However, I do this thing quite often by using a tiny procedure written in Python with ArcPy. I usually create a text field ("desc" in this case) in a feature class which has a subtype and then run UpdateCursor to write the description of the subtype code for each feature (the field used for subtype is "Typecode" in this case). After that, one can export a feature class to shapefile and this text field of course will be preserved as is.


This should work fine in 10.1 SP1 +.


import arcpy


fc = r"C:\Program Files (x86)\ArcGIS\ArcTutor\BuildingaGeodatabase\Montgomery.gdb\Water\Fittings"

# Create a dictionary of subtype codes and descriptions.
desc_lu = {key: value['Name'] for (key, value) in arcpy.da.ListSubtypes(fc).iteritems()}
with arcpy.da.UpdateCursor(fc, ["Typecode","desc"]) as cur:
for row in cur:
row[1] = (desc_lu[row[0]])
cur.updateRow(row)


More information on using arcpy.da.ListSubtypes.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...