Simple Python Field Calculator ex, converting degrees minutes & seconds to decimal

4717
4
Jump to solution
12-17-2012 12:26 PM
MichaelMiller2
Occasional Contributor III
Afternoon all,

I'm trying to use the field calculator (Python parser) to convert a field of degrees/minutes/seconds(DMS) to decimal degrees.  The DMS field (latTemp) is formatted as such; 463704, 462331, etc. I'm trying to apply to the calculation to a decimal field (Latdecimal).

Pre-Logic code:
def lat(dms)     deg = !latTemp![0:2]     min = !latTemp![2:4]     sec = !latTemp![4:6]     dms = deg + (min / 60) + (sec / 3600)     return dms

Expression:
lat( !latTemp!)


I'm relatively new to python and am confused at this point.

Thanks for suggestions.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Ah yes you'll need to manipulate the types a little. You are trying to do both string and math operations on the same data.
def lat( dms ):     deg = float(str(dms)[0:2])     min = float(str(dms)[2:4])     sec = float(str(dms)[4:6])     dd = deg + (min / 60) + (sec / 3600)     return dd

View solution in original post

0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
Looks like just minor errors.
1. Need a colon after your function statement.
2. Reference the variable you pass containing the field value.

def lat(dms): # need to add colon
    deg = dms[0:2]
    min = dms[2:4]
    sec = dms[4:6]
    returnVal = deg + (min / 60) + (sec / 3600)
    return returnVal
0 Kudos
MichaelMiller2
Occasional Contributor III
Looks like just minor errors.
1. Need a colon after your function statement.
2. Reference the variable you pass containing the field value.

def lat(dms): # need to add colon
    deg = dms[0:2]
    min = dms[2:4]
    sec = dms[4:6]
    returnVal = deg + (min / 60) + (sec / 3600)
    return returnVal



Thanks for the help Matt.

But I'm still not able to get it to do the calc.

Pre-Logic Script Code:
def lat( dms ):
    deg = dms[0:2]
    min = dms[2:4]
    sec = dms[4:6]
    dd = deg + (min / 60) + (sec / 3600)
    return dd


testlat=
lat( !latdmsInt! )


Where am I all messed up?
0 Kudos
MathewCoyle
Frequent Contributor
Ah yes you'll need to manipulate the types a little. You are trying to do both string and math operations on the same data.
def lat( dms ):     deg = float(str(dms)[0:2])     min = float(str(dms)[2:4])     sec = float(str(dms)[4:6])     dd = deg + (min / 60) + (sec / 3600)     return dd
0 Kudos
MichaelMiller2
Occasional Contributor III
Ah yes you'll need to manipulate the types a little. You are trying to do both string and math operations on the same data.
def lat( dms ):
    deg = float(str(dms)[0:2])
    min = float(str(dms)[2:4])
    sec = float(str(dms)[4:6])
    dd = deg + (min / 60) + (sec / 3600)
    return dd



Awesome Matt!

Thanks for the guidance.
0 Kudos