luapknarf

legend wont adjust, mxd wont save from script

Discussion created by luapknarf on Jan 3, 2012
Latest reply on Jan 11, 2012 by jbarrette-esristaff
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

Attachments

Outcomes