The attached script is my stalled attempt to perform the following workflow:
- Create a table and create fields
- Loop through a folder of point shapefiles and perform average nearest neighbor analysis
- Create a row and write "nn_values[]" to the corresponding fields
- move to the next shapefile and repeat
The script successfully produces a table, runs the aNN analysis for a shapefile and adds an empty row to the table. As an end-product, I would like a table with all of the values from the statistical analysis. How can I write the associated aNN statistical output (i.e. "nn_values[]") to the corresponding fields for each iteration in the for loop? Many thanks for the guidance.
# Import system modules
import arcpy, os
from arcpy import env
# Set over write
env.overwriteOutput = 1
# Local variables...
env.workspace = r"C:\temp"
Dir = env.workspace
out_name = "NNtable.dbf"
### Execute CreateTable
arcpy.CreateTable_management(Dir, out_name)
### Add fields
arcpy.AddField_management(Dir + "\\" + out_name, "index", "FLOAT")
arcpy.AddField_management(Dir + "\\" + out_name, "z_score", "FLOAT")
arcpy.AddField_management(Dir + "\\" + out_name, "p_value", "FLOAT")
arcpy.AddField_management(Dir + "\\" + out_name, "expected", "FLOAT")
arcpy.AddField_management(Dir + "\\" + out_name, "observed", "FLOAT")
arcpy.AddField_management(Dir + "\\" + out_name, "pathway", "TEXT")
# List FCs
fclist = arcpy.ListFeatureClasses()
for fc in fclist:
try:
# Obtain Nearest Neighbor Ratio and z-score
# Process: Average Nearest Neighbor...
nn_output = arcpy.AverageNearestNeighbor_stats(fc, "EUCLIDEAN_DISTANCE", "NO_REPORT", "#")
# Create list of Average Nearest Neighbor output values by splitting the result object
nn_values = nn_output.split(";")
print "The nearest neighbor index is: " + nn_values[0]
print "The z-score of the nearest neighbor index is: " + nn_values[1]
print "The p-value of the nearest neighbor index is: " + nn_values[2]
print "The expected mean distance is: " + nn_values[3]
print "The observed mean distance is: " + nn_values[4]
print "The path of the HTML report: " + nn_values[5]
except:
# If an error occurred when running the tool, print out the error message.
print arcpy.GetMessages()
# Create insert cursor for table
#
rows = arcpy.InsertCursor(Dir + "\\" + out_name)
row = rows.newRow()
rows.insertRow(row)
# Delete cursor and row objects to remove locks on the data
del row
del rows