-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosm-hyperbee-index.js
48 lines (42 loc) · 1.01 KB
/
osm-hyperbee-index.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
const Hyperbee = require('hyperbee')
const hypercore = require('hypercore')
const fs = require('fs')
const through = require('through2')
const parseOSM = require('osm-pbf-parser')
const osm = parseOSM()
const osmId = (id) => {
return `osm!${id}`
}
const source = process.argv[2]
const target = process.argv[3]
const db = new Hyperbee(hypercore(target), {
keyEncoding: 'utf-8',
valueEncoding: 'binary',
})
fs.createReadStream(source)
.pipe(osm)
.pipe(through.obj(features, report))
.on('finish', process.exit)
.on('end', process.exit)
function features (items, enc, next) {
const putters = items.map((item) => {
return new Promise((resolve, reject) => {
db.put(osmId(item.id), JSON.stringify(item))
.then(() => resolve())
.catch((error) => reject(error))
})
})
Promise.all(putters)
.then(() => {
console.log('next')
next()
})
.catch((error) => {
console.log(error)
next()
})
}
function report (next) {
console.log('done')
next()
}