Stop layers used for analysis from being automatically added to map

875
4
Jump to solution
11-26-2012 08:25 AM
RachelAlbritton1
New Contributor III
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)

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
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MattSayler__Work_
New Contributor III
Try changing it in the Geoprocessing Options. Maybe the application settings are overriding the script.

Is this happening just for layers you're running a selection on? Do you have multiple dataframes or just one?

Sounds like this might be similar to something I had happen when using SelectLayerByAttribute_managment. Here's my post for that:
http://forums.arcgis.com/threads/70974-Layer-getting-copied-erroneously

Here's what I had to do to get the open map to update:

  • Use "CURRENT" for the mxd

  • Make sure I was on the "List by Drawing Order" tab in the TOC

  • Make sure the script activated the dataframe the layer was in before running the selection (shouldn't be an issue if you only have one data frame)

If the layers you're running the selections on aren't in the map initially, it might be difficult to do. It seemed like if it didn't see a layer with the same TOC name in the active data frame, it would bring a copy into that dataframe.

I didn't think to try changing the Geoprocessing Options at the time. I ultimately only cared about my output pdf's being correct and didn't dig any deeper once I figured out they were coming out fine.

View solution in original post

0 Kudos
4 Replies
markdenil
Occasional Contributor III
arcpy.env.addOutputsToMap = False

do the intermediate stuff here
just before you make the final output

arcpy.env.addOutputsToMap = True

and that final output appears on the map
0 Kudos
RachelAlbritton1
New Contributor III
Thank you for the response, unfortunately its still adding the layers to the map.

I added the line of code before the first line of the code previously posted: arcpy.env.addOutputsToMap = False

Map portion of script is here

then before adding the selected files to the map my code now looks like this


arcpy.env.addOutputsToMap = True
add2map(selectedCounties)
add2map(selectedPCA)

#Save a COPY of the map & export to jpg format
mxd.saveACopy(outputWorkspace+os.sep+"PCA_COPY.mxd")



Even if I completely remove the line of code:

arcpy.env.addOutputsToMap = True

The full files that the selected files are being pulled from are still being added to the map.
0 Kudos
MattSayler__Work_
New Contributor III
Try changing it in the Geoprocessing Options. Maybe the application settings are overriding the script.

Is this happening just for layers you're running a selection on? Do you have multiple dataframes or just one?

Sounds like this might be similar to something I had happen when using SelectLayerByAttribute_managment. Here's my post for that:
http://forums.arcgis.com/threads/70974-Layer-getting-copied-erroneously

Here's what I had to do to get the open map to update:

  • Use "CURRENT" for the mxd

  • Make sure I was on the "List by Drawing Order" tab in the TOC

  • Make sure the script activated the dataframe the layer was in before running the selection (shouldn't be an issue if you only have one data frame)

If the layers you're running the selections on aren't in the map initially, it might be difficult to do. It seemed like if it didn't see a layer with the same TOC name in the active data frame, it would bring a copy into that dataframe.

I didn't think to try changing the Geoprocessing Options at the time. I ultimately only cared about my output pdf's being correct and didn't dig any deeper once I figured out they were coming out fine.
0 Kudos
RachelAlbritton1
New Contributor III
Got it! I had to go to Geoprocessing Options and uncheck "Add results of geoprocessing results to display." A little odd since the layers that were appearing weren't the results, but it works.

Thanks!
0 Kudos