Giter VIP home page Giter VIP logo

togeojson's Introduction

build status Coverage Status

Convert KML and GPX to GeoJSON.

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

It is

  • Dependency-free
  • Tiny
  • Written in vanilla javascript that's jshint-friendly
  • Tested

It is not

  • Concerned about ugly extensions to KML
  • Concerned with having an 'internal format' of its own

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.

Using it as a console utility

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

~> togeojson file.kml > file.geojson

Using it as a nodejs library

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 jsdom
    jsdom = require('jsdom').jsdom;

var kml = jsdom(fs.readFileSync('foo.kml', 'utf8'));

var converted = tj.kml(kml);

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

Using it as a browser library

Download it into your project like

wget https://raw.github.com/tmcw/togeojson/gh-pages/togeojson.js

Or install with Bower

bower install togeojson
<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

Supported:

  • Point
  • Polygon
  • LineString
  • name & description
  • ExtendedData
  • SimpleData
  • MultiGeometry -> GeometryCollection
  • Styles with hashing
  • Tracks & MultiTracks with gx:coords, including altitude
  • TimeSpan

Not supported yet:

  • NetworkLinks
  • GroundOverlays

GPX

Supported:

  • Line Paths
  • 'name', 'desc', 'author', 'copyright', 'link', 'time', 'keywords' 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.

Protips:

Have a string of XML and need an XML DOM?

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

togeojson's People

Contributors

tmcw avatar frankrowe avatar themarex avatar yohanboniface avatar asolove avatar swatinem avatar joeybaker avatar leplatrem avatar jfirebaugh avatar vool avatar tyrasd avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.