Tuesday 29 May 2018

arcpy - How to search on values of a value list in python toolbox?


I developed a simple python toolbox that lists values of a table in the value table parameter. The user can rank each row using the Rank field drop down values.Everything is ok untill user selected value. Now i want to update the table based on the rank values.My solution is search rank values in the value table (params3) and update params4 from these values.The main problem in search cursor is access to the Rank field ( search cursor is in execute function of my code). I think if i can search these values then i can use updatecursor to update the values in the table. enter image description here


import arcpy


class Toolbox(object):

def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "Toolbox"
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 = ""
self.description = ""
self.canRunInBackground = False

def getParameterInfo(self):

"""Define parameter definitions"""
# line station parameter
params0 = arcpy.Parameter(
displayName="Line Station",
name="line_station",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
params0.filter.list = ["Polyline"]


# station parameter
params1 = arcpy.Parameter(
displayName="Station",
name="point_station",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
params1.filter.list = ["POINT"]

# Table

params2 = arcpy.Parameter(
displayName="Table",
name="table",
datatype="GPTableView",
parameterType="Required",
direction="Input")
# value table
params3 = arcpy.Parameter(
displayName='Values',
name='values',

datatype='GPValueTable',
parameterType='Required',
direction='Input')
# Table output
params4 = arcpy.Parameter(
displayName="Tableoutput",
name="tableoutput",
datatype="GPTableView",
parameterType="Derived",
direction="Output")

params4.parameterDependencies = [params2.name]


params3.columns = [['Long', 'Station Code'],['GPString','Station Name'],['GPString','Rank']]
params3.filters[2].type="ValueList"




params = [params0,params1,params2,params3,params4]

return params

def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True

def updateParameters(self, parameters):
if parameters[2].altered:
if not parameters[3].altered:
result = arcpy.GetCount_management(parameters[2].value)

count = int(result.getOutput(0))
with arcpy.da.SearchCursor(parameters[2].value,["Station","Line","Row"]) as cur:
vtab = []

for row in cur:
vtab.append([row[0],row[1],row[2]])
parameters[3].value = vtab

parameters[3].filters[2].list = range(1,count+1)


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."""


if parameters[3].altered:

with arcpy.da.SearchCursor(parameters[3].valueAsText,parameters[3].columns[2][1]) as curs:
vtab = []
for rows in curs:
vtab.append(rows[0])
# The code rised an error and can not find the Rank field.




return

Answer



I finally solved my problem.I listed fields without using arcpy functions(fields variable).Then list values of the user ranks (Ranklist). using updatecursor and these lists i updated the values in value list


def execute(self, parameters, messages):
"""The source code of the tool."""

if parameters[3].altered:
fields = [f for f in parameters[3].value]
codelist = [f[0] for f in parameters[3].value]
rankslist = zip(*fields)[-1]

with arcpy.da.UpdateCursor(parameters[4].valueAsText,["code","Rank"]) as cur:
for row in cur:
if row[0] in codelist:
row[1] = rankslist[i]
cur.updateRow(row)
i+=1



return

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...