KeeperOfAngels

Python Quirk?

Discussion created by KeeperOfAngels on Nov 2, 2012
I am experiencing an issue with a python script that was written to correct direction discrepancies.  The script was written to correct rotated linework DIRECTION attributes.

The script works just fine outside of ArcMap, and corrects the DIRECTION attribute from the 'actual' direction, to the 'recorded' direction.  When the script is run in ArcMap using the Field Calculator, there are discrepancies ranging from 2 seconds to 7 seconds.  This does not make any sense at all to me.

This is the script:

import re, sys, math

def calculatedegrees(originalval, diffbearing):

  regexpatterndiff = r'^([+-]{1})(\d{1,2})d(\d{1,2})m(\d{1,2})s'
  regexdiff = re.compile(regexpatterndiff)
  matchdiff = regexdiff.match(diffbearing)
  diffasdecimal = int(matchdiff.group(2)) + (int(matchdiff.group(3)) * 1.0/60.0) + (int(matchdiff.group(4)) * 1.0/60.0 * 1.0/60.0)
  # - and + are direction change.
  if matchdiff.group(1) == "-":
    diffasdecimal = -diffasdecimal

  
  regexpattern = r'^(\D*)(\d*)-(\d*)-(\d*)(\D*)'
  regex = re.compile(regexpattern)
  match = regex.match(originalval)
  asdecimal = int(match.group(2)) + (int(match.group(3)) * 1.0/60.0) + (int(match.group(4)) * 1.0/60.0 * 1.0/60.0)

  fd = match.group(1).strip()
  sd = match.group(5).strip()

  if (fd == "S" and sd == "E") or (fd == "N" and sd == "W"):
    diffasdecimal = -diffasdecimal

  finaldecimal = asdecimal + diffasdecimal

  # change quadrant changes
  if matchdiff.group(1) == "+":
    if finaldecimal < 0 :
      finaldecimal = math.fabs(finaldecimal)
      if fd == "S" and sd == "E":
        fd = "S"
        sd = "W"
      if fd == "N" and sd == "W":
        fd = "N"
        sd = "E"
    if finaldecimal > 90:
      diff = finaldecimal-90
      finaldecimal = 90-diff
      if fd == "N" and sd == "E":
        fd = "S"
        sd = "E"
      if fd == "S" and sd == "W":
        fd = "N"
        sd = "W"

  if matchdiff.group(1) == "-":
    if finaldecimal < 0 :
      finaldecimal = math.fabs(finaldecimal)
      if fd == "N" and sd == "E":
        fd = "N"
        sd = "W"
      if fd == "S" and sd == "W":
        fd = "S"
        sd = "E"
    if finaldecimal > 90:
      diff = finaldecimal-90
      finaldecimal = 90-diff
      if fd == "S" and sd == "E":
        fd = "N"
        sd = "E"
      if fd == "N" and sd == "W":
        fd = "S"
        sd = "W"
  
  # recalculate degrees
  mnt,sec = divmod(finaldecimal*3600,60)
  deg,mnt = divmod(mnt,60)
  deg = int(round(deg))
  mnt = int(round(mnt))
  sec = int(round(sec))
  finalstring = '{0} {1}-{2}-{3} {4}'.format(fd, deg, mnt, sec, sd)

  return finalstring


The variables can be changed in the Field Calculator using:

calculatedegrees( !DIRECTION!,'+1d21m42s')

Examples of the discrepancies:

Recorded            N 52-57-10 E
Rotated to          N 51-35-28 E
Correction           01-21-42
GIS Direction      N 52-57-15 E (5 sec discrepancy)

Recorded            S 35-42-52 E
Rotated to          S 37-04-34 E
Correction           01-21-42
GIS Direction      S 35-42-54 E (2 sec discrepancy)

Recorded            N 31-21-05 E
Rotated to          N 29-59-23 E
Correction           01-21-42
GIS Direction      N 31-21-02 E (3 sec discrepancy)

Recorded            N 61-39-18 E
Rotated to          N 60-17-36 E
Correction           01-21-42
GIS Direction      N 61-39-18 E (No discrepancy)

Has anyone experience anything similar to this?  If so, how is this corrected?  Is it possibly something in the script itself?

I am very new to python scripting, so any help, suggestions etc. would be highly appreciated.

Outcomes