Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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!)
share|improve this question
3  
7 months later I had to figure out how to do this again and ended up here, thanks for posting your code! :) –  blah238 Jun 15 '12 at 17:23
add comment

2 Answers 2

up vote 6 down vote accepted
  • 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.
share|improve this answer
    
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 –  kolor Nov 5 '11 at 4:01
    
Updated answer with more info, try it and let us know if you still have trouble. –  blah238 Nov 5 '11 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! –  kolor Nov 5 '11 at 6:15
    
Try return DD instead of return latDD. –  blah238 Nov 5 '11 at 6:21
1  
I removed print DD and changed return latDD to return DD like you suggested, and it worked! Thanks! –  kolor Nov 5 '11 at 6:28
show 1 more comment

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

share|improve this answer
2  
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 Apr 19 '12 at 22:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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