/osm-read

an openstreetmap XML and PBF data parser for node.js and the browser

Primary LanguageJavaScriptGNU Lesser General Public License v3.0LGPL-3.0

osm-read - an openstreetmap XML and PBF parser for node.js and the browser

1) Introduction
2) Usage Examples
2.1) Simple Usage Example
2.2) Parse OSM XML from URL Example
2.3) PBF random access parser
3) TODOs
4) License
5) Contact

------------------------------------------------------------------------
Introduction

osm-read parses openstreetmap XML and PBF files as described in
http://wiki.openstreetmap.org/wiki/OSM_XML and
http://wiki.openstreetmap.org/wiki/PBF_Format


------------------------------------------------------------------------
Simple Usage Example

The following code is used to parse openstreetmap XML or PBF files in a
SAX parser like callback way.

osmread.parse({
    filePath: 'path/to/osm.xml',
    endDocument: function(){
        console.log('document end');
    },
    bounds: function(bounds){
        console.log('bounds: ' + JSON.stringify(bounds));
    },
    node: function(node){
        console.log('node: ' + JSON.stringify(node));
    },
    way: function(way){
        console.log('way: ' + JSON.stringify(way));
    },
    error: function(msg){
        console.log('error: ' + msg);
    }
});

------------------------------------------------------------------------
Parse PBF in the browser

Build or update the browser bundle 'osm-read-pbf.js' with browserify:
$ npm run browserify

To install browserify (http://browserify.org/):
$ npm install -g browserify

Example, see also example/pbf.html:

    <script src="../osm-read-pbf.js"></script>
    <script>
        pbfParser.parse({
            filePath: 'test.pbf',
            endDocument: function(){
                console.log('document end');
            },
            node: function(node){
                console.log('node: ' + JSON.stringify(node));
            },
            way: function(way){
                console.log('way: ' + JSON.stringify(way));
            },
            error: function(msg){
                console.error('error: ' + msg);
                throw msg;
            }
        });
    </script>

As an alternative to passing an URL in "filePath", the option "buffer" can be 
used to pass an already loaded ArrayBuffer object:

        var buf = ... // e.g. xhr.response

        pbfParser.parse({
            buffer: buf,
        ...

------------------------------------------------------------------------
Parse OSM XML from URL Example

Currently you can only parse OSM data in XML from URLs. Here's an example:

osmread.parse({
    url: 'http://overpass-api.de/api/interpreter?data=node(51.93315273540566%2C7.567176818847656%2C52.000418429293326%2C7.687854766845703)%5Bhighway%3Dtraffic_signals%5D%3Bout%3B',
    format: 'xml',
    endDocument: function(){
        console.log('document end');
    },
    bounds: function(bounds){
        console.log('bounds: ' + JSON.stringify(bounds));
    },
    node: function(node){
        console.log('node: ' + JSON.stringify(node));
    },
    way: function(way){
        console.log('way: ' + JSON.stringify(way));
    },
    error: function(msg){
        console.log('error: ' + msg);
    }
});


------------------------------------------------------------------------
PBF random access parser

The following code allows to create a random access openstreetmap PBF
file parser:

osmread.createPbfParser({
    filePath: 'path/to/osm.pbf',
    callback: function(err, parser){
        var headers;

        if(err){
            // TODO handle error
        }

        headers = parser.findFileBlocksByBlobType('OSMHeader');

        parser.readBlock(headers[0], function(err, block){
            console.log('header block');
            console.log(block);

            parser.close(function(err){
                if(err){
                    // TODO handle error
                }
            });
        });
    }
});

Don't forget to close the parser after usage!


------------------------------------------------------------------------
TODOs

XML parser:
* parse timestamps

PBF parser:
* parse relations

------------------------------------------------------------------------
License

See file COPYING for details.


------------------------------------------------------------------------
Contact

* author: Markus Peröbner <markus.peroebner@gmail.com>