Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Maybe you can help me with maybe a strange question. 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?

Thanks in advance for any ideas.

share|improve this question
add comment

1 Answer 1

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
share|improve this answer
    
In other words, no there is no simple way. –  blah238 Jan 28 '12 at 7:07
    
Depends on your definition of simple - once you get a reader/writer like this setup, it's not that complicated. You're getting a lot done with less than 40 or so lines of code. –  Chad Cooper Jan 28 '12 at 16:06
    
Thank you Chad, it's not easy, but maybe the most simple way. I will try to use your method for my case. –  hgnomden Jan 30 '12 at 12:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.