Python Libraries for Converting KML to GeoJSON

gdalgeojsonkmlogr2ogrpython

I am writing a Python script to reproject a KML file to WGS 84 and then to convert it to a GeoJSON. I am aware of and use the GDAL command line tool ogr2ogr (which was the answer to this question in this post from 2012).

I can use subprocess to use the ogr2ogr from my script, but I was wondering if there are any libraries out there that might handle this better/make it easier for me. Is there anything else out there? Or is ogr2ogr my best option?

Best Answer

Many GDAL utilities are nowadays librarified and there is no need to call the executables as subprocess. The librarified version of ogr2ogr is "Vector.Translate" and minimal Python code to convert data from KML into GeoJSON is like

from osgeo import gdal, ogr

 srcDS = gdal.OpenEx('input.kml')
 ds = gdal.VectorTranslate('output.json', srcDS, format='GeoJSON')
Related Question