osm2geojson-lite v1.0.2
osm2geojson-lite
A lightweight (not as lightweight as xml2geojson though) yet faster convertor for OSM data whatever in XML or JSON formats to GeoJSON - much faster (the more complex the data source is, the more performance advantages it posesses) than osmtogeojson in most situations - implemented in pure JavaScript without any 3rd party dependency.
History
An internal function inside query-geo-boundary → stripped out to handle OSM XML only xml2geojson-lite → this library that supports both OSM XML and OSM/Overpass JSON
Changelog:
- Late 2018: this repository was created and the initial version was pushed to github - almost around specific usages for myself.
- 2019-2025: quite a few contributors had fixed quite a few bugs and also contributed some good ideas - thanks for their efforts!
- 2025-03-03: fixed a few bugs around non-*Polygon features processing and convert it to a pure TypeScript project (codes published to NPM have become TSC compiled from now on).
Usage
As a Node.JS Library
Installation:
$ npm install osm2geojson-liteUsage:
const osm2geojson = require('osm2geojson-lite');
let geojson = osm2geojson(osm, opts);In the Browser
<script src='your/path/to/osm2geojson-lite.js'/> let geojson = osm2geojson(osm, opts);API
osm2geojson(osm, opts)
Converts OSM data (XML/JSON) to GeoJSON.
osm: the OSM XML data in String, or OSM/Overpass JSON as object or in Stringopts?: optional, the options object, right now supports below properties/fields:completeFeature/allFeatures: the default value isfalse. When it's set totrue, the returned geojson will include all elements that meet the specified conditions inFeatureCollectionformat; otherwise, only the bare geometry of the firstrelationelement will be returned.renderTagged: the default value isfalse. When it's set totrue, the returned geojson will include all elements with tags (i.e., tagged) untilsuppressWaychanges its behavior a bit; otherwise only the unreferenced ones get returned.suppressWay/excludeWay: the default value istrue. When it's set totrue, the returnedFeatureCollectionwill exclude all referencedways even though they are tagged; otherwise the features of thoseways will be included in the resulted result as well.
Performance
- Workloads include the boundary XML and JSON of 4 administrive areas (zhucheng, hebei, tokyodo, usa)
- Call each conversion for 100 rounds to mitigate the impacts of GC and other factors
- For each script, run as many as times seperately and then calculate the average cost time (ACT for short)
- The # listed in the table below are coarse lowest values of dividing the ACT of
osmtogeojsonby the one of this library
$ cd bench
$ node o2gl-bench.js
$ node otg-bench.jsXML
| zhucheng | hebei | tokyodo | usa |
|---|---|---|---|
| >2.5x | >4.0x | >3.0x | >3.0x |
Overpass JSON
| zhucheng | hebei | tokyodo | usa |
|---|---|---|---|
| >2.5x | >11.0x | >7.0x | >5.0x |
Correctness
You can copy the converted results to geojsonlint for the correctness validation. Up until now, osm2geojson-lite behaves pretty well with all the samples (also quite representative) in the data subfolers under test and bench directories, which also outperforms osmtogeojson.
The client side example shipped along with this package, test/index.html will automatically call - because the CORS limitation, the direct post to geojsonlint was blocked, so I added a "copy to clipboard" button for you to ease the validation.geojsonlint validation service directly after each conversion, you can wait for a while to see the validation result after click the conversion button.
Node.JS version
ES5/ES6 features
Dependencies
- No 3rd party dependency
License
Copyleft
Collaborators
- tibetty
- HarelM
8 months ago
8 months ago
8 months ago
8 months ago
2 years ago
2 years ago
4 years ago
4 years ago
4 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago