Friday, 23 August 2019

arcgis desktop - Export to a Shapefile and maintain Domain Coded Values


I maintain a complex parcel fabric that uses coded value domains for a few fields. These are short integer fields with values from 1 - 6. These values represent say 1:Integrated Registered Survey Plan, 2:Survey Plan, 3:Geo-Referenced Air Photography, 4:Government Provided, 5:Expert Knowledge, 6:Unknown.


So I have several data requests and they need to be provided in Shapefile's. When I export my parcel fabric the fields obviously make no sense since they are numeric values representing accuracy codes from domains.


Is there any way to convert this field from a coded numeric field to its domain value? I know I can do this with some python programming, but I wonder if ESRI already has a solution for this.


Also I am managing the Parcel Fabric in a File Geodatabase.




Answer



You could do it with Python, but if you are unfamiliar with Arcpy, it's a simple field calculation. Add a new text field and use this in field calculator.


CODE BLOCK:


def domain(field):
if field == 1: return "Integrated Registered Survey Plan"
elif field == 2: return "Survey Plan"
elif field == 3: return "Geo-Referenced Air Photography"
elif field == 4: return "Government Provided"
elif field == 5: return "Expert Knowledge"
else: return "Unknown"


EXPRESSION:


domain()

In hindsight, it's actually easier to use field calculator than messing with dictionaries and cursors. If you had a large amount of domain values to code, then it would be worthwhile to dump the data in a .txt file and create your dictionary from that.


I only tested this on a shapefile, but it should also work on a GDB. This script takes a feature layer, list of fields, and text file as input and creates new fields to populate their domain values with.


import arcpy

shpin = arcpy.GetParameterAsText(0) #Input shapefile.
#String of field(s) to loop over, split into list.

fields = arcpy.GetParameterAsText(1).split(";")
#The text file containing comma separated find/replace values.
textfile = arcpy.GetParameterAsText(2)

values = [str(row[i]) for row in arcpy.da.SearchCursor(shpin, fields)
for i,field in enumerate(fields)]
repfind = dict([line.rstrip().split(",") for line in open(textfile, "r")])

replaced = [repfind.get(x,x) for x in values] #Use .get method to mimic find/replace.
rep_list = zip(*[iter(replaced)]*len(fields)) #Convert to list of lists.


#Add new fields, with name based on old field name with appended underscore.
[arcpy.AddField_management(shpin, "{0}_".format(field), "Text") for field in fields]
newfields = ["{0}_".format(field) for field in fields]

with arcpy.da.UpdateCursor(shpin, newfields) as rowout:
for x, row in enumerate(rowout):
for y, field in enumerate(fields):
row[y] = rep_list[x][y]
rowout.updateRow(row)

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...