[英]How to convert multiple coordinates from DMS to decimal?
I'm tryng to convert some coordinates from DMS to decimal.我正在尝试将一些坐标从 DMS 转换为十进制。 For example from S1639 W07157
to -16.65, -71.95
To achieve this I use the code found: here modified as below:例如从S1639 W07157
到-16.65, -71.95
为了实现这个我使用找到的代码: 这里修改如下:
def dm2dd(direction, degrees, minutes):
dd = round(float(degrees) + float(minutes)/60, 4);
if direction == 'S' or direction == 'W':
dd *= -1
return dd
def dd2dm(deg):
d = int(deg)
md = abs(deg - d) * 60
m = int(md)
return d, m
def parse_dm(dm):
parts = re.split('(\w)([0-9]{2})([0-9]{2})\s(\w)([0-9]{3})([0-9]{2})', dm)
lat = dm2dd(parts[1], parts[2], parts[3])
lng = dm2dd(parts[4], parts[5], parts[6])
return lat, lng
Using this functions I can convert a couple of coordinates (for example S1639 W07157
) without any problem, but sometimes I need to convert a string where multiple "couple" can be found (for example S1549 W07150 - S1632 W07148 - S1653 W07154 - S1647 W07204 - S1630 W07156 - S1549 W07150
).使用这个函数我可以毫无问题地转换几个坐标(例如S1639 W07157
),但有时我需要转换一个可以找到多个“couple”的字符串(例如S1549 W07150 - S1632 W07148 - S1653 W07154 - S1647 W07204 - S1630 W07156 - S1549 W07150
)。 How can I modify the code to get an output like:如何修改代码以获得 output,例如:
-15.8167 -71.8333 -16.5333 -71.8 -16.8833 -71.9 -16.7833 -72.0667 -16.5 -71.9333 -15.8167 -71.8333
Say this is your input:假设这是您的输入:
in = 'S1549 W07150 - S1632 W07148 - S1653 W07154 - S1647 W07204 - S1630 W07156 - S1549 W07150'
Then you can split it on the -
character:然后你可以在-
字符上拆分它:
coords = in.split('-')
and run your function on each set of coordinates:并在每组坐标上运行 function:
for coord in coords:
parse_dm(coord)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.