I have a collection of about 200 tables, and I would like to make sure that all the fields are text before merging them into a single table using python. There is at least one field ("Number_of") which I know for sure is integer. All the tables have the exact same schema.
This Geonet thread has been really helpful, but I still can't get it.
Here is my code:
fieldList = ['Feature','Number_of','Water_Type','GlobalID']
fieldMappings = arcpy.FieldMappings()
for field in fieldList:
vars()[field] = arcpy.FieldMap()
outField = vars()[field].outputField
vars()[field].type = "Text"
vars()[field].outputField = outField
fieldMappings.addFieldMap([field])
This current code returns an error saying, "NameError: The attribute 'outputField' is not supported on this instance of FieldMap."
Essentially, all I need to do is create a Field Map that I can use in the python Merge tool which will convert the "Number_of" field to text.
Answer
Well this script might be a bit convoluted but it worked for me. You may need to tweak with the fldConvTypes
variable - I'm not sure if it works for Date and/or Guid field types.
Essentially it figures out all the fields between all the feature classes, and throws them in a list. It then iterates through all the fields in the feature classes once again. If the field map hasn't been added to the field mappings yet, it will add it. It then checks if the field type is a type in the fldConvTypes
, and changes it to text if it is.
You can use this script to merge as many feature classes as you like. Just update your fcs
variable with each feature class.
#In feature class 1
inFc1 = r"C:\Users\e1b8\Desktop\E1B8\Workspace\Workspace.gdb\test"
#in feature class2
inFc2 = r"C:\Users\e1b8\Desktop\E1B8\Workspace\Workspace.gdb\tst2"
#out feature class
outFc = r"C:\Users\e1b8\Desktop\E1B8\Workspace\Workspace.gdb\test_merge20"
#list feature classes
fcs = [inFc1, inFc2]
import arcpy
#field types to convert
fldConvTypes = ["Date",
"Double",
"Guid",
"Integer",
"Single",
"SmallInteger"]
#field map dict
fldMapDi = {}
#add all fields to list
for fc in fcs:
#get shape field name
shpFld = arcpy.Describe (fc).shapeFieldName
#iterate field objects in feature class
for fldOb in arcpy.ListFields (fc):
#get field name
fld = fldOb.name
#skip shape field
if fld == shpFld:
continue
#check if field map has been created for field
if not fld in fldMapDi:
#create new field map
fm = arcpy.FieldMap ()
#add input field
fm.addInputField(fc, fld)
#check if field should be switched to text
if fldOb.editable and fldOb.type in fldConvTypes:
print "changing field", fld, "type to text"
#change field type to text
fldOb.type = "Text"
#update field map output field
fm.outputField = fldOb
#and field map to dict
fldMapDi [fld] = fm
else:
#get field map object from di
fm = fldMapDi [fld]
#add input field
fm.addInputField(fc, fld)
#change field map object in dict
fldMapDi [fld] = fm
##done creating field maps
#create empty field mappings
fms = arcpy.FieldMappings()
#iterate fields in dict
for fld in fldMapDi:
#get field map from dict
fm = fldMapDi [fld]
#add field map to field mappings
fms.addFieldMap (fm)
#merge
print "merging"
arcpy.Merge_management (fcs, outFc, fms)
print "created:", outFc
No comments:
Post a Comment