| 1 | // License: GPL. See LICENSE file for details.
|
|---|
| 2 | package org.openstreetmap.josm.data.validation.tests;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.text.MessageFormat;
|
|---|
| 8 | import java.util.Arrays;
|
|---|
| 9 | import java.util.Collections;
|
|---|
| 10 | import java.util.HashSet;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 | import java.util.Set;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 18 | import org.openstreetmap.josm.data.validation.Severity;
|
|---|
| 19 | import org.openstreetmap.josm.data.validation.Test;
|
|---|
| 20 | import org.openstreetmap.josm.data.validation.TestError;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Check area type ways for errors
|
|---|
| 24 | *
|
|---|
| 25 | * @author stoecker
|
|---|
| 26 | * @since 3669
|
|---|
| 27 | */
|
|---|
| 28 | public class UnclosedWays extends Test {
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Constructs a new {@code UnclosedWays} test.
|
|---|
| 32 | */
|
|---|
| 33 | public UnclosedWays() {
|
|---|
| 34 | super(tr("Unclosed Ways"), tr("This tests if ways which should be circular are closed."));
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * A check performed by UnclosedWays test.
|
|---|
| 39 | * @since 6390
|
|---|
| 40 | */
|
|---|
| 41 | private class UnclosedWaysCheck {
|
|---|
| 42 | /** The unique numeric code for this check */
|
|---|
| 43 | public final int code;
|
|---|
| 44 | /** The OSM key checked */
|
|---|
| 45 | public final String key;
|
|---|
| 46 | /** The English message */
|
|---|
| 47 | private final String engMessage;
|
|---|
| 48 | /** The special values, to be ignored if ignore is set to true; to be considered only if ignore is set to false */
|
|---|
| 49 | private final List<String> specialValues;
|
|---|
| 50 | /** The boolean indicating if special values must be ignored or considered only */
|
|---|
| 51 | private final boolean ignore;
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Constructs a new {@code UnclosedWaysCheck}.
|
|---|
| 55 | * @param code The unique numeric code for this check
|
|---|
| 56 | * @param key The OSM key checked
|
|---|
| 57 | * @param engMessage The English message
|
|---|
| 58 | */
|
|---|
| 59 | @SuppressWarnings("unchecked")
|
|---|
| 60 | public UnclosedWaysCheck(int code, String key, String engMessage) {
|
|---|
| 61 | this(code, key, engMessage, Collections.EMPTY_LIST);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Constructs a new {@code UnclosedWaysCheck}.
|
|---|
| 66 | * @param code The unique numeric code for this check
|
|---|
| 67 | * @param key The OSM key checked
|
|---|
| 68 | * @param engMessage The English message
|
|---|
| 69 | * @param ignoredValues The ignored values.
|
|---|
| 70 | */
|
|---|
| 71 | public UnclosedWaysCheck(int code, String key, String engMessage, List<String> ignoredValues) {
|
|---|
| 72 | this(code, key, engMessage, ignoredValues, true);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Constructs a new {@code UnclosedWaysCheck}.
|
|---|
| 77 | * @param code The unique numeric code for this check
|
|---|
| 78 | * @param key The OSM key checked
|
|---|
| 79 | * @param engMessage The English message
|
|---|
| 80 | * @param specialValues The special values, to be ignored if ignore is set to true; to be considered only if ignore is set to false
|
|---|
| 81 | * @param ignore indicates if special values must be ignored or considered only
|
|---|
| 82 | */
|
|---|
| 83 | public UnclosedWaysCheck(int code, String key, String engMessage, List<String> specialValues, boolean ignore) {
|
|---|
| 84 | this.code = code;
|
|---|
| 85 | this.key = key;
|
|---|
| 86 | this.engMessage = engMessage;
|
|---|
| 87 | this.specialValues = specialValues;
|
|---|
| 88 | this.ignore = ignore;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | /**
|
|---|
| 92 | * Returns the test error of the given way, if any.
|
|---|
| 93 | * @param w The way to check
|
|---|
| 94 | * @return The test error if the way is erroneous, {@code null} otherwise
|
|---|
| 95 | */
|
|---|
| 96 | public final TestError getTestError(Way w) {
|
|---|
| 97 | String value = w.get(key);
|
|---|
| 98 | if (isValueErroneous(value)) {
|
|---|
| 99 | String type = engMessage.contains("{0}") ? tr(engMessage, tr(value)) : tr(engMessage);
|
|---|
| 100 | String etype = engMessage.contains("{0}") ? MessageFormat.format(engMessage, value) : engMessage;
|
|---|
| 101 | return new TestError(UnclosedWays.this, Severity.WARNING, tr("Unclosed way"),
|
|---|
| 102 | type, etype, code, Arrays.asList(w),
|
|---|
| 103 | // The important parts of an unclosed way are the first and
|
|---|
| 104 | // the last node which should be connected, therefore we highlight them
|
|---|
| 105 | Arrays.asList(w.firstNode(), w.lastNode()));
|
|---|
| 106 | }
|
|---|
| 107 | return null;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | protected boolean isValueErroneous(String value) {
|
|---|
| 111 | return value != null && ignore != specialValues.contains(value);
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * A check performed by UnclosedWays test where the key is treated as boolean.
|
|---|
| 117 | * @since 6390
|
|---|
| 118 | */
|
|---|
| 119 | private final class UnclosedWaysBooleanCheck extends UnclosedWaysCheck {
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * Constructs a new {@code UnclosedWaysBooleanCheck}.
|
|---|
| 123 | * @param code The unique numeric code for this check
|
|---|
| 124 | * @param key The OSM key checked
|
|---|
| 125 | * @param engMessage The English message
|
|---|
| 126 | */
|
|---|
| 127 | public UnclosedWaysBooleanCheck(int code, String key, String engMessage) {
|
|---|
| 128 | super(code, key, engMessage);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | @Override
|
|---|
| 132 | protected boolean isValueErroneous(String value) {
|
|---|
| 133 | Boolean btest = OsmUtils.getOsmBoolean(value);
|
|---|
| 134 | // Not a strict boolean comparison to handle building=house like a building=yes
|
|---|
| 135 | return (btest != null && btest) || (btest == null && value != null);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | private final UnclosedWaysCheck[] checks = {
|
|---|
| 140 | new UnclosedWaysCheck(1101, "natural", marktr("natural type {0}"), Arrays.asList("coastline", "cliff", "tree_row", "ridge", "arete")),
|
|---|
| 141 | new UnclosedWaysCheck(1102, "landuse", marktr("landuse type {0}")),
|
|---|
| 142 | new UnclosedWaysCheck(1103, "amenities", marktr("amenities type {0}")),
|
|---|
| 143 | new UnclosedWaysCheck(1104, "sport", marktr("sport type {0}"), Arrays.asList("water_slide", "climbing")),
|
|---|
| 144 | new UnclosedWaysCheck(1105, "tourism", marktr("tourism type {0}"), Arrays.asList("attraction")),
|
|---|
| 145 | new UnclosedWaysCheck(1106, "shop", marktr("shop type {0}")),
|
|---|
| 146 | new UnclosedWaysCheck(1107, "leisure", marktr("leisure type {0}"), Arrays.asList("track")),
|
|---|
| 147 | new UnclosedWaysCheck(1108, "waterway", marktr("waterway type {0}"), Arrays.asList("riverbank"), false),
|
|---|
| 148 | new UnclosedWaysBooleanCheck(1120, "building", marktr("building")),
|
|---|
| 149 | new UnclosedWaysBooleanCheck(1130, "area", marktr("area")),
|
|---|
| 150 | };
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * Returns the set of checked OSM keys.
|
|---|
| 154 | * @return The set of checked OSM keys.
|
|---|
| 155 | * @since 6390
|
|---|
| 156 | */
|
|---|
| 157 | public Set<String> getCheckedKeys() {
|
|---|
| 158 | Set<String> keys = new HashSet<String>();
|
|---|
| 159 | for (UnclosedWaysCheck c : checks) {
|
|---|
| 160 | keys.add(c.key);
|
|---|
| 161 | }
|
|---|
| 162 | return keys;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | @Override
|
|---|
| 166 | public void visit(Way w) {
|
|---|
| 167 |
|
|---|
| 168 | if (!w.isUsable() || w.isArea())
|
|---|
| 169 | return;
|
|---|
| 170 |
|
|---|
| 171 | for (OsmPrimitive parent: w.getReferrers()) {
|
|---|
| 172 | if (parent instanceof Relation && ((Relation)parent).isMultipolygon())
|
|---|
| 173 | return;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | for (UnclosedWaysCheck c : checks) {
|
|---|
| 177 | TestError error = c.getTestError(w);
|
|---|
| 178 | if (error != null) {
|
|---|
| 179 | errors.add(error);
|
|---|
| 180 | return;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|