首页 > 解决方案 > 将 hddd° mm.mm' 转换为十进制度

问题描述

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

GPS 坐标如下所示:N53°09.20 E009°11.82,分割不是问题,但我需要将它们形成正常的纬度和经度,如 (53.119897, 7.944012)。

有没有人有同样的问题或者有我可以使用的库?

标签: pythonmapsgisopenstreetmaplatitude-longitude

解决方案


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

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

推荐阅读