Trying to make a KML file in Python

Question:

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()
Asked By: Michael Cooper

||

Answers:

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.

Answered By: Simon Fromme

In brief:

  • You should change KMLFile to f or vice versa.
  • You should call the 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("tt<decription>" + str(row[0]) + "</description>")
    f.write("tt<Point>")
    f.write("ttt<coordinates>" + str(row[2]) + str(row[1]) + "</coordinates>")
    f.write("tt</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("tt<decription>" + str(row[0]) + "</description>")
        f.write("tt<Point>")
        f.write("ttt<coordinates>" + str(row[2]) + str(row[1]) + "</coordinates>")
        f.write("tt</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("ttt<coordinates>{}{}/coordinates>".format(row[2], row[1]))
Answered By: PurpleJo

Hard-coding XML output to create a KML file in a series of print statements is 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 produces valid KML with code that is cleaner and easier to understand.

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")

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 )
  • df should contain description, geometry, name.
Answered By: CodeMonkey
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.