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
  • 4
    7 months later I had to figure out how to do this again and ended up here, thanks for posting your code! :)
    – blah238
    Commented Jun 15, 2012 at 17:23

2 Answers 2

  • 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.
  • The following is what I have as my code in the Field Calculator, but it's indicating that the first line has an error. Any ideas? x = (!Latitude!) D = int(x[1:3]) M = int(x[3:5]) S = float(x[5:10]) DD = D + float(M)/60 + float(S)/3600 And in the code block I have !Latitude! = DD
    – kaoscify
    Commented Nov 5, 2011 at 4:01
  • Updated answer with more info, try it and let us know if you still have trouble.
    – blah238
    Commented Nov 5, 2011 at 5:46
  • Thanks! Getting there, but I've encountered a new error. I've edited my first post with the new code. Thoughts? Sorry, I'm new to Python, but I really appreciate your help!
    – kaoscify
    Commented Nov 5, 2011 at 6:15
  • Try return DD instead of return latDD.
    – blah238
    Commented Nov 5, 2011 at 6:21
  • 1
    I removed print DD and changed return latDD to return DD like you suggested, and it worked! Thanks!
    – kaoscify
    Commented Nov 5, 2011 at 6:28

Using just Field Calculator with no pre logic I was able to make this work for me. The string I had was a little different format with some spaces.

LAT value like "dd mm ss.ss" and i used this in the calculator.

float( !LAT! [0:2]) + float( !LAT! [3:5])/60+ float( !LAT! [6:])/3600

long value like "-dd mm ss.ss" and this for long

float( !LONG_! [0:3]) + float( !LONG_! [4:6])/60+ float( !LONG_! [7:])/3600

  • 3
    Actually, your long value is wrong since you have possible negative degrees and positive minutes and seconds. Better use a regular expression import re in pre-logic
    – Mike T
    Commented Apr 19, 2012 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.