Code Monkey home page Code Monkey logo

Comments (8)

LingboTang avatar LingboTang commented on May 29, 2024 11

@whatnick You shouldn't replace the newline character, you could just do myfile.read().encode('utf-8') and you should be able to parse your file through fastkml. Although I think a user shouldn't do this encoding/decoding explicitly, it should be wrapped in their implementation and hide from user. @cleder

from fastkml.

withoutwaxaryan avatar withoutwaxaryan commented on May 29, 2024 5

Since the documentation hasn't been updated , even for Python 3 + , I wrote a detailed article here : https://medium.com/@wwaryan/the-definite-only-guide-to-fastkml-58b8e19b8454

Cheers!

from fastkml.

samifouad avatar samifouad commented on May 29, 2024 4

@whatnick You shouldn't replace the newline character, you could just do myfile.read().encode('utf-8') and you should be able to parse your file through fastkml. Although I think a user shouldn't do this encoding/decoding explicitly, it should be wrapped in their implementation and hide from user. @cleder

There should be a documentation update with the method you described presented to new users of this. Because right now if you follow along with the starter examples, you get an error only solved by using this snippet.

from fastkml.

answerquest avatar answerquest commented on May 29, 2024 1

yep, we need a documentation update here: https://fastkml.readthedocs.io/en/latest/usage_guide.html#read-a-kml-file-string

from fastkml.

davepeck avatar davepeck commented on May 29, 2024

At least when lxml is configured, it seems like the from_string(...) method actually expects bytes, not a string.

Ultimately, lxml wants to decide the encoding from the encoding declaration in the document itself (like <?xml version="1.0" encoding="UTF-8"?>) if it can.

Perhaps the method should be renamed to from_bytes(...)?

from fastkml.

LingboTang avatar LingboTang commented on May 29, 2024

@samifouad I haven't worked with this lib for a while, not sure if they have already updated.

from fastkml.

galloramiro avatar galloramiro commented on May 29, 2024

Agree with the comumentation update

from fastkml.

Shohreh avatar Shohreh commented on May 29, 2024

None of this works. What else could I try? Thank you.

#with open(INPUT_KML_FILE, 'rb') as myfile: #AttributeError: 'bytes' object has no attribute 'encode'
#with open(INPUT_KML_FILE, 'rt', encoding="utf-8") as myfile: #ValueError
#with open(INPUT_KML_FILE, 'rt') as myfile: #ValueError
#with open(INPUT_KML_FILE, 'r') as myfile: #ValueError
#BAD doc=myfile.read()
doc = myfile.read().encode('utf-8')

k = kml.KML()
k.from_string(doc)


Edit: It worked with another KML file. Is there a way to get FastKML/lxml to say which line it didn't like?

k = kml.KML()
with open(INPUT_KML_FILE,'rb') as kmlFile:
k.from_string(kmlFile.read())

c:\fastkml>Edit.KML.py
Traceback (most recent call last):
  File "C:\fastkml\Edit.KML.py", line 15, in <module>
    k.from_string(kmlFile.read())
  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

from fastkml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.