python - 我想从 KML 文件中提取坐标
问题描述
我正在尝试从 python 中的 kml 文件中提取坐标,但它给了我错误。下面是我的代码和 kml 文件。
代码:
from pykml import parser
root = parser.fromstring(open('task_2_sensor.kml', 'r').read())
print (root.Document.Placemark.Point.coordinates)
错误:
ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
KML 文件:
<coordinates>
13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.323018,52.499687,0 13.310096,52.4893,0 13.310096,52.4893,0 13.309909,52.48929,0 13.309909,52.48929,0 13.309753,52.489235,0
</coordinates>
</LineString>
</Placemark>
</Folder>
<LookAt>
</LookAt>
</Document>
</kml>
解决方案
如果源 KML 具有显式编码,则您必须从 KML 中删除 XML 声明行或使用parse()而不是fromstring()。
使用此表单使用 pykml 解析 KML 文件。
with open('task_2_sensor.kml', 'r') as f:
root = parser.parse(f).getroot()
print(root.Document.Placemark.Point.coordinates)
推荐阅读
- c# - ASP.NET Core Identity - 使令牌(电子邮件确认、密码重置等)在不同服务器上有效?
- c# - 如何与警报 Sur-Gard System 1 通信
- javascript - 从 unix 时间戳获取 ISOString(以微秒为单位)
- vue.js - 当我尝试在 VueJS 中加载组件时,为什么会出现此错误?
- spring-mvc - 使用 Spring 5.2 删除 eot 字体的“Vary”标头
- vue.js - vue-cli 与 vuetify 项目无法运行 IE 11 polyfilling 问题?
- spring-boot - 缺少无参数构造函数,但应该存在
- javascript - 为什么内联 XMLHttpRequest 在稍后外部启动之后