Changeset 11118 in josm


Ignore:
Timestamp:
2016-10-11T20:20:06+02:00 (8 years ago)
Author:
simon04
Message:

fix #13094 - SimplifyWayAction for first node of closed way (patch by Tyndare, modified)

Location:
trunk
Files:
2 edited

Legend:

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

    r11109 r11118  
    99import java.awt.event.KeyEvent;
    1010import java.util.ArrayList;
     11import java.util.Arrays;
    1112import java.util.Collection;
    1213import java.util.Collections;
     
    134135     */
    135136    protected boolean isRequiredNode(Way way, Node node) {
    136         boolean isRequired = Collections.frequency(way.getNodes(), node) > 1;
     137        int frequency = Collections.frequency(way.getNodes(), node);
     138        if ((way.getNode(0) == node) && (way.getNode(way.getNodesCount()-1) == node)) {
     139            frequency = frequency - 1; // closed way closing node counted only once
     140        }
     141        boolean isRequired = frequency > 1;
    137142        if (!isRequired) {
    138143            List<OsmPrimitive> parents = new LinkedList<>();
     
    187192            lower = i;
    188193            i++;
     194        }
     195
     196        if ((newNodes.size() > 3) && (newNodes.get(0) == newNodes.get(newNodes.size() - 1))) {
     197            // Closed way, check if the first node could also be simplified ...
     198            if (!isRequiredNode(w, newNodes.get(0))) {
     199                final List<Node> l1 = Arrays.asList(newNodes.get(newNodes.size() - 2), newNodes.get(0), newNodes.get(1));
     200                final List<Node> l2 = new ArrayList<>(3);
     201                buildSimplifiedNodeList(l1, 0, 2, threshold, l2);
     202                if (!l2.contains(newNodes.get(0))) {
     203                    newNodes.remove(0);
     204                    newNodes.set(newNodes.size() - 1, newNodes.get(0)); // close the way
     205                }
     206            }
    189207        }
    190208
     
    253271     * http://williams.best.vwh.net/avform.htm
    254272     */
    255     public static double dist(double lat1, double lon1, double lat2, double lon2) {
     273    private static double dist(double lat1, double lon1, double lat2, double lon2) {
    256274        return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
    257275                * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
    258276    }
    259277
    260     public static double course(double lat1, double lon1, double lat2, double lon2) {
     278    private static double course(double lat1, double lon1, double lat2, double lon2) {
    261279        return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
    262280                * Math.cos(lat2) * Math.cos(lon1 - lon2))
     
    264282    }
    265283
    266     public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
     284    private static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
    267285        double distAD = dist(lat1, lon1, lat3, lon3);
    268286        double crsAD = course(lat1, lon1, lat3, lon3);
  • trunk/test/unit/org/openstreetmap/josm/actions/SimplifyWayActionTest.java

    r10436 r11118  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertNotNull;
    56import static org.junit.Assert.assertTrue;
     7
     8import java.util.Collection;
     9import java.util.Collections;
     10import java.util.stream.Stream;
    611
    712import org.junit.BeforeClass;
     
    914import org.openstreetmap.josm.JOSMFixture;
    1015import org.openstreetmap.josm.Main;
     16import org.openstreetmap.josm.command.DeleteCommand;
     17import org.openstreetmap.josm.command.SequenceCommand;
    1118import org.openstreetmap.josm.data.coor.LatLon;
    1219import org.openstreetmap.josm.data.osm.DataSet;
     
    1421import org.openstreetmap.josm.data.osm.Way;
    1522import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     23import org.openstreetmap.josm.tools.Utils;
    1624
    1725/**
     
    97105        }
    98106    }
     107
     108    /**
     109     * Tests that also the first node may be simplified, see #13094.
     110     */
     111    @Test
     112    public void testSimplifyFirstNode() {
     113        final DataSet ds = new DataSet();
     114        final Node n1 = new Node(new LatLon(47.26269614984, 11.34044231149));
     115        final Node n2 = new Node(new LatLon(47.26274590831, 11.34053120859));
     116        final Node n3 = new Node(new LatLon(47.26276562382, 11.34034715039));
     117        final Node n4 = new Node(new LatLon(47.26264639132, 11.34035341438));
     118        final Way w = new Way();
     119        Stream.of(n1, n2, n3, n4, w).forEach(ds::addPrimitive);
     120        Stream.of(n1, n2, n3, n4, n1).forEach(w::addNode);
     121        final SequenceCommand command = action.simplifyWay(w);
     122        assertNotNull(command);
     123        assertEquals(2, command.getChildren().size());
     124        final Collection<DeleteCommand> deleteCommands = Utils.filteredCollection(command.getChildren(), DeleteCommand.class);
     125        assertEquals(1, deleteCommands.size());
     126        assertEquals(Collections.singleton(n1), deleteCommands.iterator().next().getParticipatingPrimitives());
     127    }
    99128}
Note: See TracChangeset for help on using the changeset viewer.