I'm still very new to python, i am trying to export the locations on a list (List2) into a kml file which will then display the results on google maps. I have no idea really what i am doing and atm all i am getting is a syntax error around every ,", symbol. Can someone help me with this please.
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()
Hard-coding XML output to create a KML file in a series of print statements can be error-prone and hard to maintain. Rather use a Python KML library such as simplekml or pyKML to generate the KML. The simplekml API simplifies writing KML and also produces valid KML with code that is cleaner and easier to understand.
import simplekml
kml = simplekml.Kml()
for row in List2:
kml.newpoint(description=row[0],
coords=[(row[2], row[1])]) # lon, lat, optional height
kml.save("test.kml")
Using this test input for a single point:
List2 = [ [ 'description', 51.500152, -0.126236 ] ] # description, lat, lon
The KML output would be this:
<?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>
In your code you haven't defined the variable f
which should reference the file-object you want to write to. You could either do
f = open("KML.txt", "w")
f.write("<KML_File>\n")
...
f.close()
or better:
with open("KML.txt", "w") as f:
f.write("<KML_File>\n")
...
which makes sure to always close the file even if some code in between fails.
For writing XML-files you might want to take a look at the Python xml-package .
In brief:
KMLFile
to f
or vice versa.close()
method like this : f.close()
.Your corrected code:
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()
In addition, if you do not want to write the f.close()
line and let python manage the file closure:
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")
Eventually, if you do not want to have many +
into your f.write()
lines, you can also opt for the format()
method:
f.write("\t\t\t<coordinates>{}{}/coordinates>".format(row[2], row[1]))
import geopandas as gpd
polys = gpd.GeoDataFrame(df)
polys.to_file(r'../docs/database.kml', driver = 'KML')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.