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?
#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


Messages In This Thread
RE: Read KML files, edit items, and rewrite files? - by Winfried - Aug-21-2020, 11:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to generating multiple json files using python script dzgn989 4 164 Yesterday, 03:09 PM
Last Post: deanhystad
  Filer and sort files by modification time in a directory tester_V 5 345 May-02-2024, 05:39 PM
Last Post: tester_V
  [SOLVED] Loop through directories and files one level down? Winfried 3 279 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 539 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
Question Right way to open files with different encodings? Winfried 2 308 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 381 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Using zipfile module - finding folders not files darter1010 2 324 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,127 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 914 Feb-10-2024, 07:08 AM
Last Post: buran
  Copy Paste excel files based on the first letters of the file name Viento 2 493 Feb-07-2024, 12:24 PM
Last Post: Viento

Forum Jump:

User Panel Messages

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