尝试使用Python生成KML文件

3

我还是Python的新手,我正在尝试将列表(List2)中的位置导出到一个KML文件中,然后在Google地图上显示结果。我真的不知道我在做什么,目前我得到的都是围绕每个",符号的语法错误。有人能帮我吗?

KMLFile = open("KML.txt", "w")
f.write("<KML_File>\n")
f.write("<Document>\n")
for line in List2:
    f.write("   <Placemark>")
    f.write("       <decription>" + str(row[0]) + "</description>")
    f.write("       <Point>")
    f.write("          <coordinates>" + str(row[2]) + str(row[1])"</coordinates>")
    f.write("       </Point>")
    f.write("   </Placemark>")
f.write("</Document>\n")
f.write("</kml>\n")
KMLFile = close()
4个回答

13

在一系列的打印语句中硬编码XML输出以创建KML文件容易出错且难以维护。相反,最好使用Python KML库,例如simplekmlpyKML来生成KML。simplekml API简化了编写KML并生成有效的KML代码,使代码更清晰、更易于理解。

import simplekml

# list2 = ...some assignment with list of point data elements
kml = simplekml.Kml()
for desc,lat,lon in list2:
    kml.newpoint(description=desc,
        coords=[(lon, lat)])  # lon, lat, optional height
# save KML to a file
kml.save("test.kml")

使用此测试输入进行单点测试:

list2 = [ ('description', 51.500152, -0.126236) ] # description, lat, lon

对于 KML 输出,应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_1">
        <Placemark id="feat_2">
            <description>description</description>
            <Point id="geom_0">
                <coordinates>-0.126236,51.500152,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

3
在您的代码中,您没有定义变量f,它应该引用您想要写入的文件对象。您可以选择执行以下操作:
f = open("KML.txt", "w")
f.write("<KML_File>\n")
...
f.close()

或者更好的方法是:
with open("KML.txt", "w") as f:
    f.write("<KML_File>\n")
    ...

即使在代码中间出现错误,也会确保始终关闭文件。

如果要编写XML文件,您可能需要查看Python的xml包


我太蠢了,好吧我改过来了,但是在代码的这一部分我仍然得到一个语法错误:f.write("\t\t\t<coordinates>" + str(row[2]) + str(row[1])"</coordinates>"),就在最后一个","符号那里出了问题。 - Michael Cooper
你在 str(row[1]) 后面忘记了一个 + 符号。 - Simon Fromme

3
简要概述:
  • 你应该将KMLFile更改为f或反之亦然。
  • 你应该像这样调用close()方法:f.close()

你更正后的代码:

f = open("KML.txt", "w")
f.write("<KML_File>\n")
f.write("<Document>\n")
for line in List2:
    f.write("\t<Placemark>")
    f.write("\t\t<decription>" + str(row[0]) + "</description>")
    f.write("\t\t<Point>")
    f.write("\t\t\t<coordinates>" + str(row[2]) + str(row[1]) + "</coordinates>")
    f.write("\t\t</Point>")
    f.write("\t</Placemark>")
f.write("</Document>\n")
f.write("</kml>\n")
f.close()

此外,如果您不想编写f.close()代码行并让Python管理文件关闭:
with open("KML.txt", "w") as f:
    f.write("<KML_File>\n")
    f.write("<Document>\n")
    for line in List2:
        f.write("\t<Placemark>")
        f.write("\t\t<decription>" + str(row[0]) + "</description>")
        f.write("\t\t<Point>")
        f.write("\t\t\t<coordinates>" + str(row[2]) + str(row[1]) + "</coordinates>")
        f.write("\t\t</Point>")
        f.write("\t</Placemark>")
    f.write("</Document>\n")
    f.write("</kml>\n")

最终,如果你不想在f.write()行中加入许多+,你也可以选择使用format()方法:

f.write("\t\t\t<coordinates>{}{}/coordinates>".format(row[2], row[1]))

我是一个完全的白痴,好吧,我已经改了,但是在代码的这一部分仍然会出现语法错误:f.write("\t\t\t<coordinates>" + str(row[2]) + str(row[1])"</coordinates>")在最后一个",符号处出现了某些问题。 - Michael Cooper

-1
import geopandas as gpd

polys = gpd.GeoDataFrame(df) 
polys.to_file(r'../docs/database.kml', driver = 'KML')
  • df应包含描述、几何和名称。

Geopandas使用的Fiona不支持KML格式,因此这将无法工作。 - sermomon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接