WIP: Add insertHint to actionAddMember, actionSplit

This commit is contained in:
Bryan Housel
2018-01-15 23:13:59 -05:00
parent 03fa6e7be9
commit 221158e918
4 changed files with 30 additions and 22 deletions
+11 -5
View File
@@ -1,18 +1,21 @@
import { osmJoinWays } from '../osm';
export function actionAddMember(relationId, member, memberIndex) {
return function(graph) {
export function actionAddMember(relationId, member, memberIndex, insertHint) {
var action = function(graph) {
var relation = graph.entity(relationId);
var numAdded = 0;
// If we weren't passed a memberIndex,
// try to perform sensible inserts based on how the ways join together
if (isNaN(memberIndex) && member.type === 'way') {
if ((isNaN(memberIndex) || insertHint) && member.type === 'way') {
var members = relation.indexedMembers();
members.push(member);
if (!insertHint) {
members.push(member); // just push and let osmJoinWays sort it out
}
var joined = osmJoinWays(members, graph);
var joined = osmJoinWays(members, graph, insertHint);
for (var i = 0; i < joined.length; i++) {
var segment = joined[i];
@@ -40,4 +43,7 @@ export function actionAddMember(relationId, member, memberIndex) {
return graph.replace(relation);
};
return action;
}
+5 -7
View File
@@ -134,14 +134,12 @@ export function actionSplit(nodeId, newWayIds) {
role: relation.memberById(wayA.id).role
};
// how many times should this member be inserted?
// var matches = relation.members.filter(function(member) {
// return member.id === wayA.id;
// });
var insertHint = {
item: member,
nextTo: wayA.id
};
// matches.forEach(function() {
graph = actionAddMember(relation.id, member)(graph);
// });
graph = actionAddMember(relation.id, member, undefined, insertHint)(graph);
}
});
+6 -6
View File
@@ -88,12 +88,12 @@ export function osmSimpleMultipolygonOuterMember(entity, graph) {
// Incomplete members (those for which `graph.hasEntity(element.id)` returns
// false) and non-way members are ignored.
//
// `tryInsert` is an optional object.
// If supplied, insert the given way/member after an existing way/member:
// `{ item: wayOrMember, afterID: id }`
// `insertHint` is an optional object.
// If supplied, insert the given way/member next to an existing way/member:
// `{ item: wayOrMember, nextTo: id }`
// (This feature is used by `actionSplit`)
//
export function osmJoinWays(toJoin, graph, tryInsert) {
export function osmJoinWays(toJoin, graph, insertHint) {
function resolve(member) {
return graph.childNodes(graph.entity(member.id));
}
@@ -132,8 +132,8 @@ export function osmJoinWays(toJoin, graph, tryInsert) {
// If it is time to attempt an insert, try that item first.
// Otherwise, search for a next item in `toJoin`
var toCheck;
if (!isInserting && tryInsert && tryInsert.afterID === currWays[currWays.length - 1].id) {
toCheck = [tryInsert.item];
if (!isInserting && insertHint && insertHint.nextTo === currWays[currWays.length - 1].id) {
toCheck = [insertHint.item];
isInserting = true;
} else {
toCheck = toJoin.slice();
+8 -4
View File
@@ -101,7 +101,7 @@ describe('iD.actionAddMember', function() {
expect(members(graph)).to.eql(['-', '=', '~']);
});
it('inserts the member multiple times if the way exists multiple times (middle)', function() {
it('inserts the member multiple times if hint provided (middle)', function() {
// Before: a ---> b .. c ~~~> d <~~~ c .. b <--- a
// After: a ---> b ===> c ~~~> d <~~~ c <=== b <--- a
var graph = iD.coreGraph([
@@ -120,11 +120,13 @@ describe('iD.actionAddMember', function() {
]})
]);
graph = iD.actionAddMember('r', {id: '=', type: 'way'})(graph);
var member = { id: '=', type: 'way' };
var hint = { item: member, nextTo: '-' };
graph = iD.actionAddMember('r', member, undefined, hint)(graph);
expect(members(graph)).to.eql(['-', '=', '~', '~', '=', '-']);
});
it('inserts the member multiple times if the way exists multiple times (beginning/end)', function() {
it('inserts the member multiple times if hint provided (beginning/end)', function() {
// Before: b ===> c ~~~> d <~~~ c <=== b
// After: a ---> b ===> c ~~~> d <~~~ c <=== b <--- a
var graph = iD.coreGraph([
@@ -143,7 +145,9 @@ describe('iD.actionAddMember', function() {
]})
]);
graph = iD.actionAddMember('r', {id: '-', type: 'way'})(graph);
var member = { id: '-', type: 'way' };
var hint = { item: member, nextTo: '=' };
graph = iD.actionAddMember('r', member, undefined, hint)(graph);
expect(members(graph)).to.eql(['-', '=', '~', '~', '=', '-']);
});