3

I would like to parse a KML document in order to put the information (names of placemarks, coordinates) into a python array for further processing. This is what I have so far:

from fastkml import kml

with open('evalPoints.kml') as myfile:
    doc=myfile.read()
k = kml.KML()
k.from_string(doc)

outerFeature = list(k.features())
innerFeature = list(outerFeature[0].features())

placemarks = list(innerFeature[0].features())

for p in placemarks:
    coords = p.coordinates #this does not work

Unfortunately, parsing the coordinates does not work, while parsing the name of the placemarks works like a charm with p.name. What am I doing wrong?

2
  • Does this print(p.geometry) give you anything?
    – Taras
    Nov 28, 2019 at 7:44
  • Yes! It gives POINT Z (-98.88773425196743 19.19703568013584 0). The question now is, how can this POINT Z thing be transformed into pure numbers?
    – Michael
    Nov 28, 2019 at 8:00

3 Answers 3

3

Thanks to the comment of Taras, I found the answer. The coordinates can be extracted based on Shapely commands, i.e. by using p.geometry.x, p.geometry.y

1

The above question is specific to Placemarks being Points. However, there are a number of other geometries available such as LineStrings, Polygons and more.

To know how to extract coordinates from all types of geometries, you can check out this article: https://medium.com/@wwaryan/the-definite-only-guide-to-fastkml-58b8e19b8454

Also, for the other answer, Shapely is not used now. I think they use pygeoif. You can check it in geometry.py file of fastkml --> Shapely imports have all been commented.

2
  • 1
    Link-only answers are often flagged and deleted. An Answer here should have enough details that the question can be resolved without following the link.
    – Vince
    Sep 12, 2020 at 16:17
  • Thank you for letting me know. I'll make answers more specific. However, this link is not spam. Its really helpful, as its detailed, and the question above is quite open ended. Hope thats not an issue :))
    – Aryan
    Sep 12, 2020 at 16:36
0

This is what worked for my kml files:

from fastkml import geometry
from fastkml import kml

kml_filename = "foo.kml"

with open(kml_filename) as kml_file:
    doc = kml_file.read().encode('utf-8')
    k = kml.KML()
    k.from_string(doc)
    for feature0 in k.features():
        print("{}, {}".format(feature0.name, feature0.description))
        for feature1 in feature0.features():
            if isinstance(feature1.geometry, geometry.Polygon):
                polygon = feature1.geometry
                for coord in polygon.exterior.coords:
                    # these are long, lat tuples
                    print(coord)

My kml files are structured like this:

  ...
  <Document>
    <Placemark>
      ...
      <Polygon>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates>
            ...

As-is my example wouldn't be directly useful for any other kml format unless it is structured like mine, but for a quick extraction of points from a particular set of like kml it could be modified (I used dir(feature) a lot to figure out what something is at some level of descent into the document tree, maybe there are better python tools than that), or should useful as an example to build something more general out of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.