Average Nearest Neighbour results table?

3154
1
12-08-2011 08:00 AM
AnnaScott
New Contributor
Hello,

I'd like to run an Average Nearest Neighbour analysis for a number of different layers (multiple habitat types). The Average Nearest Neighbour tool seems to fit what I want to do, except that it only runs one at a time, and the only way to access the results is through the results window. I have to go through all of the results and write down the answers so that I can put them into a table. Is there any way to get the tool to write the results to a table? I've go quite a few of these to do!

I found a toolbox and python script that someone had helpfully created on an archived forum from 2007; it creates a csv. file (http://forums.esri.com/Thread.asp?c=93&f=983&t=233103). But it won't run properly -it was possibly intended for an earlier version of Arc (I'm running ArcGIS 10). I don't know much about python script etc so I don't know how to fix it. Can anyone help?

Or if there are any other ideas I'd appreciate it!

A
0 Kudos
1 Reply
OmarBarrantes
New Contributor
Hello,

I'd like to run an Average Nearest Neighbour analysis for a number of different layers (multiple habitat types). The Average Nearest Neighbour tool seems to fit what I want to do, except that it only runs one at a time, and the only way to access the results is through the results window. I have to go through all of the results and write down the answers so that I can put them into a table. Is there any way to get the tool to write the results to a table? I've go quite a few of these to do!

I found a toolbox and python script that someone had helpfully created on an archived forum from 2007; it creates a csv. file (http://forums.esri.com/Thread.asp?c=93&f=983&t=233103). But it won't run properly -it was possibly intended for an earlier version of Arc (I'm running ArcGIS 10). I don't know much about python script etc so I don't know how to fix it. Can anyone help?

Or if there are any other ideas I'd appreciate it!

A


Hello Anna.

Like you, i need to run ANN for multiple layers in a fc datasets. As you remark, Luke Pinner's script http://forums.esri.com/Thread.asp?c=93&f=983&t=233103, was very helpful for the task to export results in csv file, after some modifications for my data, i wrote this script:

# Name: ANN [AverageNearestNeighbor].py
# Description: AverageNearestNeighbor for FC in Datasets. Also export results to csv file.
# Created by: Omar Barrantes; obarrantes@gmail.com

import arcinfo, arcpy, glob, os, string, sys, arcgisscripting, math
from arcpy import env, mapping, conversion, sa

# Workspace
arcpy.env.workspace = r"D:/Data/Landscape.gdb"
out_feature_class = r"D:/Data/Landscape.gdb"

# Display workspace
print 'workspace: ' + out_feature_class

# Geoprocessor object
gp = arcgisscripting.create()
gp.overwriteoutput = True

# Tables output path
path = r"D:/Data/Tables/ANN/"

# Search for feature classes datasets name start with "Land"
datasetList = arcpy.ListDatasets("Land*", "All")
for dataset in datasetList: 
    
    # Search for feature classes name start with "Biometric"
    FileList=arcpy.ListFeatureClasses("Biometric*", "", dataset) #
    FileList.sort()
    for File in FileList:
 print "File: " + str(File)
 desc = arcpy.Describe(File)
 SFR = desc.featureType # Example "Simple"
 SST = desc.shapeType # Example "Polygon", "Polyline", "Point"
 
 #List of unique landscape codes from [UP] Field
 field = "UP"
 valueList = []
 List = []
 rows = arcpy.SearchCursor(File)
 for row in rows:
     valueList.append(row.getValue(field))
 uniqueSet = set(valueList)
 uniqueList = list(uniqueSet)
 uniqueList.sort()
 List = uniqueList
 del rows
 del row
 print uniqueList
 
 NoSelectCount = 0
  
 outFile = open(path+str(File)+"_ANN"+".csv", 'a')
 outFile.write('FC,UP,ANN_INDEX,ZSCORE,PSCORE,EMEAN,OMEAN,CA\n') 
 for row in uniqueList:
     print "Class " + str(row)
     # Layer feature class
     layername = str(File) + '_'+ str(row)
     
     NoSelectCount=NoSelectCount+1
     where = " \"UP\" = " + "'" + row + "'"
     where2 = ' "UP" = ' + '"' + row + '"'
     arcpy.MakeFeatureLayer_management(File, layername, where) 
     
     # Create search cursor
     rows1 = arcpy.SearchCursor(layername)
     # Class Area from landscape
     CA= 0
     shapeName = arcpy.Describe(layername).shapeFieldName
     for row1 in rows1:
  feat = row1.getValue(shapeName)
  CA += feat.area
     
     nn_output = arcpy.AverageNearestNeighbor_stats(layername, "EUCLIDEAN_DISTANCE", "GENERATE_REPORT", "#")
     index = nn_output[0]
     zscore = nn_output[1]
     pscore = nn_output[2]
     emean = nn_output[3]
     omean = nn_output[4]
     
     print arcpy.GetMessages()
     outFile.write('%s,%s,%s,%s,%s,%s,%s,%s\n' %(str(File),str(row),index,zscore,pscore,emean,omean,CA))
 outFile.close()
     
print ("Done ......") 


Hope work for you.
0 Kudos