I am learning to append and use fieldmap, flieldmappings to get my fields squared away.
basically I'm appending data into a "shell" featureclass that is empty. There's only a few fields I'm appending and trying to match.
I'm using this post as a reference:
Append Tool and Field Mapping ... Help? Examples?
I'm able to add ONE field change with this script:
(append and target layers are defined earlier in the script as well as importing the arpy library
<SPAN class="comment token"># field map and append</SPAN>
append_layer <SPAN class="operator token">=</SPAN> outputclip
target_layer <SPAN class="operator token">=</SPAN> out_name
<SPAN class="comment token">#This is like defining an empty grid of fields you see when you run it manually in the toolbox</SPAN>
fieldmappings <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldMappings<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Add the target datasets fields to the field map table (Like when you manually choose a tagert layer in the toolbox)</SPAN>
fieldmappings<SPAN class="punctuation token">.</SPAN>addTable<SPAN class="punctuation token">(</SPAN>target_layer<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Add the append datasets fields to the field map table (Like when you manually choose a tagert layer in the toolbox)</SPAN>
fieldmappings<SPAN class="punctuation token">.</SPAN>addTable<SPAN class="punctuation token">(</SPAN>append_layer<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#At this point, you have a grid like when you run it manually saved in your field mappings.</SPAN>
<SPAN class="comment token">#####Lets map a field that have different names!</SPAN>
<SPAN class="comment token">#Find which "Index" the field has as we cant refer to them by name when editing the data only index</SPAN>
field_to_map_index <SPAN class="operator token">=</SPAN> fieldmappings<SPAN class="punctuation token">.</SPAN>findFieldMapIndex<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"PropertyAddress"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#Field name that exists in the target layer but not append data source!</SPAN>
<SPAN class="comment token">#Grab "A copy" of the field map object for this particular field</SPAN>
field_to_map <SPAN class="operator token">=</SPAN> fieldmappings<SPAN class="punctuation token">.</SPAN>getFieldMap<SPAN class="punctuation token">(</SPAN>field_to_map_index<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Update its data source to add the input from the the append layer</SPAN>
field_to_map<SPAN class="punctuation token">.</SPAN>addInputField<SPAN class="punctuation token">(</SPAN>append_layer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Addy_Concat"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#####Lets update the master field map using this updated copy of a field</SPAN>
fieldmappings<SPAN class="punctuation token">.</SPAN>replaceFieldMap<SPAN class="punctuation token">(</SPAN>field_to_map_index<SPAN class="punctuation token">,</SPAN> field_to_map<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Create a list of append datasets and run the the tool</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Append_management<SPAN class="punctuation token">(</SPAN>append_layer<SPAN class="punctuation token">,</SPAN> target_layer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NO_TEST"</SPAN><SPAN class="punctuation token">,</SPAN> fieldmappings<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"1"</SPAN><SPAN class="punctuation token">)</SPAN>
So, this works for me. I end up appending the "Addy_Concat" field into the "PropertyAddress" field. Great.
But I have 3 other fields to do this with. So I used this:
<SPAN class="comment token"># field matching for append</SPAN>
<SPAN class="comment token"># Name the Append and Target Layer (target layer is named in the ArcGIS Pro Map)</SPAN>
append_layer <SPAN class="operator token">=</SPAN> outputclip
target_layer <SPAN class="operator token">=</SPAN> out_name
fieldmappings <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FieldMappings<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Like when you manually choose a layer in the toolbox and it adds the fields to grid</SPAN>
fieldmappings<SPAN class="punctuation token">.</SPAN>addTable<SPAN class="punctuation token">(</SPAN>target_layer<SPAN class="punctuation token">)</SPAN>
fieldmappings<SPAN class="punctuation token">.</SPAN>addTable<SPAN class="punctuation token">(</SPAN>append_layer<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Lets map fields that have different names!</SPAN>
list_of_fields_we_will_map <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token"># list_of_fields_we_will_map.append(( append layer field name, target layer field name)</SPAN>
list_of_fields_we_will_map<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Addy_Concat'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'PropertyAddress'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#list_of_fields_we_will_map.append(('CMUNITY', 'PropertyCity'))</SPAN>
<SPAN class="comment token">#list_of_fields_we_will_map.append(('ZipCode', 'PropertyZip'))</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>list_of_fields_we_will_map<SPAN class="punctuation token">,</SPAN> sep <SPAN class="operator token">=</SPAN> <SPAN class="string token">", "</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> field_map <SPAN class="keyword token">in</SPAN> list_of_fields_we_will_map<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Find the fields index by name.</SPAN>
field_to_map_index <SPAN class="operator token">=</SPAN> fieldmappings<SPAN class="punctuation token">.</SPAN>findFieldMapIndex<SPAN class="punctuation token">(</SPAN>field_map<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Grab "A copy" of the current field map object for this particular field</SPAN>
field_to_map <SPAN class="operator token">=</SPAN> fieldmappings<SPAN class="punctuation token">.</SPAN>getFieldMap<SPAN class="punctuation token">(</SPAN>field_to_map_index<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Update its data source to add the input from the the append layer</SPAN>
field_to_map<SPAN class="punctuation token">.</SPAN>addInputField<SPAN class="punctuation token">(</SPAN>append_layer<SPAN class="punctuation token">,</SPAN> field_map<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># We edited a copy, update our data grid object with it</SPAN>
fieldmappings<SPAN class="punctuation token">.</SPAN>replaceFieldMap<SPAN class="punctuation token">(</SPAN>field_to_map_index<SPAN class="punctuation token">,</SPAN> field_to_map<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Create a list of append datasets and run the the tool - the "1" at the end is for the subtype.</SPAN>
inData <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>append_layer<SPAN class="punctuation token">]</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>Append_management<SPAN class="punctuation token">(</SPAN>inData<SPAN class="punctuation token">,</SPAN> target_layer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NO_TEST"</SPAN><SPAN class="punctuation token">,</SPAN> fieldmappings<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"1"</SPAN><SPAN class="punctuation token">)</SPAN>
I keep on getting this error:
Traceback (most recent call last):
File "<string>", line 119, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\arcobjects\arcobjects.py", line 666, in addInputField
return convertArcObjectToPythonObject(self._arc_object.AddInputField(*gp_fixargs(args)))
RuntimeError: FieldMap: Error in adding input field to field map
Specifically, it's this line (119) that's causing issues:
field_to_map<SPAN class="punctuation token">.</SPAN>addInputField<SPAN class="punctuation token">(</SPAN>append_layer<SPAN class="punctuation token">,</SPAN> field_map<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
If you noticed, I remarked out my multi fields to let the list just take the one field match combo that worked in the simpler, non-list script.
Still throws this error.
I'm confused how this list loops works. Not sure how it's figuring out position 0,1 with the case like
('Addy_Concat', 'PropertyAddress')
Any ideas what I'm doing wrong here? Or what I can do to try accomplish what I want to do?
There must be an easier way to field match when appending.