mirror of
https://github.com/FoggedLens/iD.git
synced 2026-05-21 15:56:56 +02:00
merge way nodes action is added
This commit is contained in:
@@ -18,6 +18,7 @@ export { actionDiscardTags } from './discard_tags';
|
||||
export { actionDisconnect } from './disconnect';
|
||||
export { actionJoin } from './join';
|
||||
export { actionMerge } from './merge';
|
||||
export { actionMergeWayNodes } from './merge_way_nodes';
|
||||
export { actionMergePolygon } from './merge_polygon';
|
||||
export { actionMergeRemoteChanges } from './merge_remote_changes';
|
||||
export { actionMove } from './move';
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import _sum from 'lodash-es/sum';
|
||||
|
||||
import { osmNode } from '../osm/node';
|
||||
|
||||
|
||||
export function actionMergeWayNodes (ids) {
|
||||
function getSelectedEntities (graph) {
|
||||
return ids.map(function (id) { return graph.entity(id); });
|
||||
}
|
||||
|
||||
function calcAverageLoc (nodes) {
|
||||
return [
|
||||
_sum(nodes.map(function (node) { return node.loc[0]; })) / nodes.length,
|
||||
_sum(nodes.map(function (node) { return node.loc[1]; })) / nodes.length
|
||||
];
|
||||
}
|
||||
|
||||
function replaceWithinWays (newNode) {
|
||||
return function (graph, node) {
|
||||
return graph.parentWays(node).reduce(function (graph, way) {
|
||||
return graph.replace(way.replaceNode(node.id, newNode.id));
|
||||
}, graph);
|
||||
};
|
||||
}
|
||||
|
||||
function removeFromGraph (graph, entity) {
|
||||
return graph.remove(entity);
|
||||
}
|
||||
|
||||
var action = function (graph) {
|
||||
var nodes = getSelectedEntities(graph),
|
||||
newNode = new osmNode({ loc: calcAverageLoc(nodes) });
|
||||
|
||||
graph = graph.replace(newNode);
|
||||
|
||||
graph = nodes.reduce(replaceWithinWays(newNode), graph);
|
||||
|
||||
graph = nodes.reduce(removeFromGraph, graph);
|
||||
|
||||
return graph;
|
||||
};
|
||||
|
||||
action.disabled = function (graph) {
|
||||
function isNotWayNode (entity) { return entity.type !== 'node' || graph.parentWays(entity) <= 0; }
|
||||
function isNotExtremeNode (node) { return !node.isEndpoint(graph); }
|
||||
|
||||
var entities = getSelectedEntities(graph);
|
||||
|
||||
if (entities.some(isNotWayNode) || entities.some(isNotExtremeNode)) {
|
||||
return 'not_eligible';
|
||||
}
|
||||
};
|
||||
|
||||
return action;
|
||||
}
|
||||
Reference in New Issue
Block a user