Using python how do i return the Nearest Neighbor Analysis Results window as an output (the r-value) to use as a input for Kernel Density? We are using the if-else statement. So if the r-value is below 0.5 then we want to run the tool.

Using python how do i return the Nearest Neighbor Analysis Results window as an output (the r-value) to use as a input for Kernel Density? We are using the if-else statement. So if the r-value is below 0.5 then we want to run the tool.

IDLE (through ArcGIS) is not recognizing the output we want to use (nn_output[0]). We have tried many different workflows but it is still not reading it. This is out current code (with spatial analyst activated):

nn_output = arcpy.AverageNearestNeighbor_stats(outputfc, "EUCLIDEAN_DISTANCE", "GENERATE_REPORT", "")

arcpy.AddMessage("Average Nearest Neighbor Results:")

arcpy.AddMessage("The nearest neighbor index (r) is: " + nn_output[0])

arcpy.AddMessage("The z-score of the nearest neighbor index is: " + nn_output[1])

arcpy.AddMessage("The p-value of the nearest neighbor index is: " + nn_output[2])

arcpy.AddMessage("The expected mean distance is: " + nn_output[3])

arcpy.AddMessage("The observed mean distance is: " + nn_output[4])

arcpy.AddMessage("The path of the HTML report: " + nn_output[5])

nnK = nn_output.getOutput(0)

#p-value

if (nnK >= 0.0) and (nnK <= 0.5):

#Kernel Density

KernelDen = KernelDensity(outputfc, None, "", "", "", "", "")

KernelDen.save("I:/Share/Delta/Data_Shp/BurglaryAnalysis.gdb/KernelDen")

arcpy.AddMessage("Kernel Density Complete.")

arcpy.AddMessage("Pattern is clustered.")

else:

arcpy.AddMessage("Pattern is not clustered.")

if the outputs are numbers... str( ) it within the addmessage the error message would be useful to confirm this supposition. As for the 'if' statement can you chec to see if the returned value is a string or a float?

also build your strings using mini-formatting language, they handle strings etc nicely

`frmt ="""`

the first output {}

2nd {}

another one {}

again ... {}

some more stuff {}

"""

nn_outputs = [1,2,"stuff",4,"text"]

outstring = frmt.format(*nn_outputs)

print(outstring)

the first output 1

2nd 2

another one stuff

again ... 4

some more stuff textsubstitute 'print' with arcpy.AddMessage(outstring)

If this is the nearest neighbor tool you are using, then a result is output and the results can be accessed from there... see the code sample at the end

Average Nearest Neighbor—Help | ArcGIS for Desktop