Changeset 27971 in osm for applications/editors/josm/plugins/utilsplugin2
- Timestamp:
- 2012-03-02T06:02:28+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/utilsplugin2/src/utilsplugin2/dumbutils/ReplaceGeometryAction.java
r27937 r27971 80 80 * Replace or upgrade a node to a way or multipolygon 81 81 * 82 * @param node83 * @param target82 * @param subjectNode node to be replaced 83 * @param referenceObject object with greater spatial quality 84 84 */ 85 public boolean replaceNode(Node node, OsmPrimitive target) {86 if (!OsmPrimitive.getFilteredList( node.getReferrers(), Way.class).isEmpty()) {85 public boolean replaceNode(Node subjectNode, OsmPrimitive referenceObject) { 86 if (!OsmPrimitive.getFilteredList(subjectNode.getReferrers(), Way.class).isEmpty()) { 87 87 JOptionPane.showMessageDialog(Main.parent, tr("Node belongs to way(s), cannot replace."), 88 88 TITLE, JOptionPane.INFORMATION_MESSAGE); … … 90 90 } 91 91 92 if ( target instanceof Relation && !((Relation) target).isMultipolygon()) {92 if (referenceObject instanceof Relation && !((Relation) referenceObject).isMultipolygon()) { 93 93 JOptionPane.showMessageDialog(Main.parent, tr("Relation is not a multipolygon, cannot be used as a replacement."), 94 94 TITLE, JOptionPane.INFORMATION_MESSAGE); … … 98 98 Node nodeToReplace = null; 99 99 // see if we need to replace a node in the replacement way to preserve connection in history 100 if (! node.isNew()) {100 if (!subjectNode.isNew()) { 101 101 // Prepare a list of nodes that are not important 102 102 Collection<Node> nodePool = new HashSet<Node>(); 103 if ( target instanceof Way) {104 nodePool.addAll(getUnimportantNodes((Way) target));105 } else if ( target instanceof Relation) {106 for (RelationMember member : ((Relation) target).getMembers()) {103 if (referenceObject instanceof Way) { 104 nodePool.addAll(getUnimportantNodes((Way) referenceObject)); 105 } else if (referenceObject instanceof Relation) { 106 for (RelationMember member : ((Relation) referenceObject).getMembers()) { 107 107 if ((member.getRole().equals("outer") || member.getRole().equals("inner")) 108 108 && member.isWay()) { … … 115 115 assert false; 116 116 } 117 nodeToReplace = findNearestNode( node, nodePool);117 nodeToReplace = findNearestNode(subjectNode, nodePool); 118 118 } 119 119 120 120 List<Command> commands = new ArrayList<Command>(); 121 AbstractMap<String, String> nodeTags = (AbstractMap<String, String>) node.getKeys();121 AbstractMap<String, String> nodeTags = (AbstractMap<String, String>) subjectNode.getKeys(); 122 122 123 123 // merge tags 124 Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands( node, target);124 Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(subjectNode, referenceObject); 125 125 if (tagResolutionCommands == null) { 126 126 // user canceled tag merge dialog … … 135 135 List<Node> wayNodes = parentWay.getNodes(); 136 136 int idx = wayNodes.indexOf(nodeToReplace); 137 wayNodes.set(idx, node);137 wayNodes.set(idx, subjectNode); 138 138 if (idx == 0 && parentWay.isClosed()) { 139 139 // node is at start/end of way 140 wayNodes.set(wayNodes.size() - 1, node);140 wayNodes.set(wayNodes.size() - 1, subjectNode); 141 141 } 142 142 commands.add(new ChangeNodesCommand(parentWay, wayNodes)); 143 commands.add(new MoveCommand( node, nodeToReplace.getCoor()));143 commands.add(new MoveCommand(subjectNode, nodeToReplace.getCoor())); 144 144 commands.add(new DeleteCommand(nodeToReplace)); 145 145 … … 147 147 if (!nodeTags.isEmpty()) { 148 148 for (String key : nodeTags.keySet()) { 149 commands.add(new ChangePropertyCommand( node, key, null));149 commands.add(new ChangePropertyCommand(subjectNode, key, null)); 150 150 } 151 151 … … 153 153 } else { 154 154 // no node to replace, so just delete the original node 155 commands.add(new DeleteCommand( node));156 } 157 158 getCurrentDataSet().setSelected( target);155 commands.add(new DeleteCommand(subjectNode)); 156 } 157 158 getCurrentDataSet().setSelected(referenceObject); 159 159 160 160 Main.main.undoRedo.add(new SequenceCommand( 161 tr("Replace geometry for node {0}", node.getDisplayName(DefaultNameFormatter.getInstance())),161 tr("Replace geometry for node {0}", subjectNode.getDisplayName(DefaultNameFormatter.getInstance())), 162 162 commands)); 163 163 return true; … … 184 184 } 185 185 } 186 Way geometry = selection.get(idxNew);187 Way way = selection.get(1 - idxNew);188 189 if( !overrideNewCheck && ( way.isNew() || !geometry.isNew()) ) {186 Way referenceWay = selection.get(idxNew); 187 Way subjectWay = selection.get(1 - idxNew); 188 189 if( !overrideNewCheck && (subjectWay.isNew() || !referenceWay.isNew()) ) { 190 190 JOptionPane.showMessageDialog(Main.parent, 191 191 tr("Please select one way that exists in the database and one new way with correct geometry."), … … 193 193 return false; 194 194 } 195 return replaceWayWithWay(subjectWay, referenceWay); 196 } 197 198 public static boolean replaceWayWithWay(Way subjectWay, Way referenceWay) { 195 199 196 200 Area a = getCurrentDataSet().getDataSourceArea(); 197 if (!isInArea( way, a) || !isInArea(geometry, a)) {201 if (!isInArea(subjectWay, a) || !isInArea(referenceWay, a)) { 198 202 JOptionPane.showMessageDialog(Main.parent, 199 203 tr("The ways must be entirely within the downloaded area."), … … 202 206 } 203 207 204 if (hasImportantNode( geometry, way)) {208 if (hasImportantNode(referenceWay, subjectWay)) { 205 209 JOptionPane.showMessageDialog(Main.parent, 206 210 tr("The way to be replaced cannot have any nodes with properties or relation memberships unless they belong to both ways."), … … 212 216 213 217 // merge tags 214 Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands( geometry, way);218 Collection<Command> tagResolutionCommands = getTagConflictResolutionCommands(referenceWay, subjectWay); 215 219 if (tagResolutionCommands == null) { 216 220 // user canceled tag merge dialog … … 220 224 221 225 // Prepare a list of nodes that are not used anywhere except in the way 222 Collection<Node> nodePool = getUnimportantNodes( way);226 Collection<Node> nodePool = getUnimportantNodes(subjectWay); 223 227 224 228 // And the same for geometry, list nodes that can be freely deleted 225 229 Set<Node> geometryPool = new HashSet<Node>(); 226 for( Node node : geometry.getNodes() ) {230 for( Node node : referenceWay.getNodes() ) { 227 231 List<OsmPrimitive> referrers = node.getReferrers(); 228 232 if( node.isNew() && !node.isDeleted() && referrers.size() == 1 229 && referrers.get(0).equals( geometry) && !way.containsNode(node)233 && referrers.get(0).equals(referenceWay) && !subjectWay.containsNode(node) 230 234 && !hasInterestingKey(node)) 231 235 geometryPool.add(node); … … 248 252 249 253 // And prepare a list of nodes with all the replacements 250 List<Node> geometryNodes = geometry.getNodes();254 List<Node> geometryNodes = referenceWay.getNodes(); 251 255 for( int i = 0; i < geometryNodes.size(); i++ ) 252 256 if( nodeAssoc.containsKey(geometryNodes.get(i)) ) … … 254 258 255 259 // Now do the replacement 256 commands.add(new ChangeNodesCommand( way, geometryNodes));260 commands.add(new ChangeNodesCommand(subjectWay, geometryNodes)); 257 261 258 262 // Move old nodes to new positions … … 261 265 262 266 // Remove geometry way from selection 263 getCurrentDataSet().clearSelection( geometry);267 getCurrentDataSet().clearSelection(referenceWay); 264 268 265 269 // And delete old geometry way 266 commands.add(new DeleteCommand( geometry));270 commands.add(new DeleteCommand(referenceWay)); 267 271 268 272 // Delete nodes that are not used anymore … … 272 276 // Two items in undo stack: change original way and delete geometry way 273 277 Main.main.undoRedo.add(new SequenceCommand( 274 tr("Replace geometry for way {0}", way.getDisplayName(DefaultNameFormatter.getInstance())),278 tr("Replace geometry for way {0}", subjectWay.getDisplayName(DefaultNameFormatter.getInstance())), 275 279 commands)); 276 280 return true; … … 283 287 * @return 284 288 */ 285 protected Collection<Node> getUnimportantNodes(Way way) {289 protected static Collection<Node> getUnimportantNodes(Way way) { 286 290 Set<Node> nodePool = new HashSet<Node>(); 287 291 for (Node n : way.getNodes()) { … … 302 306 * @return 303 307 */ 304 protected boolean hasImportantNode(Way geometry, Way way) {308 protected static boolean hasImportantNode(Way geometry, Way way) { 305 309 for (Node n : way.getNodes()) { 306 310 // if original and replacement way share a node, it's safe to replace … … 321 325 } 322 326 323 protected boolean hasInterestingKey(OsmPrimitive object) {327 protected static boolean hasInterestingKey(OsmPrimitive object) { 324 328 for (String key : object.getKeys().keySet()) { 325 329 if (!OsmPrimitive.isUninterestingKey(key)) { … … 355 359 * needed. 356 360 * 357 * @param source 358 * @param target 361 * @param source object tags are merged from 362 * @param target object tags are merged to 359 363 * @return 360 364 */ 361 p ublic List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) {365 protected static List<Command> getTagConflictResolutionCommands(OsmPrimitive source, OsmPrimitive target) { 362 366 Collection<OsmPrimitive> primitives = Arrays.asList(source, target); 363 367 … … 394 398 * @return null if there is no such node. 395 399 */ 396 pr ivateNode findNearestNode( Node node, Collection<Node> nodes ) {400 protected static Node findNearestNode( Node node, Collection<Node> nodes ) { 397 401 if( nodes.contains(node) ) 398 402 return node;
Note:
See TracChangeset
for help on using the changeset viewer.