Python Forum
Read KML files, edit items, and rewrite files?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read KML files, edit items, and rewrite files?
#1
Hello,

From existing KML files, I need to read all Placemark items, and then add some elements to each:
  • If they're waypoints, add a "styleUrl" element
  • If they're LineString (tracks/routes, really), add a "Style" element
… and save the whole thing into a new KML file.

SimpleKML works fine to create brand new KML files, but how can I read existing KML files first?

Thank you.
Reply
#2
check fastKML
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks.

"Unfortunately, simplekml is just a kml generator, it cannot read and manipulate existing kml, only create it. You will have to use an alternative, such as pyKML." (Source)
Reply
#4
Just started… and already stuck :-/ Seems like the parser doesn't like UTF-8:

Try 1
from fastkml import kml
from lxml import etree

INPUT_KML_FILE = "myfile.kml"

#BAD with open(INPUT_KML_FILE, 'rt', encoding="utf-8") as myfile:
with open(INPUT_KML_FILE, 'rt') as myfile:
	doc=myfile.read()
k = kml.KML()
k.from_string(doc)

Traceback (most recent call last):
  File "C:\fastkml\Edit.KML.py", line 8, in <module>
    k.from_string(doc)
  File "C:\Python38\lib\site-packages\fastkml\kml.py", line 89, in from_string
    element = etree.fromstring(
  File "src\lxml\etree.pyx", line 3237, in lxml.etree.fromstring
  File "src\lxml\parser.pxi", line 1871, in lxml.etree._parseMemoryDocument

ValueError: Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
Try 2
from fastkml import kml
from lxml import etree

INPUT_KML_FILE = "myfile.kml"

k = kml.KML()
doc = etree.parse(INPUT_KML_FILE)
#ValueError: can only parse strings
k.from_string(doc)
Try 3
with open(INPUT_KML_FILE, 'rb') as myfile:
#with open(INPUT_KML_FILE, 'rt') as myfile:
	doc=myfile.read()
k = kml.KML()
k.from_string(doc)

Traceback (most recent call last):
  File "C:\fastkml\Edit.KML.py", line 43, in <module>
    k.from_string(doc)
  File "C:\Python38\lib\site-packages\fastkml\kml.py", line 101, in from_string
    feature.from_element(document)
  File "C:\Python38\lib\site-packages\fastkml\kml.py", line 991, in from_element
    feature.from_element(folder)
  File "C:\Python38\lib\site-packages\fastkml\kml.py", line 1030, in from_element
    feature.from_element(folder)
  File "C:\Python38\lib\site-packages\fastkml\kml.py", line 1035, in from_element
    feature.from_element(placemark)
  File "C:\Python38\lib\site-packages\fastkml\kml.py", line 1077, in from_element
    geom.from_element(line)
  File "C:\Python38\lib\site-packages\fastkml\geometry.py", line 413, in from_element
    geom = self._get_geometry(element)
  File "C:\Python38\lib\site-packages\fastkml\geometry.py", line 347, in _get_geometry
    return LineString(coords)
  File "C:\Python38\lib\site-packages\pygeoif\geometry.py", line 334, in __init__
    raise ValueError
ValueError
What would you recommend?

"FastKML reads KML snippets from strings rather than files. Reading files using Pythons standard document parser makes assumptions about encoding and it the UTF / Unicode encoding string is forwarded to lxml it does not want to parse it and wants to perform its own interpretation or obtain the data as raw bytes. It would be good to extend fastKML to include a from_file method in the parser to circumvent this and pass the file directly to lxml for encoding interpretation etc. […] The error is indicative of encoding conflicts and direct parsing from files will be really useful for my use-cases."

https://www.bountysource.com/issues/4910...from-files
Reply
#5
Turns out there's something in the KML file I used that Fastkml/lxml doesn't like: The code runs OK with another KML file.

Too bad the error message doesn't say on which line the error lies, instead of just saying "#ValueError".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 937 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 682 Feb-10-2024, 07:08 AM
Last Post: buran
  Copy Paste excel files based on the first letters of the file name Viento 2 346 Feb-07-2024, 12:24 PM
Last Post: Viento
  Class test : good way to split methods into several files paul18fr 4 401 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 398 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Upload Files to Azure Storage Container phillyfa 6 578 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  merge all xlsb files into csv mg24 0 303 Nov-13-2023, 08:25 AM
Last Post: mg24
  Newbie question about switching between files - Python/Pycharm Busby222 3 543 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Move Files based on partial Match mohamedsalih12 2 743 Sep-20-2023, 07:38 PM
Last Post: snippsat
  open python files in other drive akbarza 1 632 Aug-24-2023, 01:23 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020