-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
join.js
103 lines (78 loc) · 2.97 KB
/
join.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
import _extend from 'lodash-es/extend';
import _groupBy from 'lodash-es/groupBy';
import _map from 'lodash-es/map';
import { actionDeleteWay } from './delete_way';
import {
osmIsInterestingTag,
osmJoinWays
} from '../osm';
// Join ways at the end node they share.
//
// This is the inverse of `iD.actionSplit`.
//
// Reference:
// https://github.com/systemed/potlatch2/blob/master/net/systemeD/halcyon/connection/actions/MergeWaysAction.as
// https://github.com/openstreetmap/josm/blob/mirror/src/org/openstreetmap/josm/actions/CombineWayAction.java
//
export function actionJoin(ids) {
function groupEntitiesByGeometry(graph) {
var entities = ids.map(function(id) { return graph.entity(id); });
return _extend({line: []}, _groupBy(entities, function(entity) { return entity.geometry(graph); }));
}
var action = function(graph) {
var ways = ids.map(graph.entity, graph),
survivor = ways[0];
// Prefer to keep an existing way.
for (var i = 0; i < ways.length; i++) {
if (!ways[i].isNew()) {
survivor = ways[i];
break;
}
}
var joined = osmJoinWays(ways, graph)[0];
survivor = survivor.update({nodes: _map(joined.nodes, 'id')});
graph = graph.replace(survivor);
joined.forEach(function(way) {
if (way.id === survivor.id)
return;
graph.parentRelations(way).forEach(function(parent) {
graph = graph.replace(parent.replaceMember(way, survivor));
});
survivor = survivor.mergeTags(way.tags);
graph = graph.replace(survivor);
graph = actionDeleteWay(way.id)(graph);
});
return graph;
};
action.disabled = function(graph) {
var geometries = groupEntitiesByGeometry(graph);
if (ids.length < 2 || ids.length !== geometries.line.length)
return 'not_eligible';
var joined = osmJoinWays(ids.map(graph.entity, graph), graph);
if (joined.length > 1)
return 'not_adjacent';
var nodeIds = _map(joined[0].nodes, 'id').slice(1, -1),
relation,
tags = {},
conflicting = false;
joined[0].forEach(function(way) {
var parents = graph.parentRelations(way);
parents.forEach(function(parent) {
if (parent.isRestriction() && parent.members.some(function(m) { return nodeIds.indexOf(m.id) >= 0; }))
relation = parent;
});
for (var k in way.tags) {
if (!(k in tags)) {
tags[k] = way.tags[k];
} else if (tags[k] && osmIsInterestingTag(k) && tags[k] !== way.tags[k]) {
conflicting = true;
}
}
});
if (relation)
return 'restriction';
if (conflicting)
return 'conflicting_tags';
};
return action;
}