Created
August 9, 2013 13:30
-
-
Save chadcooper/6193592 to your computer and use it in GitHub Desktop.
Tool validator script for ArcGIS 10.0 Toolbox that reads a config.ini or XML config file to use to populate tool fields.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ToolValidator: | |
"""Class for validating a tool's parameter values and controlling | |
the behavior of the tool's dialog. | |
""" | |
class ParseIni(object): | |
"""Class for parsing VIESORE ini configuration files""" | |
def __init__(self): | |
self.viesore_ini = r"C:\Program Files (x86)\VIESORE\Config\ViesoreConfig.ini" | |
def parse_ini(self): | |
"""Parse the VIESORE configuration file and return options/values as a | |
dictionary | |
""" | |
import ConfigParser | |
ini_dict = {} | |
parser = ConfigParser.SafeConfigParser() | |
parser.read(self.viesore_ini) | |
for section in parser.sections(): | |
for option, value in parser.items(section): | |
ini_dict[option] = value | |
return ini_dict | |
class ParseXmlConfig(object): | |
"""Class for parsing VIESORE XML configuration files""" | |
def __init__(self, config_file): | |
"""Parse the default XML config file""" | |
import xml.dom.minidom | |
self.dom = xml.dom.minidom.parse(config_file) | |
def get_text(self, nodelist): | |
rc = [] | |
for node in nodelist: | |
if node.nodeType == node.TEXT_NODE: | |
rc.append(node.data) | |
return ''.join(rc) | |
def get_config_value(self, node): | |
node_tag = self.dom.getElementsByTagName(node)[0] | |
node_text = self.get_text(node_tag.childNodes) | |
return node_text | |
def get_n_e_array(self, child_node): | |
""" | |
Returns a list of tuple pairs, each one representing a northing/easting | |
location which is stored in XML config file. | |
""" | |
array_list = [] | |
rootNode = self.dom.documentElement | |
for node in rootNode.childNodes: | |
for child in node.childNodes: | |
if child.nodeName == child_node: | |
for node in child.childNodes: | |
if node.nodeName == "VueNorthing": | |
vn = self.get_text(node.childNodes) | |
if node.nodeName == "VueEasting": | |
ve = self.get_text(node.childNodes) | |
array_list.append((vn, ve)) | |
return array_list | |
def __init__(self): | |
"""Setup arcpy and the list of tool parameters.""" | |
import arcpy | |
import os | |
self.params = arcpy.GetParameterInfo() | |
def initializeParameters(self): | |
"""Refine the properties of a tool's parameters. This method is | |
called when the tool is opened. | |
""" | |
import os | |
import arcpy | |
# Parse the ini config file | |
ini = ToolValidator.ParseIni() | |
ini_values = ini.parse_ini() | |
# Parse the xml project config file (default) | |
xml_file = os.path.join(ini_values["default_xml_directory"], "Config.xml") | |
self.get_set_tool_parameters(xml_file) | |
mxd = arcpy.mapping.MapDocument("CURRENT") | |
mxd_name = os.path.basename(mxd.filePath)[:-4] | |
self.params[4].value = mxd_name | |
return | |
def get_set_tool_parameters(self, xml_file): | |
"""Pull values out of XML config file and set them into tool parameters""" | |
xml_values = ToolValidator.ParseXmlConfig(xml_file) | |
project_dir = str(xml_values.get_config_value("ProjectDirectory")) | |
terr_z_elev = str(xml_values.get_config_value("TerrainZElevValue")) | |
terr_file = str(xml_values.get_config_value("TerrainFile")) | |
# Set value from XML config file into tool parameter | |
self.params[0].value = project_dir | |
self.params[1].value = terr_z_elev | |
self.params[2].value = terr_file | |
return | |
def updateParameters(self): | |
"""Modify the values and properties of parameters before internal | |
validation is performed. This method is called whenever a parameter | |
has been changed. | |
""" | |
if self.params[3].value: | |
# MUST cast the value we get for XML folder/file to string or we will | |
# get a arcobjects error thrown | |
user_xml = str(self.params[3].value) | |
self.get_set_tool_parameters(user_xml) | |
return | |
def updateMessages(self): | |
"""Modify the messages created by internal validation for each tool | |
parameter. This method is called after internal validation.""" | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment