在Python中将DD(十进制度)转换为DMS(度分秒)?
如何在 Python 中将十进制度转换为度分秒?有没有已经写好的公式?
How do you convert Decimal Degrees to Degrees Minutes Seconds In Python? Is there a Formula already written?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
这正是
divmod
的发明目的:打印:
This is exactly what
divmod
was invented for:Prints:
这是我根据 Paul McGuire 的更新版本。这个应该正确处理底片。
Here is my updated version based upon Paul McGuire's. This one should handle negatives correctly.
如果要正确处理负数,请将第一个非零度量设置为负数。将所有度数、分数和秒数指定为负数是违反惯例的(Wikipedia 显示 40 ° 26.7717, -79° 56.93172 作为度分表示法的有效示例,其中度数为负数,分没有符号),如果度数部分为 0,则将度数设置为负数不会产生任何影响。这是一个函数基于 Paul McGuire 和 baens 的函数,可以充分处理这个问题:
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:
只需几个
* 60
乘法和几个int
截断,即:Just a couple of
* 60
multiplications and a couple ofint
truncations, i.e.:改进@chqrlie 答案:
Improving @chqrlie answer:
这是我的Python代码:
This is my Python code:
这是我的稍微不同的方法,其工作原理与我的 HP Prime 上的正十进制和负十进制相同......
Here's my slightly different approach that works the same as on my HP Prime for positive and negative decimal degrees...
该符号最好单独返回,以便可以用来选择
('N', 'S')
或('E', 'W')
, 例如。The sign has better be returned separately, so that it can be used to choose from
('N', 'S')
or('E', 'W')
, for example.这是 PaulMcG 答案的 numpy 版本,另外它会舍入到十分之一秒(您可以将第二个参数更改为舍入函数),并且它返回符号(-1 或 1)作为单独的值(这使得它更容易)让我处理为纬度/经度值)。这里的主要区别是您可以传入一个十进制_度数组或单个双精度值(请注意,如果您传递一个列表,您会得到一个列表列表)。使用之前,请确保您对舍入行为感到满意,否则您可以将其删除。
This is a numpy version of PaulMcG's answer with the addition that it will round to tenths of a second (you can change second argument to round function) and it returns the sign (-1 or 1) as a separate value (this made it easier for me to handle as latitude/longitude values). Main difference here is that you can pass in an array of decimal_degrees or a single double value (note if you pass a list you get a list-of-lists back). Before using please make sure you are happy with the rounding behavior or you can remove it.
使用 fmod 和舍入来分离度数和分数。将分数乘以 60 并重复以获得分钟和余数。然后将最后一部分再次乘以 60 即可得到秒数。
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.现在我们可以使用 LatLon 库...
https://pypi.org/project/LatLon/
Now we can use LatLon library...
https://pypi.org/project/LatLon/
您可以使用函数
clean_lat_long()
如果您的数据位于 DataFrame 中,则库 DataPrep 。使用pip install dataprep
安装 DataPrep。或者,如果纬度和经度位于不同的列中:
You can use the function
clean_lat_long()
from the library DataPrep if your data is in a DataFrame. Install DataPrep withpip install dataprep
.Or if latitude and longitude are in separate columns:
我的做法:
My approach: