ljvasil

Finding adjacent neighbors for Cellular Automata

Discussion created by ljvasil on Sep 26, 2011
Latest reply on Aug 5, 2012 by Jamal432@gmail.com
Hi all,

I'm trying to write a simple cellular automata code on a reclassified land use point shapefile.  It is reclassified by likelihood of land use conversion based on the number of like adjacent neighbors.  I would like to the code to run cell by cell and do the following:
Find adjacent neighbors
Get Grid_Code value
Find out if there is 4 of more cells with same land use grid code
If there is and this value is greater than the center cell, then the center cell converts.

I've written the following code, but it gets caught in select by attribute command.  The error that comes back is the following:
"C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\Flash\Documents\School\CA\CellularAutomata.py", line 32, in <module>
    arcpy.SelectLayerByAttribute_management(shp, "New_Selection", sql)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 4259, in SelectLayerByAttribute
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
The value cannot be a feature class
ERROR 000840: The value is not a Raster Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByAttribute).

Here is the code itself: 

#Import arc geoprocessing module
import arcpy, sys

#Set variable to the point shapefile
shp = sys.argv[1]

#Add LRG_Value to the point shapefile
arcpy.AddField_management(shp, "LRG_Value", "Long")

#Place an update cursor in the point shapefile table
#move to the first record in the table
cur = arcpy.UpdateCursor(shp)
row = cur.next()

#Loop through each record in the table
while row:
    #Extract Grid Code and Object ID
    GCP = row.getValue("Grid_Code")       #GCP = Grid Code of Processing Cell
    PIP = row.getValue("PointID")            #PIP = Point ID of Processing Cell
   
    #Put ObjectID in sequential order
    sql = "PointID" + " = " + str(PIP)
   
    #Select the sequential order of Object ID
    arcpy.SelectLayerByAttribute_management(shp, "New_Selection", sql)  #Where the mess up is!
   
    #Select only the Object ID that are surrounding the cursor cell
    Adj_Neighbors = arcpy.SelectLayerbyLocation_management(shp, "Boundary_Touches", shp, " ", "New Selection")
   
    for codes in Adj_Neighbors:
        GCA = row.getValue("Grid_Code")                  #GCA = Grid Codes from adjacent cells           
   
        #Put Grid codes in a list
        GCA_List = list(GCA)
   
        #Sort Grid Codes from smallest to largest
        srt_GCA_List = sorted(GCA_List)
   
        #select the largest number in the list (last number in sequence)
        LR_Value = srt_GCA_List[-1]
   
        #count the number of times the largest occurs
        tie_LR = GCA_List.count(LR_Value)
   
        if tie_LR >= 4 and LR_Value > GCA:  #is this syntax correct...seems a little messy to me.
            row.setValue("LRG_Value", LR_Value)
        #else:
        #    LR_Value = srt_GCA_List[-2]
        #keep going until the end of the list -- I will write this later
       
        #Update the row values
        cur.updateRow(row)

    #Delect temporary Variables
    del GCP, OIP, sql, result, GCA, GCA_List, srt_GCA_List, LR_Value, tie_LR

    #move to the next row
    row = cur.next()

    #delete row and cursor object, arcpy, module
    del cur, row, arcpy


I would appreciate any help on that line, or any other lines that are obviously wrong.  I'm pretty new to the code writing.

Thanks!
Linda

Outcomes