From 092e26f7b6b78e9167f15f6c050ca0cdf22d2c4a Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Wed, 2 Oct 2019 14:27:37 -0600 Subject: [PATCH] Refactor create commands to make it more understandable, and to (hopefully) get primitives not in the dataset Signed-off-by: Taylor Smock --- .../commands/CreateConnectionsCommand.java | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/rapid/commands/CreateConnectionsCommand.java b/src/main/java/org/openstreetmap/josm/plugins/rapid/commands/CreateConnectionsCommand.java index 85ba158..9a2880c 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/rapid/commands/CreateConnectionsCommand.java +++ b/src/main/java/org/openstreetmap/josm/plugins/rapid/commands/CreateConnectionsCommand.java @@ -63,31 +63,10 @@ public class CreateConnectionsCommand extends Command { List changedKeyList = new ArrayList<>(); for (Node node : nodes) { if (node.hasKey(CONN_KEY)) { - // Currently w,n,n - OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get(CONN_KEY)); - for (int i = 0; i < primitiveConnections.length / 3; i++) { - if (primitiveConnections[i] instanceof Way && primitiveConnections[i + 1] instanceof Node - && primitiveConnections[i + 2] instanceof Node) { - Command addNodesToWayCommand = addNodesToWay(node, (Way) primitiveConnections[i], - (Node) primitiveConnections[i + 1], (Node) primitiveConnections[i + 2]); - if (addNodesToWayCommand != null) { - changedKeyList.add(addNodesToWayCommand); - } - } else { - Logging.error("{0}: {1}, {2}: {3}, {4}: {5}", i, primitiveConnections[i].getClass(), i + 1, - primitiveConnections[i + 1].getClass(), i + 2, primitiveConnections[i + 2].getClass()); - } - } - Logging.debug("RapiD: Removing conn from {0} in {1}", node, dataSet.getName()); - changedKeyList.add(new ChangePropertyCommand(node, CONN_KEY, null)); + changedKeyList.addAll(connectedCommand(dataSet, node)); } if (node.hasKey(DUPE_KEY)) { - OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get(DUPE_KEY)); - if (primitiveConnections.length != 1) { - Logging.error("RapiD: dupe connection connected to more than one node? (dupe={0})", - node.get(DUPE_KEY)); - } - Command replaceCommand = replaceNode(node, (Node) primitiveConnections[0]); + Command replaceCommand = duplicateNode(dataSet, node); if (replaceCommand != null) { changedKeyList.add(replaceCommand); } @@ -98,6 +77,35 @@ public class CreateConnectionsCommand extends Command { return null; } + private List connectedCommand(DataSet dataSet, Node node) { + List commands = new ArrayList<>(); + OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get(CONN_KEY)); + for (int i = 0; i < primitiveConnections.length / 3; i++) { + if (primitiveConnections[i] instanceof Way && primitiveConnections[i + 1] instanceof Node + && primitiveConnections[i + 2] instanceof Node) { + Command addNodesToWayCommand = addNodesToWay(node, (Way) primitiveConnections[i], + (Node) primitiveConnections[i + 1], (Node) primitiveConnections[i + 2]); + if (addNodesToWayCommand != null) { + commands.add(addNodesToWayCommand); + } + } else { + Logging.error("{0}: {1}, {2}: {3}, {4}: {5}", i, primitiveConnections[i].getClass(), i + 1, + primitiveConnections[i + 1].getClass(), i + 2, primitiveConnections[i + 2].getClass()); + } + } + Logging.debug("RapiD: Removing conn from {0} in {1}", node, dataSet.getName()); + commands.add(new ChangePropertyCommand(node, CONN_KEY, null)); + return commands; + } + + private Command duplicateNode(DataSet dataSet, Node node) { + OsmPrimitive[] primitiveConnections = getPrimitives(dataSet, node.get(DUPE_KEY)); + if (primitiveConnections.length != 1) { + Logging.error("RapiD: dupe connection connected to more than one node? (dupe={0})", node.get(DUPE_KEY)); + } + return replaceNode(node, (Node) primitiveConnections[0]); + } + /** * Get the primitives from a dataset with specified ids *