
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
togeojson
Advanced tools
This converts KML & GPX to GeoJSON, in a browser or with Node.js.
Want to use this with Leaflet? Try leaflet-omnivore!
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.
Install it into your path with npm install -g togeojson
.
~> togeojson file.kml > file.geojson
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 });
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.
gx:coords
, including altitudeKML'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.
Have a string of XML and need an XML DOM?
var dom = (new DOMParser()).parseFromString(xmlStr, 'text/xml');
FAQs
convert KML and GPX to GeoJSON
We found that togeojson demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 22 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.