npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-osm-stream

v0.2.2

Published

A fast and flexible NodeJS-based streaming parser for OpenStreetMap (.osm) files.

Downloads

24

Readme

node-osm-stream

Overview

A fast and flexible NodeJS-based streaming parser for OpenStreetMap (.osm) files.

  • Both incoming and outgoing streams available for piping from/to other streams
  • Able to deal with large .osm files easily without loading the whole file onto memory
  • Provided with the flexibility to work with and modify OSM objects (nodes, ways, relations) easily as it streams to you

Powered by node-expat for blazing fast parsing.

Table of Content


Quick Start

Install

npm install node-osm-stream

No fuss, no lints

Example: Reading from a local .osm file

var fs = require('fs');
var OSMStream = require('node-osm-stream');
var parser = OSMStream();

// open a local .osm filestream
fs.createReadStream('./path/to/file.osm')
	.pipe(parser);

parser.on('node', function(node, callback){
	// Modify current node object as you wish
	// and pass it back to the callback.
	// Or pass 'null' or 'false' to prevent the object being 
	// written to outgoing stream
	console.log(node);
	callback(node);
});

parser.on('way', function(way, callback){ callback(way); });

parser.on('relation', function(way, callback){ callback(relation); });

Easy-peasy, lemon-squeezy.

More examples

More advanced examples are available in the ./examples folder

Stream and format outgoing data to console (process.stdout)

Source: /examples/stream-to-stdout.js

To run example:

node ./examples/stream-to-stdout.js

Writing to a JSON file using Writeable steam (fs.createWriteStream)

Source: /examples/write-to-json.js

To run example:

node ./examples/write-to-json.js

API

Class: OSMStream

Class inherited from stream.Transform

Methods

All methods are inherited from stream.Transform

Events

Events: 'node', 'way', 'relation'

When an object (node/way/relation) from the .osm file has been parsed fully with its attributes and children (if any) ready, it will emit a 'node' or 'way' or 'relation' event, depending on the object type.

You can modify the outgoing data and passing it back to the callback. Or you can prevent the data from being passed downstream by passing back a null or false

It's important to note that since this is a streaming parser, any other objects (ways/relations) that may have referenced a skipped node may still hold its reference. It is up to the implementation to remove its references.

To see an example of a possible implementation, take a look at /examples/write-to-json.js

Note: If this event was registered, the callback must be passed back.

parser.on('node', function(node, callback) {
  // modify the node object as necessary and pass back to callback
  // or send a null or false to prevent it from going downstream
  callback(node);
});

parser.on('way', function(way, callback) {
  ...
  callback(way);
});
parser.on('relation', function(relation, callback) {
  ...
  callback(relation);
});

Event: 'writeable'

When a chunk of data is ready to be written to the outgoing stream, it will emit a 'writeable' event.

You can modify the outgoing data and passing it back to the callback. Or you can prevent the data from being passed downstream by passing back a null or false

Note: If this event was registered, the callback must be passed back.

parser.on('writeable', function(data, callback) {
  // there is some data to be passed to outgoing stream
  // modify 'data' as needed
  callback(data);
});

Event: 'flush'

After all the written data has been consumed through the outgoing stream, it will emit a 'flush' event. This will happened before the 'end' event is sent to signal the end of the readable side.

You can choose to pass in as much data as needed by passing it through the callback. Any data passed back will be written to the outgoing stream before the 'end' event is emitted.

Note: If this event was registered, the callback must be passed back.

parser.on('flush', function(callback) {
  var last_data = 'This is the last string sent to the outgoing stream';
  callback(last_data);
});

Events inherited from stream.Transform

In addition to the events above, the following are events inherited from stream.Transform class. Please refer to the offical documentation for more info: NodeJS API Documentation: stream.Transform

  • Event: 'readable'
  • Event: 'data'
  • Event: 'end'
  • Event: 'close'
  • Event: 'error'
  • Event: 'drain'
  • Event: 'finish'
  • Event: 'pipe'
  • Event: 'unpipe'

Test

npm test

Known Issues

Credits

Links

License

Copyright (c) 2014 Hafiz Ismail. This software is licensed under the MIT License.