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 🙏

© 2024 – Pkg Stats / Ryan Hefner

osm2obj

v3.0.0

Published

Converts an OSM XML file to OSM objects as a transform stream

Downloads

2,046

Readme

osm2obj

Build Status npm js-standard-style

Streaming parser from OSM XML to OSM objects

Implements a Node Transform Stream. Takes a readable stream of OSM XML and outputs a stream of objects compatible with Overpass OSM JSON. Also reads OsmChange XML and outputs the same format but with an additional property action which is one of create, modify, delete. Uses sax-js to work in both node and the browser.

Table of Contents

Install

npm install osm2obj

Usage

var fs = require('fs')
var Osm2Obj = require('../lib/osm2obj')

var rs = fs.createReadableStream(__dirname + './osm.xml')

rs.pipe(new Osm2Obj()).pipe(process.stdout)

Example Output

// node
{
  type: 'node',
  id: 1,
  version: 0,
  timestamp: '2013-09-05T19:38:11.187Z',
  uid: 1,
  user: 'gregor',
  lat: 0,
  lon: 0,
  tags: { null: 'island' }
}

// way
{
  type: 'way',
  id: 3,
  version: 3,
  timestamp: '2013-09-05T19:38:11Z',
  changeset: 49,
  nodes: [ 19, 20, 21, 22, 26, 27 ],
  tags: { name: 'York St' }
}

// relation
{
  type: 'relation',
  id: 1,
  members: [
    {
      type: 'relation',
      ref: 1745069,
      role: 'outer'
    },
    {
      type: 'relation',
      ref: 172789
    }
  ],
  tags: {
    from: 'Konrad-Adenauer-Platz',
    name: 'VRS 636'
  }
}

Example: Actual JSON output

var through = require('through2')
var fs = require('fs')
var path = require('path')
var Osm2Obj = require('../lib/osm2obj')

var rs = fs.createReadStream(path.join(__dirname, '../test/osm.xml'))

var jsonStream = through.obj(write, end)

jsonStream.push('[')
var start = true

rs.pipe(new Osm2Obj()).pipe(jsonStream).pipe(process.stdout)

function write (row, enc, next) {
  if (!start) {
    this.push(', ')
  } else {
    start = false
  }
  next(null, JSON.stringify(row, null, 2))
}

function end (next) {
  this.push(']\n')
}

API

var Osm2Obj = require('osm2obj')

var stream = new Osm2Obj(opts)

Create a transform stream with:

  • opts.coerceIds - coerce id-type fields (id, uid, version, changeset, ref) to Number (default false) - useful for osm-p2p-db where ids can be strings.
  • opts.bounds - Also parse bounds (default true)
  • opts.types - An array of element types you are interested in, e.g. opts.types = ['node'] (default ['node', 'way', 'relation', 'changeset'])
  • opts.strict - Be a jerk about XML (default false). In strict mode will throw an error if:
    • XML is badly formatted
    • Case of element names differs from spec
    • Root node is not one of osm, osmChange, diffResult
    • An action element (create, modify, delete) appears when the root is not osmChange
    • Any element in the XML which is not one of create, modify, delete, node, way, relation, changeset, bounds, nd, tag, member

Any attribute that is not a valid OSM XML attribute will be ignored (see WHITELISTS). tag, member, or nd elements without the required attributes will throw an error. The readable side of the stream is in objectMode.

Parses OsmChange XML. Output objects will have property action which is one of create, modify, delete.

If a <delete> block in osmChange XML has an if-unused attribute, then each object within the block will have a prop ifUnused=true. The value of the attribute is ignored, as per the OSM API 0.6 spec.

stream.parse(str)

Parse str and return the result. Will throw any error.

Contribute

PRs welcome. Please follow JS Standard Style. Right now this could do with some tests. If you are feeling ambitious, this could be sped up by using node-expat on node. The interface is similar to sax-js and it should be possible to wrap this to use sax-js on the browser and node-expat on the server using the browserify browser field

License

MIT (c) 2016, Digital Democracy.