Changeset 4315 in josm


Ignore:
Timestamp:
Aug 13, 2011 11:19:16 AM (22 months ago)
Author:
bastiK
Message:

applied #6696 - Another way of computing merged node location (patch by Martin Ždila)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/MergeNodesAction.java

    r4310 r4315  
    22package org.openstreetmap.josm.actions; 
    33 
    4 import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.combineTigerTags; 
    5 import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.completeTagCollectionForEditing; 
    6 import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.normalizeTagCollectionBeforeEditing; 
    74import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 
    85import static org.openstreetmap.josm.tools.I18n.tr; 
     
    2522import org.openstreetmap.josm.command.DeleteCommand; 
    2623import org.openstreetmap.josm.command.SequenceCommand; 
     24import org.openstreetmap.josm.data.coor.EastNorth; 
    2725import org.openstreetmap.josm.data.coor.LatLon; 
    2826import org.openstreetmap.josm.data.osm.Node; 
     
    3533import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec; 
    3634import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog; 
     35import org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil; 
    3736import org.openstreetmap.josm.gui.layer.OsmDataLayer; 
    3837import org.openstreetmap.josm.tools.CheckParameterUtil; 
    3938import org.openstreetmap.josm.tools.ImageProvider; 
    4039import org.openstreetmap.josm.tools.Shortcut; 
     40 
    4141/** 
    4242 * Merges a collection of nodes into one 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; 
     
    101106            return targetNode; 
    102107        } 
    103  
    104         double lat = 0, lon = 0; 
    105         for (final Node n : candidates) { 
    106             lat += n.getCoor().lat(); 
    107             lon += n.getCoor().lon(); 
    108         } 
    109  
    110         return new Node(new LatLon(lat / candidates.size(), lon / candidates.size())); 
     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            } 
     114 
     115            return new Node(new EastNorth(east / size, north / size)); 
     116        } 
     117        case 2: { 
     118            final double[] weights = new double[size]; 
     119 
     120            for (int i = 0; i < size; i++) { 
     121                final LatLon c1 = candidates.get(i).getCoor(); 
     122                for (int j = i + 1; j < size; j++) { 
     123                    final LatLon c2 = candidates.get(j).getCoor(); 
     124                    final double d = c1.distance(c2); 
     125                    weights[i] += d; 
     126                    weights[j] += d; 
     127                } 
     128            } 
     129 
     130            double east = 0, north = 0, weight = 0; 
     131            for (int i = 0; i < size; i++) { 
     132                final EastNorth en = candidates.get(i).getEastNorth(); 
     133                final double w = weights[i]; 
     134                east += en.east() * w; 
     135                north += en.north() * w; 
     136                weight += w; 
     137            } 
     138 
     139            return new Node(new EastNorth(east / weight, north / weight)); 
     140        } 
     141        default: 
     142            throw new RuntimeException("unacceptable merge-nodes.mode"); 
     143        } 
     144 
    111145    } 
    112146 
     
    231265        // 
    232266        TagCollection nodeTags = TagCollection.unionOfAllPrimitives(nodes); 
    233         combineTigerTags(nodeTags); 
    234         normalizeTagCollectionBeforeEditing(nodeTags, nodes); 
     267        TagConflictResolutionUtil.combineTigerTags(nodeTags); 
     268        TagConflictResolutionUtil.normalizeTagCollectionBeforeEditing(nodeTags, nodes); 
    235269        TagCollection nodeTagsToEdit = new TagCollection(nodeTags); 
    236         completeTagCollectionForEditing(nodeTagsToEdit); 
     270        TagConflictResolutionUtil.completeTagCollectionForEditing(nodeTagsToEdit); 
    237271 
    238272        // launch a conflict resolution dialog, if necessary 
Note: See TracChangeset for help on using the changeset viewer.