Permalink
Browse files

use tiny-osmpbf to process pbf files

replaces and closes #28

still todo (see #61):
* tests
* documentation
* browser support?
  • Loading branch information...
1 parent beb94fd commit 1ea8a32e75fe8165f297ca6d666ad3003a3a4e1e @tyrasd committed Nov 24, 2016
Showing with 20 additions and 3 deletions.
  1. +19 −3 osmtogeojson
  2. +1 −0 package.json
View
@@ -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');
@@ -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)
@@ -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();
View
@@ -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": {

0 comments on commit 1ea8a32

Please sign in to comment.