| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.validation.tests;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.util.HashSet;
|
|---|
| 7 | import java.util.Map;
|
|---|
| 8 | import java.util.Set;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.Main;
|
|---|
| 11 | import org.openstreetmap.josm.command.Command;
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.RelationMember;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 17 | import org.openstreetmap.josm.data.validation.Severity;
|
|---|
| 18 | import org.openstreetmap.josm.data.validation.Test;
|
|---|
| 19 | import org.openstreetmap.josm.data.validation.TestError;
|
|---|
| 20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Checks for untagged ways
|
|---|
| 24 | *
|
|---|
| 25 | * @author frsantos
|
|---|
| 26 | */
|
|---|
| 27 | public class UntaggedWay extends Test {
|
|---|
| 28 |
|
|---|
| 29 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
|---|
| 30 | /** Empty way error */
|
|---|
| 31 | protected static final int EMPTY_WAY = 301;
|
|---|
| 32 | /** Untagged way error */
|
|---|
| 33 | protected static final int UNTAGGED_WAY = 302;
|
|---|
| 34 | /** Unnamed way error */
|
|---|
| 35 | protected static final int UNNAMED_WAY = 303;
|
|---|
| 36 | /** One node way error */
|
|---|
| 37 | protected static final int ONE_NODE_WAY = 304;
|
|---|
| 38 | /** Unnamed junction error */
|
|---|
| 39 | protected static final int UNNAMED_JUNCTION = 305;
|
|---|
| 40 | /** Untagged, but commented way error */
|
|---|
| 41 | protected static final int COMMENTED_WAY = 306;
|
|---|
| 42 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
|---|
| 43 |
|
|---|
| 44 | private Set<Way> waysUsedInRelations;
|
|---|
| 45 |
|
|---|
| 46 | /** Ways that must have a name */
|
|---|
| 47 | static final Set<String> NAMED_WAYS = new HashSet<>();
|
|---|
| 48 | static {
|
|---|
| 49 | NAMED_WAYS.add("motorway");
|
|---|
| 50 | NAMED_WAYS.add("trunk");
|
|---|
| 51 | NAMED_WAYS.add("primary");
|
|---|
| 52 | NAMED_WAYS.add("secondary");
|
|---|
| 53 | NAMED_WAYS.add("tertiary");
|
|---|
| 54 | NAMED_WAYS.add("residential");
|
|---|
| 55 | NAMED_WAYS.add("pedestrian");
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | /** Whitelist of roles allowed to reference an untagged way */
|
|---|
| 59 | static final Set<String> WHITELIST = new HashSet<>();
|
|---|
| 60 | static {
|
|---|
| 61 | WHITELIST.add("outer");
|
|---|
| 62 | WHITELIST.add("inner");
|
|---|
| 63 | WHITELIST.add("perimeter");
|
|---|
| 64 | WHITELIST.add("edge");
|
|---|
| 65 | WHITELIST.add("outline");
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Constructor
|
|---|
| 70 | */
|
|---|
| 71 | public UntaggedWay() {
|
|---|
| 72 | super(tr("Untagged, empty and one node ways"),
|
|---|
| 73 | tr("This test checks for untagged, empty and one node ways."));
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | @Override
|
|---|
| 77 | public void visit(Way w) {
|
|---|
| 78 | if (!w.isUsable())
|
|---|
| 79 | return;
|
|---|
| 80 |
|
|---|
| 81 | Map<String, String> tags = w.getKeys();
|
|---|
| 82 | if (!tags.isEmpty()) {
|
|---|
| 83 | String highway = tags.get("highway");
|
|---|
| 84 | if (highway != null && NAMED_WAYS.contains(highway) && !tags.containsKey("name") && !tags.containsKey("ref")
|
|---|
| 85 | && !"yes".equals(tags.get("noname"))) {
|
|---|
| 86 | boolean isJunction = false;
|
|---|
| 87 | boolean hasName = false;
|
|---|
| 88 | for (String key : tags.keySet()) {
|
|---|
| 89 | hasName = key.startsWith("name:") || key.endsWith("_name") || key.endsWith("_ref");
|
|---|
| 90 | if (hasName) {
|
|---|
| 91 | break;
|
|---|
| 92 | }
|
|---|
| 93 | if ("junction".equals(key)) {
|
|---|
| 94 | isJunction = true;
|
|---|
| 95 | break;
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | if (!hasName && !isJunction) {
|
|---|
| 100 | errors.add(TestError.builder(this, Severity.WARNING, UNNAMED_WAY)
|
|---|
| 101 | .message(tr("Unnamed ways"))
|
|---|
| 102 | .primitives(w)
|
|---|
| 103 | .build());
|
|---|
| 104 | } else if (isJunction) {
|
|---|
| 105 | errors.add(TestError.builder(this, Severity.OTHER, UNNAMED_JUNCTION)
|
|---|
| 106 | .message(tr("Unnamed junction"))
|
|---|
| 107 | .primitives(w)
|
|---|
| 108 | .build());
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (!w.isTagged() && !waysUsedInRelations.contains(w)) {
|
|---|
| 114 | if (w.hasKeys()) {
|
|---|
| 115 | errors.add(TestError.builder(this, Severity.WARNING, COMMENTED_WAY)
|
|---|
| 116 | .message(tr("Untagged ways (commented)"))
|
|---|
| 117 | .primitives(w)
|
|---|
| 118 | .build());
|
|---|
| 119 | } else {
|
|---|
| 120 | errors.add(TestError.builder(this, Severity.WARNING, UNTAGGED_WAY)
|
|---|
| 121 | .message(tr("Untagged ways"))
|
|---|
| 122 | .primitives(w)
|
|---|
| 123 | .build());
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | if (w.getNodesCount() == 0) {
|
|---|
| 128 | errors.add(TestError.builder(this, Severity.ERROR, EMPTY_WAY)
|
|---|
| 129 | .message(tr("Empty ways"))
|
|---|
| 130 | .primitives(w)
|
|---|
| 131 | .build());
|
|---|
| 132 | } else if (w.getNodesCount() == 1) {
|
|---|
| 133 | errors.add(TestError.builder(this, Severity.ERROR, ONE_NODE_WAY)
|
|---|
| 134 | .message(tr("One node ways"))
|
|---|
| 135 | .primitives(w)
|
|---|
| 136 | .build());
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | @Override
|
|---|
| 141 | public void startTest(ProgressMonitor monitor) {
|
|---|
| 142 | super.startTest(monitor);
|
|---|
| 143 | DataSet ds = Main.getLayerManager().getEditDataSet();
|
|---|
| 144 | if (ds == null)
|
|---|
| 145 | return;
|
|---|
| 146 | waysUsedInRelations = new HashSet<>();
|
|---|
| 147 | for (Relation r : ds.getRelations()) {
|
|---|
| 148 | if (r.isUsable()) {
|
|---|
| 149 | for (RelationMember m : r.getMembers()) {
|
|---|
| 150 | if (r.isMultipolygon() || WHITELIST.contains(m.getRole())) {
|
|---|
| 151 | OsmPrimitive member = m.getMember();
|
|---|
| 152 | if (member instanceof Way && member.isUsable() && !member.isTagged()) {
|
|---|
| 153 | waysUsedInRelations.add((Way) member);
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | @Override
|
|---|
| 162 | public void endTest() {
|
|---|
| 163 | waysUsedInRelations = null;
|
|---|
| 164 | super.endTest();
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | @Override
|
|---|
| 168 | public boolean isFixable(TestError testError) {
|
|---|
| 169 | if (testError.getTester() instanceof UntaggedWay)
|
|---|
| 170 | return testError.getCode() == EMPTY_WAY
|
|---|
| 171 | || testError.getCode() == ONE_NODE_WAY;
|
|---|
| 172 |
|
|---|
| 173 | return false;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | @Override
|
|---|
| 177 | public Command fixError(TestError testError) {
|
|---|
| 178 | return deletePrimitivesIfNeeded(testError.getPrimitives());
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | @Override
|
|---|
| 182 | public boolean isPrimitiveUsable(OsmPrimitive p) {
|
|---|
| 183 | return p.isUsable();
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|