Python – Converting from DMS to DD Using Field Calculator

arcgis-desktopdegrees minutes secondsfield-calculatorpython-parser

I need to convert lat/long that is expressed as degrees, minutes, and seconds in the data into decimal degrees. For example, in the data, they are listed as N335042.06 in the Latitude column, and W86031.04 in the Longitude column. I have done this problem before where I created a script which converted DMS to DD, and vice-versa, so I guess I could use bits from that. But the problem I'm having is how to ignore (for lack of a better word) the 'N' and 'W' in the data? Can I skip them? And DMS are listed all together without any symbols or spaces.

Can I use len(), range(), split() to specify what part to read from the value? For example, can do the following?

N335042.06 where,
33 = degrees
50 = minutes
42.06 = seconds …?

I came across this ESRI article, but it's in VB. Will probably use it as a reference but some of the terminology/syntax is different from Python.

Final code that works!

# Pre-logic
def latDD(x):
  D = int(x[1:3])
  M = int(x[3:5])
  S = float(x[5:])
  DD = D + float(M)/60 + float(S)/3600
  return DD

# Expression
latDD(!Latitude!)

Best Answer

  • Take a look at the section on slicing in the Python tutorial. You can grab a range of characters from a string using slicing syntax, e.g. D = int(x[1:2]).

    For seconds, try S = float(x[5:]). This will grab all the characters starting at index 5 to the end of the string, in the case that you have variable length values for seconds.

  • The !FieldName! syntax is only valid in the expression box. You have to define a function in the "pre-logic" section that takes in the values of the fields you want and returns the desired value. Then, in the expression box, call the function and pass in the field values using the !FieldName! syntax. See Calculate Field examples in the help.