-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (83 loc) · 2.85 KB
/
index.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
var fs = require('fs');
var argv = require('minimist')(process.argv.slice(2));
// fetch the input geojson
var data = JSON.parse(fs.readFileSync('./'+ argv.filename, 'utf8'));
var streetName = argv.streetname;
var relID = []; // array to store unique relation ID
var fromFeatureJSON;
var relGroup ={
'type': 'FeatureCollection',
'features': []
};
function getRestriction(obj){
if(obj.hasOwnProperty('restriction')) {
return obj['restriction'];
} else {
return obj['restriction:conditional'];
}
}
function setProperties(copyTo, copyFrom, relInfo){
copyTo.id = copyFrom.id;
copyTo.properties['name'] = copyFrom.properties.name;
copyTo.properties['label'] = relInfo.role;
copyTo.geometry.coordinates = copyFrom.geometry.coordinates;
copyTo.geometry.type = copyFrom.geometry.type;
if (relInfo.role === 'from') {
copyTo.properties['restriction:type'] = getRestriction(relInfo.reltags);
copyTo.properties['user'] = copyFrom.properties['@user'];
copyTo.properties['changeset'] = copyFrom.properties['@changeset'];
copyTo.properties['restriction:id'] = relInfo.rel;
copyTo.properties['relations'] =[];
}
return copyTo;
}
function constructJSON(fromFeature, relDetail){
var featureJSON = {
'type': 'Feature',
'id': '',
'properties': {},
'geometry': {
'coordinates': [],
'type': ''
}
};
featureJSON = setProperties(featureJSON, fromFeature, relDetail);
return featureJSON;
}
// iterate through geojson to store unique relation ID
function groupRelation (item) {
for (i=0; i<item.properties['@relations'].length; i++) {
if (item.properties['@relations'][i].role === 'from') {
if (relID.indexOf(item.properties['@relations'][i].rel) < 0) {
relID.push(item.properties['@relations'][i].rel);
fromFeatureJSON = constructJSON(item, item.properties['@relations'][i]);
relGroup.features.push(fromFeatureJSON);
}
} else if (item.properties['@relations'][i].role !== 'from') {
relGroup.features.forEach(function(relItem){
if (item.properties['@relations'][i].rel === relItem.properties['restriction:id']) {
fromFeatureJSON = constructJSON(item, item.properties['@relations'][i]);
//push the object to 'relations' property of the 'from' feature
relItem.properties.relations.push(fromFeatureJSON);
}
});
}
}
}
data.features.forEach(function(item){
var i;
if (streetName === undefined){
if (item.properties['@relations'] !== undefined) {
groupRelation(item);
}
} else {
if (item.properties['@relations'] !== undefined && item.properties.name == streetName) {
groupRelation(item);
}
}
});
relGroup.features.forEach(function(eachRel){
delete eachRel.properties['@relations'];
delete eachRel.properties['@id'];
});
console.log(JSON.stringify(relGroup));