[GIS] Saving and reloading legend settings using ArcPy with ArcMap

arcgis-10.0arcmaparcpylegend

I know how to change settings of a legend individually by arcpy (like: Patch, Title, Items, Columns, Frame, Element Size & Position, etc.), but now I would like to use the exact legend settings another time and load them using ArcPy.

Is there a simple way to save/load these legend settings all together in/from a text file?

Best Answer

So it looks like you know how to get at the elements you need, and they are all read/write. I've done something similar where I read properties from a raster and write them out to a XML configuration file that in turn gets read by another Python script and the raster parameters get fed into a 3D modeling package. xml.etree.Elementree and xml.dom.minidom are the key modules (at least for my version).

First, read your legend elements and get the values into variables, then write them out to XML. Below, <..>_element each represent a element that I write out, where f is a file named Config.xml:

import xml.etree.ElementTree
import re

viesore_element = xml.etree.ElementTree.Element("VIESORE.Configuration")
comment = xml.etree.ElementTree.Comment("Auto-generated at " + str(write_time))
viesore_element.append(comment)
project_element = xml.etree.ElementTree.SubElement(viesore_element, 
                                                       "ProjectDirectory")
project_element.text = proj_dir
scene_element = xml.etree.ElementTree.SubElement(viesore_element, 
                                                     "Scene")
# Write some more values out

# Get rid of extra line returns in prettified XML
uglyXml = prettify(viesore_element)
text_re = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL)    
prettyXml = text_re.sub('>\g<1></', uglyXml)

f = open(os.path.join(proj_dir, filename), "w")
f.write(prettyXml)
f.close()

This gives me a XML file like so:

<?xml version="1.0" ?>
<VIESORE.Configuration>
  <!-- Auto-generated at 11/14/2011 12:54:01 -->
  <ProjectDirectory>D:\Projects\VIESORE\Code\viesore\Python</ProjectDirectory>
  <Scene>loader.vue</Scene>
  <RenderOutputQuality>High</RenderOutputQuality>
  <Terrain>
    <TerrainFile>raster.txt</TerrainFile>
    <TerrainWidth>1064</TerrainWidth>
    <TerrainHeight>1327</TerrainHeight>
    <TerrainMinElevation>351.042327881</TerrainMinElevation>
    <TerrainMaxElevation>538.774719238</TerrainMaxElevation>
    <TerrainCellResolution>9.36747778088</TerrainCellResolution>
  </Terrain>
  <Sun>
    <SunPitch>30</SunPitch>
    <SunAzimuth>220</SunAzimuth>
  </Sun>
</VIESORE.Configuration>

I then read the XML in another application (this below needs cleaned up some, but it works):

import xml.dom.minidom

# Parse Config file
f = r'D:\Projects\VIESORE\Code\Python\Vue\Config.xml'
doc = xml.dom.minidom.parse(f)

image_dir_tag = doc.getElementsByTagName('ProjectDirectory')
if (len(image_dir_tag) == 1):
    working_dir = str(image_dir_tag[0].childNodes[0].toxml())

scene_tag = doc.getElementsByTagName('Scene')
if (len(scene_tag) == 1):
    scene = str(scene_tag[0].childNodes[0].toxml())

# Read some more elements in
Related Question