Separate Traces

Discussion created by zhengniu on Feb 27, 2012
Latest reply on Feb 27, 2012 by mdenil
Hi, all

I got problem when running the following code. My purpose is to classify my GPS points into numbers of groups based on conditions:
if every point has same RouteId, distance (DeltaDista) between any two points is less than 50, driving direction difference between any two points is less than 45, the same class number will be added to the Field named "Trace"

>>> import arcpy
>>> path = r'C:\TempArcGISCalculate\Scratch.gdb'
>>> arcpy.env.workspace = path
>>> inFeatures = "Section0001_1"
>>> fieldname1="RouteId"
>>> fieldname2="DeltaDista"
>>> fieldname3="InitialBea"
>>> codeblock = """Record =1
... def separateTrace (p1, p2, p3):
...  global oldp1,oldp2,oldp3, Record
...  if ((p1 == oldp1) and (p2-oldp2)<=50 and (p3-oldp3)<=45):
...   trace = Record
...  else:
...   Record =Record+1
...  return trace """
>>> expression = "separateTrace(fieldname1, fieldname2, fieldname3)"
>>> arcpy.CalculateField_management(inFeatures, "Trace",expression,"PYTHON",codeblock)


Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 2727, in CalculateField
    raise e
ExecuteError: ERROR 000539: Error running expression: separateTrace(fieldname1, fieldname2, fieldname3) <type 'exceptions.NameError'>: global name 'oldp1' is not defined
Failed to execute (CalculateField).