[GIS] Get points from a kml LineString

fastkmlkml

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.

Best Answer

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))
Related Question