-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathpbf.js
75 lines (62 loc) · 1.92 KB
/
pbf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
The pbf parser is responsible for configuring and executing a pbf parser and
returning a valid readable stream.
**/
var fs = require('fs'),
pbf2json = require('pbf2json'),
settings = require('pelias-config').generate(require('../schema')),
features = require('../config/features'),
path = require('path');
// Create a new pbf parser stream
function createPbfStream(opts){
var conf = config(opts);
validatePath( conf.file, 'failed to stat pbf file: ' + conf.file );
validatePath( conf.leveldb, 'failed to stat leveldb path: ' + conf.leveldb );
validateTags( conf.tags );
return pbf2json.createReadStream(conf);
}
// Generate configuration options for pbf2json, apply default
// configurations where not explicitly specified.
function config(opts){
if (!opts){
opts = {};
}
// Use datapath setting from your config file
// @see: github://pelias/config for more info.
if(!opts.file){
var filename = settings.imports.openstreetmap.import[0].filename;
opts.file = path.join(settings.imports.openstreetmap.datapath, filename);
}
// Use leveldb setting from your config file
// @see: github://pelias/config for more info.
if(!opts.leveldb){
opts.leveldb = settings.imports.openstreetmap.leveldbpath;
}
// Use default parser tags
if(!opts.tags){
// check if we import venues
opts.importVenues = settings.imports.openstreetmap.import[0].importVenues;
if(opts.importVenues){
opts.tags = features.tags.concat(features.venue_tags);
} else {
opts.tags = features.tags;
}
}
return opts;
}
// Check path exists
function validatePath( path, message ){
try {
fs.statSync( path );
} catch( e ){
throw new Error( message );
}
}
// Validate the tag list
function validateTags( tags ){
if( !Array.isArray(tags) || !tags.length ) {
throw new Error( 'invalid tags' );
}
}
module.exports.config = config;
module.exports.parser = createPbfStream;