frankv3

having trouble renaming and updating layers

Discussion created by frankv3 on Sep 14, 2011
Latest reply on Oct 13, 2016 by GNOMELLISA
in the following script all processes run fin until it gets to the remove, renmame, and update layers commands, however if the commands are put into a seperate script and run everything goes fine
it would be best if it could run as one script, any help would be appreciated:

the script where it fails:

## Import standard modules
import sys, os

## Set the necessary product code
import arcinfo

## Import arcpy module and environment module
import arcpy
import arcpy.mapping
from arcpy import env

## Set the output workspace
arcpy.env.workspace = r"G:\AGIS\2011\Special_Projects\Dist_1\Dieldrin_Well_Sampling\DieldrinParcelData.gdb\ParcelData"

## Overwrite pre-existing files
arcpy.env.overwriteOutput = True

## Sets the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")

## Sets the Dataframe
DF = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]

Date = arcpy.GetParameterAsText(0)
NewSampleData = arcpy.GetParameterAsText(1)
PastSample = arcpy.GetParameterAsText(2)


NameStart = NewSampleData.rfind("\\") + 1
AddNewLayer = str(NewSampleData[NameStart:])


arcpy.MakeFeatureLayer_management(NewSampleData, AddNewLayer)
arcpy.SetParameterAsText(3,AddNewLayer)
arcpy.RefreshTOC()


Selection = "\"SAMPLE\" <> '' AND \"SAMPLE\" <> ' ' AND \"SAMPLE\" <> 'C' "

arcpy.SelectLayerByAttribute_management(AddNewLayer, "NEW_SELECTION", Selection)


SelectedSamples = arcpy.MakeFeatureLayer_management(AddNewLayer, "SelectedSamples")

arcpy.SetParameterAsText(4, SelectedSamples)

arcpy.RefreshTOC()


arcpy.SelectLayerByLocation_management("Volusia_Parcels", "INTERSECT", "SelectedSamples")

SelectedParcels = arcpy.MakeFeatureLayer_management("Volusia_Parcels", "SelectedParcels")

arcpy.SetParameterAsText(5, SelectedParcels)

arcpy.RefreshTOC()

OutParcels = r"Parcels_%s"%(Date)

arcpy.SpatialJoin_analysis("SelectedParcels", "SelectedSamples", OutParcels, "JOIN_ONE_TO_ONE", "KEEP_ALL")

NewParcels = arcpy.MakeFeatureLayer_management(OutParcels, "NewParcelLayer")
arcpy.SetParameterAsText(6, NewParcels)

JoinLayer = arcpy.mapping.ListLayers(IMXD, PastSample,DF)[0]

arcpy.AddJoin_management(NewParcels, "ALTKEY", JoinLayer, "ALTKEY", "KEEP_ALL")

ChangeSelection = r"%s.SAMPLE <> %s.SAMPLE"%(OutParcels, PastSample)

arcpy.SelectLayerByAttribute_management(NewParcels, "NEW_SELECTION", ChangeSelection)

arcpy.RemoveJoin_management(NewParcels, PastSample)

OutSample = r"Parcels_Changed_%s"%(Date)

arcpy.CopyFeatures_management(NewParcels, OutSample)

NewChanges = arcpy.MakeFeatureLayer_management(OutSample, "NewParcelChanges")
arcpy.SetParameterAsText(7, NewChanges)



########## These changes do not take effect ######################

arcpy.AddMessage("Removing Layers")

for ILayer in arcpy.mapping.ListLayers(IMXD):
   if ILayer.name == "SelectedSamples":
       arcpy.mapping.RemoveLayer(DF, ILayer)
   if ILayer.name == "SelectedParcels":
       arcpy.mapping.RemoveLayer(DF, ILayer)
       
arcpy.AddMessage("Setting Varibles")

PLayer = r"Parcels_%s"%(Date)
CLayer = r"Parcels_Changed_%s"%(Date)


