473,466 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

3 New Member
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] 

6 9418
bvdet
2,851 Recognized Expert Moderator Specialist
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
dwblas
626 Recognized Expert Contributor
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
nevy
3 New Member
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
2,851 Recognized Expert Moderator Specialist
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
dwblas
626 Recognized Expert Contributor
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
nevy
3 New Member
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

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

Similar topics

4
by: Dean G | last post by:
I need to compare two values. one from a text field 'bid' and the other from a field in an sql server database 'maxbid'. The problem is the column in the database has decimal as its data type...
3
by: JenHu | last post by:
Hi all, I have to read from a text file and generate values and insert to database. But first of all, when the text file contains '0000000000', I received a sEfundAmt value = 0D instead of 0.0...
5
by: Allerdyce.John | last post by:
Do I need to convert string to integer in python? or it will do it for me (since dynamic type)? In my python script, I have this line: x /= 10; when i run it, I get this error: TypeError:...
3
by: mlafarlett | last post by:
In the example below, the 'convert.ToDecimal' removes the trailing 5. I'd like to get the number, in tact, moved to the decimal variable. Any help would be greatly appreciated. Dim dcml As...
5
by: Lance | last post by:
Hi all, Below is a funtion that converts a Lat or Lon coordinate (as a Double) to a string of Degrees, Minutes and Seconds. It's based on an MS Exmaple for VBA found here...
9
by: Leo jay | last post by:
i'd like to implement a class template to convert binary numbers to decimal at compile time. and my test cases are: BOOST_STATIC_ASSERT((bin<1111,1111,1111,1111>::value == 65535));...
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
2
karthickbabu
by: karthickbabu | last post by:
Hi In my application i want to convert integer to decimal. I get a input and using convert function to convert into decimal. But it shows as it self. My code like as below, Is any wrong in...
5
by: arial | last post by:
Hi all, I have a sql .bak file which i need to convert into xml file. can someone help with this? pointing out to some tutorial or some suggetion on how to start? Thak you,
1
by: lenora | last post by:
How to convert string to decimal ? input - output 45 - 45.00 45.5 - 45.50 45.51 - 45.51 Thank you in advance
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.