By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,050 Members | 1,009 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,050 IT Pros & Developers. It's quick & easy.

How to convert degrees, min, secs to decimal degrees with Python

P: 3
Does anyone know why this doesn't work? its meant to convert degrees, min, secs to decimal degrees. It should be simple right - but I'm very new to python (yesterday...)

Expand|Select|Wrap|Line Numbers
  1.  class DMStoDD:
  2.     def__init__(self, degrees, min, sec):
  3.         self.degrees = abs(degrees) % 360
  4.         self.minute = abs(min) % 60
  5.         self.second = abs(sec) % 60
  6.     def DD:
  7.         self.degrees = (self.degrees) % 360
  8.         self.minute = self.minute/60
  9.         self.seconds = self.seconds/3600
  10.     def AsList(self):
  11.         print [self.degrees, self.minute, self.seconds] 
also how do I run my class?
Aug 26 '11 #1

✓ answered by dwblas

You also have to add (self) to DD if you want it to be a function that is a member of the class. This is what I get using your original post:
Expand|Select|Wrap|Line Numbers
  1. class DMStoDD:
  2.     def __init__(self, degrees, mins, sec):
  3.         ## anything less than 360 will yield the same number
  4.         self.degrees = abs(degrees) % 360
  5.  
  6.         ## "min" is a reserved name==minimum
  7.         self.minute = abs(mins) % 60
  8.         self.seconds = abs(sec) % 60
  9.  
  10.     def DD(self):    ## same as __init__ calcs
  11.         self.degrees = (self.degrees) % 360
  12.         self.minute = self.minute/60
  13.         self.seconds = self.seconds/3600
  14.  
  15.     def AsList(self):
  16.         print [self.degrees, self.minute, self.seconds]
  17.  
  18. DS = DMStoDD(29, 12, 30)
  19. DS.AsList()       ## prints [29, 12, 30]
  20. DS = DMStoDD(390, 12, 30)
  21. DS.AsList()       ## prints [30, 12, 30] 

Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
What would the input look like? Why are you using "modulo 360" for self.degrees? A degree is 1 degree, a minute is 1/60 of a degree, and a second is 1/3600 of a degree. If you intend "DD" to be a method, you need an argument tuple with "self" in it. def DD(self):

Expand|Select|Wrap|Line Numbers
  1. >>> degrees, minutes, seconds = 30, 25, 15
  2. >>> decdeg = degrees+minutes/60.+seconds/3600.
  3. >>> decdeg
  4. 30.420833333333334
  5. >>> 
To run it, create an instance by calling the class like a function: obj = DMStoDD(degrees, minutes, seconds)
Aug 27 '11 #2

Expert 100+
P: 624
Figure it out on paper first and use google if you can't get some of the calcs to work out.
Aug 28 '11 #3

P: 3
well I got it to work, there was a space missing between def and __init

the problem is I get this as a result <__main__.DDtoDMS object at 0x0216D4B0> when I want a number format (10.006)
Aug 31 '11 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Add a __str__ method to the class definition, and print obj, where obj is an instance of DMStoDD, will write whatever you want to stdout.
Aug 31 '11 #5

Expert 100+
P: 624
You also have to add (self) to DD if you want it to be a function that is a member of the class. This is what I get using your original post:
Expand|Select|Wrap|Line Numbers
  1. class DMStoDD:
  2.     def __init__(self, degrees, mins, sec):
  3.         ## anything less than 360 will yield the same number
  4.         self.degrees = abs(degrees) % 360
  5.  
  6.         ## "min" is a reserved name==minimum
  7.         self.minute = abs(mins) % 60
  8.         self.seconds = abs(sec) % 60
  9.  
  10.     def DD(self):    ## same as __init__ calcs
  11.         self.degrees = (self.degrees) % 360
  12.         self.minute = self.minute/60
  13.         self.seconds = self.seconds/3600
  14.  
  15.     def AsList(self):
  16.         print [self.degrees, self.minute, self.seconds]
  17.  
  18. DS = DMStoDD(29, 12, 30)
  19. DS.AsList()       ## prints [29, 12, 30]
  20. DS = DMStoDD(390, 12, 30)
  21. DS.AsList()       ## prints [30, 12, 30] 
Aug 31 '11 #6

P: 3
thanks for everyones help. It works now, I think the main problem was I was using python 3 not 2. Its a bit strange that nothing seems to work in python 3 - sort, sorted doesn't work either. I kept getting DMStoDD is not defined. But it works in python 2 now. The only thing I can't get is to limit the values degrees(0-360), minutes(0-60)
Sep 1 '11 #7

Post your reply

Sign in to post your reply or Sign up for a free account.