Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

togeojson

mapbox33.6kBSD-2-Clausedeprecated0.16.0

This module has moved: please install @mapbox/togeojson instead

convert KML and GPX to GeoJSON

kml, geojson, gpx, geo, parser, formatter, input, leaflet, maps

readme

Build status Coverage status stable

Convert KML and GPX to GeoJSON.

This converts KML & GPX to GeoJSON, in a browser or with Node.js.

  • <input checked="" disabled="" type="checkbox"> Dependency-free
  • <input checked="" disabled="" type="checkbox"> Tiny
  • <input checked="" disabled="" type="checkbox"> Tested
  • <input checked="" disabled="" type="checkbox"> Node.js + Browsers

Want to use this with Leaflet? Try leaflet-omnivore!

API

toGeoJSON.kml(doc)

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 like mapbox.js.

toGeoJSON.gpx(doc)

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.

CLI

Install it into your path with npm install -g togeojson.

~> togeojson file.kml > file.geojson

Node.js

Install it into your project with npm install --save togeojson.

// using togeojson in nodejs

var tj = require('togeojson'),
    fs = require('fs'),
    // node doesn't have xml parsing or a dom. use xmldom
    DOMParser = require('xmldom').DOMParser;

var kml = new DOMParser().parseFromString(fs.readFileSync('foo.kml', 'utf8'));

var converted = tj.kml(kml);

var convertedWithStyles = tj.kml(kml, { styles: true });

Browser

Download it into your project like

wget https://raw.github.com/tmcw/togeojson/gh-pages/togeojson.js
<script src='jquery.js'></script>
<script src='togeojson.js'></script>
<script>
$.ajax('test/data/linestring.kml').done(function(xml) {
    console.log(toGeoJSON.kml(xml));
});
</script>

toGeoJSON doesn't include AJAX - you can use jQuery for just AJAX.

KML Feature Support

  • <input checked="" disabled="" type="checkbox"> Point
  • <input checked="" disabled="" type="checkbox"> Polygon
  • <input checked="" disabled="" type="checkbox"> LineString
  • <input checked="" disabled="" type="checkbox"> name & description
  • <input checked="" disabled="" type="checkbox"> ExtendedData
  • <input checked="" disabled="" type="checkbox"> SimpleData
  • <input checked="" disabled="" type="checkbox"> MultiGeometry -> GeometryCollection
  • <input checked="" disabled="" type="checkbox"> Styles with hashing
  • <input checked="" disabled="" type="checkbox"> Tracks & MultiTracks with gx:coords, including altitude
  • <input checked="" disabled="" type="checkbox"> TimeSpan
  • <input checked="" disabled="" type="checkbox"> TimeStamp
  • <input disabled="" type="checkbox"> NetworkLinks
  • <input disabled="" type="checkbox"> GroundOverlays

GPX Feature Support

  • <input checked="" disabled="" type="checkbox"> Line Paths
  • <input checked="" disabled="" type="checkbox"> Properties
    • <input checked="" disabled="" type="checkbox"> 'name', 'cmt', 'desc', 'link', 'time', 'keywords', 'sym', 'type' tags
    • <input checked="" disabled="" type="checkbox"> 'author', 'copyright' tags

FAQ

What is hashing?

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.

Protips:

Have a string of XML and need an XML DOM?

var dom = (new DOMParser()).parseFromString(xmlStr, 'text/xml');

changelog

0.16.0

  • Support for waypoint, link, and type tags in GPX

0.15.0

  • Supports the cmt (comment) tag in GPX data.

0.14.2

  • Fixes a potential crash with QGIS-generated GPX files.

0.14.0

  • Now includes TimeStamp property from Placemarks, if it exists.

0.13.0

  • Added support for StyleMap elements in Google-flavored KML
  • Improved test coverage
  • Made # prefix for internal style references optional
  • Uses eslint for code style uniformity

0.12.2

  • Fix # prefix on exported hex colors

0.12.1

  • Fix trackpoints with elevation=0 having their elevation skipped

0.12.0

  • Fix rte based GPX based tracks
  • Add CDATA support to KML

0.11.0

  • Add heartrate support for GPX tracks
  • Fix elevation support
  • Fix test runner

0.10.1

  • Fix an IE9 error: IE9 'supports' XMLSerializer inasmuch as it will create an instance that fails always and hard.

0.10.0

  • Encode timestamps along lines in GPX and KML as a coordTimes property.

0.9.0

  • Encode KML id attributes on Placemarks as GeoJSON Feature id properties.

0.8.0

  • Support for the gx:Track and gx:MultiTrack extensions to GPX

0.7.0

  • GPX conversion creates MultiLineString geometries when given multiple track segments.

0.6.0

0.5.0

  • Elevation values along linestrings and in points for GPX are now parsed correctly into Z coordinate values in GeoJSON.

0.4.2

  • No longer bundles xmldom for browsers

0.3.1

  • Stricter check for browser versus browserify when requiring xmldom

0.3.0

  • Support for pipes and streams
echo "LineString(0 0, 10 10)" | wellknown | tokml | togeojson > map.geojson

0.2.0

  • Improve documentation
  • Make style hashing on by default, remove options object

0.1.1

  • Fix global leak.

0.1.0

  • Comments
  • GPX Support

0.0.1

  • Support GeometryCollections as themselves.