Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading KML Files Using Fastkml

Tags:

python

kml

I've searched around quite a bit for this specific Python module, and haven't been able to find a source which points me in the right direction.

I'm trying to read a KML file and display all of the features inside of a folder, I believe I'm achieving this using fastkml, but I have a problem.

Using the following code everything works until the line, len(k.features). I tried printing it, adding quotes as shown in the documentation, but nothing works. Can anyone point me in the right direction?

Thanks.

Example: https://github.com/cleder/fastkml

Code:

from fastkml import  kml
doc = file("Allpoints.kml").read()
k = kml.KML()
k.from_string(doc)
len(k.features())
like image 812
Vaironl Avatar asked Sep 07 '13 15:09

Vaironl


People also ask

How do I read a KML file?

Use Google Earth to open KML files You can view most simple KML files with Google Earth for Chrome (version 9) or in the Google Earth app on your mobile device. If you're unable to view complex KML files, use Google Earth for desktop (version 7), which supports all KML features.

How do I open a KML file in text editor?

Since KML files are saved in plain text, users may create KML files from scratch or by copying features from Google Earth Pro. To do this, right-click a feature in the 3D Viewer of Google Earth Pro and select Copy, then paste it into a TXT file open in a text editor. Users can then save .

What app reads KML?

Google Earth is a popular and highly suggested tool to open KML files. This wikiHow teaches you how to open KML files in Google Earth with both Mac and PC as well as the Google Earth app available for both Android and iOS devices.

How do I open a KML file in Chrome?

On chrome go to https://www.google.com/earth/ • Click Launch Earth in Chrome. Click the Menu button on the left side navigation bar. Click Settings. Scroll to the bottom of the settings and where it says Enable KML file import turn it on then click save.


1 Answers

features() returns a generator object that you can iterate over but it doesn't have a len function:

for f in k.features():
    print(f.name)

if you really need the length then you can use a list comprehension to turn the generator into a list:

features = list(k.features())
len(features)
like image 106
AChampion Avatar answered Oct 26 '22 22:10

AChampion