将 hddd° mm.mm′ 转换为十进制度



我通过电子邮件附带的HTML文档获取数据(不要问为什么…(。我需要从该文件中读取GPS坐标,并希望使用OSM生成路线。我把GPS坐标作为一个字符串没有问题,但我真的很难把它们形成OSM可以使用的东西。

GPS坐标看起来是这样的:N53°09.20 E009°11.82,分裂不是问题,但我需要将它们形成正常的纬度和经度(53.119897,7.944012(

有没有人也有同样的问题,或者有没有我可以使用的图书馆?

以下代码可用于将您提供的格式中的度、分和秒转换为十进制经度和纬度:

import re
coords = "N53°09.20 E009°11.82"
regex = "N(d+)°(d+).(d+) E(d+)°(d+).(d+)"
match = re.split(regex, coords)
x = int(match[1]) + (int(match[2]) / 60) + (int(match[3]) / 3600)
y = int(match[4]) + (int(match[5]) / 60) + (int(match[6]) / 3600)
print("%f, %f" %(x, y))

输出:

53.155556, 9.206111

如果你的坐标只有度和十进制分钟,那么代码可以稍作修改,如下所示:

import re
coords = "N53°09.20 E009°11.82"
regex = "N(d+)°(d+).(d+) E(d+)°(d+).(d+)"
match = re.split(regex, coords)
x = int(match[1]) + ((int(match[2]) + (int(match[3]) / 100)) / 60)
y = int(match[4]) + ((int(match[5]) + (int(match[6]) / 100)) / 60)

print("%f, %f" %(x, y))

输出:

53.153333, 9.197000

最新更新