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