• Stars
    star
    102
  • Rank 335,584 (Top 7 %)
  • Language
    JavaScript
  • License
    GNU Lesser Genera...
  • Created over 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

  1. Introduction
  2. Usage Examples
    1. Simple Usage Example
    2. Parse OSM XML from URL Example
    3. PBF random access parser
  3. Version Upgrade Guide
  4. TODOs
  5. License
  6. 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


Continuous Integration

Build Status


Simple Usage Example

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

var parser = 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));
    },
    relation: function(relation){
        console.log('relation: ' + JSON.stringify(relation));
    },
    error: function(msg){
        console.log('error: ' + msg);
    }
});

// you can pause the parser
parser.pause();

// and resume it again
parser.resume();

Parse PBF in the browser

The browser bundle 'osm-read-pbf.js' provides a global variable 'pbfParser' with a 'parse' method.

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));
        },
        relation: function(relation){
            console.log('relation: ' + JSON.stringify(relation));
        },
        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,
...

A third alternative is to let the user choose a local file using the HTML5 File API, passing the file object as "file" option:

<input type="file" id="file" accept=".pbf">
<script>
    document.getElementById("file").addEventListener("change", parse, false);

    function parse(evt) {
        var file = evt.target.files[0];

        pbfParser.parse({
            file: file,
        ...

See also example/file.html


Build

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

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));
    },
    relation: function(relation){
        console.log('relation: ' + JSON.stringify(relation));
    },
    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!


Version Upgrade Guide

Sometimes APIs change... they break your code but things get easier for the rest of us. I'm sorry if a version upgrade gives you some extra hours. To makes things a little less painfull you can find migration instructions in the file ChangeLog.


TODOs

XML parser:

  • parse timestamps

License

See file COPYING for details.


Contact

author: Markus Peröbner [email protected]

More Repositories

1

tagfs

Fuse tag file system
Python
121
star
2

eclipse-remote-control

Eclipse plug-in project which adds remote control features to eclipse.
Java
25
star
3

minecraft-world-io

export minecraft worlds to povray
Python
7
star
4

emacs-perkeep

perkeep integration for emacs
Emacs Lisp
6
star
5

whiteboard-cleaner

Clean up photographs of whiteboard or paper notes
Shell
5
star
6

everarch

the hopefully ever lasting archive
C
4
star
7

statifier

collects dynamically linked libraries for an elf binary
Python
4
star
8

pop3-server

POP3 server library
JavaScript
4
star
9

gps4earth

Display your current GPS position in google earth.
Python
3
star
10

tagfs-utils

utility scripts for tagfs
Python
3
star
11

fs-performance

file system performance measuring tool
Python
3
star
12

chromebook

chromebook pixel configurations for the use with debian linux
Shell
3
star
13

icalendar

An iCalendar formatter for node.js
JavaScript
2
star
14

ts-index

a fast typescript artifact index
Emacs Lisp
2
star
15

locator

an i18n files lookup tool
Emacs Lisp
2
star
16

last_ripper

ripper for last.fm tracks from youtube
Python
2
star
17

python-crucible

python bindings for the crucible REST API
Python
2
star
18

batl

A templating language based on bash shell scripts.
Makefile
1
star
19

tagfs-gui

tag file system GUI tools
Python
1
star
20

sharefs

synchronizes remote user space file systems
Shell
1
star
21

python-sonar

python bindings for the sonar REST API
Python
1
star
22

maven-tree

maven dependency tree graph builder
Python
1
star
23

db-fitnesse

A DB fixture plugin for the FitNesse automated testing tool.
Java
1
star
24

povray-scene-merger

merges povray scenes into one file
Python
1
star
25

gmftext

A textual DSL for generating GMF editors within Eclipse.
Java
1
star
26

temper-q

Queries temperature from a TEMPer 413d:2107 USB thermometer.
Python
1
star
27

node_cpdb

crazy pili's data base for node.js
JavaScript
1
star