Changeset 2578 in josm for trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java
- Timestamp:
- 2009-12-05T18:23:03+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java
r2575 r2578 29 29 30 30 import org.openstreetmap.josm.Main; 31 import org.openstreetmap.josm.actions.CombineWayAction;32 import org.openstreetmap.josm.actions.JosmAction;33 import org.openstreetmap.josm.actions.ReverseWayAction;34 import org.openstreetmap.josm.actions.SplitWayAction;35 31 import org.openstreetmap.josm.command.AddCommand; 36 32 import org.openstreetmap.josm.command.ChangeCommand; … … 75 71 if(this.pos == o.pos) 76 72 return (this.dis - o.dis) > 0 ? 1 : -1; 77 return this.pos - o.pos;78 } 79 } ;73 return this.pos - o.pos; 74 } 75 } 80 76 81 77 // HelperClass 82 78 // Saves a relation and a role an OsmPrimitve was part of until it was stripped from all relations 83 79 private class RelationRole { 84 public Relation rel;85 public String role;80 public final Relation rel; 81 public final String role; 86 82 public RelationRole(Relation rel, String role) { 87 83 this.rel = rel; 88 84 this.role = role; 85 } 86 87 @Override 88 public int hashCode() { 89 return rel.hashCode(); 89 90 } 90 91 … … 100 101 public JoinAreasAction() { 101 102 super(tr("Join overlapping Areas"), "joinareas", tr("Joins areas that overlap each other"), Shortcut.registerShortcut("tools:joinareas", tr("Tool: {0}", tr("Join overlapping Areas")), 102 KeyEvent.VK_J, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);103 KeyEvent.VK_J, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true); 103 104 } 104 105 … … 116 117 OsmDataLayer dataLayer = Main.map.mapView.getEditLayer(); 117 118 for (DataSource ds : dataLayer.data.dataSources) { 118 if (ds.bounds != null) 119 if (ds.bounds != null) { 119 120 bounds.add(ds.bounds); 121 } 120 122 } 121 123 … … 137 139 // This is copied from SimplifyAction and should be probably ported to tools 138 140 for (Node node : way.getNodes()) { 139 if(askedAlready) break; 141 if(askedAlready) { 142 break; 143 } 140 144 boolean isInsideOneBoundingBox = false; 141 145 for (Bounds b : bounds) { … … 151 155 + "This can lead to nodes being deleted accidentally.\n" 152 156 + "Are you really sure to continue?"), 153 tr("Please abort if you are not sure"), JOptionPane.YES_NO_OPTION,154 JOptionPane.WARNING_MESSAGE);157 tr("Please abort if you are not sure"), JOptionPane.YES_NO_OPTION, 158 JOptionPane.WARNING_MESSAGE); 155 159 156 160 if (option != JOptionPane.YES_OPTION) return; … … 173 177 DataSet ds = Main.main.getCurrentDataSet(); 174 178 ds.fireSelectionChanged(); 175 } else 179 } else { 176 180 JOptionPane.showMessageDialog(Main.parent, tr("No intersection found. Nothing was changed.")); 181 } 177 182 } 178 183 … … 199 204 // Remove ways from all relations so ways can be combined/split quietly 200 205 ArrayList<RelationRole> relations = removeFromRelations(a); 201 if(!same) relations.addAll(removeFromRelations(b)); 206 if(!same) { 207 relations.addAll(removeFromRelations(b)); 208 } 202 209 203 210 // Don't warn now, because it will really look corrupted … … 217 224 218 225 // Delete the remaining inner ways 219 if(innerWays != null && innerWays.size() > 0) 226 if(innerWays != null && innerWays.size() > 0) { 220 227 cmds.add(DeleteCommand.delete(Main.map.mapView.getEditLayer(), innerWays, true)); 228 } 221 229 commitCommands(marktr("Delete Ways that are not part of an inner multipolygon")); 222 230 … … 228 236 stripTags(newInnerWays); 229 237 makeCommitsOneAction( 230 same238 same 231 239 ? marktr("Joined self-overlapping area") 232 : marktr("Joined overlapping areas")240 : marktr("Joined overlapping areas") 233 241 ); 234 242 235 if(warnAboutRelations) 243 if(warnAboutRelations) { 236 244 JOptionPane.showMessageDialog(Main.parent, tr("Some of the ways were part of relations that have been modified. Please verify no errors have been introduced.")); 245 } 237 246 238 247 return true; … … 254 263 for (Way w : ways) { 255 264 for (Entry<String,String> e : w.entrySet()) { 256 if (!props.containsKey(e.getKey())) 265 if (!props.containsKey(e.getKey())) { 257 266 props.put(e.getKey(), new TreeSet<String>()); 267 } 258 268 props.get(e.getKey()).add(e.getValue()); 259 269 } … … 338 348 continue; 339 349 } else 340 if(a.getNode(i).equals(b.getNode(j+1)) || a.getNode(i+1).equals(b.getNode(j+1))) {341 nodes.add(b.getNode(j+1));342 continue;343 }350 if(a.getNode(i).equals(b.getNode(j+1)) || a.getNode(i+1).equals(b.getNode(j+1))) { 351 nodes.add(b.getNode(j+1)); 352 continue; 353 } 344 354 LatLon intersection = getLineLineIntersection( 345 355 a.getNode(i) .getEastNorth().east(), a.getNode(i) .getEastNorth().north(), … … 347 357 b.getNode(j) .getEastNorth().east(), b.getNode(j) .getEastNorth().north(), 348 358 b.getNode(j+1).getEastNorth().east(), b.getNode(j+1).getEastNorth().north()); 349 if(intersection == null) continue; 359 if(intersection == null) { 360 continue; 361 } 350 362 351 363 // Create the node. Adding them to the ways must be delayed because we still loop over them … … 355 367 // The distance is needed to sort and add the nodes in direction of the way 356 368 nodesA.add(new NodeToSegs(i, n, a.getNode(i).getCoor())); 357 if(same) 369 if(same) { 358 370 nodesA.add(new NodeToSegs(j, n, a.getNode(j).getCoor())); 359 else371 } else { 360 372 nodesB.add(new NodeToSegs(j, n, b.getNode(j).getCoor())); 373 } 361 374 } 362 375 } 363 376 364 377 addNodesToWay(a, nodesA); 365 if(!same) addNodesToWay(b, nodesB); 378 if(!same) { 379 addNodesToWay(b, nodesB); 380 } 366 381 367 382 return nodes; … … 373 388 */ 374 389 static private LatLon getLineLineIntersection( 375 376 390 double x1, double y1, double x2, double y2, 391 double x3, double y3, double x4, double y4) { 377 392 378 393 if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null; … … 392 407 393 408 return Main.proj.eastNorth2latlon(new EastNorth( 394 (b1*c2 - b2*c1)/det,395 (a2*c1 -a1*c2)/det409 (b1*c2 - b2*c1)/det, 410 (a2*c1 -a1*c2)/det 396 411 )); 397 412 } … … 421 436 private void commitCommands(String description) { 422 437 switch(cmds.size()) { 423 424 425 426 427 428 429 430 431 438 case 0: 439 return; 440 case 1: 441 Main.main.undoRedo.add(cmds.getFirst()); 442 break; 443 default: 444 Command c = new SequenceCommand(tr(description), cmds); 445 Main.main.undoRedo.add(c); 446 break; 432 447 } 433 448 … … 444 459 ArrayList<RelationRole> result = new ArrayList<RelationRole>(); 445 460 for (Relation r : Main.main.getCurrentDataSet().getRelations()) { 446 if (r.isDeleted() || r.incomplete) continue; 461 if (r.isDeleted() || r.isIncomplete()) { 462 continue; 463 } 447 464 for (RelationMember rm : r.getMembers()) { 448 if (rm.getMember() != osm) continue; 465 if (rm.getMember() != osm) { 466 continue; 467 } 449 468 450 469 Relation newRel = new Relation(r); … … 455 474 cmds.add(new ChangeCommand(r, newRel)); 456 475 RelationRole saverel = new RelationRole(r, rm.getRole()); 457 if(!result.contains(saverel)) result.add(saverel); 476 if(!result.contains(saverel)) { 477 result.add(saverel); 478 } 458 479 break; 459 480 } … … 471 492 ArrayList<Way> ways = new ArrayList<Way>(); 472 493 ways.add(a); 473 if(!a.equals(b)) ways.add(b); 494 if(!a.equals(b)) { 495 ways.add(b); 496 } 474 497 475 498 List<OsmPrimitive> affected = new ArrayList<OsmPrimitive>(); … … 493 516 Collection<Way> result = new ArrayList<Way>(); 494 517 for(OsmPrimitive w: ways) { 495 if(w instanceof Way) result.add((Way) w); 518 if(w instanceof Way) { 519 result.add((Way) w); 520 } 496 521 } 497 522 return result; … … 505 530 private Collection<Node> getNodesFromWays(Collection<Way> ways) { 506 531 Collection<Node> allNodes = new ArrayList<Node>(); 507 for(Way w: ways) allNodes.addAll(w.getNodes()); 532 for(Way w: ways) { 533 allNodes.addAll(w.getNodes()); 534 } 508 535 return allNodes; 509 536 } … … 521 548 for(Way w: multigonWays) { 522 549 Polygon poly = new Polygon(); 523 for(Node n: (w).getNodes()) poly.addPoint(latlonToXY(n.getCoor().lat()), latlonToXY(n.getCoor().lon())); 550 for(Node n: (w).getNodes()) { 551 poly.addPoint(latlonToXY(n.getCoor().lat()), latlonToXY(n.getCoor().lon())); 552 } 524 553 525 554 for(Node n: multigonNodes) { … … 546 575 private void getWaysByNode(Collection<Way> innerWays, Collection<Way> w, Node n) { 547 576 for(Way way : w) { 548 if(!(way).containsNode(n)) continue; 549 if(!innerWays.contains(way)) innerWays.add(way); // Will need this later for multigons 577 if(!(way).containsNode(n)) { 578 continue; 579 } 580 if(!innerWays.contains(way)) { 581 innerWays.add(way); // Will need this later for multigons 582 } 550 583 } 551 584 } … … 561 594 for(Way w: multigonWays) { 562 595 // Skip inner ways 563 if(innerWays.contains(w)) continue; 564 565 if(w.getNodesCount() <= 2) 596 if(innerWays.contains(w)) { 597 continue; 598 } 599 600 if(w.getNodesCount() <= 2) { 566 601 cmds.add(new DeleteCommand(w)); 567 else602 } else { 568 603 join.add(w); 604 } 569 605 } 570 606 … … 606 642 } 607 643 if(a.getNode(0).equals(b.getNode(0)) || 608 a.getNode(a.getNodesCount()-1).equals(b.getNode(b.getNodesCount()-1))) {644 a.getNode(a.getNodesCount()-1).equals(b.getNode(b.getNodesCount()-1))) { 609 645 Main.main.getCurrentDataSet().setSelected(b); 610 646 new ReverseWayAction().actionPerformed(null); … … 644 680 boolean hasInnerNodes = false; 645 681 for(Node n : w.getNodes()) { 646 if(outerNodes.contains(n)) continue wayIterator; 647 if(!hasInnerNodes && innerNodes.contains(n)) hasInnerNodes = true; 648 } 649 if(!hasInnerNodes || w.getNodesCount() < 2) continue; 682 if(outerNodes.contains(n)) { 683 continue wayIterator; 684 } 685 if(!hasInnerNodes && innerNodes.contains(n)) { 686 hasInnerNodes = true; 687 } 688 } 689 if(!hasInnerNodes || w.getNodesCount() < 2) { 690 continue; 691 } 650 692 possibleWays.add(w); 651 693 } … … 671 713 for(Way w2 : possibleWays) { 672 714 // w2 cannot be closed, otherwise it would have been removed above 673 if(!waysCanBeCombined(w1, w2)) continue; 715 if(!waysCanBeCombined(w1, w2)) { 716 continue; 717 } 674 718 675 719 ArrayList<Way> joinThem = new ArrayList<Way>(); … … 719 763 List<Way> removables = new ArrayList<Way>(); 720 764 for(Way a : ways) { 721 if(a.isClosed()) continue; 765 if(a.isClosed()) { 766 continue; 767 } 722 768 boolean connectedStart = false; 723 769 boolean connectedEnd = false; 724 770 for(Way b : ways) { 725 if(a.equals(b)) 771 if(a.equals(b)) { 726 772 continue; 727 if(b.isFirstLastNode(a.firstNode())) 773 } 774 if(b.isFirstLastNode(a.firstNode())) { 728 775 connectedStart = true; 729 if(b.isFirstLastNode(a.lastNode())) 776 } 777 if(b.isFirstLastNode(a.lastNode())) { 730 778 connectedEnd = true; 731 } 732 if(!connectedStart || !connectedEnd) 779 } 780 } 781 if(!connectedStart || !connectedEnd) { 733 782 removables.add(a); 783 } 734 784 } 735 785 ways.removeAll(removables); … … 749 799 for(Node n : w.getNodes()) { 750 800 x.removeNode(n); 751 if(x.containsNode(n)) count++; 801 if(x.containsNode(n)) { 802 count++; 803 } 752 804 if(count == 2) return true; 753 805 } … … 784 836 Relation newRel = new Relation(); 785 837 newRel.put("type", "multipolygon"); 786 for(Way w : inner) 838 for(Way w : inner) { 787 839 newRel.addMember(new RelationMember("inner", w)); 840 } 788 841 cmds.add(new AddCommand(newRel)); 789 842 … … 806 859 for(RelationRole r : rels) { 807 860 if( r.rel.get("type") != null && 808 r.rel.get("type").equalsIgnoreCase("multipolygon") &&809 r.role.equalsIgnoreCase("outer")810 861 r.rel.get("type").equalsIgnoreCase("multipolygon") && 862 r.role.equalsIgnoreCase("outer") 863 ) { 811 864 multiouters.add(r); 812 865 continue; … … 820 873 Relation newRel = null; 821 874 switch(multiouters.size()) { 822 case 0: 823 return; 824 case 1: 825 // Found only one to be part of a multipolygon relation, so just add it back as well 826 newRel = new Relation(multiouters.get(0).rel); 827 newRel.addMember(new RelationMember(multiouters.get(0).role, outer)); 828 cmds.add(new ChangeCommand(multiouters.get(0).rel, newRel)); 829 return; 830 default: 831 // Create a new relation with all previous members and (Way)outer as outer. 832 newRel = new Relation(); 833 for(RelationRole r : multiouters) { 834 // Add members 835 for(RelationMember rm : r.rel.getMembers()) 836 if(!newRel.getMembers().contains(rm)) newRel.addMember(rm); 837 // Add tags 838 for (String key : r.rel.keySet()) { 839 newRel.put(key, r.rel.get(key)); 875 case 0: 876 return; 877 case 1: 878 // Found only one to be part of a multipolygon relation, so just add it back as well 879 newRel = new Relation(multiouters.get(0).rel); 880 newRel.addMember(new RelationMember(multiouters.get(0).role, outer)); 881 cmds.add(new ChangeCommand(multiouters.get(0).rel, newRel)); 882 return; 883 default: 884 // Create a new relation with all previous members and (Way)outer as outer. 885 newRel = new Relation(); 886 for(RelationRole r : multiouters) { 887 // Add members 888 for(RelationMember rm : r.rel.getMembers()) 889 if(!newRel.getMembers().contains(rm)) { 890 newRel.addMember(rm); 840 891 } 841 // Delete old relation 842 cmds.add(new DeleteCommand(r.rel)); 843 } 844 newRel.addMember(new RelationMember("outer", outer)); 845 cmds.add(new AddCommand(newRel)); 892 // Add tags 893 for (String key : r.rel.keySet()) { 894 newRel.put(key, r.rel.get(key)); 895 } 896 // Delete old relation 897 cmds.add(new DeleteCommand(r.rel)); 898 } 899 newRel.addMember(new RelationMember("outer", outer)); 900 cmds.add(new AddCommand(newRel)); 846 901 } 847 902 } … … 851 906 */ 852 907 private void stripTags(Collection<Way> ways) { 853 for(Way w: ways) stripTags(w); 908 for(Way w: ways) { 909 stripTags(w); 910 } 854 911 commitCommands(marktr("Remove tags from inner ways")); 855 912 } … … 861 918 if(x.getKeys() == null) return; 862 919 Way y = new Way(x); 863 for (String key : x.keySet()) 920 for (String key : x.keySet()) { 864 921 y.remove(key); 922 } 865 923 cmds.add(new ChangeCommand(x, y)); 866 924 } … … 875 933 cmds.clear(); 876 934 int i = Math.max(ur.commands.size() - cmdsCount, 0); 877 for(; i < ur.commands.size(); i++) 935 for(; i < ur.commands.size(); i++) { 878 936 cmds.add(ur.commands.get(i)); 879 880 for(i = 0; i < cmds.size(); i++) 937 } 938 939 for(i = 0; i < cmds.size(); i++) { 881 940 ur.undo(); 941 } 882 942 883 943 commitCommands(message == null ? marktr("Join Areas Function") : message);
Note:
See TracChangeset
for help on using the changeset viewer.