-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.js
72 lines (68 loc) · 1.78 KB
/
parser.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
class Parser{
createNode(node){
let newnode = Object.assign({},node.attribs || {});
this.parseCoordinate(newnode);
this.parseTags(node,newnode);
this.parseOthers(node,newnode);
newnode.id = parseInt(newnode.id);
return newnode;
}
parseCoordinate(newnode){
var lat = parseFloat(newnode.lat);
var lng = parseFloat(newnode.lon);
if(lat && lng){
newnode.loc={
type:"Point",
coordinates:[lng,lat]
}
newnode.lat = lat;
newnode.lng = lng;
delete newnode.lon;
}
}
parseTags(node,newnode){
if(node.children && node.children.tag){
var tags = node.children.tag;
let tag;
newnode.tags = {};
if(Array.isArray(tags)){
for(var i=0;i<tags.length;i++){
tag = tags[i];
//console.log(tag.attribs.k)
if(tag.attribs && tag.attribs.k){
if(tag.attribs.k.trim() === "name"){
newnode.name = tag.attribs.v.trim();
}
else{
newnode.tags[tag.attribs.k.trim()]=tag.attribs.v.trim();
}
}
}
}
else if(tags.attribs && tags.attribs.k){
newnode.tags[tags.attribs.k.trim()]=tags.attribs.v.trim();
}
}
//console.log(newnode)
}
parseOthers(node,newnode){
var childname,child;
if(!node.children) return;
for(childname in node.children){
if(childname !== "tag"){
child = node.children[childname];
if(Array.isArray(child)){
newnode[childname] = []
child.forEach((child)=>{
newnode[childname].push(child.attribs);
})
}
else{
newnode[childname] = child.attribs;
}
}
}
}
}
const nodeParser = new Parser();
exports.nodeParser = nodeParser;