diff --git a/modules/osm/intersection.js b/modules/osm/intersection.js index 9d0b7a83c..b145c5833 100644 --- a/modules/osm/intersection.js +++ b/modules/osm/intersection.js @@ -407,18 +407,24 @@ export function osmIntersection(graph, startVertexId, maxDistance) { var matchesViaTo = false; var isAlongOnlyPath = false; - if (t.id === way.id) { // match VIA, TO - if (v.length === 1 && v[0].type === 'node' && v[0].id === entity.id) { - matchesViaTo = true; // match VIA node + if (t.id === way.id) { // match TO - } else { // match all VIA ways + if (v.length === 1 && v[0].type === 'node') { // match VIA node + matchesViaTo = (v[0].id === entity.id && ( + (matchesFrom && currPath.length === 2) || + (!matchesFrom && currPath.length > 2) + )); + + } else { // match all VIA ways var pathVias = []; - for (k = 1; k < currPath.length; k++) { // k = 1 skips FROM way - if (currPath[k][0] === 'w') pathVias.push(currPath[k]); + for (k = 2; k < currPath.length; k +=2 ) { // k = 2 skips FROM + pathVias.push(currPath[k]); // (path goes way-node-way...) } var restrictionVias = []; for (k = 0; k < v.length; k++) { - if (v[k].type === 'way') restrictionVias.push(v[k].id); + if (v[k].type === 'way') { + restrictionVias.push(v[k].id); + } } var diff = _difference(pathVias, restrictionVias); matchesViaTo = !diff.length; diff --git a/modules/ui/fields/restrictions.js b/modules/ui/fields/restrictions.js index 23e3e0f5e..32e6e9790 100644 --- a/modules/ui/fields/restrictions.js +++ b/modules/ui/fields/restrictions.js @@ -359,13 +359,13 @@ export function uiFieldRestrictions(field, context) { datumOnly.only = true; restrictionType = restrictionType.replace(/^no/, 'only'); - // Adding an ONLY restriction should destroy all other direct restrictions from the FROM. + // Adding an ONLY restriction should destroy all other direct restrictions from the FROM towards the VIA. // We will remember them in _oldTurns, and restore them if the user clicks again. turns = _intersection.turns(_fromWayID, 2); extraActions = []; _oldTurns = []; for (i = 0; i < turns.length; i++) { - if (turns[i].direct) { + if (turns[i].direct && turns[i].path[1] === datum.path[1]) { turns[i].restrictionType = osmInferRestriction(vgraph, turns[i], projection); _oldTurns.push(turns[i]); extraActions.push(actionUnrestrictTurn(turns[i])); diff --git a/test/spec/osm/intersection.js b/test/spec/osm/intersection.js index 01a822b2f..ecb23a265 100644 --- a/test/spec/osm/intersection.js +++ b/test/spec/osm/intersection.js @@ -680,7 +680,7 @@ describe('iD.osmIntersection', function() { members: [ { role: 'from', id: '|', type: 'way' }, { role: 'via', id: 'e', type: 'node' }, - { role: 'to', id: '≈', type: 'way' }, + { role: 'to', id: '≈', type: 'way' } ] }) ]); @@ -789,7 +789,7 @@ describe('iD.osmIntersection', function() { members: [ { role: 'from', id: '/', type: 'way' }, { role: 'via', id: '|', type: 'way' }, - { role: 'to', id: '≈', type: 'way' }, + { role: 'to', id: '≈', type: 'way' } ] }) ]); @@ -889,7 +889,7 @@ describe('iD.osmIntersection', function() { members: [ { role: 'from', id: '|', type: 'way' }, { role: 'via', id: 'e', type: 'node' }, - { role: 'to', id: '≈', type: 'way' }, + { role: 'to', id: '≈', type: 'way' } ] }) ]); @@ -1045,7 +1045,7 @@ describe('iD.osmIntersection', function() { members: [ { role: 'from', id: '=', type: 'way' }, { role: 'via', id: '|', type: 'way' }, - { role: 'to', id: '≈', type: 'way' }, + { role: 'to', id: '≈', type: 'way' } ] }), iD.osmRelation({ @@ -1054,7 +1054,7 @@ describe('iD.osmIntersection', function() { members: [ { role: 'from', id: '/', type: 'way' }, { role: 'via', id: '|', type: 'way' }, - { role: 'to', id: '≈', type: 'way' }, + { role: 'to', id: '≈', type: 'way' } ] }) ]); @@ -1170,6 +1170,400 @@ describe('iD.osmIntersection', function() { }); }); + describe('complex intersection - via 2 ways', function() { + // + // a <--- b <=== c + // | + // * + // ‖ + // d ~~~> e ≈≈≈> f + // + var graph = iD.coreGraph([ + iD.osmNode({id: 'a'}), + iD.osmNode({id: 'b'}), + iD.osmNode({id: 'c'}), + iD.osmNode({id: 'd'}), + iD.osmNode({id: 'e'}), + iD.osmNode({id: 'f'}), + iD.osmNode({id: '*'}), + iD.osmWay({id: '-', nodes: ['b', 'a'], tags: {highway: 'residential', oneway: 'yes'}}), + iD.osmWay({id: '=', nodes: ['c', 'b'], tags: {highway: 'residential', oneway: 'yes'}}), + iD.osmWay({id: '~', nodes: ['d', 'e'], tags: {highway: 'residential', oneway: 'yes'}}), + iD.osmWay({id: '≈', nodes: ['e', 'f'], tags: {highway: 'residential', oneway: 'yes'}}), + iD.osmWay({id: '|', nodes: ['b', '*'], tags: {highway: 'residential'}}), + iD.osmWay({id: '‖', nodes: ['*', 'e'], tags: {highway: 'residential'}}) + ]); + + it('with no restrictions, allows via node and via way turns', function() { + var turns; + turns = iD.osmIntersection(graph, 'b').turns('=', 2); + expect(turns.length).to.eql(4); + + expect(turns[0]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[0].key).to.eql('=_b_-'); // straight to - + expect(turns[0].u).to.be.not.ok; + + expect(turns[1]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[1].key).to.eql('=_b_|'); // left to | + expect(turns[1].u).to.be.not.ok; + + expect(turns[2]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[2].key).to.eql('=_b_|_*_‖'); // left to ‖ via | + expect(turns[2].u).to.be.not.ok; + + expect(turns[3]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[3].key).to.eql('=_b_|_*_‖_e_≈'); // u-turn via |,‖ to ≈ + expect(turns[3].u).to.be.not.ok; + }); + + + it('supports `no_` via 2 way restriction (ordered)', function() { + // 'r1': `no_u_turn` FROM '=' VIA WAYS '|','‖' TO '≈' + var r1 = iD.osmRelation({ + id: 'r1', + tags: { type: 'restriction', restriction: 'no_u_turn' }, + members: [ + { role: 'from', id: '=', type: 'way' }, + { role: 'via', id: '|', type: 'way' }, + { role: 'via', id: '‖', type: 'way' }, + { role: 'to', id: '≈', type: 'way' } + ] + }); + graph = graph.replace(r1); + + var turns; + turns = iD.osmIntersection(graph, 'b').turns('=', 2); + expect(turns.length).to.eql(4); + + expect(turns[0]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[0].key).to.eql('=_b_-'); // straight to - + expect(turns[0].u).to.be.not.ok; + + expect(turns[1]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[1].key).to.eql('=_b_|'); // left to | + expect(turns[1].u).to.be.not.ok; + + expect(turns[2]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[2].key).to.eql('=_b_|_*_‖'); // left to ‖ via | + expect(turns[2].u).to.be.not.ok; + + expect(turns[3]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[3].key).to.eql('=_b_|_*_‖_e_≈'); // u-turn via |,‖ to ≈ + expect(turns[3].u).to.be.not.ok; + expect(turns[3].restrictionID).to.eql('r1'); + expect(turns[3].direct).to.be.true; // direct + expect(turns[3].no).to.be.true; // restricted! + expect(turns[3].only).to.be.not.ok; + }); + + + it('supports `no_` via 2 way restriction (unordered)', function() { + // 'r1': `no_u_turn` FROM '=' VIA WAYS '|','‖' TO '≈' + var r1 = iD.osmRelation({ + id: 'r1', + tags: { type: 'restriction', restriction: 'no_u_turn' }, + members: [ + { role: 'from', id: '=', type: 'way' }, + { role: 'via', id: '‖', type: 'way' }, // out of order + { role: 'via', id: '|', type: 'way' }, // out of order + { role: 'to', id: '≈', type: 'way' } + ] + }); + graph = graph.replace(r1); + + var turns; + turns = iD.osmIntersection(graph, 'b').turns('=', 2); + expect(turns.length).to.eql(4); + + expect(turns[0]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[0].key).to.eql('=_b_-'); // straight to - + expect(turns[0].u).to.be.not.ok; + + expect(turns[1]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[1].key).to.eql('=_b_|'); // left to | + expect(turns[1].u).to.be.not.ok; + + expect(turns[2]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[2].key).to.eql('=_b_|_*_‖'); // left to ‖ via | + expect(turns[2].u).to.be.not.ok; + + expect(turns[3]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[3].key).to.eql('=_b_|_*_‖_e_≈'); // u-turn via |,‖ to ≈ + expect(turns[3].u).to.be.not.ok; + expect(turns[3].restrictionID).to.eql('r1'); + expect(turns[3].direct).to.be.true; // direct + expect(turns[3].no).to.be.true; // restricted! + expect(turns[3].only).to.be.not.ok; + }); + + + it('supports `only_` via 2 way restriction (ordered)', function() { + // 'r1': `only_u_turn` FROM '=' VIA WAYS '|','‖' TO '≈' + var r1 = iD.osmRelation({ + id: 'r1', + tags: { type: 'restriction', restriction: 'only_u_turn' }, + members: [ + { role: 'from', id: '=', type: 'way' }, + { role: 'via', id: '|', type: 'way' }, + { role: 'via', id: '‖', type: 'way' }, + { role: 'to', id: '≈', type: 'way' } + ] + }); + graph = graph.replace(r1); + + var turns; + turns = iD.osmIntersection(graph, 'b').turns('=', 2); + expect(turns.length).to.eql(4); + + expect(turns[0]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[0].key).to.eql('=_b_-'); // straight to - + expect(turns[0].u).to.be.not.ok; + expect(turns[0].restrictionID).to.eql('r1'); + expect(turns[0].direct).to.be.false; // indirect + expect(turns[0].no).to.be.true; // restricted! + expect(turns[0].only).to.be.not.ok; + + expect(turns[1]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[1].key).to.eql('=_b_|'); // left to | + expect(turns[1].u).to.be.not.ok; + expect(turns[1].restrictionID).to.eql('r1'); + expect(turns[1].direct).to.be.false; // indirect + expect(turns[1].no).to.be.not.ok; + expect(turns[1].only).to.be.true; // only (along via path) + + expect(turns[2]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[2].key).to.eql('=_b_|_*_‖'); // left to ‖ via | + expect(turns[2].u).to.be.not.ok; + expect(turns[2].restrictionID).to.eql('r1'); + expect(turns[2].direct).to.be.false; // indirect + expect(turns[2].no).to.be.not.ok; + expect(turns[2].only).to.be.true; // only (along via path) + + expect(turns[3]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[3].key).to.eql('=_b_|_*_‖_e_≈'); // u-turn via |,‖ to ≈ + expect(turns[3].u).to.be.not.ok; + expect(turns[3].restrictionID).to.eql('r1'); + expect(turns[3].direct).to.be.true; // direct + expect(turns[3].no).to.be.not.ok; + expect(turns[3].only).to.be.true; // only! + }); + + it('supports `only_` via 2 way restriction (unordered)', function() { + // 'r1': `only_u_turn` FROM '=' VIA WAYS '‖','|' TO '≈' + var r1 = iD.osmRelation({ + id: 'r1', + tags: { type: 'restriction', restriction: 'only_u_turn' }, + members: [ + { role: 'from', id: '=', type: 'way' }, + { role: 'via', id: '‖', type: 'way' }, // out of order + { role: 'via', id: '|', type: 'way' }, // out of order + { role: 'to', id: '≈', type: 'way' } + ] + }); + graph = graph.replace(r1); + + var turns; + turns = iD.osmIntersection(graph, 'b').turns('=', 2); + expect(turns.length).to.eql(4); + + expect(turns[0]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[0].key).to.eql('=_b_-'); // straight to - + expect(turns[0].u).to.be.not.ok; + expect(turns[0].restrictionID).to.eql('r1'); + expect(turns[0].direct).to.be.false; // indirect + expect(turns[0].no).to.be.true; // restricted! + expect(turns[0].only).to.be.not.ok; + + expect(turns[1]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[1].key).to.eql('=_b_|'); // left to | + expect(turns[1].u).to.be.not.ok; + expect(turns[1].restrictionID).to.eql('r1'); + expect(turns[1].direct).to.be.false; // indirect + expect(turns[1].no).to.be.not.ok; + expect(turns[1].only).to.be.true; // only (along via path) + + expect(turns[2]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[2].key).to.eql('=_b_|_*_‖'); // left to ‖ via | + expect(turns[2].u).to.be.not.ok; + expect(turns[2].restrictionID).to.eql('r1'); + expect(turns[2].direct).to.be.false; // indirect + expect(turns[2].no).to.be.not.ok; + expect(turns[2].only).to.be.true; // only (along via path) + + expect(turns[3]).to.be.an.instanceOf(iD.osmTurn); + expect(turns[3].key).to.eql('=_b_|_*_‖_e_≈'); // u-turn via |,‖ to ≈ + expect(turns[3].u).to.be.not.ok; + expect(turns[3].restrictionID).to.eql('r1'); + expect(turns[3].direct).to.be.true; // direct + expect(turns[3].no).to.be.not.ok; + expect(turns[3].only).to.be.true; // only! + }); + }); + + + describe('complex intersection - via 2 ways with loops - gotchas', function() { + // + // e + // / \ + // / \ + // a --- b === c ~~~ d + // + var graph = iD.coreGraph([ + iD.osmNode({id: 'a'}), + iD.osmNode({id: 'b'}), + iD.osmNode({id: 'c'}), + iD.osmNode({id: 'd'}), + iD.osmNode({id: 'e'}), + iD.osmWay({id: '-', nodes: ['a', 'b'], tags: {highway: 'residential'}}), + iD.osmWay({id: '=', nodes: ['b', 'c'], tags: {highway: 'residential'}}), + iD.osmWay({id: '~', nodes: ['c', 'd'], tags: {highway: 'residential'}}), + iD.osmWay({id: '/', nodes: ['b', 'e'], tags: {highway: 'residential'}}), + iD.osmWay({id: '\\', nodes: ['e', 'c'], tags: {highway: 'residential'}}) + ]); + + it('with no restrictions, finds all turns', function() { + var turns = iD.osmIntersection(graph, 'c').turns('=', 2); + expect(turns.length).to.eql(10); + + expect(turns[0].key).to.eql('=_b_='); + expect(turns[0].u).to.be.true; + + expect(turns[1].key).to.eql('=_b_/'); + expect(turns[1].u).to.be.not.ok; + + expect(turns[2].key).to.eql('=_b_/_e_\\'); + expect(turns[2].u).to.be.not.ok; + + expect(turns[3].key).to.eql('=_b_/_e_\\_c_~'); + expect(turns[3].u).to.be.not.ok; + + expect(turns[4].key).to.eql('=_b_-'); + expect(turns[4].u).to.be.not.ok; + + expect(turns[5].key).to.eql('=_c_='); + expect(turns[5].u).to.be.true; + + expect(turns[6].key).to.eql('=_c_~'); + expect(turns[6].u).to.be.not.ok; + + expect(turns[7].key).to.eql('=_c_\\'); + expect(turns[7].u).to.be.not.ok; + + expect(turns[8].key).to.eql('=_c_\\_e_/'); + expect(turns[8].u).to.be.not.ok; + + expect(turns[9].key).to.eql('=_c_\\_e_/_b_-'); + expect(turns[9].u).to.be.not.ok; + }); + + it('matches from-via-to strictly when alternate paths exist between from-via-to', function() { + var r1 = iD.osmRelation({ + id: 'r1', + tags: { type: 'restriction', restriction: 'no_straight_on' }, + members: [ + { role: 'from', id: '=', type: 'way' }, + { role: 'via', id: 'c', type: 'node' }, + { role: 'to', id: '~', type: 'way' } + ] + }); + graph = graph.replace(r1); + + var turns = iD.osmIntersection(graph, 'c').turns('=', 2); + expect(turns.length).to.eql(10); + + expect(turns[0].key).to.eql('=_b_='); + expect(turns[0].u).to.be.true; + + expect(turns[1].key).to.eql('=_b_/'); + expect(turns[1].u).to.be.not.ok; + + expect(turns[2].key).to.eql('=_b_/_e_\\'); + expect(turns[2].u).to.be.not.ok; + + expect(turns[3].key).to.eql('=_b_/_e_\\_c_~'); + expect(turns[3].u).to.be.not.ok; + expect(turns[3].restrictionID).to.be.undefined; // the alternate path should not match + expect(turns[3].direct).to.be.undefined; + + expect(turns[4].key).to.eql('=_b_-'); + expect(turns[4].u).to.be.not.ok; + + expect(turns[5].key).to.eql('=_c_='); + expect(turns[5].u).to.be.true; + + expect(turns[6].key).to.eql('=_c_~'); + expect(turns[6].u).to.be.not.ok; + expect(turns[6].restrictionID).to.eql('r1'); + expect(turns[6].direct).to.be.true; // direct + expect(turns[6].no).to.be.true; // restricted! + expect(turns[6].only).to.be.not.ok; + + expect(turns[7].key).to.eql('=_c_\\'); + expect(turns[7].u).to.be.not.ok; + + expect(turns[8].key).to.eql('=_c_\\_e_/'); + expect(turns[8].u).to.be.not.ok; + + expect(turns[9].key).to.eql('=_c_\\_e_/_b_-'); + expect(turns[9].u).to.be.not.ok; + }); + + + it('`only_` restriction is only effective towards the via', function() { + var r1 = iD.osmRelation({ + id: 'r1', + tags: { type: 'restriction', restriction: 'only_straight_on' }, + members: [ + { role: 'from', id: '=', type: 'way' }, + { role: 'via', id: 'c', type: 'node' }, + { role: 'to', id: '~', type: 'way' } + ] + }); + graph = graph.replace(r1); + + var turns = iD.osmIntersection(graph, 'c').turns('=', 2); + expect(turns.length).to.eql(8); + + expect(turns[0].key).to.eql('=_b_='); // not towards via + expect(turns[0].u).to.be.true; + + expect(turns[1].key).to.eql('=_b_/'); // not towards via + expect(turns[1].u).to.be.not.ok; + + expect(turns[2].key).to.eql('=_b_/_e_\\'); // not towards via + expect(turns[2].u).to.be.not.ok; + + expect(turns[3].key).to.eql('=_b_/_e_\\_c_~'); // not towards via + expect(turns[3].u).to.be.not.ok; + expect(turns[3].restrictionID).to.be.undefined; // the alternate path should not match + expect(turns[3].direct).to.be.undefined; + + expect(turns[4].key).to.eql('=_b_-'); // not towards via + expect(turns[4].u).to.be.not.ok; + + expect(turns[5].key).to.eql('=_c_='); + expect(turns[5].u).to.be.true; + expect(turns[5].restrictionID).to.eql('r1'); + expect(turns[5].direct).to.be.false; // indirect + expect(turns[5].no).to.be.true; // restricted! + expect(turns[5].only).to.be.not.ok; + + expect(turns[6].key).to.eql('=_c_~'); + expect(turns[6].u).to.be.not.ok; + expect(turns[6].restrictionID).to.eql('r1'); + expect(turns[6].direct).to.be.true; // direct + expect(turns[6].no).to.be.not.ok; + expect(turns[6].only).to.be.true; // only! + + expect(turns[7].key).to.eql('=_c_\\'); + expect(turns[7].u).to.be.not.ok; + expect(turns[7].restrictionID).to.eql('r1'); + expect(turns[7].direct).to.be.false; // indirect + expect(turns[7].no).to.be.true; // restricted! + expect(turns[7].only).to.be.not.ok; + }); + }); + }); });