AnsweredAssumed Answered

Exported spatial join python command does gives different results as in ArcMap!

Question asked by oliver_ on Dec 11, 2017
Latest reply on Dec 12, 2017 by oliver_

Hi,

 

i am currently trying to join two shapefiles (polylines) that have "intersecting" features using the "spatial join" tool. In my example i additionally want to add one field from the attribute table of the join features to the target features.

 

My problem is:
This works in ArcMap (10.4.1.5686) but not when using a Python 2.7 script. For me it seems that there has to be a bug involved. To make this clear (and hopefully get some helpful answers) i have made some screenshots.

 

At first a screenshot of the spatial join window showing all the settings i was using:

 

Screenshot of the working Spatial Join tool settings in ArcMap

 

next is a screenshot of the attribute table of the output shapefile, as you can see the last column shows all the correct values:

output attribute table after the Spatial Join

 

But this was just for testing, for my actual project i need to run this in a python script, so i tried to reproduce the results by copying the python command from the ArcMap results window. This is what i got:

arcpy.SpatialJoin_analysis(target_features="temp_noDTV", join_features="temp_DTV", out_feature_class="D:/arcgis/output/temp.shp",
join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_COMMON",
field_mapping="""t_FID "t_FID" true true false 10 Long 0 10 ,First,#,temp_noDTV,t_FID,-1,-1""",
match_option="INTERSECT", search_radius="", distance_field_name="")

 

to make this work i have to adjust the paths a bit, here is my final python script:

 

import arcpy, sys

arcpy.env.workspace = sys.path[0] + "\.."

arcpy.SpatialJoin_analysis(target_features="temp_noDTV.shp", join_features="temp_DTV.shp", out_feature_class="temp",
join_operation="JOIN_ONE_TO_ONE", join_type="KEEP_COMMON",
field_mapping="""t_FID "t_FID" true true false 10 Long 0 10 ,First,#,temp_noDTV,t_FID,-1,-1""",
match_option="INTERSECT", search_radius="", distance_field_name="")

 

This script basically works, which means i get the same output features (the features of the TARGET that intersect with the JOIN features) but the important t_FID field/column in the output table contains only zeros!

 

I know there was or is a bug with spatial join when the target contains features that does not INTERSECT with the join features but there is a working workaround by selecting the intersecting features first and then to the spatial join OR just do the spatial join on the output again (as you can see the output has indeed the correct features). But i also tested this and got the same result.

 

I hope i have just made a stupid mistake and someone smart can point out my mistake, because i need this to work.

Outcomes