My python code is not selecting by location?  Help...

Discussion created by ljvasil on Sep 29, 2011
Latest reply on Sep 30, 2011 by ljvasil
Hi all,

I'm trying to write a python code that will select the grid_codes for the adjacent neighbors for a point shapefile. I want this list to be iterative so it will create temporary selections do some processing and update the center cell. I used select by location method, but when I make a list of the Grid_Codes, only the cursor cell value is returned. I get an error -- "Error in getting output". I tried putting a print statement and a getOutput(0) statement in the code, but I'm getting the same error. I'm stumped! Here's my code up to the point where I'm getting an error:

#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()
GCA_List = []
#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)

arcpy.MakeFeatureLayer_management(shp, "shp_lyr")

#Select the sequential order of Object ID
arcpy.SelectLayerByAttribute_management("shp_lyr", "NEW_SELECTION", sql)

#Select only the Object ID that are surrounding the cursor cell
Adj_Neighbors = arcpy.SelectLayerByLocation_management("shp_lyr", "WITHIN_A_DISTANCE", "shp_lyr", "40")

print Adj_Neighbors
Adj_NeighborsValue = Adj_Neighbors.getOutput(0)

for codes in Adj_Neighbors:
GCA = row.getValue("Grid_Code") #GCA = Grid Codes from adjacent cells

#Put Grid codes in a list
print GCA_List

Here's the error:
[11.0] ====> This is the Grid_Code of the first cursor point. I need the surrounding cells as well.
Traceback (most recent call last):
File "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 43, in <module>
for codes in Adj_Neighbors:
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\mixins.py", line 866, in __getitem__
return self.getOutput(item)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\arcobjects.py", line 919, in getOutput
return convertArcObjectToPythonObject(self._arc_object.GetOutput(*gp_fixargs(args)))
RuntimeError: ResultObject: Error in getting output

I'm not sure what I'm doing wrong. The points are all 30 meters apart so selecting within a distance of 40 meters should be fine. Can anyone give any suggestions?

Thanks for any help,