Converting SB Script to Python in Script Evaluator Pre-Logic Code

259
6
05-25-2021 12:31 PM
MicahReed
New Contributor

Short Version: Is there an equivalent to the "ParameterValueByName" function found in the VB Script parser of a network attribute script evaluator. 

Long Version: I  have a custom road network that has unique impedance values for a handful of of vehicles. (for example a tractor trailer may take 30 seconds to traverse a segment with a steep incline where a car may only take 15 seconds). I have a Python tool that allows the user to select the vehicle of interest and solves the route with the cost and impedance specific to that vehicle. It also gives the ability to select multiple vehicles at once, and it automatically looks at each segment, finds the quickest cost and applies it. So if you pick car and tractor trailer, it will compare the costs of the two for each segment and apply the quickest one. I have accomplished this using the attached VB Script code in the attributes evaluator. The selected vehicles are passed to the evaluator as a string using the "Update Analysis Layer Attribute Parameter" tool in the network analyst toolbox. The VB Script code works, but this network is destined for ArcGIS Enterprise so as i understand it the work must be done in python. I have also attached how I believe the python logic should go. This returns a "NameError: 'ParameterValueByName' is not defined.

 

The attribute in question has 2 parameters. "Vehicles" set to string with no default value, and defaultSpeed set to double, with a default value of 60.

0 Kudos
6 Replies
JoeBorgione
MVP Esteemed Contributor

I'm not familiar at with VB any more and not too familiar with the tools now available in Network Analyst, but if you look at the online help for Update Analysis Layer Attribute Parameter and click on the Python tab, it provides the parameters the tool is expecting.  I don't know if that'll help you or not.

can't wait to retire....
MicahReed
New Contributor

Thanks for the reply. Unfortunately, I'm not having any trouble with the Update Analysis Layer Attribute Parameter tool. It is my network attribute evaluators that I cannot get to work correctly. 

0 Kudos
MattCrowder1
New Contributor

I think you just need to prefix ParameterValueByName with "Parameters."

For example, the following code worked for me as a python script evaluator that just reads the number for an attribute parameter value named "ScaleFactor" and uses that value to multiply the existing Minutes cost attribute.

preLogic

cost = Edge.AttributeValueByName( "Minutes" )
scaleFactor = Parameters.ParameterValueByName("ScaleFactor")

value:

cost * scaleFactor

0 Kudos
MicahReed
New Contributor

When I use the syntax you suggested above, the NameError identifies the 'Parameters' before the '.' as not defined. 

Do you know whether or not this works across all versions of ArcMap. I am using 10.8.1 running python 2.7.18.

0 Kudos
MattCrowder1
New Contributor

Sorry, when I was testing this, I had accidentally forgotten to switch the interpreter to python.  I believe you need to put the logic in functions.  The following works for me when using the python script interpreter.  I think you could do something similar...

 

PreLogic:

def getCost():
  return Edge.AttributeValueByName("Minutes")

def getScaleFactor():
  return ParameterValueByName("ScaleFactor")

 

 

Value:

getCost() * getScaleFactor()

0 Kudos
MattCrowder1
New Contributor

Or you can do it in one function:

 PreLogic

def getValue():
  cost = Edge.AttributeValueByName("Minutes")
  scaleFactor = ParameterValueByName("ScaleFactor")
  return cost * scaleFactor

 

Value:

getValue()

0 Kudos