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

Package detail

osmtogeojson

tyrasd19.8kMIT3.0.0-beta.5TypeScript support: included

convert OSM to geojson

openstreetmap, geojson

readme

osmtogeojson

Converts OSM data to GeoJSON. Try the demo!

This code is used in and maintained by the overpass turbo project.

Build Status

Usage

command line tool

Installation:

  $ npm install -g osmtogeojson

Usage:

$ osmtogeojson file.osm > file.geojson

Supported command line options are shown with:

$ osmtogeojson --help

When working with extra large data files (≳ 100 MB) it is recommended to run the programm with a little extra memory to avoid process out of memory errors. The easiest way to do this is by running the command as node <path-to-osmtogeojson> and setting the --max_old_space_size=… parameter to the available memory size in MB (osmtogeojson typically needs about 4-5 times the input data size). On a Unix system, you can use the following command:

$ node --max_old_space_size=8192 `which osmtogeojson` large.osm > large.geojson

nodejs library

Installation:

$ npm install osmtogeojson

Usage:

var osmtogeojson = require('osmtogeojson');
osmtogeojson(osm_data);

browser library

<script src='osmtogeojson.js'></script>

osmtogeojson(osm_data);

API

osmtogeojson( data, options )

Converts OSM data into GeoJSON.

  • data: the OSM data. Either as a XML DOM or in OSM JSON.
  • options: optional. The following options can be used:
    • flatProperties: If true, the resulting GeoJSON feature's properties will be a simple key-value list instead of a structured json object (with separate tags and metadata). default: false
    • uninterestingTags: Either a blacklist of tag keys or a callback function. Will be used to decide if a feature is interesting enough for its own GeoJSON feature.
    • polygonFeatures: Either a json object or callback function that is used to determine if a closed way should be treated as a Polygon or LineString. read more

The result is a javascript object of GeoJSON data:

GeoJSON

The GeoJSON produced by this library will include exactly one GeoJSON-feature for each of the following OSM objects (that is everything that is also visible in overpass turbo's map view):

  • all unconnected or interesting tagged nodes (POIs)
  • all ways (except uninteresting multipolygon outlines)
  • all multipolygons (simple multipolygons with exactly one closed outer way are present via their outer way)

All data is given as a FeatureCollection. Each Feature in the collection has an id property that is formed from the type and id of the original OSM object (e.g. node/123) and has the member properties containing the following data:

  • type: the OSM data type
  • id: the OSM id
  • tags: a collection of all tags
  • meta: metainformaton about the feature (e.g. version, timestamp, user, etc.)
  • relations: an array of relations the feature is member of. Each relation is encoded as an object literal containing the following properties: role (membership role), rel (the relation's id) and reltags (contains all tags of the relation)
  • tainted: this flag is set when the feature's geometry is incomplete (e.g. missing nodes of a way or missing ways of a multipolygon)

If the option flatProperties is set to true, the properties object will not contain any nested object literals, but directly provide a concise id, meta data and the tags of the respective OSM object.

changelog

3.0.0-beta.5

  • update dependencies

3.0.0-beta.4

  • update dependencies

3.0.0-beta.3

3.0.0-beta.2

  • output linestrings for waterway relations, too
  • improve handling of data with duplicate osm objects

3.0.0-beta.1

  • make multipolygon/route handlers robust against empty members

3.0.0-beta.0

  • (breaking) objects are by default returned with flat (un-nested) properties ("flatProperties" option is enabled by default)
  • create (Multi)LineString features for route relations
  • add pbf file support
  • add newline delimited GeoJSON output mode (ndjson compatible)
  • new API option: feature callback for custom output
  • new API option: allow customized object deduplication handling

2.2.12

backported from 3.0.0-dev: de-namespace full geometry content in output (internal state leak)

2.2.11

revert "use strict" because of some issues on older (0.…) nodejs

2.2.10

  • fix another undeclared variable breaking the module in strict mode
  • enable "use strict";

2.2.9

split off polygon detection data: https://github.com/tyrasd/osm-polygon-features

2.2.8

fix variable leaking into global scope

2.2.7

fix a bug where loading certain complex out geom content resulted in invalid polygon geometries

2.2.6

add bower support #45

2.2.5

add -m parameter that minifies output json

2.2.4

fixed a bug where full geometry information caused additional Point features in the output

2.2.3

  • updates to polygon detection: natural=cliff is not automatically an area, golf=* is rendered as polygons
  • speed optimizations for xml input in CLI mode #34

2.2.1

  • fix bug with ref-less, clipped full geometry ways in JSON mode

2.2.0

  • support for Overpass API "full" geometry

2.1.0

  • implemented support for Overpass API geometry types "center" and "bounds"
  • new command line option -n to make properties numeric
  • added verbose option/mode that displays some debug info to the console/stderr

2.0.5

  • support input files larger than 256 MB. #17

2.0.4

  • fix unresolved xml entities in command line mode

2.0.2

  • fix a dangling dependency (which led fresh installations to crash prematurely)

2.0.0

  • simpler API (module exports as a plain function), old .toGeojson still available as a fallback
  • output (multi)polygons with consistent winding orders
  • improve handling of simple multipolygons
  • use browserify for browser library (comes bundeled with dependencies)
  • some minor speed improvements

1.4.0

  • fix various speed bottlenecks (xml parsing, geojson construction, …)
  • better performance when handling large xml files with the cli tool:

1.3.1

  • add --help and --version parameters to cli tool
  • fix bug with not automatically detected file formats in pipe mode (cli tool)

1.3.0

  • more versatile cli tool (can read osm json, optionally enhanced output)
  • many more unit tests
  • fixed bugs with some partially incomplete data

1.2.1

  • fix wrong (inverse) logic in uninterestingTags callback evaluation (backported from master)

1.2.0

  • add demo page
  • support for pipes in cli tool: echo '<osm><node lat="1.23" lon="3.21" id="-1" /></osm>' | osmtogeojson
  • add flat-properties output mode (default for the cli tool)
  • add options to override default uninterestingTags and polygonFeatures
  • better documentation
  • more test cases
  • further improvements in polygon feature detection

1.1.1

  • bugfix: remove unneeded debugging code

1.1.0

  • command line tool added

1.0.0

  • initial release