Copy Python Command, Append, and Field Mapping

447
1
11-29-2022 09:09 AM
NevinMcIntyreGIS
New Contributor

Hey all,


I'm writing a script to append some data. This append uses field map to reconcile field differences. In effort to save some time, I was using the "Copy Python Command" feature in ArcGIS Pro. After reading the copied code I had some questions about the syntax. Hopefully someone can shed some light on it, the code block is below.

arcpy.management.Append(gpwADDR, ofkyADDR, "NO_TEST", r'creationuser "Creation User" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,creationuser,0,50;datecreated "Date Created" true true false 8 Date 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,datecreated,-1,-1;lastuser "Last User" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,lastuser,0,50;datemodified "Date Modified" true true false 8 Date 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,datemodified,-1,-1;clli "CLLI" true true false 100 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,clli,0,100;owner "Owner" true true false 255 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,owner,0,255;addressid "Address ID" true true false 255 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,addressid,0,255;addressstate "Address State" true true false 50 Text 0 0,First,#;verified "Verified" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,verified,0,50;addresstext "Address Text" true true false 255 Text 0 0,First,#;addressstatus "Address Status" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,addressstatus,0,50;addresstype "Address Type" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,addresstype,0,50;addressclass "Address Class" true true false 50 Text 0 0,First,#;normalizedaddress "Normalized Address" true true false 255 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,normalizedaddress,0,255;streetaddress "Street Address" true true false 255 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,streetaddress,0,255;house_number "House Number" true true false 20 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,house_number,0,20;unitnumber "Unit Number" true true false 250 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,unitnumber,0,250;street_direction "Street Direction" true true false 20 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,street_direction,0,20;street_suffix "Street Suffix" true true false 20 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,street_suffix,0,20;city "City" true true false 150 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,city,0,150;state "State" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,state,0,50;zipcode "Zip Code" true true false 20 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,zipcode,0,20;terminationid "Termination ID" true true false 4 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,terminationid,0,4;strict_commit "Strict Commit" true true false 5 Text 0 0,First,#;overload "Overload" true true false 5 Text 0 0,First,#;map_ref "Map Reference" true true false 5 Text 0 0,First,#;promo_site "Promo Site" true true false 10 Text 0 0,First,#;fiberhood_override_id "Fiberhood Ovwerride ID" true true false 50 Text 0 0,First,#;landlord_id "Landlord ID" true true false 255 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,landlord_id,0,255;coord_sys "Coordinate System" true true false 100 Text 0 0,First,#;latitude "Latitude" true true false 8 Double 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,latitude,-1,-1;longitude "Longitude" true true false 8 Double 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,longitude,-1,-1;address_notes "Address Notes" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,address_notes,0,50;comments "Comments" true true false 255 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,comments,0,255;ipid "IPID" true true false 90 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,ipid,0,90;hyperlink "Hyperlink" true true false 255 Text 0 0,First,#;workorderid "Work Order ID" true true false 50 Text 0 0,First,#;threegisid "3-GIS ID" true true false 38 Guid 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,threegisid,-1,-1;globalid "GlobalID" false false true 38 GlobalID 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,globalid,-1,-1;id "ID" true true false 38 Guid 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,id,-1,-1;rotation "Symbol Rotation" true true false 8 Double 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,rotation,-1,-1;groupid "Group ID" true true false 90 Text 0 0,First,#;level_begin "level_begin" true true false 4 Long 0 0,First,#;DB_Source "DB Source" true true false 2 Short 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,DB_Source,-1,-1;source_database "Source Database" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,source_database,0,50;source_table "Source Table" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,source_table,0,50;source_id "Source ID" true true false 50 Text 0 0,First,#,C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address,source_id,0,100', '', '')

 I'm aware that the first two parameters are the input feature classes, and I've created variable for those and have replaced them with these variables. The next parameter is "NO_TEST" which is fine. The next parameters starting with r'.... is what I have a questions about:

  • This is the field mapping section in which a path is being referenced at each field: "C:\Users\nmcintyre\Documents\AccelecomProject\GPWData\gpw_2022114.gdb\TelecomDataset\address"
  • All of this code is surrounded by ' ' which makes it a string. Replacing the path with the above set variables doesn't seem like an option.

My question is: With the code the way as it is, will the script work on other machines without access to that path? Is this a viable option to appending with field mapping without using the Field Mapping python object? What is the purpose of that path being repeated?

 

0 Kudos
1 Reply
by Anonymous User
Not applicable

The tool output converts the fieldmapping object to its string value when you export it. I do not think that it will work on other pc's that do not have access to that path since fieldmappings are exclusive to the input and output. 

It is repeated because the field map object uses full paths from the input and output fields for each map since you can have multiple inputs coming in and it needs to know which dataset it's coming from.

You'll have to create a fieldmap in your code, which isn't that hard to automate. Using those strings, you can create a dictionary or list of tuples of the in/out fields and then manipulate the fieldmap object to map them.

For example:

fldMap = arcpy.FieldMappings()
fldMap.addTable(tmp_stops)

# you can list the fields of the desination fc using arcpy.ListFields()
keepfields = ['OID_', 'Sequence', 'USER_address', 'USER_address_apartment', 'USER_address_city', 'USER_address_common_name', 'USER_date_of_birth', 'USER_first_name', 'USER_last_name', 'USER_ori','USER_number', 'USER_status', 'USER_type']

# Create a old/ new fieldname tuple of fields, or manually do it: [('owner', 'Owner'), ...] to create the update fields
updateFlds = [(fld, fld.replace('USER_', '')) for fld in keepfields if 'USER_' in fld]
 
# iterate over the fields from the source fc to remove those that are not listed above
for f in arcpy.ListFields(tmp_stops):
    if f.name not in keepfields and not f.required:
        # Remove the old lowercase fieldmap
        fldMap.removeFieldMap(fldMap.findFieldMapIndex(f.name))

        # remap the fields listed in the updateFlds
        for fldPair in updateFlds:
            # get index of fieldmap
            indx = fldMap.findFieldMapIndex(fldPair[0])
            mapToReplace = fldMap.getFieldMap(indx)

            # set the fldMap name
            fldMap_name = mapToReplace.outputField
            # set to the new name
            fldMap_name.name = fldPair[1]
            fldMap_name.aliasName = fldPair[1]

            # Replace the names in the fieldmap object
            mapToReplace.outputField = fldMap_name

            fldMap.replaceFieldMap(indx, mapToReplace)

 

Then you can use the fldmap to Append.

0 Kudos