[GIS] Using ArcPy FieldMappings for Spatial Join

arcgis-10.1arcpyerror-000725python-script-tool

I'm trying to create a python script tool to use within ArcMap 10.1. I created a Model and exported it as a python script and added custom field mappings for the first Spatial Join by reading through others' example scripts, however, the resulting fields when the script is run don't join using the "; " delimiter, and the output fields are the same as the input. I currently run the script as a python tool in ArcMap 10.1.

I'm stumped at this point even after reading all articles from ESRI on the FieldMappings object and its methods. The script is as follows if anyone would like to take a look:

import arcpy

# Script arguments
Waterbodies_UTM13_20130228 = arcpy.GetParameterAsText(0)

MT_ESA_20131023 = arcpy.GetParameterAsText(1)

wbdhu8_a_mt = arcpy.GetParameterAsText(2)

Montana_Soils__2_ = arcpy.GetParameterAsText(3)

WB_HUC_Intersect_ESA_UTM13_M1 = arcpy.GetParameterAsText(4)


# Local variables:
Waterbodies_UTM13_20130228__2_ = Waterbodies_UTM13_20130228
Waterbodies_or_Wetlands_on_ESA = Waterbodies_UTM13_20130228__2_
WL_WB_Joined_with_HUC_Basins_Data = Waterbodies_or_Wetlands_on_ESA
Biological_ESA_MT_Intersect = WL_WB_Joined_with_HUC_Basins_Data
Biological_ESA_MT_Intersect_ = Biological_ESA_MT_Intersect


# Process: Select Layer By Attribute: Selects all USACE = "Yes" features
arcpy.SelectLayerByAttribute_management(Waterbodies_UTM13_20130228, "NEW_SELECTION", "\"USACE\" = 'Yes'")

# Process: Select Layer By Location: Selects all features from the USACE = "Yes" features that intersect the ESA
arcpy.SelectLayerByLocation_management(Waterbodies_UTM13_20130228__2_, "INTERSECT", MT_ESA_20131023, "", "SUBSET_SELECTION")


# FieldMappings for Spatial Join process: Create a new "fieldmappings" and add the join features' attribute table
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(Waterbodies_UTM13_20130228)
fieldmappings.addTable(wbdhu8_a_mt)

# FieldMappings for Spatial Join process: Get the fields maps for the fields: REGION, SUBREGION, BASIN, and SUBBASIN
RegionFieldIndex = fieldmappings.findFieldMapIndex("REGION")
fieldmap1 = fieldmappings.getFieldMap(RegionFieldIndex)

SubRegionFieldIndex = fieldmappings.findFieldMapIndex("SUBREGION")
fieldmap2 = fieldmappings.getFieldMap(SubRegionFieldIndex)

BasinFieldIndex = fieldmappings.findFieldMapIndex("BASIN")
fieldmap3 = fieldmappings.getFieldMap(BasinFieldIndex)

SubBasinFieldIndex = fieldmappings.findFieldMapIndex("SUBBASIN")
fieldmap4 = fieldmappings.getFieldMap(SubBasinFieldIndex)

# FieldMappings for Spatial Join process: Set output fields and allowed field length to max (255)
field1 = fieldmap1.outputField
field2 = fieldmap2.outputField
field3 = fieldmap3.outputField
field4 = fieldmap4.outputField

field1.length = "255"
field2.length = "255"
field3.length = "255"
field4.length = "255"

fieldmap1.outputField = field1
fieldmap2.outputField = field2
fieldmap3.outputField = field3
fieldmap4.outputField = field4

# FieldMappings for Spatial Join process: Set the merge rule to join using the "; " delimiter and replace the old fieldmaps for each of the fields in the mappings object with updated one
fieldmap1.mergeRule = "Join"
fieldmap1.joinDelimiter = "; "
fieldmappings.replaceFieldMap(RegionFieldIndex, fieldmap1)

fieldmap2.mergeRule = "Join"
fieldmap2.joinDelimiter = "; "
fieldmappings.replaceFieldMap(SubRegionFieldIndex, fieldmap2)

fieldmap3.mergeRule = "Join"
fieldmap3.joinDelimiter = "; "
fieldmappings.replaceFieldMap(BasinFieldIndex, fieldmap3)

fieldmap4.mergeRule = "Join"
fieldmap4.joinDelimiter = "; "
fieldmappings.replaceFieldMap(SubBasinFieldIndex, fieldmap4)


# Process: Spatial Join: Spatially joins the HUC Basins data to the target WL or WB features (attributes are not "joined"; if a WB or WL falls on more than one basin,
# fieldmappings will concatenate attributes from select fields using "; " as a delimiter
arcpy.SpatialJoin_analysis(Waterbodies_or_Wetlands_on_ESA, wbdhu8_a_mt, WL_WB_Joined_with_HUC_Basins_Data, "JOIN_ONE_TO_ONE", "KEEP_ALL", fieldmappings, "INTERSECT")

# Process: Intersect: Intersects the temp output file from Spatial Join containing the joined WB or WL with the ESA, to create WL or WB pieces within the ESA
arcpy.Intersect_analysis([WL_WB_Joined_with_HUC_Basins_Data, MT_ESA_20131023], Biological_ESA_MT_Intersect, "ALL", "", "INPUT")

# Process: Multipart To Singlepart: Explodes all WL or WB pieces into singlepart features
arcpy.MultipartToSinglepart_management(Biological_ESA_MT_Intersect, Biological_ESA_MT_Intersect_)

# Process: Spatial Join (2): Spatially joins the exploded WL or WB features containing the HUC basins data with the Soils Data; attributes ARE "joined" and
# use "; " to deliminate all attributes from features that intersect the WL or WB features
arcpy.SpatialJoin_analysis(Biological_ESA_MT_Intersect_, Montana_Soils__2_, WB_HUC_Intersect_ESA_UTM13_M1, "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT", "", "")

Screenshot of the error window in ArcMap:
error message

Line 95:

arcpy.SpatialJoin_analysis(Waterbodies_or_Wetlands_on_ESA, wbdhu8_a_mt, WL_WB_Joined_with_HUC_Basins_Data, "JOIN_ONE_TO_ONE", "KEEP_ALL", fieldmappings, "INTERSECT")

Best Answer

This error means that you have already created an output feature class and now the script is trying to overwrite the existing dataset which is not allowed by default. You should either delete this dataset each time prior to run scripts (manually or automatically from the script) or set the environment variable responsible for overwriting outputs to True.

arcpy.env.overwriteOutput = True
Related Question