jazmateta

AddLayer appears to work but not really

Discussion created by jazmateta on Aug 12, 2011
Latest reply on Aug 12, 2011 by jazmateta
Have a script that tests if a layer is in the data frame, if so, remove it.  Works.

Then I geocode a text file that is named the same as the layer previously tested.  Reason for the testing and removing is because you can't overwrite the geocoded file in the geodatabase (file) if it is in the mxd - hence the removal of the layer from the data frame.  Geocode works.

Then I add the layer back in to the data frame using a layer file that has been preset.  Works.

Visably the layer is there, has the proper symbology, can even open the attribute table. 

However, when I run arcpy.Exists("Layer") it returns False.  This is causing the remainder of my script to fail because it doesn't realize that the data is there.  Can't get a count on the ungeocoded records or anything.  If I rerun the script without exiting ArcMap, the script returns that the layer doesn't exist (this is an if..else statement testing for the existance of the layer). 

You don't have to save the mxd first do you?  There are times where saving the mxd is not the best option.

code snippet:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
sourcelayerfile = arcpy.mapping.Layer(r"c:\tcas\Crimes.lyr")
layer2manipulate = arcpy.mapping.ListLayers(mxd,"Crimes",df)[0]


arcpy.mapping.AddLayer(df,sourcelayerfile)

Running:
arcpy.Exists(layer2manipulate)

Returns:
False

Outcomes