-
Notifications
You must be signed in to change notification settings - Fork 10
/
map.js
301 lines (288 loc) · 9.68 KB
/
map.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
'use strict';
var turf = require('@turf/turf');
var _ = require('underscore');
var rbush = require('rbush');
var flatten = require('geojson-flatten');
module.exports = function(tileLayers, tile, writeData, done) {
var layer = tileLayers.osm.osm;
var bboxLayer = turf.bboxPolygon(turf.bbox(layer));
bboxLayer.geometry.type = 'LineString';
bboxLayer.geometry.coordinates = bboxLayer.geometry.coordinates[0];
var bufferLayer = turf.buffer(bboxLayer, 0.01, {
units: 'miles'
});
var majorRoads = {
motorway: true,
trunk: true,
primary: true,
secondary: true,
tertiary: true,
motorway_link: true,
trunk_link: true,
primary_link: true,
secondary_link: true,
tertiary_link: true
};
var minorRoads = {
unclassified: true,
residential: true,
living_street: true,
// 'service': true,
road: true
};
var pathRoads = {
pedestrian: true,
track: true,
footway: true,
path: true,
cycleway: true,
steps: true
};
var preserveType = {};
preserveType = _.extend(preserveType, majorRoads);
preserveType = _.extend(preserveType, minorRoads);
// preserveType = _.extend(preserveType, pathRoads);
var unit = {
units: 'meters'
};
var distance = 5;
var highways = {};
var bboxes = [];
var output = {};
var osmlint = 'unconnectedhighways';
var avoidPoints = {};
for (var i = 0; i < layer.features.length; i++) {
var val = layer.features[i];
// Linestring evaluation
if (val.geometry.type === 'LineString' && val.properties.highway) {
var valIdL = val.properties['@id'] + 'L';
var bboxL = objBbox(val, valIdL);
bboxes.push(bboxL);
highways[valIdL] = {
highway: val,
buffer: turf.buffer(val, distance, unit)
};
} else if (
val.geometry.type === 'MultiLineString' &&
val.properties.highway
) {
//MultiLineString evaluation
var flat = flatten(val);
var id = val.properties['@id'] + 'L';
for (var f = 0; f < flat.length; f++) {
if (flat[f].geometry.type === 'LineString') {
var idFlat = id + 'M' + f;
var bboxM = objBbox(flat[f], idFlat);
bboxes.push(bboxM);
highways[idFlat] = {
highway: flat[f],
buffer: turf.buffer(flat[f], distance, unit)
};
}
}
}
//check entrance, noexit and barrier
if (
val.geometry.type === 'Point' &&
(val.properties.entrance ||
val.properties.noexit === 'yes' ||
val.properties.barrier ||
val.properties.highway === 'turning_circle')
) {
avoidPoints[val.geometry.coordinates.join('-')] = true;
}
}
var highwaysTree = rbush(bboxes.length);
highwaysTree.load(bboxes);
for (var m = 0; m < bboxes.length; m++) {
var valueBbox = bboxes[m];
var valueHighway = highways[valueBbox.id].highway;
//obtaining first and last coordinates
var firstCoord = valueHighway.geometry.coordinates[0];
var firstPoint = turf.point(firstCoord);
var endCoord =
valueHighway.geometry.coordinates[
valueHighway.geometry.coordinates.length - 1
];
var endPoint = turf.point(endCoord);
if (
preserveType[valueHighway.properties.highway] &&
!_.isEqual(firstCoord, endCoord)
) {
var overlapsFirstPoint = [];
if (!turf.booleanPointInPolygon(firstPoint, bufferLayer)) {
overlapsFirstPoint = highwaysTree.search(
objBbox(
turf.bboxPolygon(
turf.bbox(turf.buffer(firstPoint, distance, unit)),
'id'
)
)
);
}
var overlapsEndPoint = [];
if (!turf.booleanPointInPolygon(endPoint, bufferLayer)) {
overlapsEndPoint = highwaysTree.search(
objBbox(
turf.bboxPolygon(
turf.bbox(turf.buffer(endPoint, distance, unit)),
'id'
)
)
);
}
var overlapBboxes = overlapsFirstPoint.concat(overlapsEndPoint);
var arrayCorrd = [];
for (var j = 0; j < overlapBboxes.length; j++) {
var overlapBbox = overlapBboxes[j];
if (valueBbox.id !== overlapBbox.id) {
arrayCorrd = arrayCorrd.concat(
_.flatten(highways[overlapBbox.id].highway.geometry.coordinates)
);
}
}
var props = {
_fromWay: valueHighway.properties['@id'],
_osmlint: osmlint,
_type: classification(
majorRoads,
minorRoads,
pathRoads,
valueHighway.properties.highway
)
};
if (!avoidPoints[firstCoord.join('-')]) {
for (var k = 0; k < overlapsFirstPoint.length; k++) {
var overlapPointFirst = overlapsFirstPoint[k];
var toHighwayFirst = highways[overlapPointFirst.id].highway;
if (
valueBbox.id !== overlapPointFirst.id &&
(arrayCorrd.indexOf(firstCoord[0]) === -1 ||
arrayCorrd.indexOf(firstCoord[1]) === -1)
) {
if (
turf.booleanPointInPolygon(
firstPoint,
highways[overlapPointFirst.id].buffer
)
) {
props.toWay = toHighwayFirst.properties['@id'];
firstPoint.properties = props;
//Check out whether the streets are connected at some point
var coordinatesF = valueHighway.geometry.coordinates;
var valueCoorF = _.flatten([coordinatesF[1], coordinatesF[2]]);
var overlapCoorF = _.flatten(toHighwayFirst.geometry.coordinates);
if (_.intersection(valueCoorF, overlapCoorF).length < 2) {
valueHighway.properties._osmlint = osmlint;
toHighwayFirst.properties._osmlint = osmlint;
//both roads must have the same layer and road to connect should not be in construction
if (
valueHighway.properties.layer ===
toHighwayFirst.properties.layer &&
toHighwayFirst.properties.highway !== 'construction'
) {
output[valueBbox.id] = valueHighway;
output[overlapPointFirst.id] = toHighwayFirst;
if (
valueHighway.properties['@id'] >
toHighwayFirst.properties['@id']
) {
output[
valueBbox.id.toString().concat(overlapPointFirst.id)
] = firstPoint;
} else {
output[
overlapPointFirst.id.toString().concat(valueBbox.id)
] = firstPoint;
}
}
}
}
}
}
}
if (!avoidPoints[endCoord.join('-')]) {
for (var l = 0; l < overlapsEndPoint.length; l++) {
var overlapPointEnd = overlapsEndPoint[l];
var toHighwayEnd = highways[overlapPointEnd.id].highway;
if (
valueBbox.id !== overlapPointEnd.id &&
(arrayCorrd.indexOf(endCoord[0]) === -1 ||
arrayCorrd.indexOf(endCoord[1]) === -1)
) {
if (
turf.booleanPointInPolygon(
endPoint,
highways[overlapPointEnd.id].buffer
)
) {
props.toWay = toHighwayEnd.properties['@id'];
endPoint.properties = props;
//Check out whether the streets are connected at some point
var coordinatesE = valueHighway.geometry.coordinates;
var valueCoorE = _.flatten([
coordinatesE[coordinatesE.length - 1],
coordinatesE[coordinatesE.length - 2]
]);
var overlapCoorE = _.flatten(toHighwayEnd.geometry.coordinates);
if (_.intersection(valueCoorE, overlapCoorE).length < 2) {
valueHighway.properties._osmlint = osmlint;
toHighwayEnd.properties._osmlint = osmlint;
//both roads must have the same layer and road to connect should not be in construction
if (
valueHighway.properties.layer ===
toHighwayEnd.properties.layer &&
toHighwayEnd.properties.highway !== 'construction'
) {
output[valueBbox.id] = valueHighway;
output[overlapPointEnd.id] = toHighwayEnd;
if (
valueHighway.properties['@id'] >
toHighwayEnd.properties['@id']
) {
output[
valueHighway.properties['@id']
.toString()
.concat(toHighwayEnd.properties['@id'])
] = endPoint;
} else {
output[
toHighwayEnd.properties['@id']
.toString()
.concat(valueHighway.properties['@id'])
] = endPoint;
}
}
}
}
}
}
}
}
}
var result = _.values(output);
if (result.length > 0) {
var fc = turf.featureCollection(result);
writeData(JSON.stringify(fc) + '\n');
}
done(null, null);
};
function classification(major, minor, path, highway) {
if (major[highway]) {
return 'major';
} else if (minor[highway]) {
return 'minor';
} else if (path[highway]) {
return 'path';
}
}
function objBbox(obj, id) {
var bboxExtent = ['minX', 'minY', 'maxX', 'maxY'];
var bbox = {};
var valBbox = turf.bbox(obj);
for (var d = 0; d < valBbox.length; d++) {
bbox[bboxExtent[d]] = valBbox[d];
}
bbox.id = id || obj.properties['@id'];
return bbox;
}