Ticket #6696: merge.3.patch
File merge.3.patch, 2.9 KB (added by , 13 years ago) |
---|
-
src/org/openstreetmap/josm/actions/MergeNodesAction.java
24 24 import org.openstreetmap.josm.command.Command; 25 25 import org.openstreetmap.josm.command.DeleteCommand; 26 26 import org.openstreetmap.josm.command.SequenceCommand; 27 import org.openstreetmap.josm.data.coor.EastNorth; 27 28 import org.openstreetmap.josm.data.coor.LatLon; 28 29 import org.openstreetmap.josm.data.osm.Node; 29 30 import org.openstreetmap.josm.data.osm.OsmPrimitive; … … 93 94 * @return the coordinates of this node are later used for the target node 94 95 */ 95 96 public static Node selectTargetLocationNode(List<Node> candidates) { 96 if (! Main.pref.getBoolean("merge-nodes.average-location", false)) { 97 Node targetNode = null; 97 int size = candidates.size(); 98 if (size == 0) 99 throw new IllegalArgumentException("empty list"); 100 101 switch (Main.pref.getInteger("merge-nodes.mode", 0)) { 102 case 0: { 103 Node targetNode = candidates.get(size - 1); 98 104 for (final Node n : candidates) { // pick last one 99 105 targetNode = n; 100 106 } 101 107 return targetNode; 102 108 } 109 case 1: { 110 double east = 0, north = 0; 111 for (final Node n : candidates) { 112 east += n.getEastNorth().east(); 113 north += n.getEastNorth().north(); 114 } 103 115 104 double lat = 0, lon = 0; 105 for (final Node n : candidates) { 106 lat += n.getCoor().lat(); 107 lon += n.getCoor().lon(); 116 return new Node(new EastNorth(east / size, north / size)); 108 117 } 118 case 2: { 119 final double[] weights = new double[size]; 109 120 110 return new Node(new LatLon(lat / candidates.size(), lon / candidates.size())); 121 for (int i = 0; i < size; i++) { 122 final LatLon c1 = candidates.get(i).getCoor(); 123 for (int j = i + 1; j < size; j++) { 124 final LatLon c2 = candidates.get(j).getCoor(); 125 final double d = c1.distance(c2); 126 weights[i] += d; 127 weights[j] += d; 128 } 129 } 130 131 double east = 0, north = 0, weight = 0; 132 for (int i = 0; i < size; i++) { 133 final EastNorth en = candidates.get(i).getEastNorth(); 134 final double w = weights[i]; 135 east += en.east() * w; 136 north += en.north() * w; 137 weight += w; 138 } 139 140 return new Node(new EastNorth(east / weight, north / weight)); 141 } 142 default: 143 throw new RuntimeException("unacceptable merge-nodes.mode"); 144 } 145 146 111 147 } 112 148 113 149 /**