This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
wrong_turn_restriction.js
80 lines (76 loc) · 2.58 KB
/
wrong_turn_restriction.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
'use strict';
module.exports = wrongTurnRestriction;
/**
* Checks for existence of name tag and if it is modified between old and new version, it callbacks with the result.
* @param {object} newVersion Features new version in GeoJSON.
* @param {object} oldVersion Features old version in GeoJSON.
* @returns {bool} Boolean indicating a turn restriction.
*/
function via_in_ways(via, ways) {
return ways.filter(function(e) {
return JSON.stringify(via.geometry.coordinates) !==
JSON.stringify(e.geometry.coordinates[0]) &&
JSON.stringify(via.geometry.coordinates) !==
JSON.stringify(
e.geometry.coordinates[e.geometry.coordinates.length - 1]
);
}).length > 0;
}
function wrongTurnRestriction(newVersion, oldVersion) {
if (
newVersion &&
newVersion.properties &&
newVersion.properties.hasOwnProperty('restriction') &&
newVersion.properties.relations
) {
var roles = newVersion.properties.relations.map(function(e) {
return e.properties.role;
});
// a turn restriction needs at least 3 members
if (roles.length < 3) {
return {'result:wrong_turn_restriction': true};
}
// detect members with wrong roles
if (
roles.filter(function(e) {
return ['from', 'via', 'to', 'location_hint'].indexOf(e) === -1;
}).length > 0
) {
return {'result:wrong_turn_restriction': true};
}
// only no_entry restrictions can have more than one FROM way
if (
roles.filter(function(e) {
return e === 'from';
}).length > 1 && newVersion.properties.restriction !== 'no_entry'
) {
return {'result:wrong_turn_restriction': true};
}
// only no_exit restrictions can have more than one TO way
if (
roles.filter(function(e) {
return e === 'to';
}).length > 1 && newVersion.properties.restriction !== 'no_exit'
) {
return {'result:wrong_turn_restriction': true};
}
// a turn restriction can have only one VIA node
var via_nodes = newVersion.properties.relations.filter(function(e) {
return e.properties.role === 'via' && e.properties.type === 'node';
});
if (via_nodes.length > 1) {
return {'result:wrong_turn_restriction': true};
}
// check if the VIA node is present in the FROM and TWO ways
var via = via_nodes[0];
var ways = newVersion.properties.relations.filter(function(e) {
return e.properties.type === 'way';
});
if (
via && via.geometry && via.geometry.coordinates && via_in_ways(via, ways)
) {
return {'result:wrong_turn_restriction': true};
}
}
return false;
}