POST
|
I am sure this is already out there but I could not find it so I thought I would share this code sample for storing and setting python toolbox tool parameter values using python pickles. I am not a programmer so any feedback for doing this a better way is always appreciated. import arcpy, pickle, os, sys
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Pickle_Test"
self.alias = ""
# List of tool classes associated with this toolbox
self.tools = [Tool]
class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Tool"
self.description = ""
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
#set default values for tool parameters
scriptPath = sys.path[0]
test_val = pickle.load(open(scriptPath + "\\test_params.txt", "rb"))
#First parameter
param0 = arcpy.Parameter(
displayName="Input Feature Class",
name="in_fc",
datatype=["Feature Class"],
parameterType="Required",
direction="Input")
param0.value = test_val['param0']
params = [param0]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
scriptPath = sys.path[0]
test_val = {}
test_val = {"param0": parameters[0].valueAsText}
pickle.dump (test_val, open(scriptPath + "\\test_params.txt", "wb"))
return
... View more
02-13-2015
11:51 AM
|
2
|
1
|
7136
|
POST
|
Thank you for your feedback and link to the fieldmapping code example.
... View more
10-31-2014
08:06 AM
|
0
|
0
|
1695
|
POST
|
Thank you for suggestions, I appreciate hearing other approaches and will definitely look at your ideas further..
... View more
10-31-2014
08:05 AM
|
0
|
0
|
1695
|
POST
|
I am creating a pivot table in python and have come across the scenario that when there are more than 9 pivot fields created that the order of the fields is technically out of order (e.g., pivotFld_1, pivotFld_10, pivotFld_11, pivotFld_2, pivotFld_3, pivotFld_4....). I would like the order to be pivotFld_1, pivotFld_2, pivotFld_3, pivotFld_4....pivotFld_9, pivotFld_10, pivotFld_11.... I have written the following code to reorder the fields but I am wondering if there is a better way to accomplish this? testTbl = "path\\pivot table" testTbl_vw = "testTbl_vw" arcpy.MakeTableView_management(testTbl, testTbl_vw) testFlds = list(f.name for f in arcpy.ListFields(testTbl_vw)) seqTest = [] for f in testFlds: if not f.find("pivotField_"): seqTest.append(f.lstrip("pivotField_")) newFlds = [] for f in seqTest: if int(f) > 9: newFlds.append(f) for f in newFlds: arcpy.AddField_management(testTbl_vw, "tmp_"+f, "LONG") arcpy.CalculateField_management(testTbl_vw, "tmp_"+f, "!pivotField_"+f+"!", "PYTHON") arcpy.DeleteField_management(testTbl_vw, "pivotField_"+f) arcpy.AddField_management(testTbl_vw, "pivotField_"+f, "LONG") arcpy.CalculateField_management(testTbl_vw, "pivotField_"+f, "!tmp_"+f+"!", "PYTHON") arcpy.DeleteField_management(testTbl_vw, "tmp_"+f)
... View more
10-30-2014
12:55 PM
|
0
|
4
|
5661
|
POST
|
Hi Luke, Using a string data type parameter I have been able to populate a filter list with field names (though your code was more succinct than mine so thank you for the sample code). I was wondering if I was using a field data type parameter if there was way a to set the value for the 'Obtained from' property, for the field data type parameter, using python code or if that property can only be set manually in the tool's properties dialog box. The tool I am working lets the user select one of three tables in a value list and based on the selection I want to display a list of the fields for the selected table name.
... View more
06-16-2014
01:31 PM
|
0
|
0
|
756
|
POST
|
Hi Adam, thank you for looking at my question. I hope this is helpful. def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parmater
has been changed."""
if self.params[0].altered:
tblList = [" "]
desc = arcpy.Describe(self.params[0].value)
tblList.append(desc.name)
if self.params[1].value:
desc = arcpy.Describe(self.params[1].value)
tblList.append(desc.name)
if self.params[2].value:
desc = arcpy.Describe(self.params[2].value)
tblList.append(desc.name)
self.params[6].filter.list = tblList
self.params[8].filter.list = tblList
self.params[10].filter.list = tblList
if self.params[1].altered:
tblList = [" "]
if self.params[0].value:
desc = arcpy.Describe(self.params[0].value)
tblList.append(desc.name)
desc = arcpy.Describe(self.params[1].value)
tblList.append(desc.name)
if self.params[2].value:
desc = arcpy.Describe(self.params[2].value)
tblList.append(desc.name)
self.params[6].filter.list = tblList
self.params[8].filter.list = tblList
self.params[10].filter.list = tblList
if self.params[2].altered:
tblList = [" "]
if self.params[0].value:
desc = arcpy.Describe(self.params[0].value)
tblList.append(desc.name)
if self.params[1].value:
desc = arcpy.Describe(self.params[1].value)
tblList.append(desc.name)
desc = arcpy.Describe(self.params[2].value)
tblList.append(desc.name)
self.params[6].filter.list = tblList
self.params[8].filter.list = tblList
self.params[10].filter.list = tblList
if self.params[6].altered:
"this is the point where I was wondering if I can use the Obtained from property to populate
"self.params[6] with the fields for the table name selected in self.params[6]
return
... View more
06-14-2014
05:52 AM
|
0
|
0
|
756
|
POST
|
Is is possible to set the Obtained from property for a parameter in Validation using python code? I have populated a string parameter with a list of tables selected in Table View parameters. If the user selects a table name in the string parameter I would like to populate a Field data type parameter with the list of fields for the selected table. def updateParameters(self): """Modify the values and properties of parameters before internal validation is performed. This method is called whenever a parmater has been changed.""" if self.params[0].altered: tblList = [""] desc = arcpy.Describe(self.params[0].value) tblList.append(desc.name) if self.params[1].value: desc = arcpy.Describe(self.params[1].value) tblList.append(desc.name) if self.params[2].value: desc = arcpy.Describe(self.params[2].value) tblList.append(desc.name) self.params[6].filter.list = tblList self.params[8].filter.list = tblList self.params[10].filter.list = tblList if self.params[1].altered: tblList = [""] if self.params[0].value: desc = arcpy.Describe(self.params[0].value) tblList.append(desc.name) desc = arcpy.Describe(self.params[1].value) tblList.append(desc.name) if self.params[2].value: desc = arcpy.Describe(self.params[2].value) tblList.append(desc.name) self.params[6].filter.list = tblList self.params[8].filter.list = tblList self.params[10].filter.list = tblList if self.params[2].altered: tblList = [""] if self.params[0].value: desc = arcpy.Describe(self.params[0].value) tblList.append(desc.name) if self.params[1].value: desc = arcpy.Describe(self.params[1].value) tblList.append(desc.name) desc = arcpy.Describe(self.params[2].value) tblList.append(desc.name) self.params[6].filter.list = tblList self.params[8].filter.list = tblList self.params[10].filter.list = tblList if self.params[6].altered: At this point I would like to populate self.params[7] with a list of fields corresponding to the table selected in self.params[6] self.params[7] is currently a Field data type
... View more
06-11-2014
10:45 AM
|
0
|
6
|
3349
|
POST
|
Hello, I am trying to write variable values to a config.py file so that I access the values from other scripts. I am having problems properly saving an SQL query statement to the config.py file. Sample of my code: sqlSel = arcpy.GetParameterAsText(1) (e.g. sqlSel will have the value "CNTYNAME" = 'ALACHUA' fo.write("sqlSel = \"" + sqlSel + '''\"''' + "\n") (I have tried variations of this syntax with no luck - fo.write("sqlSel = \"??? + sqlSel + "\???\n") What is written to the config.py file; [ATTACH=CONFIG]23615[/ATTACH] sqlSel = ""CNTYNAME" = 'ALACHUA'" (CNTYNAME is not green indicating it is part of the string value)
... View more
04-18-2013
12:18 PM
|
0
|
2
|
569
|
POST
|
I finally found another post that answered my question... Error handling with Python script tools posted Feb 2011 for anyone having this same issue
... View more
12-12-2012
07:21 AM
|
0
|
0
|
198
|
POST
|
I am a novice at programming but have been successful at writing some python code. I am trying to write a custom error handler and have the error msg displayed in the in tool's process dialog box. Message will print in python shell but not in process dialog box when run from a tool. # import the required modules for the python script import arcpy, os, sys, traceback class NoCaseFld(Exception): pass # define common variables here caseFlds = "lU_CURRENT;TRACT" caseOrder = "tract lucurrent" orderErr = "" fldFound = 1 try: # 1. If case field(s) selected, check for a specified sort order and create list object if caseFlds != "": if caseOrder != "": orderUpper = caseOrder.upper() if " " in orderUpper: orderList = orderUpper.split(" ") elif "," in orderUpper: orderList = orderUpper.split(",") elif ";" in orderUpper: orderList = orderUpper.split(";") else: fldFound = -1 orderErr = caseOrder #error in reading case field order # i. Verify sort order list values are found in case field(s) list caseUpper = caseFlds.upper() for f in orderList: for c in caseUpper.split(";"): if c == f: fldFound = 1 continue else: fldFound = -1 if fldFound == -1: orderErr = orderErr + f if fldFound == -1: raise NoCaseFld except NoCaseFld: print "Error: Case Field Name Order\nfield name(s) not found in table:\n" + orderErr except: #error handeling code tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n" msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n" arcpy.AddError(msgs) arcpy.AddError(pymsg) print msgs print pymsg arcpy.AddMessage(arcpy.GetMessages(1)) print arcpy.GetMessages(1)
... View more
12-12-2012
06:53 AM
|
0
|
1
|
445
|
Title | Kudos | Posted |
---|---|---|
2 | 02-13-2015 11:51 AM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|