-
Notifications
You must be signed in to change notification settings - Fork 10
/
postProcessing.js
133 lines (127 loc) · 3.75 KB
/
postProcessing.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
'use strict';
var stream = require('stream');
var turf = require('@turf/turf');
function createReadlineStream() {
var transform = new stream.Transform();
var lineBuffer = '';
transform._transform = function (data, encoding, done) {
var lines = data.toString().split('\n');
lines[0] = lineBuffer + lines[0];
lineBuffer = lines.pop();
var output = '';
lines.forEach(function(line) {
output += line + '\n';
});
done(null, output);
};
return transform;
}
function createGeometryFilterTypeStream(types) {
var transform = new stream.Transform();
transform._transform = function (line, encoding, done) {
var obj = JSON.parse(line.toString());
var features = [];
for (var i = 0; i < obj.features.length; i++) {
if (types.indexOf(obj.features[i].geometry.type) > -1) {
features.push(obj.features[i]);
}
}
if (features.length > 0) {
var geojson = {
type: 'FeatureCollection',
features: features
};
this.push(JSON.stringify(geojson));
}
done();
};
return transform;
}
function createConvertToGeojsonStream() {
var geojson = {
'type': 'FeatureCollection',
'features': []
};
var transform = new stream.Transform();
transform._transform = function (line, encoding, done) {
var obj = JSON.parse(line.toString());
if (obj.type === 'FeatureCollection') {
geojson.features = geojson.features.concat(obj.features);
} else {
geojson.features = geojson.features.concat(obj);
}
done();
};
transform._flush = function (done) {
this.push(JSON.stringify(geojson));
done();
};
return transform;
}
function createMergeByIdStream() {
var transform = new stream.Transform();
transform._transform = function (line, encoding, done) {
var obj = JSON.parse(line);
var result = {};
var features = obj.features;
for (var i = 0; i < features.length; i++) {
var val = features[i];
if (val.geometry.type === 'Point') {
var id = val.properties._fromWay + ',' + val.properties._toWay;
if (!result[id]) {
result[id] = [val];
} else {
result[id].push(val);
}
}
}
var geojson = {
'type': 'FeatureCollection',
'features': []
};
var values = Object.keys(result).map(function(e) {
return result[e];
});
values.forEach(function (val) {
var points = {
'type': 'FeatureCollection',
'features': val
};
var multipoints = turf.combine(points);
multipoints.features[0].properties = val[0].properties;
geojson.features.push(multipoints.features[0]);
});
this.push(JSON.stringify(geojson) + '\n');
done();
};
return transform;
}
function createToMultipointStream() {
var transform = new stream.Transform();
transform._transform = function (line, encoding, done) {
var obj = JSON.parse(line);
var geojson = {
'type': 'FeatureCollection',
'features': []
};
for (var i = 0; i < obj.features.length; i++) {
if (obj.features[i].geometry.type === 'LineString' || obj.features[i].geometry.type === 'Polygon') {
var multipoints = turf.combine(turf.explode(obj.features[i]));
multipoints.features[0].properties = obj.features[i].properties;
geojson.features.push(multipoints.features[0]);
} else {
geojson.features.push(obj.features[i]);
}
}
this.push(JSON.stringify(geojson) + '\n');
done();
};
return transform;
}
module.exports = {
createReadlineStream: createReadlineStream,
createGeometryFilterTypeStream: createGeometryFilterTypeStream,
createConvertToGeojsonStream: createConvertToGeojsonStream,
createMergeByIdStream: createMergeByIdStream,
createToMultipointStream: createToMultipointStream
};