Ticket #6696: merge.2.patch
File merge.2.patch, 3.2 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. LatLon;27 import org.openstreetmap.josm.data.coor.EastNorth; 28 28 import org.openstreetmap.josm.data.osm.Node; 29 29 import org.openstreetmap.josm.data.osm.OsmPrimitive; 30 30 import org.openstreetmap.josm.data.osm.RelationToChildReference; … … 93 93 * @return the coordinates of this node are later used for the target node 94 94 */ 95 95 public static Node selectTargetLocationNode(List<Node> candidates) { 96 if (! Main.pref.getBoolean("merge-nodes.average-location", false)) { 97 Node targetNode = null; 96 int size = candidates.size(); 97 if (size == 0) 98 throw new IllegalArgumentException("empty list"); 99 100 switch (Main.pref.getInteger("merge-nodes.mode", 0)) { 101 case 0: { 102 Node targetNode = candidates.get(size - 1); 98 103 for (final Node n : candidates) { // pick last one 99 104 targetNode = n; 100 105 } 101 106 return targetNode; 102 107 } 108 case 1: { 109 double east = 0, north = 0; 110 for (final Node n : candidates) { 111 east += n.getEastNorth().east(); 112 north += n.getEastNorth().north(); 113 } 103 114 104 double lat = 0, lon = 0; 105 for (final Node n : candidates) { 106 lat += n.getCoor().lat(); 107 lon += n.getCoor().lon(); 115 return new Node(new EastNorth(east / size, north / size)); // TODO: not exact for long distances 108 116 } 117 case 2: { 118 class WeightCoord { 119 EastNorth eastNorth; 120 double weight; 121 } 109 122 110 return new Node(new LatLon(lat / candidates.size(), lon / candidates.size())); 123 List<WeightCoord> wcl = new ArrayList<WeightCoord>(); 124 double weight = 0; 125 for (int i = 0; i < size; i++) { 126 for (int j = i + 1; j < size; j++) { 127 final EastNorth c1 = candidates.get(i).getEastNorth(); 128 final EastNorth c2 = candidates.get(j).getEastNorth(); 129 130 WeightCoord wc = new WeightCoord(); 131 wc.eastNorth = c1.interpolate(c2, 0.5); 132 wc.weight = c1.distance(c2); 133 wcl.add(wc); 134 weight += wc.weight; 135 } 136 } 137 138 double east = 0, north = 0; 139 for (final WeightCoord wc : wcl) { 140 east += wc.eastNorth.east() * wc.weight; 141 north += wc.eastNorth.north() * wc.weight; 142 } 143 144 return new Node(new EastNorth(east / weight, north / weight)); // TODO: not exact for long distances 145 } 146 default: 147 throw new RuntimeException("unacceptable merge-nodes.mode"); 148 } 149 150 111 151 } 112 152 113 153 /**