arcpy.AddMessage("Renaming Layers")

for ILayer in arcpy.mapping.ListLayers(IMXD):
  if ILayer.name == "NewParcelLayer":
    ILayer.name = PLayer
  elif ILayer.name == "NewParcelChanges":
    ILayer.name = CLayer
    
arcpy.RefreshTOC()
IMXD.save()

arcpy.AddMessage("Setting Layer Symbology")

try: 
  SymbolLayer1 = arcpy.mapping.Layer(r"G:\AGIS\2011\Special_Projects\Dist_1\Dieldrin_Well_Sampling\DieldrinWellSamples.lyr")
  NewLayer1 = arcpy.mapping.ListLayers(IMXD, PLayer,DF)[0]
  arcpy.mapping.UpdateLayer(DF, NewLayer1, SymbolLayer1)

  SymbolLayer2 = arcpy.mapping.Layer(r"G:\AGIS\2011\Special_Projects\Dist_1\Dieldrin_Well_Sampling\Parcels_Changed.lyr")
  NewLayer2 = arcpy.mapping.ListLayers(IMXD, CLayer,DF)[0]
  arcpy.mapping.UpdateLayer(DF, NewLayer2, SymbolLayer2)
except:
  arcpy.GetMessages()
   
############### All STOP #########################   
   
## Refresh map
arcpy.RefreshTOC()
arcpy.RefreshActiveView
IMXD.save()


this script works if run after the previous one:

## Import standard modules
import sys, os

## Set the necessary product code
import arcinfo

## Import arcpy module and environment module
import arcpy
import arcpy.mapping
from arcpy import env

## Set the output workspace
arcpy.env.workspace = r"G:\AGIS\2011\Special_Projects\Dist_1\Dieldrin_Well_Sampling\DieldrinParcelData.gdb\ParcelData"

## Overwrite pre-existing files
arcpy.env.overwriteOutput = True

## Sets the MXD file
IMXD = arcpy.mapping.MapDocument("CURRENT")

## Sets the Dataframe
DF = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]

Date = arcpy.GetParameterAsText(0)

arcpy.AddMessage("Removing Layers")

for ILayer in arcpy.mapping.ListLayers(IMXD):
   if ILayer.name == "SelectedSamples":
       arcpy.mapping.RemoveLayer(DF, ILayer)
   if ILayer.name == "SelectedParcels":
       arcpy.mapping.RemoveLayer(DF, ILayer)
       
arcpy.AddMessage("Setting Varibles")
PLayer = r"Parcels_%s"%(Date)
CLayer = r"Parcels_Changed_%s"%(Date)


arcpy.AddMessage("Renaming Layers")
for ILayer in arcpy.mapping.ListLayers(IMXD):
  if ILayer.name == "NewParcelLayer":
    ILayer.name = PLayer
  elif ILayer.name == "NewParcelChanges":
    ILayer.name = CLayer
    
arcpy.RefreshTOC()

arcpy.AddMessage("Setting Layer Symbology")

SymbolLayer1 = arcpy.mapping.Layer(r"G:\AGIS\2011\Special_Projects\Dist_1\Dieldrin_Well_Sampling\DieldrinWellSamples.lyr")
NewLayer1 = arcpy.mapping.ListLayers(IMXD, PLayer,DF)[0]
arcpy.mapping.UpdateLayer(DF, NewLayer1, SymbolLayer1)

SymbolLayer2 = arcpy.mapping.Layer(r"G:\AGIS\2011\Special_Projects\Dist_1\Dieldrin_Well_Sampling\Parcels_Changed.lyr")
NewLayer2 = arcpy.mapping.ListLayers(IMXD, CLayer,DF)[0]
arcpy.mapping.UpdateLayer(DF, NewLayer2, SymbolLayer2)

   
## Refresh map
arcpy.RefreshTOC()
arcpy.RefreshActiveView
IMXD.save()

Outcomes