Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add validation for connecting highway with extension #5646

Merged
merged 1 commit into from
Jan 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data/core.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,9 @@ en:
crossing_ways:
message: Crossing ways without connection
tooltip: "Roads are crossing other roads, buildings, railroads, or waterways without connection nodes or a bridge tag."
highway_almost_junction:
message: Almost junction
tooltip: "This node is very close but not connected to way {wid}."
fix:
delete_feature:
title: Delete this feature
Expand Down
4 changes: 4 additions & 0 deletions dist/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,10 @@
"message": "Crossing ways without connection",
"tooltip": "Roads are crossing other roads, buildings, railroads, or waterways without connection nodes or a bridge tag."
},
"highway_almost_junction": {
"message": "Almost junction",
"tooltip": "This node is very close but not connected to way {wid}."
},
"fix": {
"delete_feature": {
"title": "Delete this feature"
Expand Down
115 changes: 115 additions & 0 deletions modules/validations/highway_almost_junction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import {
geoExtent,
geoLineIntersection,
geoMetersToLat,
geoMetersToLon,
geoSphericalDistance,
geoVecInterp,
} from '../geo';
import { set as d3_set } from 'd3-collection';
import { t } from '../util/locale';
import {
ValidationIssueType,
ValidationIssueSeverity,
validationIssue,
} from './validation_issue';


/**
* Look for roads that can be connected to other roads with a short extension
*/
export function validationHighwayAlmostJunction() {

function isHighway(entity) {
return entity.type === 'way' && entity.tags.highway;
}

function findConnectableEndNodesByExtension(way, graph, tree) {
var results = [],
nidFirst = way.nodes[0],
nidLast = way.nodes[way.nodes.length - 1],
nodeFirst = graph.entity(nidFirst),
nodeLast = graph.entity(nidLast);

if (nidFirst === nidLast) return results;
if (!nodeFirst.tags.noexit && graph.parentWays(nodeFirst).length === 1) {
var widNearFirst = canConnectByExtend(way, 0, graph, tree);
if (widNearFirst !== null) {
results.push({
node: nodeFirst,
wid: widNearFirst,
});
}
}
if (!nodeLast.tags.noexit && graph.parentWays(nodeLast).length === 1) {
var widNearLast = canConnectByExtend(way, way.nodes.length - 1, graph, tree);
if (widNearLast !== null) {
results.push({
node: nodeLast,
wid: widNearLast,
});
}
}
return results;
}

function canConnectByExtend(way, endNodeIdx, graph, tree) {
var EXTEND_TH_METERS = 5,
tipNid = way.nodes[endNodeIdx], // the 'tip' node for extension point
midNid = endNodeIdx === 0 ? way.nodes[1] : way.nodes[way.nodes.length - 2], // the other node of the edge
tipNode = graph.entity(tipNid),
midNode = graph.entity(midNid),
lon = tipNode.loc[0],
lat = tipNode.loc[1],
lon_range = geoMetersToLon(EXTEND_TH_METERS, lat) / 2,
lat_range = geoMetersToLat(EXTEND_TH_METERS) / 2,
queryExtent = geoExtent([
[lon - lon_range, lat - lat_range],
[lon + lon_range, lat + lat_range]
]);

// first, extend the edge of [midNode -> tipNode] by EXTEND_TH_METERS and find the "extended tip" location
var edgeLen = geoSphericalDistance(midNode.loc, tipNode.loc),
t = EXTEND_TH_METERS / edgeLen + 1.0,
extTipLoc = geoVecInterp(midNode.loc, tipNode.loc, t);

// then, check if the extension part [tipNode.loc -> extTipLoc] intersects any other ways
var intersected = tree.intersects(queryExtent, graph);
for (var i = 0; i < intersected.length; i++) {
if (!isHighway(intersected[i]) || intersected[i].id === way.id) continue;
var way2 = intersected[i];
for (var j = 0; j < way2.nodes.length - 1; j++) {
var nA = graph.entity(way2.nodes[j]),
nB = graph.entity(way2.nodes[j + 1]);
if (geoLineIntersection([tipNode.loc, extTipLoc], [nA.loc, nB.loc])) {
return way2.id;
}
}
}
return null;
}

var validation = function(changes, graph, tree) {
var edited = changes.created.concat(changes.modified),
issues = [];
for (var i = 0; i < edited.length; i++) {
if (!isHighway(edited[i])) continue;
var extendableNodes = findConnectableEndNodesByExtension(edited[i], graph, tree);
for (var j = 0; j < extendableNodes.length; j++) {
issues.push(new validationIssue({
type: ValidationIssueType.highway_almost_junction,
severity: ValidationIssueSeverity.warning,
message: t('issues.highway_almost_junction.message'),
tooltip: t('issues.highway_almost_junction.tooltip', {wid: extendableNodes[j].wid}),
entities: [extendableNodes[j].node, graph.entity(extendableNodes[j].wid)],
coordinates: extendableNodes[j].node.loc,
}));
}
}

return issues;
};


return validation;
}
1 change: 1 addition & 0 deletions modules/validations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { validationDeprecatedTag } from './deprecated_tag';
export { validationDisconnectedHighway } from './disconnected_highway';
export { validationHighwayCrossingOtherWays } from './crossing_ways';
export { validationHighwayAlmostJunction } from './highway_almost_junction';
export { ValidationIssueType, ValidationIssueSeverity } from './validation_issue';
export { validationManyDeletions } from './many_deletions';
export { validationMapCSSChecks } from './mapcss_checks';
Expand Down
5 changes: 3 additions & 2 deletions modules/validations/validation_issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var ValidationIssueType = Object.freeze({
old_multipolygon: 'old_multipolygon',
tag_suggests_area: 'tag_suggests_area',
map_rule_issue: 'map_rule_issue',
crossing_ways: 'crossing_ways'
crossing_ways: 'crossing_ways',
highway_almost_junction: 'highway_almost_junction',
});


Expand Down Expand Up @@ -47,7 +48,7 @@ export function validationIssue(attrs) {
this.tooltip = attrs.tooltip;
this.entities = attrs.entities; // expect an array of entities
this.coordinates = attrs.coordinates; // expect a [lon, lat] array
this.info = attrs.info; // an object containing arbitrary extra information
this.info = attrs.info; // an object containing arbitrary extra information
this.fixes = attrs.fixes; // expect an array of functions for possible fixes

if (this.fixes) {
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
<script src='spec/validations/disconnected_highway.js'></script>
<script src='spec/validations/tag_suggests_area.js'></script>
<script src='spec/validations/crossing_ways.js'></script>
<script src='spec/validations/highway_almost_junction.js'></script>
<script src='spec/operations/detach_node.js'></script>
<script>
window.mocha.run();
Expand Down
187 changes: 187 additions & 0 deletions test/spec/validations/highway_almost_junction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
describe('iD.validations.highway_almost_junction', function () {
var context;

beforeEach(function() {
context = iD.Context();
});

function horizontalVertialCloserThanThd() {
// horizontal road
var n1 = iD.Node({id: 'n-1', loc: [22.42357, 0]});
var n2 = iD.Node({id: 'n-2', loc: [22.42367, 0]});
var w1 = iD.Way({id: 'w-1', nodes: ['n-1', 'n-2'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n1),
iD.actionAddEntity(n2),
iD.actionAddEntity(w1)
);

// vertical road to the west of w1 by 0.00001 logitude degree
// 5th digit after decimal point has a resolution of ~1 meter
var n3 = iD.Node({id: 'n-3', loc: [22.42356, 0.001]});
var n4 = iD.Node({id: 'n-4', loc: [22.42356, -0.001]});
var w2 = iD.Way({id: 'w-2', nodes: ['n-3', 'n-4'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n3),
iD.actionAddEntity(n4),
iD.actionAddEntity(w2)
);
}

function horizontalTiltedCloserThanThd() {
// horizontal road
var n1 = iD.Node({id: 'n-1', loc: [22.42357, 0]});
var n2 = iD.Node({id: 'n-2', loc: [22.42367, 0]});
var w1 = iD.Way({id: 'w-1', nodes: ['n-1', 'n-2'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n1),
iD.actionAddEntity(n2),
iD.actionAddEntity(w1)
);

// tilted road to the west of w1 by 0.00001 logitude degree
var n3 = iD.Node({id: 'n-3', loc: [22.423555, 0.001]});
var n4 = iD.Node({id: 'n-4', loc: [22.423565, -0.001]});
var w2 = iD.Way({id: 'w-2', nodes: ['n-3', 'n-4'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n3),
iD.actionAddEntity(n4),
iD.actionAddEntity(w2)
);
}

function horizontalVertialFurtherThanThd() {
// horizontal road
var n1 = iD.Node({id: 'n-1', loc: [22.42357, 0]});
var n2 = iD.Node({id: 'n-2', loc: [22.42367, 0]});
var w1 = iD.Way({id: 'w-1', nodes: ['n-1', 'n-2'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n1),
iD.actionAddEntity(n2),
iD.actionAddEntity(w1)
);

// vertical road to the west of w1 by 0.00007 logitude degree
var n3 = iD.Node({id: 'n-3', loc: [22.42350, 0.001]});
var n4 = iD.Node({id: 'n-4', loc: [22.42350, -0.001]});
var w2 = iD.Way({id: 'w-2', nodes: ['n-3', 'n-4'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n3),
iD.actionAddEntity(n4),
iD.actionAddEntity(w2)
);
}

function twoHorizontalCloserThanThd() {
// horizontal road
var n1 = iD.Node({id: 'n-1', loc: [22.42357, 0]});
var n2 = iD.Node({id: 'n-2', loc: [22.42367, 0]});
var w1 = iD.Way({id: 'w-1', nodes: ['n-1', 'n-2'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n1),
iD.actionAddEntity(n2),
iD.actionAddEntity(w1)
);

// another horizontal road to the north of w1 by 0.0001 latitude degree
var n3 = iD.Node({id: 'n-3', loc: [22.42357, 0.00001]});
var n4 = iD.Node({id: 'n-4', loc: [22.42367, 0.00001]});
var w2 = iD.Way({id: 'w-2', nodes: ['n-3', 'n-4'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n3),
iD.actionAddEntity(n4),
iD.actionAddEntity(w2)
);
}

function horizontalVertialWithNoExit() {
// horizontal road
var n1 = iD.Node({id: 'n-1', loc: [22.42357, 0], tags: { noexit: 'yes' }});
var n2 = iD.Node({id: 'n-2', loc: [22.42367, 0]});
var w1 = iD.Way({id: 'w-1', nodes: ['n-1', 'n-2'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n1),
iD.actionAddEntity(n2),
iD.actionAddEntity(w1)
);

// vertical road to the west of w1 by 0.00001 logitude degree
var n3 = iD.Node({id: 'n-3', loc: [22.42356, 0.001]});
var n4 = iD.Node({id: 'n-4', loc: [22.42356, -0.001]});
var w2 = iD.Way({id: 'w-2', nodes: ['n-3', 'n-4'], tags: { highway: 'residential' }});

context.perform(
iD.actionAddEntity(n3),
iD.actionAddEntity(n4),
iD.actionAddEntity(w2)
);
}

function validate() {
var validator = iD.validationHighwayAlmostJunction();
var changes = context.history().changes();
return validator(changes, context.graph(), context.history().tree());
}

it('has no errors on init', function() {
var issues = validate();
expect(issues).to.have.lengthOf(0);
});

it('horizontal and vertical road, closer than threshold', function() {
horizontalVertialCloserThanThd();
var issues = validate();
expect(issues).to.have.lengthOf(1);
var issue = issues[0];
expect(issue.type).to.eql(iD.ValidationIssueType.highway_almost_junction);
expect(issue.entities).to.have.lengthOf(2);
expect(issue.entities[0].id).to.eql('n-1');
expect(issue.entities[1].id).to.eql('w-2');

expect(issue.coordinates).to.have.lengthOf(2);
expect(issue.coordinates[0]).to.eql(22.42357);
expect(issue.coordinates[1]).to.eql(0);
});

it('horizontal and tilted road, closer than threshold', function() {
horizontalTiltedCloserThanThd();
var issues = validate();
expect(issues).to.have.lengthOf(1);
var issue = issues[0];
expect(issue.type).to.eql(iD.ValidationIssueType.highway_almost_junction);
expect(issue.entities).to.have.lengthOf(2);
expect(issue.entities[0].id).to.eql('n-1');
expect(issue.entities[1].id).to.eql('w-2');

expect(issue.coordinates).to.have.lengthOf(2);
expect(issue.coordinates[0]).to.eql(22.42357);
expect(issue.coordinates[1]).to.eql(0);
});

it('horizontal and vertical road, further than threshold', function() {
horizontalVertialFurtherThanThd();
var issues = validate();
expect(issues).to.have.lengthOf(0);
});

it('horizontal and vertical road, closer than threshold but with noexit tag', function() {
horizontalVertialWithNoExit();
var issues = validate();
expect(issues).to.have.lengthOf(0);
});

it('two horizontal roads, closer than threshold', function() {
twoHorizontalCloserThanThd();
var issues = validate();
expect(issues).to.have.lengthOf(0);
});
});