How do you convert Decimal Degrees to Degrees Minutes Seconds In Python? Is there a Formula already written?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Use
fmod
and rounding to get the degrees and fraction separated. Multiply the fraction by 60 and repeat to get minutes and a remainder. Then multiply that last part by 60 again to get the number of seconds.Just a couple of
* 60
multiplications and a couple ofint
truncations, i.e.:This is exactly what
divmod
was invented for:If you want to handle negatives properly, the first non-zero measure is set negative. It is counter to common practice to specify all of degrees, minutes and seconds as negative (Wikipedia shows 40° 26.7717, -79° 56.93172 as a valid example of degrees-minutes notation, in which degrees are negative and minutes have no sign), and setting degrees as negative does not have any effect if the degrees portion is 0. Here is a function that adequately handles this, based on Paul McGuire's and baens' functions:
The sign has better be returned separately, so that it can be used to choose from
('N', 'S')
or('E', 'W')
, for example.Here is my updated version based upon Paul McGuire's. This one should handle negatives correctly.