| 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.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.util.ArrayList;
|
|---|
| 7 | import java.util.Arrays;
|
|---|
| 8 | import java.util.Collections;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
|---|
| 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 |
|
|---|
| 21 | public class TurnrestrictionTest extends Test {
|
|---|
| 22 |
|
|---|
| 23 | protected static final int NO_VIA = 1801;
|
|---|
| 24 | protected static final int NO_FROM = 1802;
|
|---|
| 25 | protected static final int NO_TO = 1803;
|
|---|
| 26 | protected static final int MORE_VIA = 1804;
|
|---|
| 27 | protected static final int MORE_FROM = 1805;
|
|---|
| 28 | protected static final int MORE_TO = 1806;
|
|---|
| 29 | protected static final int UNKNOWN_ROLE = 1807;
|
|---|
| 30 | protected static final int UNKNOWN_TYPE = 1808;
|
|---|
| 31 | protected static final int FROM_VIA_NODE = 1809;
|
|---|
| 32 | protected static final int TO_VIA_NODE = 1810;
|
|---|
| 33 | protected static final int FROM_VIA_WAY = 1811;
|
|---|
| 34 | protected static final int TO_VIA_WAY = 1812;
|
|---|
| 35 | protected static final int MIX_VIA = 1813;
|
|---|
| 36 | protected static final int UNCONNECTED_VIA = 1814;
|
|---|
| 37 |
|
|---|
| 38 | public TurnrestrictionTest() {
|
|---|
| 39 | super(tr("Turnrestrictions"), tr("This test checks if turnrestrictions are valid"));
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | @Override
|
|---|
| 43 | public void visit(Relation r) {
|
|---|
| 44 | if (!"restriction".equals(r.get("type")))
|
|---|
| 45 | return;
|
|---|
| 46 |
|
|---|
| 47 | Way fromWay = null;
|
|---|
| 48 | Way toWay = null;
|
|---|
| 49 | List<OsmPrimitive> via = new ArrayList<OsmPrimitive>();
|
|---|
| 50 |
|
|---|
| 51 | boolean morefrom = false;
|
|---|
| 52 | boolean moreto = false;
|
|---|
| 53 | boolean morevia = false;
|
|---|
| 54 | boolean mixvia = false;
|
|---|
| 55 |
|
|---|
| 56 | /* find the "from", "via" and "to" elements */
|
|---|
| 57 | for (RelationMember m : r.getMembers()) {
|
|---|
| 58 | if (m.getMember().isIncomplete())
|
|---|
| 59 | return;
|
|---|
| 60 |
|
|---|
| 61 | ArrayList<OsmPrimitive> l = new ArrayList<OsmPrimitive>();
|
|---|
| 62 | l.add(r);
|
|---|
| 63 | l.add(m.getMember());
|
|---|
| 64 | if (m.isWay()) {
|
|---|
| 65 | Way w = m.getWay();
|
|---|
| 66 | if (w.getNodesCount() < 2) {
|
|---|
| 67 | continue;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if ("from".equals(m.getRole())) {
|
|---|
| 71 | if (fromWay != null) {
|
|---|
| 72 | morefrom = true;
|
|---|
| 73 | } else {
|
|---|
| 74 | fromWay = w;
|
|---|
| 75 | }
|
|---|
| 76 | } else if ("to".equals(m.getRole())) {
|
|---|
| 77 | if (toWay != null) {
|
|---|
| 78 | moreto = true;
|
|---|
| 79 | } else {
|
|---|
| 80 | toWay = w;
|
|---|
| 81 | }
|
|---|
| 82 | } else if ("via".equals(m.getRole())) {
|
|---|
| 83 | if (!via.isEmpty() && via.get(0) instanceof Node) {
|
|---|
| 84 | mixvia = true;
|
|---|
| 85 | } else {
|
|---|
| 86 | via.add(w);
|
|---|
| 87 | }
|
|---|
| 88 | } else {
|
|---|
| 89 | errors.add(new TestError(this, Severity.WARNING, tr("Unknown role"), UNKNOWN_ROLE,
|
|---|
| 90 | l, Collections.singletonList(m)));
|
|---|
| 91 | }
|
|---|
| 92 | } else if (m.isNode()) {
|
|---|
| 93 | Node n = m.getNode();
|
|---|
| 94 | if ("via".equals(m.getRole())) {
|
|---|
| 95 | if (!via.isEmpty()) {
|
|---|
| 96 | if (via.get(0) instanceof Node) {
|
|---|
| 97 | morevia = true;
|
|---|
| 98 | } else {
|
|---|
| 99 | mixvia = true;
|
|---|
| 100 | }
|
|---|
| 101 | } else {
|
|---|
| 102 | via.add(n);
|
|---|
| 103 | }
|
|---|
| 104 | } else {
|
|---|
| 105 | errors.add(new TestError(this, Severity.WARNING, tr("Unknown role"), UNKNOWN_ROLE,
|
|---|
| 106 | l, Collections.singletonList(m)));
|
|---|
| 107 | }
|
|---|
| 108 | } else {
|
|---|
| 109 | errors.add(new TestError(this, Severity.WARNING, tr("Unknown member type"), UNKNOWN_TYPE,
|
|---|
| 110 | l, Collections.singletonList(m)));
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | if (morefrom) {
|
|---|
| 114 | errors.add(new TestError(this, Severity.ERROR, tr("More than one \"from\" way found"), MORE_FROM, r));
|
|---|
| 115 | }
|
|---|
| 116 | if (moreto) {
|
|---|
| 117 | errors.add(new TestError(this, Severity.ERROR, tr("More than one \"to\" way found"), MORE_TO, r));
|
|---|
| 118 | }
|
|---|
| 119 | if (morevia) {
|
|---|
| 120 | errors.add(new TestError(this, Severity.ERROR, tr("More than one \"via\" node found"), MORE_VIA, r));
|
|---|
| 121 | }
|
|---|
| 122 | if (mixvia) {
|
|---|
| 123 | errors.add(new TestError(this, Severity.ERROR, tr("Cannot mix node and way for role \"via\""), MIX_VIA, r));
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | if (fromWay == null) {
|
|---|
| 127 | errors.add(new TestError(this, Severity.ERROR, tr("No \"from\" way found"), NO_FROM, r));
|
|---|
| 128 | return;
|
|---|
| 129 | }
|
|---|
| 130 | if (toWay == null) {
|
|---|
| 131 | errors.add(new TestError(this, Severity.ERROR, tr("No \"to\" way found"), NO_TO, r));
|
|---|
| 132 | return;
|
|---|
| 133 | }
|
|---|
| 134 | if (via.isEmpty()) {
|
|---|
| 135 | errors.add(new TestError(this, Severity.ERROR, tr("No \"via\" node or way found"), NO_VIA, r));
|
|---|
| 136 | return;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | Node viaNode;
|
|---|
| 140 | if (via.get(0) instanceof Node) {
|
|---|
| 141 | viaNode = (Node) via.get(0);
|
|---|
| 142 | Way viaPseudoWay = new Way();
|
|---|
| 143 | viaPseudoWay.addNode(viaNode);
|
|---|
| 144 | checkIfConnected(fromWay, viaPseudoWay,
|
|---|
| 145 | tr("The \"from\" way does not start or end at a \"via\" node"), FROM_VIA_NODE);
|
|---|
| 146 | checkIfConnected(viaPseudoWay, toWay,
|
|---|
| 147 | tr("The \"to\" way does not start or end at a \"via\" node"), TO_VIA_NODE);
|
|---|
| 148 | } else {
|
|---|
| 149 | // check if consecutive ways are connected: from/via[0], via[i-1]/via[i], via[last]/to
|
|---|
| 150 | checkIfConnected(fromWay, (Way) via.get(0),
|
|---|
| 151 | tr("The \"from\" and the first \"via\" way are not connected."), FROM_VIA_WAY);
|
|---|
| 152 | if (via.size() > 1) {
|
|---|
| 153 | for (int i = 1; i < via.size(); i++) {
|
|---|
| 154 | Way previous = (Way) via.get(i - 1);
|
|---|
| 155 | Way current = (Way) via.get(i);
|
|---|
| 156 | checkIfConnected(previous, current,
|
|---|
| 157 | tr("The \"via\" ways are not connected."), UNCONNECTED_VIA);
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | checkIfConnected((Way) via.get(via.size() - 1), toWay,
|
|---|
| 161 | tr("The last \"via\" and the \"to\" way are not connected."), TO_VIA_WAY);
|
|---|
| 162 |
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | private void checkIfConnected(Way previous, Way current, String msg, int code) {
|
|---|
| 167 | int onewayPrevious = isOneway(previous);
|
|---|
| 168 | int onewayCurrent = isOneway(current);
|
|---|
| 169 | Node endPrevious = onewayPrevious != -1 ? previous.lastNode() : previous.firstNode();
|
|---|
| 170 | Node startCurrent = onewayCurrent != -1 ? current.firstNode() : current.lastNode();
|
|---|
| 171 | //System.out.println(previous.getUniqueId() + " -- " + current.getUniqueId() + ": " + onewayPrevious + "/" + onewayCurrent + " " + endPrevious.getUniqueId() + "/" + startCurrent.getUniqueId());
|
|---|
| 172 | boolean c;
|
|---|
| 173 | if (onewayPrevious != 0 && onewayCurrent != 0) {
|
|---|
| 174 | // both oneways: end/start node must be equal
|
|---|
| 175 | c = endPrevious.equals(startCurrent);
|
|---|
| 176 | } else if (onewayPrevious != 0) {
|
|---|
| 177 | // previous way is oneway: end of previous must be start/end of current
|
|---|
| 178 | c = current.isFirstLastNode(endPrevious);
|
|---|
| 179 | } else if (onewayCurrent != 0) {
|
|---|
| 180 | // current way is oneway: start of current must be start/end of previous
|
|---|
| 181 | c = previous.isFirstLastNode(startCurrent);
|
|---|
| 182 | } else {
|
|---|
| 183 | // otherwise: start/end of previous must be start/end of current
|
|---|
| 184 | c = current.isFirstLastNode(previous.firstNode()) || current.isFirstLastNode(previous.lastNode());
|
|---|
| 185 | }
|
|---|
| 186 | if (!c) {
|
|---|
| 187 | errors.add(new TestError(this, Severity.ERROR, msg, code, Arrays.asList(previous, current)));
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | private static int isOneway(Way w) {
|
|---|
| 192 | String onewayviastr = w.get("oneway");
|
|---|
| 193 | if (onewayviastr != null) {
|
|---|
| 194 | if ("-1".equals(onewayviastr)) {
|
|---|
| 195 | return -1;
|
|---|
| 196 | } else {
|
|---|
| 197 | Boolean onewayvia = OsmUtils.getOsmBoolean(onewayviastr);
|
|---|
| 198 | if (onewayvia != null && onewayvia) {
|
|---|
| 199 | return 1;
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | return 0;
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|