I'm trying to create a tool that compares two data sets. I do not want the Feature Compare tool. Most of the field names in the two data sets are the same, but not all. I want a way to map one field onto another, so I thought a Field Mappings parameter would be best suited for associating fields together. My expectation was that it would populate similar to how setting the 'Obtained From' sub-parameter to a layer populates the fields list with all available fields from that layer, but that did not happen. I want functionality almost exactly like the Append tool. It populates the 'Field Map (optional)' window with a list of field maps where the user can add input fields.
I've tried working with the tool validator class, in the UpdateParameters method, I've added code to create a FieldMappings object and fill it with FieldMap objects created from the fields in another parameter, but I can't get the window on the tool itself to fill with the FieldMap objects. I've debugged to the point where I know for a fact the FieldMappings object is filled how I want it. I just can't get it to display.
Here is my code for the updateParameters method. param[0]
and param[1]
are feature layers. param[2]
is a field mappings parameter.
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if self.params[0].altered:
self.params[2].value = arcpy.FieldMappings()
for field in [a for a in arcpy.ListFields(self.params[0].value.dataSource) if not a.required]:
fm = arcpy.FieldMap()
fm.addInputField(self.params[0].value.dataSource,field.name)
self.params[2].value.addFieldMap(fm)
return
I have also tried this:
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if self.params[0].altered:
fms = arcpy.FieldMappings()
for field in [a for a in arcpy.ListFields(self.params[0].value.dataSource) if not a.required]:
fm = arcpy.FieldMap()
fm.addInputField(self.params[0].value.dataSource,field.name)
fms.addFieldMap(fm)
self.params[2].value = fms
return
I can't find a single post on creating a field mapping parameter in a script similar to the append or spatial join tool.
If the autopopulation is not possible, even getting the FieldMapping parameter to work properly would be an acceptable middle ground. Right now, the 'Add Input Field' never appears when right clicking a field map parameter. If I set the Field Mapping parameter to single value, clicking the window gives 2 options, Add Output Field and Reset. If I add an output field, right clicking that gives more options, Delete, Rename, Merge Rule, and Properties. None of which allow me to add an input field to the field map.
self.params[2].value = fms.exportToString()
). This takes a looooong time in a Python toolbox (minutes). Even then, it doesn't function properly (you can't add new fields and setup input fields to them).