Select to view content in your preferred language

legend wont adjust, mxd wont save from script

604
6
01-03-2012 01:30 PM
PaulFrank
Emerging Contributor
Hello - this is another situation where I can perform all of the tasks from the python window, but not from a script.  The legend is supposed to turn into 2 columns at the end, but remains as 1.  Then, the .mxd saves, but the "FLUM_Select layer disappears, as does its parent layer, which is expected.  Ive attached the resulting mxd files.

I would much prefer to run this entirely from python, without having to go to Arcmap first and run it, but that has never completely worked either. 

Thanks for your help.

Paul Frank
City of Austin

# ------------------------------------------------------------------------------
# Name:         make_plan_amendment_map.py
# Purpose:      Make and print a plan amendment map.  The user
#               enters a case number, and the script would zoom the map to that
#               case and fill out the title and other map elements based on the
#               case information.
# Created on:   December 20, 2011
# By:           Paul Frank
# ------------------------------------------------------------------------------

# Import system modules
import arcpy
from arcpy import env
import os
env.overwriteOutput = True

#declare setup variables
inworkspace = r"G:\NEIGHBOR PLAN\ArcView\Projects\Templates\\" #declare workspace variable
incasenum = "NPA-2010-0012.01" #case number to center map
qyrstring = "\"CASENUM\" = " + "'" + incasenum + "'" #string to create definition query
env.workspace = inworkspace #set workspace
mxd = arcpy.mapping.MapDocument("CURRENT") #set mxd to current map

#create definition query for case and zoom to that polygon
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] #set dataframe object
flumlyr = arcpy.mapping.ListLayers(mxd, "FLUM", df)[0] #set layer object for selection target
zcaselyr = arcpy.mapping.ListLayers(mxd, "ZoningCases", df)[0] #set layer object to select from
zcaselyr.definitionQuery = qyrstring #create definition query for centering map
df.extent = zcaselyr.getSelectedExtent(False)
df.scale = 2000

#select nearby features to draw in legend
arcpy.SelectLayerByLocation_management(flumlyr, "WITHIN_A_DISTANCE", zcaselyr, "2000 Feet", "NEW_SELECTION") #select nearby features
outputlyr = "FLUM_Select" #declare variable to add selected features to display
arcpy.MakeFeatureLayer_management(flumlyr, outputlyr) #make feature layer from selection
arcpy.SetParameterAsText(0, outputlyr) #add to display.  Also set up output in add script wizard

#adjust symbology and legend
arcpy.ApplySymbologyFromLayer_management("FLUM_Select", flumlyr) #use symbology from parent layer
arcpy.mapping.RemoveLayer(df, flumlyr) #remove parent layer
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0] #set legend object
legend.adjustColumnCount(2) #adjust legend object

#refresh display and save mxd
#arcpy.RefreshActiveView() #refresh view
mxd.saveACopy(r"G:\NEIGHBOR PLAN\ArcView\Projects\Templates\test2.mxd") #save to another file
del mxd
Tags (2)
0 Kudos
6 Replies
AndrewChapkowski
Esri Regular Contributor
If you are running this script outside of an ArcMap session, it will fail due to the fact that "CURRENT" only works when inside arcmap. 

Try changing the following:
mxd = arcpy.mapping.MapDocument("CURRENT") #set mxd to current map

to the path of the arcmap document:
mxd = arcpy.mapping.MapDocument(r"c:\temp\mymapdoc.mxd") #set mxd to current map


Hope this helps.
0 Kudos
PaulFrank
Emerging Contributor
If you are running this script outside of an ArcMap session, it will fail due to the fact that "CURRENT" only works when inside arcmap. 

Try changing the following:
mxd = arcpy.mapping.MapDocument("CURRENT") #set mxd to current map

to the path of the arcmap document:
mxd = arcpy.mapping.MapDocument(r"c:\temp\mymapdoc.mxd") #set mxd to current map


Hope this helps.


Chris,

Thanks for the reply.  You may have given me some ideas, but I understand that "CURRENT" only works in an active Arcmap session, which is in fact where I run this script, via Arctoolbox after adding the script there.  Again, everything works except the saveACopy and legend.adjustColumnCount(2) code.  But id appreciate additional suggestions.

Thanks
0 Kudos
JeffBarrette
Esri Regular Contributor
I tested the following code using your MXD.  I tried loading the code into the Python window and running it as a script tool from within ArcMap and it works just fine.

mxd = arcpy.mapping.MapDocument("CURRENT")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")


I also ran this as a stand-alone script using the full MXD path and that worked too.  All resulting MXDs opened as expected.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\correctly_saved.mxd")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")


Can you try the tests above?  If you can't repro with these simple scipts, then the issue is somewhere else in your code.

Jeff
0 Kudos
PaulFrank
Emerging Contributor
I tested the following code using your MXD.  I tried loading the code into the Python window and running it as a script tool from within ArcMap and it works just fine.

mxd = arcpy.mapping.MapDocument("CURRENT")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")


I also ran this as a stand-alone script using the full MXD path and that worked too.  All resulting MXDs opened as expected.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\correctly_saved.mxd")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")


Can you try the tests above?  If you can't repro with these simple scipts, then the issue is somewhere else in your code.

Jeff

Jeff,

Progress.  Yours worked.  Now mine works when loaded to the python window!  But it doesnt work when I run it from the Script tool.  "Always run in foreground" and "Run python script in process" are checked.  I agree its something above this code, but not finding where.  I have a feeling its how I am trying to select the layer by location, then add the result to the TOC.  I had to use an ESRI workaround to accomplish that - namely creating an output in the script tool.
0 Kudos
JeffBarrette
Esri Regular Contributor
If you send me a map package of an isolated area along with your script, I take a look at it.  Send to jbarrette@esri.com.

Jeff
0 Kudos
JeffBarrette
Esri Regular Contributor
I think we have a fix.

arcpy.MakeFeatureLayer works well in CURRENT because it automatically adds the result to the current MXD.   If you use a script tool or run as a stand alone script you need to modify your code as follows to make a layer object from MakeFeatureLayer and manage it accordingly:

selLyr = arcpy.MakeFeatureLayer_management(flumlyr, "FLUM_Select").getOutput(0)  #added variable at beginning and .getOutput at end to return a real layer object

arcpy.ApplySymbologyFromLayer_management(selLyr, flumlyr) #changed the first parameter

arcpy.mapping.AddLayer(df, selLyr) #new line - this added a real layer object (and also to the legend if the legend is set up to auto add new layers)

Jeff
0 Kudos