Questions tagged [pykml]

Use for questions about the pyKML Python module used for creating, parsing, manipulating, and validating KML files.

pyKML is a Python package for creating, parsing, manipulating, and validating KML, a language for encoding and annotating geographic data.

pyKML is based on the lxml.objectify API which provides a Pythonic API for working with XML documents. pyKML adds additional functionality specific to the KML language.

Flat learning curve:

Concise, readable and expressive syntax, easy to learn for developers

Hello World

Example code to create a KML object then output it to a file.

from pykml.factory import KML_ElementMaker as KML
from lxml import etree
 
kml = KML.Placemark(
   KML.name("Hello World!"),
   KML.Point(
     KML.coordinates("-64.5253,18.4607")
   )
)

with open('hello.kml', 'wb') as output:
   output.write(etree.tostring(kml, pretty_print=True, xml_declaration = True)) 

This is what is generated:

<?xml version='1.0' encoding='UTF-8'?>
<Placemark xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2">
  <name>Hello World!</name>
  <Point>
    <coordinates>-64.5253,18.4607</coordinates>
  </Point>
</Placemark>

Asking Questions:

  • Before asking the question, make sure you have gone through the Examples and Tutorials. It covers all the basic functionality of pyKML.

Useful Canonicals:

35 questions
1
vote
1 answer

How to display multiple placemarks and paths?

I'm using PyKml module in order to form kml inside my Python script. I want to display path which consists of array of coordinates and also display all points as placemarks. Currently, I'm trying (without success) to do it following way doc =…
qutron
  • 1,710
  • 4
  • 18
  • 30
1
vote
1 answer

Updating coordinates in a KML file using python

I have a KML that looks like this file (only the "Document" portion is repeated another 2,000 times or so with slightly different coordinates in each entry).
1
vote
0 answers

How can I sort the children in a gx:Track?

I'm using pyKML to generate a KML file containing several gx:Tracks with several , , and tags. It seems that the schema wants these tags in order, meaning first all tags, then the tags, and so on. For my…
mwil.me
  • 1,134
  • 1
  • 19
  • 33
1
vote
1 answer

PyKML : appending a Placemark to more than one KML document

I am using PyKML to create several KML files and am running into some strange behavior that I hope someone can explain. The following reproduces the problem: from lxml import etree from pykml.factory import KML_ElementMaker as KML doc1 =…
Donna
  • 1,390
  • 1
  • 14
  • 30
1
vote
1 answer

How to reduce the coordinates decimal in a local kml file using pykml?

I have a kml file that has cell with more than 1 million character. I want to reduce the number of decimal from 12 to 3. I imported lxml and pykml. import pykml from pykml.helpers import set_max_decimal_places file1=open('\United States divisions.…
1
vote
1 answer

Appending an empty folder to an existing folder in a KML file using Python

I've been trying to figure this out on my own but I'm stuck. I want to add an empty folder to an existing folder in an existing KML file. This is what I have so far, when I open the file, there's no new folder named "test". import pykml from…
Schack
  • 833
  • 2
  • 7
  • 18
1
vote
1 answer

parsing coordinates from a single kml file with a whole lot of polygons in it

i have this kml, file a very long one, and i've managed to obtain the coordinates from a single polygon. but my loop doesn't seem to transfer to the next polygon. is there a way to "point" to the next polygon so i could obtain the coordinates from…
yvan
  • 11
  • 1
  • 2
0
votes
0 answers

Unable to use pykml library, "moduleNotFountError" error every time

I can't use the pykml library with Python 3.10, I get an moduleNotFoundError return every time. I installed with pip install pykml, installation successful but moduleNotFoundError. I downloaded the tar.zip folder and installed with python setup.py…
calform
  • 9
  • 3
0
votes
1 answer

PYKML searches the KML file for node gx: LatLonQuad> and its children

I have a KML file with the following content. Only the first part of the KML file is shown, because the problem is here. I am parsing the file with PYKML. In node GroundOverlay, there is a child gx:LatLonQuad, which again has a child "coordinates".…
0
votes
1 answer

Trying to parse KML file with pyKML and extract data

EP-1B-03 WIDEBAND [1]
0
votes
0 answers

pyKML: lxml.ettree.XMLSyntaxError: Failed to parse QName , line, column

from pykml import parser from os import path import pandas as pd kml_file = path.join( r'file.kml') with open(kml_file) as f: doc = parser.parse(f) Trying to make this one working in python and pyKML, but it's failing to parse Qname in line…
0
votes
1 answer

checking and fixing values appended to a node with lxml.objectify in python

I'm trying to modify PyKML, which uses lxml.objectify. With a track node append, this gets turned into a string with the default str() behavior. I'd like to catch appends of lists or tuples and convert them to proper place separated lines rather…
Kurt Schwehr
  • 2,638
  • 3
  • 24
  • 41
0
votes
1 answer

Parsing KML file using pyKML

I'm learning how to parse KML files in Python using the pyKML module. The specific file I'm using can be found here and I've also added it at the bottom of this post. I have saved the file on my computer and name it test.kml. After some research, I…
glpsx
  • 587
  • 1
  • 7
  • 21
0
votes
2 answers

pykml and utf8: either the input is incorrect, or I don't get it

I have a kmz file from the www and wish to read it into csv or such using pykml. The file is in UTF8, or at least it claims to - see header below. Reading it works, but triggers an error when coming on the first accented character.
Karel Adams
  • 185
  • 2
  • 19
0
votes
1 answer

Lxml error when parsing kml using pykml

Im trying to parse a kml file containing multiple placemarks using pykml. I want to edit the HTML code inside the kml's description, mainly for visualization purposes of geographic data in Google Earth. Ive researched a lot of ways to do…
Ci-em
  • 15
  • 7