I try to export a bunch of layers with the arcpy.mapping.ListLayoutElements function. Each layer should be exported as a single .jpg-map and each with a legend. Because not all layers have the same number of classes the legends have different sizes. To approach this issue, i wanted to use the following piece of code:
Here I was obviously inspired by the 2nd legend example on the official help page: http://resources.arcgis.com/en/help/...00000041000000
But i get the followong error message:
Traceback (most recent call last):
File "H:\Desktop\Masterarbeit\Python scripts\map_production_test 6.py", line 98, in <module>
while legend.isOverflowing == True:
AttributeError: 'LegendElement' object has no attribute 'isOverflowing'
>>>
Does anyone see, what I do wrong? I use ArcGIS 10.1... (even if it seems i've got an older version)...
To better convey to the relationship i post here my whole code. But tob e honest, i dont think that is necessary (and i has still several constructions sites..)... Even the print messages or some comments are sometimes in german: Sorry for that...
Code:
#LEGENDSTUFF:
print "legende generieren"
legend=arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT","Legend")[0]
legend.autoAdd = True
while legend.isOverflowing == True:
legend.elementHeight = legend.elementHeight +0.1
But i get the followong error message:
Traceback (most recent call last):
File "H:\Desktop\Masterarbeit\Python scripts\map_production_test 6.py", line 98, in <module>
while legend.isOverflowing == True:
AttributeError: 'LegendElement' object has no attribute 'isOverflowing'
>>>
Does anyone see, what I do wrong? I use ArcGIS 10.1... (even if it seems i've got an older version)...
To better convey to the relationship i post here my whole code. But tob e honest, i dont think that is necessary (and i has still several constructions sites..)... Even the print messages or some comments are sometimes in german: Sorry for that...
Code:
print "START map production"
print "arcpy and os importieren"
import arcpy
import os, sys, string
print "done arcpy and os and so on import"
#Workspacedefinitionen
workspace = r"H:\Desktop\Masterarbeit\test\RESULTATE"
arcpy.env.workspace = workspace
out_dir=r"H:\\Desktop\\maps\\e95ci\\"
counter = 1
name_var= "test__" + str(counter) + ".jpg"
outfile = os.path.join (out_dir, name_var)
#Specifies layer file with the symbology you want to apply to the other raster layers
sourceLayer1 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_95CI.lyr")
sourceLayer2 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_95CI_diff.lyr")
sourceLayer3 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_mean.lyr")
sourceLayer4 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\E_mean_diff.lyr")
sourceLayer5 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_depos_diff.lyr")
sourceLayer6 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_deposited.lyr")
sourceLayer7 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_pass_diff.lyr")
sourceLayer8 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_passages.lyr")
sourceLayer9 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Nr_tree_impacts.lyr")
sourceLayer10 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_95CI.lyr")
sourceLayer11 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_95CI_diff.lyr")
sourceLayer12 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_mean.lyr")
sourceLayer13 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Ph_mean_diff.lyr")
sourceLayer14 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Reach Probability [%].lyr")
sourceLayer15 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Reach_prob_diff.lyr")
sourceLayer16 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Rvol.lyr")
sourceLayer17 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Rvol_diff.lyr")
sourceLayer18 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Tree_impact_heights [m].lyr")
sourceLayer19 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Vmax.lyr")
sourceLayer20 = arcpy.mapping.Layer(r"H:\Desktop\Masterarbeit\test\RESULTATE\lyr_files_neu\Vmax_diff.lyr")
#Specifies Functions
def shortname(name):
name = name.rpartition("\\SH_")[2]
print name
name = name.replace(".","_")
name = name.replace("\\","_")
name = name.replace("_mdb_","_")
print "this is the used name: "+ name
return name
print "specify mxd"
#Specifies mxd
mxd = arcpy.mapping.MapDocument(r"H:\\Desktop\\Masterarbeit\\test\\test1.mxd")
print "done mxd spezifizieren"
print "specify dataframe"
#Specifies dataframe of interest
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
print "done specify df "
#Turns all layers off
print "turn visibility of all layers off"
for lyr in arcpy.mapping.ListLayers(mxd,"",df):
lyr.visible = False
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
print "visibility = False"
#Turns backgroundlayers on
print "turn backgroundlayer on: Visibility = true"
for lyr in arcpy.mapping.ListLayers(mxd, "hill*", df):
print lyr
lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
for lyr in arcpy.mapping.ListLayers(mxd, "up*", df):
print lyr
lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
for lyr in arcpy.mapping.ListLayers(mxd, "swiss*", df):
print lyr
lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
print "background = visible"
#lEGENDSTUFF:
print "legende generieren"
legend=arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT","Legend")[0]
legend.autoAdd = True
while legend.isOverflowing == True:
legend.elementHeight = legend.elementHeight +0.1
print "start withE_95CI"
#Loops through the layers given in the quotas (eg. beginning with E_95CI) and creates a list of Layers:
for lyr in arcpy.mapping.ListLayers(mxd, "E_95CI", df):
print lyr
if lyr.isRasterLayer:
arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer1)
#For Each: Turn it on:
lyr.visible = True
print " visible: on"
name =lyr.dataSource
name=shortname(name)
#name = name.rpartition("\\SH_")[2]
#print name
#name = name.replace(".","_")
#name = name.replace("\\","_")
#name = name.replace("_mdb_","_")
print "this is the used name: "+ name
arcpy.RefreshTOC()
print " TOC refreshed"
arcpy.RefreshActiveView()
print " Active View refreshed"
print "exporting .JPG"
mxd.activeView = "PAGE_LAYOUT"
arcpy.mapping.ExportToJPEG(mxd, r"H:\\Desktop\\maps\\e95ci\\SH_" + name+ ".jpg")
lyr.visible = False
#legend=arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT","")[0]
#legend=arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
#legend.autoAdd = True #newlz added lazers will be automaticallz added to the legend
#legend.elementHeight=4
#legend.elementWidth=4
for lyr in arcpy.mapping.ListLayers(mxd, "N*", df):
print lyr
if lyr.isRasterLayer:
arcpy.ApplySymbologyFromLayer_management(lyr, sourceLayer6)
#For Each: Turn it on:
lyr.visible = True
print " visible: on"
print " this is the Source"
name =lyr.dataSource
name=shortname(name)
print "this is the used name: "+ name
arcpy.RefreshTOC()
print " TOC refreshed"
arcpy.RefreshActiveView()
print " Active View refreshed"
print "exporting .JPG"
mxd.activeView = "PAGE_LAYOUT"
arcpy.mapping.ExportToJPEG(mxd, r"H:\\Desktop\\maps\\nr_deposited\\SH_" + name + ".jpg")
lyr.visible = False
#
#print legend
#legend=arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
#legend.autoAdd = True #newlz added lazers will be automaticallz added to the legend
#legend.elementHeight=4
#legend.elementWidth=4
print "done_"
#print "start reach_probability"
##Loops through the layers given in the quotas (eg. reach_probability) and creates a list of Layers:
#for lyr in arcpy.mapping.ListLayers(mxd, "reach_probability", df):
#For Each: Turn it on:
# lyr.visible= True
# arcpy.RefreshTOC()
# arcpy.RefreshActiveView()
# arcpy.mapping.ExportToJPEG(mxd, r"H:\\Desktop\\map_export\\" + outfile, df)
#print"done_2"
#Saves mxd
print "__________________________________________"
print "_xxx____xx___xx___x__xxxxx________________"
print "_x__x__x__x__x_x__x__x____________________"
print "_x__x__x__x__x__x_x__xxx__________________"
print "_xxx____xx___x___xx__xxxxx________________"
print "__________________________________________"
#Deletes the reference to the mxd from memory (not the actual mxd file)
del mxd