Field Calculator - field values as variables in Pre-Logic Script Code

563
2
Jump to solution
01-08-2013 02:51 AM
PSArcOnlinePSArcOnline
New Contributor III
Hello, i need a litte help. Maybe just a syntax error.

I want to do some little vector math.

Pre-Logic Script Code
import sys def GetVector(shape):   x0 = 1  y0 = 2  x1 = 3  y1 = 4  a = [x0, y0]  b = [x1, y1]  vecsum = [a[0]+b[0], a[1]+b[1] ]  return vecsum[1]


Assignment Code
GetVector( !Shape!)


This works. But i want to use field values, stored in the shape file, for the variables. As one example i tried this:

Pre-Logic Script Code
import sys def GetVector(shape):   x0 = !xorigin!   y0 = 2  x1 = 3  y1 = 4  a = [x0, y0]  b = [x1, y1]  vecsum = [a[0]+b[0], a[1]+b[1] ]  return vecsum[1]


which gives a syntax error. What is wrong? In the Shape file there is a field named "xorigin" (type double) with a value of 3773506,87592.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
You have to pass it from the calculation expression as a variable.

def GetVector(shape, xorigin):     x0 = xorigin     y0 = 2     x1 = 3     y1 = 4     a = [x0, y0]     b = [x1, y1]     vecsum = [a[0] + b[0], a[1] + b[1]]     return vecsum[1]

GetVector(!Shape!, !xorigin!)

View solution in original post

0 Kudos
2 Replies
MathewCoyle
Frequent Contributor
You have to pass it from the calculation expression as a variable.

def GetVector(shape, xorigin):     x0 = xorigin     y0 = 2     x1 = 3     y1 = 4     a = [x0, y0]     b = [x1, y1]     vecsum = [a[0] + b[0], a[1] + b[1]]     return vecsum[1]

GetVector(!Shape!, !xorigin!)
0 Kudos
PSArcOnlinePSArcOnline
New Contributor III
Thanks, that did the trick.
0 Kudos