-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagging.js
97 lines (83 loc) · 2.92 KB
/
tagging.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
const reader = require('geojson-writer').reader
const xmldom = require('xmldom').DOMParser
var XMLSerializer = require('xmldom').XMLSerializer;
const fs = require('fs');
const inTaggedRoadsFiles = ["data/roads_with_both_sidewalks.json", "data/roads_with_left_sidewalks.json", "data/roads_with_right_sidewalks.json", "data/roads_without_sidewalks.json"]
const inOsmSource = "osm/barrhaven_all_roads.osm"
const outOsmSource = "osm/barrhaven_all_roads_with_sidewalks.osm"
const serializer = new XMLSerializer();
const road_sidewalk_tags = {}
let totalTagged = 0
console.time('Time')
console.log('Loading tagged roads ...')
for(let i in inTaggedRoadsFiles){
let tagged_roads = reader(inTaggedRoadsFiles[i])
for(let feature of tagged_roads.features) {
if(feature.properties.sidewalk){
road_sidewalk_tags[feature.properties.osm_id] = feature.properties.sidewalk
}
}
}
fs.readFile(inOsmSource, 'utf-8', function (err, data) {
if (err) {
throw err;
}
const doc = new xmldom().parseFromString(data, 'application/xml');
const ways = doc.getElementsByTagName('way');
loop1:
for (let i in ways) {
let way = ways[i]
for(let j in way.attributes){
let attr = way.attributes[j]
if (attr.name=='user'){
if(attr.value=='Ottawa_Sidewalk_Tagging'){ //if we already added this tag - skip
continue loop1
}
else{
break
}
}
}
for(let j in way.attributes){
let attr = way.attributes[j]
if (attr.name=='id') {
const id = attr.value;
const sidewalk_tag = road_sidewalk_tags[id];
if(sidewalk_tag){
let replaced = false
for(let k in way.childNodes){
let node = way.childNodes[k]
if(!node.attributes){continue}
let attr = node.attributes[0]
if (node.attributes[0].name=='k' && node.attributes[0].value=='sidewalk') {
replaced = true
if(node.attributes[1].value!=sidewalk_tag){
way.setAttribute('action', 'modify');
node.setAttribute('v', sidewalk_tag)
console.log('For way id#', id, 'changed sidewalk tag to', sidewalk_tag)
totalTagged++
}
}
}
if(!replaced){
tag = doc.createElement("tag");
tag.setAttribute('k', 'sidewalk')
tag.setAttribute('v', sidewalk_tag);
way.appendChild(tag)
way.setAttribute('action', 'modify');
console.log('For way id#', id, 'created sidewalk tag with', sidewalk_tag)
totalTagged++
}
//console.log('Modified way id#', id, 'sidewalk tag to', sidewalk_tag)
}
}
}
}
fs.writeFile(outOsmSource, serializer.serializeToString(doc), function(err) {
if(err) {
return console.log(err);
}
console.log("Saved! Total tagged roads:", totalTagged);
});
});
console.timeEnd('Time')