-
Notifications
You must be signed in to change notification settings - Fork 3
/
read-shapes.js
56 lines (47 loc) · 1.31 KB
/
read-shapes.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
'use strict'
const expectSorting = require('./lib/expect-sorting')
const readShapes = async function* (readFile, filters = {}) {
if ('function' !== typeof readFile) {
throw new Error('readFile must be a function.')
}
const {
shapesRow: shapesRowFilter,
} = {
shapesRow: () => true,
...filters,
}
if ('function' !== typeof shapesRowFilter) {
throw new Error('filters.shapesRow must be a function.')
}
const shapes = await readFile('shapes')
const checkShapesSorting = expectSorting('shapes', (a, b) => {
if (a.shape_id < b.shape_id) return -1
if (a.shape_id > b.shape_id) return 1
const seqA = parseInt(a.shape_pt_sequence)
const seqB = parseInt(b.shape_pt_sequence)
if (seqA === seqB) return 0
return seqA < seqB ? -1 : 1
})
let shape_id = NaN, points = []
for await (const s of shapes) {
if (!shapesRowFilter(s)) continue
checkShapesSorting(s)
if (s.shape_id !== shape_id) {
if (points.length > 0) {
yield [shape_id, points]
points = []
}
shape_id = s.shape_id
}
points.push({
shape_pt_lat: parseFloat(s.shape_pt_lat),
shape_pt_lon: parseFloat(s.shape_pt_lon),
shape_pt_sequence: parseInt(s.shape_pt_sequence),
shape_dist_traveled: parseFloat(s.shape_dist_traveled),
})
}
if (points.length > 0) {
yield [shape_id, points]
}
}
module.exports = readShapes