I have a rather long script that first references an SDE feature class (non-versioned) to perform an attribute selection based on a SQL selection. I've been unable to figure out why the script will run once fine, but if I run it again I always get an error at the point of the SelectLayerByAttribute_management step.
My environment: ArcGIS 10.1 SP1, Win7 SP1, and the source file is on an Oracle(11g) SDE server accessed through database authentication.
Error message: "ARCPY ERROR 000582: Error occurred during execution."
This is the code I start with:
arcpy.env.workspace = "IN_MEMORY" mxd = arcpy.mapping.MapDocument("CURRENT") mm = arcpy.mapping.ListDataFrames(mxd, "MainMap")[0] qsLayer = arcpy.mapping.ListLayers(mxd, "EAS QS Grid", mm)[0] qs = "10-28" qry = "TILE = '%s'" % (qs) arcpy.SelectLayerByAttribute_management(qsLayer, "NEW_SELECTION", qry) When I look at the GP Results window, I see that the Output Layer Name is GPL0, not "EAS QS Grid". I've tried using the Select by Attribute tool from the toolbox and after running the results show the correct layer name. So what is happening when using arcpy to have the layer name changed to GPL0? This script also performs similar selections to other layers in the MXD, five SelectLayerByAttribute and two SelectLayerByLocation. All the Output names always show as GPL0. This seems like it could cause Arc to trip over itself if GPL0 is somehow locked in memory after the script runs, but I can't find any documentation about the behavior of Geoprocessing Layers(GPL?) or why a Select Layer By Attribute tool would even need to make a new Output Layer Name.
Things I've tried: - 'del qsLayer' at the end of the script - Not using IN_MEMORY workspace - Deleting the IN_MEMORY workspace at the end of the script - Setting the overwriteOutput to True at the top of the script - Using qsLayer.dataSource as the parameter for SelectByAttribute - Yes, GP Background Processing is not turned on
So far none of that seems to change the outcome.
The error message does not provide any useful feedback, so I want to ask the web if anyone has had problems with this kind of issue. Right now the only way I can use the script is to run it once, close ArcMap and reopen, then it's good to go one more time.
Thank you for any suggestions.
EDIT:
In my excitement at getting the script working I rushed to post my answer, and of course it failed anyway. Using arcpy.mapping.Layer was not the solution...
What I did figure out after picking apart the script and running over and over until I had no errors is that I'm creating a conflict with the data frame layers. My script is intended to shuffle 14 different data frames around a "Main Map" data frame layout for printing, and each data frame has an identical set of layers, the only difference is the scale and extent of each frame. What I'm trying to achieve is not having all the layers turned on at the start, so I used the code below:
allLayers = arcpy.mapping.ListLayers(mxd) for lyr in allLayers: if lyr.visible == True: lyr.visible = False else: pass del lyr, allLayers arcpy.RefreshActiveView() I do that step again at the end of the script to turn all the layers on by switch the True and False statement. That will run fine once, but run it twice and it fails.
Now that I've narrowed down my issue to this, how can I turn on and off the layers in 14 different data frames without causing conflict with the way I've referenced my other layers in the MainMap data frame?
أكثر...
My environment: ArcGIS 10.1 SP1, Win7 SP1, and the source file is on an Oracle(11g) SDE server accessed through database authentication.
Error message: "ARCPY ERROR 000582: Error occurred during execution."
This is the code I start with:
arcpy.env.workspace = "IN_MEMORY" mxd = arcpy.mapping.MapDocument("CURRENT") mm = arcpy.mapping.ListDataFrames(mxd, "MainMap")[0] qsLayer = arcpy.mapping.ListLayers(mxd, "EAS QS Grid", mm)[0] qs = "10-28" qry = "TILE = '%s'" % (qs) arcpy.SelectLayerByAttribute_management(qsLayer, "NEW_SELECTION", qry) When I look at the GP Results window, I see that the Output Layer Name is GPL0, not "EAS QS Grid". I've tried using the Select by Attribute tool from the toolbox and after running the results show the correct layer name. So what is happening when using arcpy to have the layer name changed to GPL0? This script also performs similar selections to other layers in the MXD, five SelectLayerByAttribute and two SelectLayerByLocation. All the Output names always show as GPL0. This seems like it could cause Arc to trip over itself if GPL0 is somehow locked in memory after the script runs, but I can't find any documentation about the behavior of Geoprocessing Layers(GPL?) or why a Select Layer By Attribute tool would even need to make a new Output Layer Name.
Things I've tried: - 'del qsLayer' at the end of the script - Not using IN_MEMORY workspace - Deleting the IN_MEMORY workspace at the end of the script - Setting the overwriteOutput to True at the top of the script - Using qsLayer.dataSource as the parameter for SelectByAttribute - Yes, GP Background Processing is not turned on
So far none of that seems to change the outcome.
The error message does not provide any useful feedback, so I want to ask the web if anyone has had problems with this kind of issue. Right now the only way I can use the script is to run it once, close ArcMap and reopen, then it's good to go one more time.
Thank you for any suggestions.
EDIT:
In my excitement at getting the script working I rushed to post my answer, and of course it failed anyway. Using arcpy.mapping.Layer was not the solution...
What I did figure out after picking apart the script and running over and over until I had no errors is that I'm creating a conflict with the data frame layers. My script is intended to shuffle 14 different data frames around a "Main Map" data frame layout for printing, and each data frame has an identical set of layers, the only difference is the scale and extent of each frame. What I'm trying to achieve is not having all the layers turned on at the start, so I used the code below:
allLayers = arcpy.mapping.ListLayers(mxd) for lyr in allLayers: if lyr.visible == True: lyr.visible = False else: pass del lyr, allLayers arcpy.RefreshActiveView() I do that step again at the end of the script to turn all the layers on by switch the True and False statement. That will run fine once, but run it twice and it fails.
Now that I've narrowed down my issue to this, how can I turn on and off the layers in 14 different data frames without causing conflict with the way I've referenced my other layers in the MainMap data frame?
أكثر...