-
Notifications
You must be signed in to change notification settings - Fork 18
/
osmhistory.js
153 lines (138 loc) · 5.13 KB
/
osmhistory.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
const _ = require('lodash');
const reqwest = require('reqwest');
const moment = require('moment');
const osmHistory = (function osmDeepHistory() {
const baseUrl = 'https://api.openstreetmap.org/api/0.6/';
const osmObjects = {
node: {},
way: {},
relation: {}
}
const s = {};
// reqwest doesn't support promise chaining, has to wrap it
function requestOsmApi(url) {
console.log('Request OSM API', url);
return new Promise((resolve, reject) => {
reqwest({
url,
crossOrigin: true,
type: 'xml',
success: resolve,
error: reject
});
});
}
function getObjVersionByDate(type, id, timestamp) {
const versions = osmObjects[type][id];
for (let i = versions.length - 1; i >= 0; i--) {
if (versions[i].timestamp.isSameOrBefore(timestamp)) {
return versions[i];
}
}
return versions[0];
}
s.requestReferenceHistoryForWay = function (id) /* => Promise<void> */ {
const way = osmObjects['way'][id];
if (!way) {
return Promise.reject("Can't request reference for unknown way");
}
const node_ids = _.uniq(_.flatMap(way, v => v.nodes));
return Promise
.all(_.map(node_ids, id => s.getObjectHistory('node', id)))
.then(r => { return; });
}
s.getWayHistoryFull = function (way_obj) {
return _.map(way_obj, v => {
return {
version: v.version,
latlons: _.map(v.nodes, id => {
const node = getObjVersionByDate('node', id, v.timestamp);
return [node.lat, node.lon];
})
};
});
};
s.getWayHistoryByDate = function (id, timestamp) {
const way = getObjVersionByDate('way', id, timestamp);
return {
way,
nodes: _.map(way.nodes, nid => getObjVersionByDate('node', nid, timestamp))
}
};
function xmlElementToObj(e) {
const obj = {
type: e.tagName,
user: e.getAttribute('user'),
timestamp: moment(e.getAttribute('timestamp')),
changeset: +e.getAttribute('changeset'),
version: +e.getAttribute('version'),
id: +e.getAttribute('id'),
tags: _.fromPairs(_.map(e.getElementsByTagName('tag'), t => [t.getAttribute('k'), t.getAttribute('v')])),
visible: (e.getAttribute('visible') === 'true')
};
switch (obj.type) {
case 'node':
obj.lat = +e.getAttribute('lat');
obj.lon = +e.getAttribute('lon');
break;
case 'way':
obj.nodes = _.map(e.getElementsByTagName('nd'), e => e.getAttribute('ref'));
break;
case 'relation':
obj.members = _.map(e.getElementsByTagName('member'), e => {
return {
type: e.getAttribute('type'),
ref: e.getAttribute('ref'),
role: e.getAttribute('role')
}
});
}
return obj;
}
s.getObjectHistory = function (obj_type, obj_id) {
console.log('Get history of', obj_type, obj_id);
if (osmObjects[obj_type][obj_id]) {
return Promise.resolve(osmObjects[obj_type][obj_id]);
} else {
return requestOsmApi(baseUrl + obj_type + '/' + obj_id + '/history')
.then(xml => {
const objHistory = _.map(xml.getElementsByTagName(obj_type), xmlElementToObj);
osmObjects[obj_type][obj_id] = objHistory;
return objHistory;
});
}
};
s.requestWayFull = function (way_id) /* => Promise<Array<[lat, lon]>> */ {
return requestOsmApi(baseUrl + 'way/' + way_id + '/full')
.then(x => {
const nodes =
_.keyBy(
_.map(x.getElementsByTagName('node'), e => {
return {
id: e.getAttribute('id'),
lat: e.getAttribute('lat'),
lon: e.getAttribute('lon'),
};
}),
'id');
return {
id: way_id,
latlons: _.map(x.getElementsByTagName('nd'), e => e.getAttribute('ref'))
.map(id => {
const n = nodes[id];
return [n.lat, n.lon];
})
};
});
}
s.getWaysNodeBelongsTo = function (node_id) /* => Promise */ {
return requestOsmApi(baseUrl + 'node/' + node_id + '/ways')
.then(x => {
return Promise.all(
_.map(x.getElementsByTagName('way'), e => e.getAttribute('id'))
.map(s.requestWayFull));
});
}
return s;
})();
module.exports = osmHistory;