AnsweredAssumed Answered

Spatial Join - looping with arcpy - order issues of join_count fields

Question asked by the_chimp on Feb 15, 2016
Latest reply on Feb 17, 2016 by bixb0012

I developed a python script where the user can enter

  • a polygon shapefile
  • multiple point shapefiles

and within the script the polygon shapefile is divided into 1000mx1000m grid cells and then the point shapefiles are joined to the polygon within a for loop, where the intersecting points per cell are counted.


I tested the code and everything was fine, but now I detected a huge issue. The order of the added join count fields (Join_Count, Join_Cou_1, Join_Cou_2, etc.) does not correspond to the order in which the point shapefiles were entered.


Below the input order and output order can be seen. I tried it multiple times, but it seems there is no logic behind the order, so not alphabetical, Input order, reverse order, or anything. It seems like it would be ordered randomly.

  • Bank -> Join_Count (Bank)
  • Kino -> Join_Cou_1 (Post)
  • Park -> Join_Cou_2 (Park)
  • Post -> Join_Cou_3 (Kino)


Why can this happen? Shouldn't the first point shapefile which is joined first, get the Join_Count field, the second point shapefile that is joined next, the Join_Cou_1 field and so on?


Below is my code.


import arcpy
studyarea = arcpy.GetParameterAsText(0) 
inputPoints = arcpy.GetParameterAsText(1)
outputLocation = arcpy.GetParameterAsText(2) 

arcpy.GridIndexFeatures_cartography('in_memory\studyarea_grid', studyarea, 'INTERSECTFEATURE', 'NO_USEPAGEUNIT', "#", 1000, 1000)
studyAreaGrid = arcpy.Clip_analysis('in_memory\studyarea_grid', studyarea, arcpy.Polygon)

inputPoints_set = inputPoints.split(';') 

spatialJoin = studyAreaGrid
for pointShp in inputPoints_set:
    spatialJoin = arcpy.SpatialJoin_analysis(spatialJoin, pointShp, arcpy.Polygon, 'JOIN_ONE_TO_ONE', 'KEEP_ALL', '', 'INTERSECT')

arcpy.CopyFeatures_management(spatialJoin, outputLocation)