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

415
2
09-29-2011 09:15 AM
LindaVasil
New Contributor III
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
GCA_List.append(GCA)
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,
Linda
0 Kudos
2 Replies
MarcNakleh
New Contributor III
Hello Linda,

To answer your main question, the following line
for codes in Adj_Neighbors

is bugging because Adj_Neighbors contains nothing more than "shp_lyr", i.e. the name of your layer. To actually cycle through the values, you'd need to run a Search Cursor through it as you did with your main shape, using either:
for row2 in arcpy.SearchCursor(Adj_Neighbors):
    GCA_List.append(row2.getValue("Grid_Code"))

or Python's list comprehensions,
GCA_List = [r.getValue('ELEVATION') for r in arcpy.SearchCursor('shp_lyr')]


I also don't know how successful you'll be with this approach, if I'm reading your code right. You're creating a layer for each PIP (and so I presume 1 feature per layer), and then you're running that Layer through a Select by Location to find nearby features. For one, the Select by Location can only return a subset of the layer, so either 1 or 0 features. For two, you're comparing a layer to itself: the output will be the layer, as it will always match itself (which is why you can't do a Select By Location of a shape to itself in ArcMap, if I recall.)

What you would need to do is to create two separate shapes (as arcpy doesn't allow multiple independent layers on the same shape, I believe), turn one into a layer based on your PIP, and run a select by location on the other seeing how many of its features are a certain distance from your first layer's 1 feature.
As a function of your needs, Near (for ArcInfo), Closest Feature Difference (for ET_Geowizards) or
some kind of Buffer Analysis might be mroe straightforward.

Also worth mentioning
- Cursors in 9.x need to be manually iterated through, so you would need to have another row = cur.next() inside your loop to cycle through each row
- You seem to only be looking for values (as opposed to modifiying them), so a Search Cursor would be more appropriate. Also, as you seem to be using arcpy, I can add that arcpy module's cursors can be used as iterators, so you could replace:
cur = arcpy.UpdateCursor(shp)
row = cur.next()
while row:
    ...
    row = cur.next()

with one line:
for row in arcpy.SearchCursor(shp):
    ...

- MakeFeatureLayer_management actually allows for a where clause, so you could integrate your SelectByAttribute into it, like so:
arcpy.MakeFeatureLayer_management(shp, "shp_lyr", sql)



A bit wordy, but I hope this helps!

Cheers,
0 Kudos
LindaVasil
New Contributor III
Absolutely this helps.  I'm pretty new to coding so I did not how to get around a shapefile selecting itself part.  I appreciate all the feedback and the clues of how to rewrite.

Thanks very much!
Linda
0 Kudos