python - 将 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
推荐阅读
- mysql - 需要使用 Laravel 将购物车详细信息插入 Mysql 数据库
- ms-access - 强制模态 MS-Access 表单显示为选项卡
- php - 无法在服务器上使用 php 上传文件,出现错误 7
- razor - Params are being sent again after EventCallback is triggered/invoked
- sql - Strange issue combining IF and CASE in TSQL query
- python - Error when trying to override __init__() : TypeError: __init__() takes 1 positional argument but 2 were given
- pyarrow - Pyarrow table memory compared to raw csv size
- c# - 在 Hangfire 上每 9 分钟执行一次的 cron 作业不工作
- node.js - 在 Mongoose 中跨模块管理连接
- c# - 从 postman 发送 post 请求返回状态码 415