-
Notifications
You must be signed in to change notification settings - Fork 6
/
ltsanalyzer.js
168 lines (151 loc) · 5.12 KB
/
ltsanalyzer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict'
const fs = require('fs')
const path = require('path')
const loadmodel = require('./loadmodel')
const Xml2object = require('xml2object')
class ltsanalyzer {
constructor (options) {
this.verbose = options.verbose
this.zero = options.zero
this.prefix = options.ltsprefix
this.destdir = options.destdir
this.ways = {}
this.nodes = {}
this.names = options.names
this.inputfile = ''
this.onCompleteLoadWays = this.onCompleteLoadWays.bind(this)
this.onCompleteLoadNodes = this.onCompleteLoadNodes.bind(this)
this.processWays = this.processWays.bind(this)
this.processNodes = this.processNodes.bind(this)
}
Run (name, osmfilename, onComplete) {
this.onCompleteRun = onComplete
this.model = loadmodel(name)
this.inputfile = osmfilename
if (this.model !== null) {
if (this.verbose) console.log('Loading "' + osmfilename + '"...')
this.loadWays()
} else {
onCompleteRun('Invalid model name: ' + name)
}
}
loadWays () {
// Create a new xml parser with an array of xml elements to look for
let parser = new Xml2object(['way'], this.inputfile)
// Bind to the object event to work with the objects found in the XML file
parser.on('object', this.processWays)
// Bind to the file end event to tell when the file is done being streamed
parser.on('end', this.onCompleteLoadWays)
// Start parsing the XML
parser.start()
}
processWays (name, childNode) {
if (name === 'way') {
let newway = {level: 0, tags: {}, nodes: []}
let tags = childNode.tag
if (Array.isArray(tags)) {
for (let t in childNode.tag) {
if (this.model.usesTag(childNode.tag[t].k) || (this.names && childNode.tag[t].k === 'name')) {
newway.tags[childNode.tag[t].k] = childNode.tag[t].v
}
}
} else if (typeof tags !== 'undefined') {
if (this.model.usesTag(tags.k) || (this.names && tags.k === 'name')) {
newway.tags[tags.k] = tags.v
}
}
let limit = childNode.nd.length
for (let i = 0; i < limit; i++) {
newway.nodes.push(childNode.nd[i].ref)
}
newway.level = this.model.evaluateLTS(newway).lts
if (newway.level > 0 || (this.zero && typeof newway.tags['highway'] !== 'undefined')) {
this.ways[childNode.id] = newway
for (let i = 0; i < newway.nodes.length; i++) {
this.nodes[newway.nodes[i]] = {}
}
}
}
}
onCompleteLoadWays () {
// Create a new xml parser with an array of xml elements to look for
let parser = new Xml2object(['node'], this.inputfile)
// Bind to the object event to work with the objects found in the XML file
parser.on('object', this.processNodes)
// Bind to the file end event to tell when the file is done being streamed
parser.on('end', this.onCompleteLoadNodes)
// Start parsing the XML
parser.start()
}
processNodes (name, childNode) {
if (name === 'node') {
let newnode = this.nodes[childNode.id]
if (typeof newnode !== 'undefined') {
this.nodes[childNode.id] = {lat: childNode.lat, lon: childNode.lon}
}
}
}
onCompleteLoadNodes () {
let err = null
if (!this.createLevelFiles()) {
err = 'Failure while creating the level files'
}
this.onCompleteRun(err)
}
createLevelFiles () {
if (this.verbose) console.log('Saving stress level files...')
let levels = this.model.levels
let prefix = this.prefix
let startLevel = this.zero ? 0 : 1
for (let level = startLevel; level <= levels; level++) {
let lfilename = path.join(this.destdir, prefix + level.toString() + '.json')
if (fs.existsSync(lfilename)) {
fs.unlinkSync(lfilename)
}
let buffer = '{"type":"FeatureCollection","features":['
let fsep = false
for (let id in this.ways) {
if (!this.ways.hasOwnProperty(id)) continue
let way = this.ways[id]
if (way.level === level) {
if (fsep) {
buffer += ',\n'
}
fsep = true
buffer += '{"type":"Feature","id":"way/'
buffer += id
buffer += '","properties":{"id":"way/'
buffer += id
if (this.names && way.tags['name'] !== undefined) {
buffer += '","name":"'
buffer += way.tags['name']
}
buffer += '"},"geometry":{"type":"LineString","coordinates":['
let csep = false
let ln = way.nodes.length
for (let i = 0; i < ln; i++) {
let nodeid = way.nodes[i]
let node = this.nodes[nodeid]
if (csep) {
buffer += ','
}
csep = true
buffer += '['
buffer += this.formatLatLong(node.lon)
buffer += ','
buffer += this.formatLatLong(node.lat)
buffer += ']'
}
buffer += ']}}'
}
}
buffer += ']}'
fs.writeFileSync(lfilename, buffer)
}
return true
}
formatLatLong (latlong) {
return (latlong[latlong.length - 1] === '0') ? parseFloat(latlong).toString() : latlong
}
}
module.exports = ltsanalyzer