【发布时间】:2020-12-26 10:58:34
【问题描述】:
我需要替换 KML 文件中的所有坐标:
<coordinates>
24.6206,58.0265,10
24.6218,58.0245,260
24.6234,58.0221,510
24.6257,58.0191,760
</coordinates>
我尝试过这种方式但是root最后是空的:
from pykml import parser
import requests
kmlFile = requests.get('https://predict.habhub.org/kml.php',params=ploadsKml)
root = parser.fromstring(bytes(kmlFile.text, encoding='utf8'))
coordinates_before = root.Document.Placemark.LineString.coordinates
string_updated_coordinates="'\\n34.4534,56.5675\\n34.4786,57.1274\\n'"
updated_coordinates = literal_eval(string_updated_coordinates)
coordinates_before._setText(updated_coordinates)
print (root)
但是root是空的。
这里是完整的 kml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Flight Path</name>
<description><![CDATA[Flight data be3aa40 <br>Site: 58.02651, 24.62057 at 14:5 on 26/12/2020]]></description>
<Placemark>
<name>Path</name>
<description>Test v.78</description>
<styleUrl>#yellowPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tesselate>1</tesselate>
<altitudeMode>absolute</altitudeMode>
<coordinates>
24.6206,58.0265,10
24.6218,58.0245,260
24.6234,58.0221,510
24.6257,58.0191,760
</coordinates>
</LineString></Placemark>
<Placemark>
<name>Balloon Launch</name>
<description>GPS TEST 58.0265, 24.6206 </description>
<Point><coordinates>32.6206,78.0265,0</coordinates></Point>
</Placemark>
</Document></kml>
仅需要替换这 4 个坐标,将剩余信息保留在 kml 文件中。
【问题讨论】:
-
Kml 文件是从predict.habhub.org 下载的。我使用原始坐标进行一些计算并生成新坐标。之后,新坐标必须替换原来的坐标。
-
您的 KML 无效,
<Document><description>标记包含无效字符(<和>)。 -
我更正了描述标签
-
是的,我导入解析器和请求
-
如果要打印root,需要序列化,
print(etree.tostring(etree.ElementTree(root), pretty_print=True))