Skip to content

Commit

Permalink
use tiny-osmpbf to process pbf files
Browse files Browse the repository at this point in the history
replaces and closes #28

still todo (see #61):
* tests
* documentation
* browser support?
  • Loading branch information
tyrasd committed Nov 24, 2016
1 parent beb94fd commit 1ea8a32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
22 changes: 19 additions & 3 deletions osmtogeojson
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var osmtogeojson = require('./'),
xmldom = new (require('xmldom').DOMParser)(),
osmxmlParser = require('./parse_osmxml.js'),
JSONStream = require('JSONStream'),
osmpbfParser = require('tiny-osmpbf'),
geojsonNumeric = require('geojson-numeric'),
pack = require('./package.json');

Expand All @@ -38,6 +39,7 @@ if (!format) {
if (filename.match(/\.osm$/i)) format = 'osm';
if (filename.match(/\.xml$/i)) format = 'osm';
if (filename.match(/\.json$/i)) format = 'json';
if (filename.match(/\.pbf$/i)) format = 'pbf';
}
// fall back to the native JSON parser if the file is small enough
// (unfortunately, the streaming JSON parser isn't very fast)
Expand Down Expand Up @@ -89,28 +91,42 @@ default:
}

function legacyParsers(data) {
if (!data) data = ''; else data = data.toString();
if (!data) data = new Buffer([]);

var sampleStr = data.slice(0,100).toString();

if (format === 'auto') {
if (data.match(/^\s*</)) // (osm) xml files begin with a "<"
if (data.readUInt32BE(0).toString(16) === 'd' &&
data.readUInt16BE(4).toString(16) === 'a09' &&
data.slice(6,15).toString() === 'OSMHeader') // osmpbf binary file magic
format = 'pbf';
else if (sampleStr.match(/^\s*</)) // (osm) xml files begin with a "<"
format = 'osm';
else if (data.match(/^\s*{/)) // osm json files begin with a "{"
else if (sampleStr.match(/^\s*{/)) // osm json files begin with a "{"
format = 'json';
else {
format = 'unknown';
}
}
switch (format) {
case 'xmldom':
data = data.toString();
data = xmldom.parseFromString(data);
break;
case 'json':
case 'nativejson':
data = data.toString();
data = JSON.parse(data);
break;
case 'osm':
case 'fastxml':
data = data.toString();
data = osmxmlParser.parseFromString(data);
break;
case 'pbf':
case 'tinypbf':
data = osmpbfParser(data);
break;
default:
process.stderr.write('This doesn\'t look like a recognized file format.\n');
opt.showHelp();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"htmlparser2": "3.5.1",
"optimist": "~0.3.5",
"osm-polygon-features": "^0.9.1",
"tiny-osmpbf": "^0.1.0",
"xmldom": "~0.1.16"
},
"devDependencies": {
Expand Down

0 comments on commit 1ea8a32

Please sign in to comment.