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 org.openstreetmap.josm.data.osm.Way;
007import org.openstreetmap.josm.data.validation.Severity;
008import org.openstreetmap.josm.data.validation.Test;
009import org.openstreetmap.josm.data.validation.TestError;
010import org.openstreetmap.josm.tools.Geometry;
011
012/**
013 * Check cyclic ways for errors
014 *
015 * @author jrreid
016 */
017public class WronglyOrderedWays extends Test {
018
019    // CHECKSTYLE.OFF: SingleSpaceSeparator
020    protected static final int WRONGLY_ORDERED_COAST = 1001;
021    protected static final int WRONGLY_ORDERED_LAND  = 1003;
022    // CHECKSTYLE.ON: SingleSpaceSeparator
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        }
046    }
047
048    private void reportError(Way w, String msg, int type) {
049        errors.add(TestError.builder(this, Severity.WARNING, type)
050                .message(msg)
051                .primitives(w)
052                .build());
053    }
054}