Select to view content in your preferred language

Error returned after cursor searching layers from a map

2562
4
Jump to solution
03-29-2012 11:52 AM
MikeMacRae
Frequent Contributor
Hey everyone,

I want to do this:


  1. Add a feature class as a layer to a map using python

  2. Do a select by location between the added layer and an existing layer in the map

  3. Do a cursor search through the records returned from the select by location and print some values from some fields


From my code below, everything works right up until the select by location. I get the following error:


Traceback (most recent call last):
  File "Z:\ESRI\Python\Test Scripts\test.py", line 60, in <module>
    rows = arcpy.SearchCursor(arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", "polygontest"))
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 820, in SearchCursor
    return gp.searchCursor(*args)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 357, in searchCursor
    self._gp.SearchCursor(*gp_fixargs(args)))
IOError: "GPL0" does not exist


I was thinking that when I filter down to the "WELLS" layer, then I should have no problem doing the select. I don't need to convert these to feature layers, because they already are layers in a map. I'm not sure what is causing this error.


import arcpy from arcpy import env  env.workspace = r"Z:\Test.gdb"  env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument(r"Z:\Test.mxd")  for df in arcpy.mapping.ListDataFrames(mxd, "*"):      # Add a feature class as a layer from the workspace environment. This works.     addLayer = arcpy.mapping.Layer("polygontest")     arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")      for lyr in arcpy.mapping.ListLayers(mxd, "", df):           # Filter out the layer I want to use in my select by location statement. This works and can print out the datasetName.          if lyr.name == "WELLS":               print lyr.datasetName               # Select by location any features in the "WELLS" layer that intersect "polygontest" layer. This is where the crash happens.               rows = arcpy.SearchCursor(arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", "polygontest"))                # Search through the records returned from the select by location statement.               for row in rows:                   print str(row.OBJECTID) + " = " + str(row.STATUS)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
You need to break up that line.

arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", "polygontest") rows = arcpy.SearchCursor(lyr)

View solution in original post

0 Kudos
4 Replies
DarrenWiens2
MVP Alum
I'm guessing that the problem is that SelectLayerByLocation puts a selection on a feature layer, and SearchCursors take a feature class, shapefile, or table as input.
0 Kudos
MikeMacRae
Frequent Contributor
I thought the same thing, but I did the following as a test after I listed the dataframe and it worked:

arcpy.MakeFeatureLayer_management("polygontest", "ply")
arcpy.MakeFeatureLayer_management("WELLS", "wells")


rows = arcpy.SearchCursor(arcpy.SelectLayerByLocation_management("wells", "INTERSECT", "ply"))

for row in rows:
     print str(row.OBJECTID) + " = " + str(row.STATUS)


It's doing a cursor search on the selected values from the feature layers, so it doesn't seem to have issues with that....
0 Kudos
MathewCoyle
Honored Contributor
You need to break up that line.

arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", "polygontest") rows = arcpy.SearchCursor(lyr)
0 Kudos
MikeMacRae
Frequent Contributor
Ah, thanks Matt! That worked.
0 Kudos