I have written a python script that selects a subset of areas based on location, then adds the selected feature classes to a map. The copy of the map I save to a user specified output location is correct, however, I would like to have the map document that is open and running the script also be correct. Currently the full layers in which the selections are being performed on are automatically being added to the map as well as the final selected files. I've tried adding code to remove the full layers with no success. Any suggestions? (NOTE: This is a small portion of a much larger script)
Code:
import arcpy, os
def add2map (filename):
"""adds a layer to an existing map document that is located in the tooldata folder"""
addLayer = arcpy.mapping.Layer(filename)
arcpy.mapping.AddLayer(df,addLayer)
#Add Selected Feature Classes to Map
arcpy.SetProgressorLabel("Creating Map")
printMessage("Creating map of selected counties and CA's in "+arcpy.env.workspace+"...")
mapName = os.path.dirname(arcpy.env.workspace)+"/PCAmap.mxd"
mxd = arcpy.mapping.MapDocument(mapName)
##mxd = arcpy.mapping.MapDocument("CURRENT")- tried this but it adds all layers to map not just the 2 I specify
dfs = arcpy.mapping.ListDataFrames(mxd)
df = dfs[0]
#Convert selected conservation areas and counties to layer file
#NOTE: Can not use OutputFL and pcaFL because it will map the all features with selected areas highlighted.
selectedCounties = outName(OutputFC,"_Layer")
selectedPCA = outName(pca_selected,"_Layer")
arcpy.MakeFeatureLayer_management(OutputFC, selectedCounties)
arcpy.MakeFeatureLayer_management(pca_selected,selectedPCA)
#Apply symbology
CountySymLayer = arcpy.env.workspace+"/Layers/WNC_Counties.lyr"
PCAsymLayer = arcpy.env.workspace+"/Layers/PCA.lyr"
arcpy.ApplySymbologyFromLayer_management(selectedPCA,PCAsymLayer)
arcpy.ApplySymbologyFromLayer_management(selectedCounties,CountySymLayer)
arcpy.SetParameterAsText(1,OutputFL)
arcpy.SetParameterAsText(2,pcaFL)
add2map(selectedCounties)
add2map(selectedPCA)
#Save a COPY of the map & export to jpg format
mxd.saveACopy(outputWorkspace+os.sep+"PCA_COPY.mxd")
printMessage("Map document of selected counties and selected PCA's saved to "+outputWorkspace)
JPG = outputWorkspace+"/PCAmap_COPY.jpg"
arcpy.mapping.ExportToJPEG(mxd,JPG)
printMessage("JPEG of map docment saved to "+outputWorkspace)
##Remove Layers - tried removing the pre-analysis layers after mapping the selected layers but this doesn't work.
##lyrs = arcpy.mapping.ListLayers(mxd,"",df)
##lyr1 = lyrs[2]
##lyr2 = lyrs[3]
##arcpy.mapping.RemoveLayer(df,lyr1)
##arcpy.mapping.RemoveLayer(df,lyr2)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd