Read KML files, edit items, and rewrite files? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Read KML files, edit items, and rewrite files? (/thread-29160.html) |
Read KML files, edit items, and rewrite files? - Winfried - Aug-20-2020 Hello, From existing KML files, I need to read all Placemark items, and then add some elements to each:
SimpleKML works fine to create brand new KML files, but how can I read existing KML files first? Thank you. RE: Read KML files, edit items, and rewrite files? - buran - Aug-20-2020 check fastKML RE: Read KML files, edit items, and rewrite files? - Winfried - Aug-20-2020 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) RE: Read KML files, edit items, and rewrite files? - Winfried - Aug-21-2020 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 ValueErrorWhat 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/49106032-fastkml-parsing-from-files RE: Read KML files, edit items, and rewrite files? - Winfried - Aug-21-2020 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". |