Ticket #6696: merge.2.patch

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

improved patch using NorthEast

  • 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;
    27 import org.openstreetmap.josm.data.coor.LatLon;
     27import org.openstreetmap.josm.data.coor.EastNorth;
    2828import org.openstreetmap.josm.data.osm.Node;
    2929import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3030import org.openstreetmap.josm.data.osm.RelationToChildReference;
     
    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 east = 0, north = 0;
     110            for (final Node n : candidates) {
     111                east += n.getEastNorth().east();
     112                north += n.getEastNorth().north();
     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 EastNorth(east / size, north / size));  // TODO: not exact for long distances
    108116        }
     117        case 2: {
     118            class WeightCoord {
     119                EastNorth eastNorth;
     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 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
    111151    }
    112152
    113153    /**