1

I am trying to perform a spatial join that will join all my join features (along with their attributes), within a specified distance, to my target features. Given the nature of the datasets, this should more than double the number of records in the target feature class (taking into account that not all of the join features are within the specified search radius). However, this join appears to rarely be functioning accurately.

My target feature class consists of ~7000 records and when joined accurately via a 'JOIN_ONE_TO_MANY', 'KEEP_ALL' spatial join contains ~16000 records. However, at least ~90% of the time the spatial join produces just under 8000 records. This has been frustrating, as this join is an integral part of a long script, and incorrect joins propagate into subsequent calculations.

I am able to get the spatial join tool to run correctly using the same layers and options in ArcMap 10.4 or in ArcPro 1.4. Though, the python script seems to be producing unreliable results when ran in Visual Studio leveraging arcpy.SpatialJoin_analysis.

The script was originally using Python 2 and ArcGIS 10.4. I switched to Python 3 (3.5.2) using a fresh Conda 3 download and the ArcPro Python package hoping that if the tool was buggy, this may fix it. No luck. Does anyone have any recommendations?

As requested, here is the line of code for the spatial join:

arcpy.SpatialJoin_analysis ("SubLayer_Layer_2", "pp_trans_GUJoin_Layer_2", SubLayer_pp_trans_GUJoin_UniqueSubs, 'JOIN_ONE_TO_MANY', 'KEEP_ALL', "#", 'WITHIN_A_DISTANCE', .000002) 

Here is a code leading up to the spatial join:

import arcpy
from arcpy import env
import time
start = time.time()
print(start)
print("Start")
arcpy.env.OverwriteOutput = True


defaultGdbPath = r'\\networkpath\data 

    ###########################################################


subLayer=defaultGdbPath + '\Subs'
transLayer=defaultGdbPath + '\Tran'
ppLayer=defaultGdbPath + '\PP'

transLayer_Points = defaultGdbPath + '\Tran_Pts'


################################################################################


pp_trans_GUJoin = defaultGdbPath + '\pp_trans_GU_SpatialJoin'
SubLayer_pp_trans_GUJoin = defaultGdbPath + '\SubLayer_pp_trans_GUSpatialJoin'
SubLayer_pp_trans_GUJoin_UniqueSubs = defaultGdbPath + '\SubLayer_pp_trans_GUSpatialJoin_UniqueSubs'

#######################
#add new count field for distinct count for PP count 
arcpy.AddField_management(subLayer, "SUBID", "SHORT", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

#calculate field == Join_Count
arcpy.CalculateField_management (subLayer, "SUBID", "!OBJECTID!", "PYTHON_9.3")


#########################
arcpy.FeatureVerticesToPoints_management(transLayer, transLayer_Points, "BOTH_ENDS")

##############################

#Make feature layer
arcpy.MakeFeatureLayer_management(ppLayer, "ppLayerSpatJoin")

#Make feature layer
arcpy.MakeFeatureLayer_management(transLayer_Points, "transLayerSpatJoin")

#spatial join of feature layers
arcpy.SpatialJoin_analysis ("transLayerSpatJoin", "ppLayerSpatJoin", pp_trans_GUJoin, 'JOIN_ONE_TO_ONE', 'KEEP_ALL', '#', 'WITHIN_A_DISTANCE', .0005) 


#add new count field for distinct count for PP count 
arcpy.AddField_management(pp_trans_GUJoin, "Join_Count_PP", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

#calculate field == Join_Count
arcpy.CalculateField_management (pp_trans_GUJoin, "Join_Count_PP", "!Join_Count!", "PYTHON_9.3")

#Make feature layer
arcpy.MakeFeatureLayer_management(subLayer, "SubLayer_Layer")

#Make feature layer
arcpy.MakeFeatureLayer_management(subLayer, "SubLayer_Layer_2")

#Make feature layer 
arcpy.MakeFeatureLayer_management(pp_trans_GUJoin, "pp_trans_GUJoin_Layer")

#Make feature layer 
arcpy.MakeFeatureLayer_management(pp_trans_GUJoin, "pp_trans_GUJoin_Layer_2")

arcpy.SpatialJoin_analysis ("SubLayer_Layer_2", "pp_trans_GUJoin_Layer_2", SubLayer_pp_trans_GUJoin_UniqueSubs, 'JOIN_ONE_TO_MANY', 'KEEP_ALL', "#", 'WITHIN_A_DISTANCE', .000002) 
  • I can't see anything immediately wrong with your code except for being unnecessarily verbose and missing an apostrophe on defaultGdbPath, though I assume that line was changed when adding into your question. It's a bit hard to see what is actually happening, not having any of the data to play with, can you find an incorrect situation and screen shot it then explain what it should be. – Michael Stimson Jul 11 '17 at 20:55

Your Answer

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

Browse other questions tagged or ask your own question.