001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.validation.tests;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.Collections;
007
008import org.openstreetmap.josm.data.osm.Way;
009import org.openstreetmap.josm.data.validation.Severity;
010import org.openstreetmap.josm.data.validation.Test;
011import org.openstreetmap.josm.data.validation.TestError;
012import org.openstreetmap.josm.tools.Geometry;
013
014/**
015 * Check cyclic ways for errors
016 *
017 * @author jrreid
018 */
019public class WronglyOrderedWays extends Test {
020
021    protected static final int WRONGLY_ORDERED_COAST = 1001;
022    protected static final int WRONGLY_ORDERED_LAND  = 1003;
023
024    /**
025     * Constructor
026     */
027    public WronglyOrderedWays() {
028        super(tr("Wrongly Ordered Ways"),
029                tr("This test checks the direction of water, land and coastline ways."));
030    }
031
032    @Override
033    public void visit(Way w) {
034
035        if (!w.isUsable() || !w.isClosed())
036            return;
037
038        String natural = w.get("natural");
039        if (natural == null)
040            return;
041        else if ("coastline".equals(natural) && Geometry.isClockwise(w)) {
042            reportError(w, tr("Reversed coastline: land not on left side"), WRONGLY_ORDERED_COAST);
043        } else if ("land".equals(natural) && Geometry.isClockwise(w)) {
044            reportError(w, tr("Reversed land: land not on left side"), WRONGLY_ORDERED_LAND);
045        } else
046            return;
047
048    }
049
050    private void reportError(Way w, String msg, int type) {
051        errors.add(new TestError(this, Severity.WARNING, msg, type, Collections.singletonList(w)));
052    }
053}