2

I'm writing a script which takes, for its first two parameters, feature classes (or tables). For the third parameter, I want to provide the user with a field mapping option similar to what ArcMap uses for Append or Spatial Join, but I haven't been able to figure out how to add this functionality.

I found the data type "Field Mapping" in the tool properties, but I don't know how to get it to work. Help?

Edit: The original purpose of my tool is to copy the most recent inspection data from a related table into the parent table (the child table has multiple records for each record in the parent). I need the field mapping to help me know which fields from the child table to pull into equivalent fields in the parent table.

This is what I have:

# Based off code from: http://support.esri.com/technical-article/000011948
import arcpy

def updateParent(fc1, fc1_fields, fc2, fc2_fields):
    '''
    @param fc1 : feature class : The parent feature class, which we will update
    @param fc1_fields : list of field names : First name is date field, second is field to join, could modify code and add more fields to copy over
    @param fc2 : feature class or table : The child feature class, from which we will pull data
    @param fc2_fields : list of field names : See comments on second param, fields must be in the same order
    '''

    # We want to update values in our first feature class
    cursor = arcpy.da.UpdateCursor(fc1, fc1_fields)

    for row in cursor:
        # Create a new date list for each value in fc1
        date_list = []

        # Search all the values in fc2
        cursor2 = arcpy.da.SearchCursor(fc2, fc2_fields)
        for row2 in cursor2:
            # If the value matches the currently selected on in fc1, add date to list
            if row[1] == row2[1]:
                date_list.append(row2[0])
        # Get the latest (maximum) date
        print max(date_list)
        most_recent_date = max(date_list)

        # Update row in fc1 based on that value
        row[0] = most_recent_date
        cursor.updateRow(row)

    del row, cursor, row2, cursor2

# Define the workspace that will contain the two feature classes.
arcpy.env.workspace = r"C:\GIS_Temp\Temp.gdb"

#fc1 = arcpy.GetParameterAsText(0) # Parent feature class
#fc2 = arcpy.GetParameterAsText(1) # Child feature class / table

# This is where the problem is!!!!
# I don't know how to get a field map as an input and I don't know how to extract the information once I have it.
#fldmapping = arcpy.GetParameterAsText(2) 

# If I hardcode my script, I just give it lists with the fields in the correct order so that I know DATE = DATE and Name = PostName
updateParent("TestRelateFC", ["DATE","Name"], "RelateTestTable2", ["DATE","PostName"])

Right now I'm only copying over DATE, but I would also like to be able to pull over the data that goes with that inspection date (like condition, etc.) The fields in the parent and child may not have the same name, which is why I need field mapping.

I assume I need to use updateParameters() in tool validation, but I don't know how it works for field mapping.

  • 1
    What is your original purpose in developing this tool? – fatih_dur Dec 8 '16 at 23:53
  • For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. – PolyGeo Dec 9 '16 at 0:05
  • I'm not entirely sure what you're trying to achieve, but if you're wanting to update a specific field based on the parameters in another field, look in to modifying the code in the updateParameters() function in the validation tab of the script's properties. – nwduncan Dec 9 '16 at 2:55
  • I don't have a solution to your question, but I just want to share this very good ESRI documentation on field mapping: esri.com/news/arcuser/1012/files/fieldmapping.pdf – ianbroad Dec 14 '16 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.