osmtogeojson

repository·gh-pages·Indexed 20 days ago

https://github.com/tyrasd/osmtogeojson

A tool and Node.js library for converting OpenStreetMap (OSM) data in XML or JSON formats into GeoJSON. Optimized for polygon detection and multipolygon support, it is used by the Overpass Turbo project. It provides a CLI for file conversion and a programmatic API with options for flattening properties and customizing polygon detection.

Tokens
2.4K
Snippets
7
Records
13
Agent score
63%

What's inside osmtogeojson

  1. Understand the GeoJSON output format

    gh-pages

    The output is a GeoJSON FeatureCollection. The library produces one feature for:

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

    Feature Properties

    Each feature has an id property (e.g., node/123). The properties object contains:

    • type: The OSM data type.
    • id: The OSM id.
    • tags: A collection of all tags.
    • meta: Metainformation (e.g., version, timestamp, user).
    • relations: An array of relations the feature belongs to. Each relation object contains role, rel (the relation ID), and reltags.
    • tainted: A boolean flag set to true if the geometry is incomplete (e.g., missing nodes or ways).

    Note: If flatProperties: true is used, the properties object is flattened to provide a concise list of IDs, metadata, and tags without nested objects.

  2. Use osmtogeojson as a Node.js library

    gh-pages

    To use osmtogeojson within a Node.js project, install it as a dependency and require it in your script.

    Installation

    $ npm install osmtogeojson

    Usage

    var osmtogeojson = require('osmtogeojson');
    osmtogeojson(osm_data);
    var osmtogeojson = require('osmtogeojson');
    osmtogeojson(osm_data);
  3. Use osmtogeojson in the browser

    gh-pages

    You can include osmtogeojson in your web application by adding the script tag to your HTML.

    Usage

    <script src='osmtogeojson.js'></script>
    
    <script>
      osmtogeojson(osm_data);
    </script>
    <script src='osmtogeojson.js'></script>
    
    osmtogeojson(osm_data);
  4. How osmtogeojson handles input formats

    gh-pages

    The osmtogeojson function automatically detects the input format and uses the appropriate internal conversion logic:

    1. OSM XML: Detected if the data is an instance of XMLDocument or has childNodes. It uses _osmXML2geoJSON to parse the XML structure.
    2. Overpass JSON: Detected if the input is not an XML document. It uses _overpassJSON2geoJSON to parse the JSON structure.

    Both paths eventually converge on a common _convert2geoJSON logic to build the final GeoJSON structure.

  5. Flatten GeoJSON properties using flatProperties

    gh-pages

    By default, osmtogeojson produces a nested properties structure containing type, id, tags, relations, and meta. If you set options.flatProperties to true, the properties are merged into a single flat object. The resulting structure for each feature will be:

    "properties": {
      "type": "...",
      "id": "type/id",
      "tag_key_1": "value1",
      "tag_key_2": "value2",
      ... 
    }

    This is useful for simplifying the data for consumption by tools that expect a flat attribute list.

  6. Use the osmtogeojson CLI to convert OSM data

    gh-pages

    The osmtogeojson command-line tool converts OpenStreetMap (OSM) data in various formats (XML, JSON, PBF) into GeoJSON. You can provide a file path as an argument or pipe data directly into the command via stdin.

    Basic Usage

    osmtogeojson input.osm > output.geojson

    Supported Input Formats

    The tool automatically detects the format from the file extension, but you can specify it manually using the -f flag. Supported formats include:

    • osm (or xml): OpenStreetMap XML files.
    • json: OpenStreetMap JSON files.
    • pbf: OpenStreetMap PBF binary files.
    • auto: Automatic detection (default).

    If no file is provided, the tool reads from stdin.

    osmtogeojson input.osm > output.geojson
  7. Call the osmtogeojson(data, options) API

    gh-pages

    The core function converts OSM data into a GeoJSON FeatureCollection.

    Parameters:

    • data: The OSM data. This can be an XML DOM or OSM JSON.
    • options (optional): An object to configure the conversion.

    Options:

    • flatProperties: (boolean, default: false) If true, the resulting GeoJSON feature's properties will be a simple key-value list instead of a structured JSON object containing tags and meta.
    • uninterestingTags: A blacklist of tag keys or a callback function used to decide if a feature is "interesting" enough to be included as a GeoJSON feature.
    • polygonFeatures: A JSON object or callback function used to determine if a closed way should be treated as a Polygon or LineString.
  8. Configure osmtogeojson options

    gh-pages

    When calling osmtogeojson(data, options), you can pass an options object to customize the conversion process. Key options include:

    • verbose: (Boolean) If true, enables warning messages in the console for skipped or invalid geometries (e.g., multipolygons without coordinates, ways without nodes, or tainted geometries).
    • flatProperties: (Boolean) If true, flattens the GeoJSON feature properties. Instead of a nested structure, it merges meta, tags, and a generated id (format: type/id) directly into the top-level properties object.
    • polygonFeatures: (Object or Function) Defines which OSM ways should be treated as Polygon instead of LineString.
      • If a Function: It receives the tags object and should return true if the way is a polygon.
      • If an Object: It acts as a lookup table where keys are tag keys. Values can be:
        • true: Any way with this tag key is a polygon.
        • An object with included_values: A map of values that trigger polygon status.
        • An object with excluded_values: A map of values that prevent polygon status.
      • Note: Ways with area=no are explicitly treated as non-polygons.
  9. Define custom polygon detection with polygonFeatures

    gh-pages

    To control how OSM ways are classified as Polygon vs LineString, use the polygonFeatures option.

    Using a function:

    const options = {
      polygonFeatures: (tags) => {
        return tags.building === 'yes' || tags.highway === 'residential';
      }
    };

    Using a configuration object:

    const options = {
      polygonFeatures: {
        building: true, // Any way with a 'building' tag is a polygon
        highway: {
          included_values: { residential: true, service: true }
        },
        natural: {
          excluded_values: { water: true }
        }
      }
    };
  10. Output NDJSON instead of FeatureCollection

    gh-pages

    By default, the CLI outputs a single GeoJSON FeatureCollection. If you need to process features one by one (e.g., for streaming or large datasets), use the --ndjson flag. This will output each feature as a single line of JSON, delimited by newlines. Note that using --ndjson automatically enables the minification (-m) flag.

    osmtogeojson --ndjson input.osm > output.ndjson
    osmtogeojson --ndjson input.osm > output.ndjson
  11. Install and use the osmtogeojson CLI

    gh-pages

    You can install osmtogeojson globally via npm to convert OSM XML files to GeoJSON directly from your terminal.

    Installation

    $ npm install -g osmtogeojson

    Basic Usage

    $ osmtogeojson file.osm > file.geojson

    Handling Large Files

    For files larger than 100 MB, you may encounter 'process out of memory' errors. It is recommended to increase the memory limit using Node's --max_old_space_size flag. A good rule of thumb is to allocate 4-5 times the input data size in MB.

    On Unix systems, you can run the command like this:

    $ node --max_old_space_size=8192 `which osmtogeojson` large.osm > large.geojson
    #!/bin/bash
    $ npm install -g osmtogeojson
    $ osmtogeojson file.osm > file.geojson
  12. Convert OSM data to GeoJSON with osmtogeojson()

    gh-pages

    The primary API for this library is the osmtogeojson(data, options, featureCallback) function. It accepts OpenStreetMap data in either XML format or Overpass JSON format and converts it into a GeoJSON FeatureCollection.

    Parameters

    • data: The input data. This can be an XMLDocument (for OSM XML) or a JSON object (for Overpass JSON).
    • options (optional): A configuration object to customize the conversion process (see Configure osmtogeojson options).
    • featureCallback (optional): A function called for every GeoJSON feature generated during the conversion. This allows for streaming or processing features individually instead of waiting for the full collection to be built.
    const osmtogeojson = require('osmtogeojson');
    
    // Example with Overpass JSON
    const overpassData = { /* ... JSON from Overpass API ... */ };
    const geojson = osmtogeojson(overpassData);
    
    // Example with a callback for individual features
    osmtogeojson(overpassData, {}, (feature) => {
      console.log('Generated feature:', feature);
    });