1

I performed a task ArcMap 10.1, using the Spatial Join tool in the toolbox. Then, from the Results widows, right-click and select Copy As Python Snippet. This will be the basis for a script that will loop over a series of input / output combinations to automate the repetitive steps a user would otherwise perform manually.

The script contains a large field mapping. I'm not performing any field mappings so, in my mind, I don't really need it. Is there a way to remove the field mapping section and tell arcpy to just use the same names? That's what the code is doing but I'm looking for a cleaner way. Plus, my iterations will not contain the same fields but they will not have any field mapping either. I tried removing the field mapping section and replacing it with "" but that didn't work.

Update 2 - One solution, and I'm holding out hope for something more elegant, iterate the feature classes used for each spatial join and build the field mappings in code.

Update 1 - Added script. The error is "arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid." when I replace the field mappings with "". The error occurs on the line beginning with "INTERSECT",.

arcpy.SpatialJoin_analysis("XXX-TargetFeatures-XXX",
                           "C:/.../localFileGeodatabase.gdb/XXX-JoinFeatures-XXX",
                           "C:/.../localFileGeodatabase.gdb/XXX-OutputFeatureClass-XXX",
                           "JOIN_ONE_TO_MANY",
                           "KEEP_COMMON",
                           """Field01 "Field01" true true false 4 Long 0 0 ,First,#,C:/.../localFileGeodatabase.gdb/XXX-TargetFeatures-XXX,Field01,-1,-1;
                            Field02 "Field02" true true false 4 Long 0 0 ,First,#,C:/.../localFileGeodatabase.gdb/XXX-TargetFeatures-XXX,Field02,-1,-1;
                            Field03 "Field03" true true false 2 Text 0 0 ,First,#,C:/.../localFileGeodatabase.gdb/XXX-TargetFeatures-XXX,Field03,-1,-1;
                            .
                            .
                            .
                            Field120_1 "Field120" true true false 2 Text 0 0 ,First,#,C:/.../localFileGeodatabase.gdb/XXX-TargetFeatures-XXX,Field120,-1,-1""",
                            "INTERSECT", "2000 Feet", "
  • When you omit the field mappings what error happens? Can you share some code snippets of what you are trying to do? – Farid Cheraghi Feb 16 '15 at 17:05
  • Try replacing the field mapping with "#" instead of just "", sometimes that works. – Erica Feb 16 '15 at 20:46
1

A FieldInfo object might work (I use them in MakeFeatureLayer_management operations) -- takes all the original field names, keeps the name the same, and tags them as "visible."

origFields = arcpy.ListFields(origFC)
fieldInfo = arcpy.FieldInfo()
# make feature layer with all fields named the same and visible
for field in origFields:
    fieldInfo.addField(field.name, field.name, "VISIBLE", "")

Then use the fieldInfo variable in the appropriate parameter location.

SpatialJoin_analysis (target_features, join_features, out_feature_class, {join_operation}, {join_type}, {field_mapping}, {match_option}, {search_radius}, {distance_field_name})

(ref. ArcMap Help Page)

arcpy.SpatialJoin_analysis("XXX-TargetFeatures-XXX",
                           "C:/.../localFileGeodatabase.gdb/XXX-JoinFeatures-XXX",
                           "C:/.../localFileGeodatabase.gdb/XXX-OutputFeatureClass-XXX",
                           "JOIN_ONE_TO_MANY", "KEEP_COMMON", fieldInfo, "INTERSECT", "2000 Feet",)
  • This MIGHT be completely wrong for SpatialJoin_analysis and you might need to use field mapping instead. I have only dabbled slightly in this area, so this answer might be either incomplete or outright wrong... – Erica Feb 16 '15 at 20:45
  • The underlying problem was the XXX-TargetFeatures-XXX on the first line. The feature could not be found. One I gave a full path so it could be found, your suggestion about using "#" worked. – DenaliHardtail Feb 16 '15 at 21:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.