@mapbox/togeojson

WebJar for @mapbox/togeojson

License

License

BSD 2-Clause
Categories

Categories

Geo Business Logic Libraries Geospatial JSON Data
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

mapbox__togeojson
Last Version

Last Version

0.16.0
Release Date

Release Date

Type

Type

jar
Description

Description

@mapbox/togeojson
WebJar for @mapbox/togeojson
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/mapbox/togeojson

Download mapbox__togeojson

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/mapbox__togeojson/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>mapbox__togeojson</artifactId>
    <version>0.16.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/mapbox__togeojson/
implementation 'org.webjars.npm:mapbox__togeojson:0.16.0'
// https://jarcasting.com/artifacts/org.webjars.npm/mapbox__togeojson/
implementation ("org.webjars.npm:mapbox__togeojson:0.16.0")
'org.webjars.npm:mapbox__togeojson:jar:0.16.0'
<dependency org="org.webjars.npm" name="mapbox__togeojson" rev="0.16.0">
  <artifact name="mapbox__togeojson" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='mapbox__togeojson', version='0.16.0')
)
libraryDependencies += "org.webjars.npm" % "mapbox__togeojson" % "0.16.0"
[org.webjars.npm/mapbox__togeojson "0.16.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.webjars.npm : xmldom jar [0.1.19,0.2)
org.webjars.npm : concat-stream jar [1.5.1,1.6)
org.webjars.npm : minimist jar [1.2.0]

Project Modules

There are no modules declared in this project.

Build status Coverage status stable FOSSA Status

Convert KML and GPX to GeoJSON.

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

  • Dependency-free
  • Tiny
  • Tested
  • 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 @mapbox/togeojson.

~> togeojson file.kml > file.geojson

Node.js

Install it into your project with npm install --save @mapbox/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.githubusercontent.com/mapbox/togeojson/master/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

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

GPX Feature Support

  • Line Paths
  • Line styles
  • Properties
    • 'name', 'cmt', 'desc', 'link', 'time', 'keywords', 'sym', 'type' tags
    • '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.

Why doesn't toGeoJSON support NetworkLinks?

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.

Should toGeoJSON support feature X from KML?

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.

Protips:

Have a string of XML and need an XML DOM?

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

License

FOSSA Status

org.webjars.npm

Mapbox

Mapbox is the location data platform for mobile and web applications. We're changing the way people move around cities and explore our world.

Versions

Version
0.16.0