Ticket #6696: merge.patch

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

the patch

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

     
    9393     * @return the coordinates of this node are later used for the target node
    9494     */
    9595    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);
    98103            for (final Node n : candidates) { // pick last one
    99104                targetNode = n;
    100105            }
    101106            return targetNode;
    102107        }
     108        case 1: {
     109            double lat = 0, lon = 0;
     110            for (final Node n : candidates) {
     111                lat += n.getCoor().lat();
     112                lon += n.getCoor().lon();
     113            }
    103114
    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 LatLon(lat / size, lon / size));  // TODO: not exact for long distances
    108116        }
     117        case 2: {
     118            class WeightCoord {
     119                LatLon latLon;
     120                double weight;
     121            }
    109122
    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 LatLon c1 = candidates.get(i).getCoor();
     128                    final LatLon c2 = candidates.get(j).getCoor();
     129
     130                    WeightCoord wc = new WeightCoord();
     131                    wc.latLon = c1.interpolate(c2, 0.5);
     132                    wc.weight = c1.greatCircleDistance(c2);
     133                    wcl.add(wc);
     134                    weight += wc.weight;
     135                }
     136            }
     137
     138            double lat = 0, lon = 0;
     139            for (final WeightCoord wc : wcl) {
     140                lat += wc.latLon.lat() * wc.weight;
     141                lon += wc.latLon.lon() * wc.weight;
     142            }
     143
     144            return new Node(new LatLon(lat / weight, lon / weight)); // TODO: not exact for long distances
     145        }
     146        default:
     147            throw new RuntimeException("unacceptable merge-nodes.mode");
     148        }
     149
     150
    111151    }
    112152
    113153    /**