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

780
5
Jump to solution
12-11-2017 11:35 PM
OliverGötze
New Contributor II

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.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
OliverGötze
New Contributor II

Thanks for your answer but that was not the problem. But after rereading the post i had an idea and luckily found the solution

As i said, to make the ArcMap results command work i had to adjust the paths a bit, this included adding ".shp" to the TARGET and JOIN features names or i get an error when running the script. But there was another place where i "forgot" to add the ".shp" and got no errors from the python interpreter and that was the "field_mapping" attribute. I just had to replace "temp_noDTV" with "temp_noDTV.shp" to make it work.

View solution in original post

5 Replies
DanPatterson_Retired
MVP Esteemed Contributor

I always get suspicious when I see 'true' when it might be 'True' especially if python is involved and booleans are needed

0 Kudos
OliverGötze
New Contributor II

Thanks for your answer but that was not the problem. But after rereading the post i had an idea and luckily found the solution

As i said, to make the ArcMap results command work i had to adjust the paths a bit, this included adding ".shp" to the TARGET and JOIN features names or i get an error when running the script. But there was another place where i "forgot" to add the ".shp" and got no errors from the python interpreter and that was the "field_mapping" attribute. I just had to replace "temp_noDTV" with "temp_noDTV.shp" to make it work.

DanPatterson_Retired
MVP Esteemed Contributor

That was going to be my backup answer 

JoshuaBixby
MVP Esteemed Contributor

When you called Spatial Join using the GUI, you weren't selecting data sets for target and join features but layers.  When the tool runs against layers, it looks at the selection against the layers and then reaches back to the data sets the layers point to.  When you used the exact same code in a stand-alone script, there were no layers or data sets with those names, the the tool returns an error.  If you wanted the GUI-derived code to work as-is, you would need to run Make Feature Layer on the target and join features first.

OliverGötze
New Contributor II

Thanks, after the initial try of the spatial join command given by the ArcMap i got those error messages and a came to the conclusion you described. But the actual problem i had, was due to the missing file ending in the field_mapping attribute of the arcpy.SpatialJoin_analysis function.

0 Kudos