AnsweredAssumed Answered

Different results each time I run the same script on the same dataset (Spatial Join)

Question asked by MollyKFoley on Jun 25, 2018
Latest reply on Jun 26, 2018 by Dan_Patterson

I have a dataset I am running through this script and every time I run it, I get one of three different results. It works in a cyclical pattern. I have not changed anything, I run it all in a row and it produces these three different results each time.

 

What the script is doing is taking one dataset, selecting a subset of the features, buffering those selected features by 250 meters, then spatially joining it to another dataset. I have checked and the number of features selected and buffered never changes between data runs. The number of features the other dataset starts with never changes either. 

 

The first run will produce this:

Target dataset # features: 318

Selected features of join dataset: 93

Buffered features of join dataset: 93

Features after join (join is one to many): 323

 

The second run will produce this:

Target dataset # features: 318

Selected features of join dataset: 93

Buffered features of join dataset: 93

Features after join (join is one to many): 304

 

The third run will produce this:

Target dataset # features: 318

Selected features of join dataset: 93

Buffered features of join dataset: 93

Features after join (join is one to many): 184

 

On the fourth run, it will produce the 323 result again, then 304, then 184, and repeat... the correct output is 323.

 

I cannot figure out what is producing the different results considering all the data going into the spatial join tool is exactly the same between the three runs. Anybody have any ideas?

Thhis script is huge but this is the place where problems start. I use the in_memory workspace a lot and thought maybe that was causing issues but I clean up that workspace (delete) multiple times throughout the script and I removed it entirely in the below snippet to no avail.

try:
    pcFL = arcpy.MakeFeatureLayer_management(pcFC, 'pcFL')
    arcpy.SelectLayerByAttribute_management(pcFL, 'NEW_SELECTION', '"Area_Ft" < 314')
    if int(arcpy.GetCount_management(pcFL).getOutput(0)) > 0:
        arcpy.DeleteFeatures_management(pcFL)
    arcpy.Delete_management(pcFL)
except:
    arcpy.AddError('There was a problem determining if any protective cover did not meet the size requirements.')
    pcToSmallError = traceback.format_exc()
    arcpy.AddError(pcToSmallError)
    sys.exit(1)

#Buffer MonitoringPoint feature class by 250 meters
try:
    mp4State = arcpy.Select_analysis(MonitorPtFC, os.path.join(scratchGDB, 'mpselect'), '"StateID" = \'{0}\''.format(stateID))
    mpBuffer = arcpy.Buffer_analysis(mp4State, os.path.join(scratchGDB, 'mybuffer'), "250 Meters")
except:
    arcpy.AddError("Failed to buffer the monitoring point feature class.")
    mpBuffError = traceback.format_exc()
    arcpy.AddError(mpBuffError)
    sys.exit(1)

#Perform spatial join on protective cover input feature class to transfer Monitoring Point IDs to protective
#cover layer. First, remove fields we don't want transferred to output feature class:
fieldmappings = arcpy.FieldMappings()
fieldmappings.addTable(pcFC)
fieldmappings.addTable(mpBuffer)
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("Route"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("StateObjectID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("X_Coord"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("Y_Coord"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("Notes"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("RouteOrder"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("Spring"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("Fall"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("DateAdded"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("DateModified"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("FocalRefID"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("long"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("lat"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("StatePoint"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("BUFF_DIST"))
fieldmappings.removeFieldMap(fieldmappings.findFieldMapIndex("ORIG_FID"))

#Set output feature class name
newPCFC = os.path.join(scratchGDB, 'pc_spatialjoin')

#Perform spatial join. In the case of overlapping survey points, it is possible that a patch of
#protective cover may affect both survey points. In this case, "join one to many" is necessary as one
#patch of protective cover may affect multiple monitoring points.
try:
    arcpy.SpatialJoin_analysis(pcFC, mpBuffer, newPCFC, "JOIN_ONE_TO_MANY", "KEEP_COMMON", fieldmappings, "WITHIN_A_DISTANCE", '55 Meters')
except:
    arcpy.AddError("There was a problem processing your protective cover layer.")
    spJoinError = traceback.format_exc()
    arcpy.AddError(spJoinError)
    sys.exit(1)

Outcomes