Ticket #6696: merge.3.patch

File merge.3.patch, 2.9 KB (added by *Martin*, 13 years ago)

optimized patch

  • src/org/openstreetmap/josm/actions/MergeNodesAction.java

     
    2424import org.openstreetmap.josm.command.Command;
    2525import org.openstreetmap.josm.command.DeleteCommand;
    2626import org.openstreetmap.josm.command.SequenceCommand;
     27import org.openstreetmap.josm.data.coor.EastNorth;
    2728import org.openstreetmap.josm.data.coor.LatLon;
    2829import org.openstreetmap.josm.data.osm.Node;
    2930import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    9394     * @return the coordinates of this node are later used for the target node
    9495     */
    9596    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);
    98104            for (final Node n : candidates) { // pick last one
    99105                targetNode = n;
    100106            }
    101107            return targetNode;
    102108        }
     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            }
    103115
    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));
    108117        }
     118        case 2: {
     119            final double[] weights = new double[size];
    109120
    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
    111147    }
    112148
    113149    /**