convert KML, TCX, and GPX to GeoJSON, without the fuss
See the codetogeojson development is supported by 🌎 placemark.io
This converts KML, TCX, & GPX to GeoJSON, in a browser or with Node.js.
This is a JavaScript library that lets projects convert KML and GPX to GeoJSON. If you're looking for a command line too, use @tmcw/togeojson-cli. If you want to convert one KML or GPX file, use my online tool. If you want to convert another format, consider GDAL.
In addition to converting KML’s <ExtendedData> verbatim, @tmcw/togeojson
also encodes parts of KML, GPX, and TCX files that otherwise would be lost.
KML
fill-color, fill-opacity, stroke, stroke-opacity,
icon-color, icon-opacity, label-color, label-opacity, icon-scale,
icon-heading, icon-offset, icon-offset-unitsGPX
stroke, stroke-opacity, stroke-widthTCX
totalTimeSeconds, distanceMeters, maxSpeed,
avgHeartRate, maxHeartRate, avgSpeed, avgWatts, maxWattsThis also emits the geojson-coordinate-properties format to include time and other attributes that apply to each coordinate of a LineString.
Example of working with Ground Overlays in Mapbox GL JS
KML GroundOverlays are now supported, and transformed into Features with Polygon geometries. They have two defined properties:
{
"@geometry-type": "groundoverlay",
"icon": "https://url.to.image…"
}
Both gx:LatLonQuad and LatLonBox-based ground overlays are supported.
If you're using this with TypeScript, you'll want to also install:
@types/geojson@xmldom/xmldomThese will give you accurate types for both input and output
types. Due to TypeScript limitations, it's currently necessary
to install @xmldom/xmldom for accurate input types even
if you aren't using that module for parsing XML.
If you have ideas for how to improve this, please
comment on this PR!.
Use @tmcw/togeojson-cli to use this software as a command-line tool.
Install it into your project with npm install --save @tmcw/togeojson.
// using togeojson in nodejs
const tj = require("@tmcw/togeojson");
const fs = require("fs");
// node doesn't have xml parsing or a dom. use xmldom
const DOMParser = require("xmldom").DOMParser;
const kml = new DOMParser().parseFromString(fs.readFileSync("foo.kml", "utf8"));
const converted = tj.kml(kml);
// The ES Module provides named exports, to import kml, gpx,
// and other parts of the module by name.
import { kml } from "@tmcw/togeojson";
<script type="module">
import { kml } from "https://unpkg.com/@tmcw/togeojson?module";
fetch("test/data/linestring.kml")
.then(function (response) {
return response.text();
})
.then(function (xml) {
console.log(kml(new DOMParser().parseFromString(xml, "text/xml")));
});
</script>
gx:coords, including altitudeThe NetworkLink KML construct allows KML files to refer to other online or local KML files for their content. It's often used to let people pass around files but keep the actual content on servers.
In order to support NetworkLinks, toGeoJSON would need to be asynchronous and perform network requests. These changes would make it more complex and less reliable in order to hit a limited usecase - we'd rather keep it simple and not require users to think about network connectivity and bandwith in order to convert files.
NetworkLink support could be implemented in a separate library as a pre-processing step if desired.
This module should support converting all KML and GPX features that have commonplace equivalents in GeoJSON.
Have a string of XML and need an XML DOM? There are two main options:
DOMParser, the native platform XML parserWe recommend that you use xmldom, not the platform. DOMParser requires XML to be valid, which means that any XML namespaces that a KML, GPX, or TCX file contains are valid. A lot of existing data is invalid XML, and will be parsed only in part by DOMParser, but can be fully parsed by xmldom.
Using xmldom (recommended):
const xmldom = require("@xmldom/xmldom");
const dom = new xmldom.DOMParser().parseFromString(xmlStr, "text/xml");
Using DOMParser:
var dom = new DOMParser().parseFromString(xmlStr, "text/xml");
784 followers · starred Sep 2019
302 followers · starred Jul 2024
688 followers · starred Aug 2026
47 followers · starred Dec 2021
convert KML, TCX, and GPX to GeoJSON, without the fuss
See the codetogeojson development is supported by 🌎 placemark.io
This converts KML, TCX, & GPX to GeoJSON, in a browser or with Node.js.
This is a JavaScript library that lets projects convert KML and GPX to GeoJSON. If you're looking for a command line too, use @tmcw/togeojson-cli. If you want to convert one KML or GPX file, use my online tool. If you want to convert another format, consider GDAL.
In addition to converting KML’s <ExtendedData> verbatim, @tmcw/togeojson
also encodes parts of KML, GPX, and TCX files that otherwise would be lost.
KML
fill-color, fill-opacity, stroke, stroke-opacity,
icon-color, icon-opacity, label-color, label-opacity, icon-scale,
icon-heading, icon-offset, icon-offset-unitsGPX
stroke, stroke-opacity, stroke-widthTCX
totalTimeSeconds, distanceMeters, maxSpeed,
avgHeartRate, maxHeartRate, avgSpeed, avgWatts, maxWattsThis also emits the geojson-coordinate-properties format to include time and other attributes that apply to each coordinate of a LineString.
Example of working with Ground Overlays in Mapbox GL JS
KML GroundOverlays are now supported, and transformed into Features with Polygon geometries. They have two defined properties:
{
"@geometry-type": "groundoverlay",
"icon": "https://url.to.image…"
}
Both gx:LatLonQuad and LatLonBox-based ground overlays are supported.
If you're using this with TypeScript, you'll want to also install:
@types/geojson@xmldom/xmldomThese will give you accurate types for both input and output
types. Due to TypeScript limitations, it's currently necessary
to install @xmldom/xmldom for accurate input types even
if you aren't using that module for parsing XML.
If you have ideas for how to improve this, please
comment on this PR!.
Use @tmcw/togeojson-cli to use this software as a command-line tool.
Install it into your project with npm install --save @tmcw/togeojson.
// using togeojson in nodejs
const tj = require("@tmcw/togeojson");
const fs = require("fs");
// node doesn't have xml parsing or a dom. use xmldom
const DOMParser = require("xmldom").DOMParser;
const kml = new DOMParser().parseFromString(fs.readFileSync("foo.kml", "utf8"));
const converted = tj.kml(kml);
// The ES Module provides named exports, to import kml, gpx,
// and other parts of the module by name.
import { kml } from "@tmcw/togeojson";
<script type="module">
import { kml } from "https://unpkg.com/@tmcw/togeojson?module";
fetch("test/data/linestring.kml")
.then(function (response) {
return response.text();
})
.then(function (xml) {
console.log(kml(new DOMParser().parseFromString(xml, "text/xml")));
});
</script>
gx:coords, including altitudeThe NetworkLink KML construct allows KML files to refer to other online or local KML files for their content. It's often used to let people pass around files but keep the actual content on servers.
In order to support NetworkLinks, toGeoJSON would need to be asynchronous and perform network requests. These changes would make it more complex and less reliable in order to hit a limited usecase - we'd rather keep it simple and not require users to think about network connectivity and bandwith in order to convert files.
NetworkLink support could be implemented in a separate library as a pre-processing step if desired.
This module should support converting all KML and GPX features that have commonplace equivalents in GeoJSON.
Have a string of XML and need an XML DOM? There are two main options:
DOMParser, the native platform XML parserWe recommend that you use xmldom, not the platform. DOMParser requires XML to be valid, which means that any XML namespaces that a KML, GPX, or TCX file contains are valid. A lot of existing data is invalid XML, and will be parsed only in part by DOMParser, but can be fully parsed by xmldom.
Using xmldom (recommended):
const xmldom = require("@xmldom/xmldom");
const dom = new xmldom.DOMParser().parseFromString(xmlStr, "text/xml");
Using DOMParser:
var dom = new DOMParser().parseFromString(xmlStr, "text/xml");
784 followers · starred Sep 2019
302 followers · starred Jul 2024
688 followers · starred Aug 2026
47 followers · starred Dec 2021