togeojson development is supported by 🌎 placemark.io
This converts KML, TCX, & GPX to GeoJSON, in a browser or with Node.js.
- Dependency-free
- Tiny
- Tested
- Node.js + Browsers
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.
Convert a KML document to GeoJSON. The first argument, doc
, must be a KML
document as an XML DOM - not as a string. You can get this using jQuery's default
.ajax
function or using a bare XMLHttpRequest with the .response
property
holding an XML DOM.
The output is a JavaScript object of GeoJSON data. You can convert it to a string with JSON.stringify or use it directly in libraries.
Convert KML to GeoJSON incrementally, returning a Generator that yields output feature by feature.
Convert a GPX document to GeoJSON. The first argument, doc
, must be a GPX
document as an XML DOM - not as a string. You can get this using jQuery's default
.ajax
function or using a bare XMLHttpRequest with the .response
property
holding an XML DOM.
The output is a JavaScript object of GeoJSON data, same as .kml
outputs, with the
addition of a _gpxType
property on each LineString
feature that indicates whether
the feature was encoded as a route (rte
) or track (trk
) in the GPX document.
Convert GPX to GeoJSON incrementally, returning a Generator that yields output feature by feature.
Convert a TCX document to GeoJSON. The first argument, doc
, must be a TCX
document as an XML DOM - not as a string.
Convert a TCX document to GeoJSON incrementally, returning a Generator
that yields output feature by feature. The first argument, doc
, must be a TCX
document as an XML DOM - not as a string.
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
- Style properties:
fill-color
,fill-opacity
,stroke
,stroke-opacity
,icon-color
,icon-opacity
,label-color
,label-opacity
,icon-scale
,icon-heading
,icon-offset
GPX
- Style properties:
stroke
,stroke-opacity
,stroke-width
TCX
- Line properties:
totalTimeSeconds
,distanceMeters
,maxSpeed
,avgHeartRate
,maxHeartRate
,avgSpeed
,avgWatts
,maxWatts
This also emits the geojson-coordinate-properties format to include time and other attributes that apply to each coordinate of a LineString.
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>
- Point
- Polygon
- LineString
- name & description
- ExtendedData
- SimpleData
- MultiGeometry -> GeometryCollection
- Styles with hashing
- Tracks & MultiTracks with
gx:coords
, including altitude - TimeSpan
- TimeStamp
- NetworkLinks
- GroundOverlays
- Line Paths
- Line styles
- Properties
- 'name', 'cmt', 'desc', 'link', 'time', 'keywords', 'sym', 'type' tags
- gpxx Garmin extensions on tracks and routes
- 'author', 'copyright' tags
- This repository is maintained.
- It’s available as an ES Module. If you're using a modern JavaScript bundler or using ES Modules in the browser, this makes it a bit more efficient and sometimes easier to use.
- Conversion methods are available as generators, which makes the conversion of big files more efficient.
- The command line utility was moved to tmcw/togeojson-cli, which lets this module enjoy reduced dependencies: installing @tmcw/togeojson doesn’t require any other dependencies.
- The hashing method is improved, which means that parsing is the same between browsers and Node.js, and there are no runtime dependencies.
KML's style system isn't semantic: a typical document made through official tools
(read Google) has hundreds of identical styles. So, togeojson does its best to
make this into something usable, by taking a quick hash of each style and exposing
styleUrl
and styleHash
to users. This lets you work backwards from the awful
representation and build your own styles or derive data based on the classes
chosen.
Implied here is that this does not try to represent all data contained in KML styles.
The 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.
KML is a very complex format with many features. Some of these features, like NetworkLinks, folders, and GroundOverlays, don't have a GeoJSON equivalent. In these cases, toGeoJSON doesn't convert the features. It also doesn't crash on these constructs: toGeoJSON should be able to run on all valid KML and GPX files without crashing: but for some files it may have no output.
We encourage other libraries to look into supporting these features, but support for them is out of scope for toGeoJSON.
Have a string of XML and need an XML DOM?
var dom = new DOMParser().parseFromString(xmlStr, "text/xml");