I have a KML file with a LineString. I'm trying to load the points in Python. I'd like to use the FastKml library, but am open to other solutions. FastKml has one example for reading KML, but I have not been able to convert that to get the LineString points.

  • Do you have a sample file?
    – ustroetz
    Commented Mar 13, 2014 at 16:56

3 Answers 3


You can open the file with OGR and access the points from the layer:

from osgeo import ogr

ds = ogr.Open('KML_samples.kml')

for lyr in ds:
    for feat in lyr:
        geom = feat.GetGeometryRef()
        if geom != None:
            for i in range(0, geom.GetPointCount()):
                print (geom.GetPoint(i))
  • I did not know that ogr could access kml files, really good to know! However when I run this script on a .kml file with both LineString and Points it only returns the coordinates of point features (and only some of them...) and not coordinates of points in the 'LineString'
    – GeoSharp
    Commented Mar 13, 2014 at 18:35
  • Do you have a sample file? Otherwise it is difficult to reproduce what you wrote.
    – ustroetz
    Commented Mar 13, 2014 at 22:09
  • I used the exact code you have above. For the file I posted here it just prints out 3 points, which are all cointained in a <Placemark> tag in the file. Here is the link of the sample google developers kml file I used
    – GeoSharp
    Commented Mar 13, 2014 at 22:21
  • Thanks for posting the sample file. Your file has several layers. I edited the code. Now it loops over all the layers in your file. In addition it has one feature with no geometry (which is weird). I added an if statement to catch that.
    – ustroetz
    Commented Mar 14, 2014 at 7:13

kml is a xml language focused on geographic visualization, with attention to annotation of maps/images and the navigation of them. For this reason a python script utilizing an xml parser such as xml.etree.ElementTree should be able to extract the coordinates in a fairly straightforward manner.

Understanding that for line strings they are identified by the <LineString></LineString> tag. Within the <LineString> tag is the <coordinates>...</cordinates> tag that contains the list of coordinates.

Here is an example below of how to extract the coordinates for all LineStrings in a .kml

import xml.etree.ElementTree as ET
filePath = r'C:\yourFile.kml'
tree = ET.parse(filePath) 
lineStrings = tree.findall('.//{http://www.opengis.net/kml/2.2}LineString')

for attributes in lineStrings:
    for subAttribute in attributes:
        if subAttribute.tag == '{http://www.opengis.net/kml/2.2}coordinates':
            print subAttribute.tag, subAttribute.text

This method can be used to extract the coordinates of other named tags in a .kml file, containing the coordinates tag. The key is knowing the namespace that your kml file is using in my case it is {http://www.opengis.net/kml/2.2}. But that info can also be accessed once you have created a tree via the following code:

root = tree.getroot()

root.tag then contains the namespace for the version of kml you are using.

See Google Developers guide on LineStrings here for more info on the syntax of the <LineString> tag

  • 1
    KML isn't really "modified .html". Perhaps you wanted to say it is just a form of XML?
    – BradHards
    Commented Mar 13, 2014 at 11:24
  • Yes I can parse this with pure Python and regex, or shell and regex, etc. But there are several Python libraries that are built for KML, so I am trying to use them. It ought to be a fraction of LOC, especially if there are multiple named LineStrings in one KML.
    – mankoff
    Commented Mar 13, 2014 at 12:06
  • Understood. I was just going off of your question. You said you have a kml with a LineString and you did not mention other libraries but simply solutions.
    – GeoSharp
    Commented Mar 13, 2014 at 16:29
  • Thanks BradHards. I have modified the post to show the correction.
    – GeoSharp
    Commented Mar 13, 2014 at 16:40
  • +1 JamesSLC. KML is XML, so XML libraries like xml.etree are the way to go. I've written some KML helpers for xml.etree in pypi.python.org/pypi/keytree.
    – sgillies
    Commented Mar 14, 2014 at 4:16

In fastkml, the process that I have found is this :

  • Read KML File
  • Parse Placemarks from the file
  • Parse Geometries (if any) inside the Placemarks
  • If LineString, extract coordinates

Since you didn't mention where exactly you get stuck on in this process, I think this article can help you out throughout the process : https://medium.com/@wwaryan/the-definite-only-guide-to-fastkml-58b8e19b8454

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.