dissappearing layers from arcpy.mapping.MapDocument() Layer set.

561
2
01-09-2014 07:22 AM
PatrickJurgens
New Contributor III
I am working in version 10.1.
I have several layers stored in a file GDB\Feature Dataset.
All layers are added to an MXD and organized into Group Layers.
In ArcMap midway through a session Layers that are in the TOC no longer appear as options to input into ArcToobox tools.
The layers still appear in the TOC, I can still view them in the map and view their Attribute tables. However when I try to Geoprocess them with a tool that requires a Layer for input, they "don't exist".
If I close the map and re-open, then they "exist" again.

The dissapperance occurs after using arcpy.SelectLayerByLocation_management() tool in the Python immediate window.

Ultimately my goal is to use a Python AddIn to loop through several Layers in the current MXD Selecting by Location, reporting details about the selection, deselecting all, and then moving on to the next Layer on the list. (basically a push button option to spatially select multiple layers at once and get info about them in a summary listing).
The problem (as stated before) is that after the first Layer on the list completes successfully many of the subsequent layers on the work list have 'dissappeared' from the map document and are no longer accepted as valid input to the arcpy.SelectLayerByLocation_management() tool.
Restarting ArcMap in order to get my layers back is simply not an option because it would break the process and completely defeat the efficency benefits of using an AddIn.

Here is some code from the python immediate window (I added some comments '#...' after the fact:
>>> mxd = arcpy.mapping.MapDocument('current')
df = arcpy.mapping.ListDataFrames(mxd)[0]
diflyrs = arcpy.mapping.ListLayers(mxd,'*_differences',df)
ufc = diflyrs[1].name
>>> for lyr in diflyrs: print lyr.longName
CDP\cdp_differences
BG\bg_differences
COUSUB\ccd_differences
COUSUB\mcd_differences
PLACE\place_differences
CONCITY\concity_differences
UGA\uga_differences
ELSD\elsd_differences
SCSD\scsd_differences
UNSD\unsd_differences
SLDL\sldl_differences
SLDU\sldu_differences
CD\cd_differences
AIA\aial_differences
TBG\tbg_differences
TRIBSUB\aitsl_differences
ANRC\anrc_differences
SUBMCD\submcd_differences
#all layers in this list were found by arcpy.mapping.ListLayers(), so they were recognized as valid by arcObjects
>>> for lyr in diflyrs: 
    if not lyr.isBroken and lyr.name != ufc and int(str(arcpy.GetCount_management(lyr))):
        x = []
        arcpy.SelectLayerByLocation_management(lyr,"INTERSECT",r"BG\bg_differences",'#',"NEW_SELECTION")
        for row in arcpy.SearchCursor(lyr):
            x.append(row.OBJECTID)
        arcpy.SelectLayerByAttribute_management(lyr,"CLEAR_SELECTION")
        if x:
            print lyr.name, x
ccd_differences [1, 2]
#first layer on the list runs as expected.
Runtime error  Traceback (most recent call last):   File "<string>", line 4, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6559, in SelectLayerByLocation     raise e ExecuteError: ERROR 000368: Invalid input data.
#second layer on the list fails because it is not a layer?
>>> print lyr
PLACE\place_differences
#above is the long name for the layer that failed
#by the way, if I restart the MXD and alter the diflyrs list so that PLACE\place_differences runs first, it works, 
#but then the script fails when it reaches COUSUB\ccd_differences
#in case you wondered, layers CDP\cdp_differences and BG\bg_differences both get skiped by the initial if statement


After running the code above I took this screen shot attached which shows the PLACE\place_differences layer in the TOC along side an ArcToolbox tool which does not include it as a valid input. Before running the Python Script I used this exact tool to spatially select the PLACE\place_differences layer without any issue. Also even after 'breaking' the Layer I am still able to spatially select it by using the Select menu->Select by Location wizard.
[ATTACH=CONFIG]30375[/ATTACH]
I am posting this in the Geoprocessing section as well since applys there too.
Tags (2)
0 Kudos
2 Replies
PatrickJurgens
New Contributor III
I am replying to my own post...
I found a work around, in case others are dealing with this same problem.
for lyr in diflyrs:
...     if not lyr.isBroken and lyr.name != ufc and int(str(arcpy.GetCount_management(lyr))):
...         x=[]
...         templyr = arcpy.MakeFeatureLayer_management(lyr.dataSource,"in_memory\\templyr")
...         arcpy.SelectLayerByLocation_management(templyr,"INTERSECT",r"BG\bg_differences",'#',"NEW_SELECTION")
...         for row in arcpy.SearchCursor(templyr):
...             x.append(row.OBJECTID)
...         arcpy.Delete_management("in_memory\\templyr")
...         if x:
...             print lyr.name, x

Instead of working directly on the layers in my MXD I am creating a temporary, in_memory layer for each iteration and when I am done I delete it. I use the .dataSource property from the original layer as the input for the MakeFeatureLayer_management() tool.

Since this is a lame work around and does nothing to address the true issue I am leaving the post unsolved.
If anyone can reply explaining what I was doing wrong, or how to fix it I would still appreciated it very much.

This work around would not be very helpful to anyone who is wants their selection to remain selected in the MXD \dataframe because it would result in bunches of duplicate layers in the TOC.

On another note, The Script, as presented above, is to slow for my purposes. it takes an average of about 16 Seconds per layer. Since it is running against 17 layers that's almost 5 minutes to run. Way to slow to use for a simple push button AddIn tool, especially since I could probably do the work manually through point/click in about the same amount of time.
Does anyone have ideas for how to speed it up?
0 Kudos
JeffBarrette
Esri Regular Contributor
Is it possible for you to send me a simplified map package with your difference layers in it? I'll try to see what is going on.  Send to jbarrette@esri.com.

Jeff
0 Kudos