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
  Correct/proper way to create save files snakes 0 486 Mar-11-2025, 06:58 PM
Last Post: snakes
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,446 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  how to download large files faster? kucingkembar 3 892 Feb-20-2025, 06:57 PM
Last Post: snippsat
  Inserting Python Buttons into KV Files edand19941 3 585 Feb-19-2025, 07:44 PM
Last Post: buran
Question [SOLVED] Right way to open files with different encodings? Winfried 3 4,566 Jan-18-2025, 02:19 PM
Last Post: Winfried
  Applications config files / Best practices aecordoba 2 2,294 Oct-23-2024, 12:56 PM
Last Post: aecordoba
  Compare 2 files for duplicates and save the differences cubangt 2 1,018 Sep-12-2024, 03:55 PM
Last Post: cubangt
  Convert Xls files into Csv in on premises sharpoint Andrew_andy9642 3 1,075 Aug-30-2024, 06:41 PM
Last Post: deanhystad
  deleting files in program files directory RRADC 6 3,198 Aug-21-2024, 06:11 PM
Last Post: snippsat
  I'm trying to merge 2 .csv files with no joy! Sick_Stigma 3 990 Aug-03-2024, 03:20 PM
Last Post: mariadsouza362

Forum Jump:

User Panel Messages

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