Python Convert KML to GeoJSON

By | November 17, 2017

GeoJSON and KML are formats for storing spatial information. KML files are commonly found with Google Earth type applications, so it can be useful to convert KML to GeoJSON.  The python library kml2geojson can be used to convert KML to GeoJSON. kml2geojson works as either a tool from the command line, or can be used as a library within other programs.

Installation

Install kml2geojson using pip:

pip install kml2geojson

Use As A Command Line Tool

kml2geojson can be used as a tool from the command line. You can use it this way if you have some kml files that you just need to convert, without any additional programming.

The general usage of kml2geojson is:

k2g [options] kml_file output_dir

For basic usage we can run:

k2g test.kml json_files

Which will convert the file ‘test.kml’ to ‘test.json’ (a GeoJSON ‘Feature Collection‘) and put the output into a directory ‘json_files’. If the directory ‘json_files’ does not exist, then it is created.

To see more advanced features, run:

k2g --help

 

Or checkout the kml2geojson documentation.

Use As a library

kml2geojson can also be called as a library so you can convert kml to geojson within other programs.

Basic conversion using the kml2geojson library can be done with:

kml2geojson.main.convert(kml_file, output_directory)
import kml2geojson
kml2geojson.main.convert('./test.kml', '../data/json_files')

See more advanced options at the the kml2geojson documentation.