Ticket #11389: validate-maxspeed.patch
File validate-maxspeed.patch, 7.2 KB (added by , 9 years ago) |
---|
-
src/org/openstreetmap/josm/data/validation/OsmValidator.java
diff --git a/src/org/openstreetmap/josm/data/validation/OsmValidator.java b/src/org/openstreetmap/josm/data/validation/OsmValidator.java index 1a9896c..acd2efa 100644
a b import org.openstreetmap.josm.data.validation.tests.InternetTags; 41 41 import org.openstreetmap.josm.data.validation.tests.Lanes; 42 42 import org.openstreetmap.josm.data.validation.tests.LongSegment; 43 43 import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker; 44 import org.openstreetmap.josm.data.validation.tests.Maxspeed; 44 45 import org.openstreetmap.josm.data.validation.tests.MultipolygonTest; 45 46 import org.openstreetmap.josm.data.validation.tests.NameMismatch; 46 47 import org.openstreetmap.josm.data.validation.tests.OpeningHourTest; … … public class OsmValidator implements LayerChangeListener { 126 127 InternetTags.class, // 3300 .. 3399 127 128 ApiCapabilitiesTest.class, // 3400 .. 3499 128 129 LongSegment.class, // 3500 .. 3599 130 Maxspeed.class, // 3600 .. 3699 129 131 }; 130 132 131 133 private static Map<String, Test> allTestsMap; -
new file src/org/openstreetmap/josm/data/validation/tests/Maxspeed.java
diff --git a/src/org/openstreetmap/josm/data/validation/tests/Maxspeed.java b/src/org/openstreetmap/josm/data/validation/tests/Maxspeed.java new file mode 100644 index 0000000..4c49f10
- + 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.ArrayList; 7 import java.util.Arrays; 8 import java.util.Collection; 9 import java.util.regex.Pattern; 10 11 import org.openstreetmap.josm.Main; 12 import org.openstreetmap.josm.data.osm.OsmPrimitive; 13 import org.openstreetmap.josm.data.validation.Severity; 14 import org.openstreetmap.josm.data.validation.Test.TagTest; 15 import org.openstreetmap.josm.data.validation.TestError; 16 import org.openstreetmap.josm.tools.Predicates; 17 import org.openstreetmap.josm.tools.Utils; 18 19 /** 20 * Test that validates {@code maxspeed:} tags. 21 * 22 */ 23 public class Maxspeed extends TagTest { 24 protected static Pattern maxspeedPattern = Pattern.compile("^((\\+?\\d*\\.?\\d*)(\\s(mph|knots|kmh))?|signals|none|walk)$"); 25 26 protected static Pattern sourceOrZoneMaxspeedPattern = Pattern.compile("^([A-Z]{2}):([0-9a-zA-Z_-]+)(:[0-9a-zA-Z_-]+)+$"); 27 28 private static final String[] BLACKLIST = { 29 }; 30 31 /** 32 * Constructs a new {@code Maxspeed} test. 33 */ 34 public Maxspeed() { 35 super(tr("Maxspeed"), 36 tr("This tests for maxspeeds, which are usually errors.")); 37 } 38 39 protected void checkMaxspeedByKey(final OsmPrimitive p, String maxspeedKey, Pattern pattern, int errorCode, String errorMessage) { 40 final Collection<String> keysForPattern = new ArrayList<>(Utils.filter(p.keySet(), 41 Predicates.stringContainsPattern(Pattern.compile(maxspeedKey)))); 42 keysForPattern.removeAll(Arrays.asList(BLACKLIST)); 43 if (keysForPattern.isEmpty()) { 44 // nothing to check 45 return; 46 } 47 String value = p.get(keysForPattern.iterator().next()); 48 if( !pattern.matcher(value).find()) { 49 errors.add(new TestError(this, Severity.WARNING, tr(errorMessage, maxspeedKey), errorCode, p)); 50 } 51 } 52 53 protected void checkMaxspeed(final OsmPrimitive p) { 54 final Float backward = Float.parseFloat(Utils.firstNonNull(p.get("maxspeed:backward"), 0.0).toString()); 55 final Float forward = Float.parseFloat(Utils.firstNonNull(p.get("maxspeed:forward"), 0.0).toString()); 56 try { 57 if (Float.compare(backward, forward) == 0 && backward != 0) { 58 System.out.println("backward:" + backward); 59 System.out.println("forward:" + forward); 60 errors.add(new TestError(this, Severity.WARNING, 61 tr("Value of {0} and {1} are equals. You should merge them in one tag : {2}", "maxspeed:backward", "maxspeed:forward", "maxspeed"), 3610, p)); 62 } 63 } catch (NumberFormatException ignore) { 64 Main.debug(ignore.getMessage()); 65 } 66 } 67 68 @Override 69 public void check(OsmPrimitive p) { 70 checkMaxspeedByKey(p, "maxspeed", maxspeedPattern, 3601, "Value of {0} is not correct"); 71 checkMaxspeedByKey(p, "maxspeed:backward", maxspeedPattern, 3601, "Value of {0} is not correct"); 72 checkMaxspeedByKey(p, "maxspeed:forward", maxspeedPattern, 3601, "Value of {0} is not correct"); 73 checkMaxspeedByKey(p, "source:maxspeed", sourceOrZoneMaxspeedPattern, 3602, "Value of {0} is not correct"); 74 checkMaxspeedByKey(p, "zone:maxspeed", sourceOrZoneMaxspeedPattern, 3603, "Value of {0} is not correct"); 75 checkMaxspeed(p); 76 } 77 78 } -
new file test/unit/org/openstreetmap/josm/data/validation/tests/MaxspeedTest.groovy
diff --git a/test/unit/org/openstreetmap/josm/data/validation/tests/MaxspeedTest.groovy b/test/unit/org/openstreetmap/josm/data/validation/tests/MaxspeedTest.groovy new file mode 100644 index 0000000..1ab3b72
- + 1 package org.openstreetmap.josm.data.validation.tests 2 3 import org.openstreetmap.josm.JOSMFixture 4 import org.openstreetmap.josm.data.osm.OsmUtils 5 6 class MaxspeedTest extends GroovyTestCase { 7 8 Maxspeed lanes = new Maxspeed() 9 10 @Override 11 void setUp() { 12 JOSMFixture.createUnitTestFixture().init() 13 lanes.initialize() 14 lanes.startTest(null) 15 } 16 17 void test1() { 18 lanes.check(OsmUtils.createPrimitive("way maxspeed=30z")) 19 assert lanes.errors.get(0).getMessage() == "Value of maxspeed is not correct" 20 } 21 22 void test2() { 23 lanes.check(OsmUtils.createPrimitive("way maxspeed=40knot")) 24 assert lanes.errors.get(0).getMessage() == "Value of maxspeed is not correct" 25 } 26 27 void test3() { 28 lanes.check(OsmUtils.createPrimitive("way maxspeed=50mph")) 29 assert lanes.errors.get(0).getMessage() == "Value of maxspeed is not correct" 30 } 31 32 void test4() { 33 lanes.check(OsmUtils.createPrimitive("way maxspeed=50")) 34 assert lanes.errors.size() == 0 35 } 36 37 void test5() { 38 lanes.check(OsmUtils.createPrimitive("way maxspeed=50 mph")) 39 assert lanes.errors.size() == 0 40 } 41 42 void test6() { 43 lanes.check(OsmUtils.createPrimitive("way maxspeed:forward=50 maxspeed:backward=50")) 44 assert lanes.errors.get(0).getMessage() == "Value of maxspeed:backward and maxspeed:forward are equals. You should merge them in one tag : maxspeed" 45 } 46 47 void test7() { 48 lanes.check(OsmUtils.createPrimitive("way maxspeed=50 source:maxspeed=50")) 49 assert lanes.errors.get(0).getMessage() == "Value of source:maxspeed is not correct" 50 } 51 52 void test8() { 53 lanes.check(OsmUtils.createPrimitive("way maxspeed=50 source:maxspeed=DE:")) 54 assert lanes.errors.get(0).getMessage() == "Value of source:maxspeed is not correct" 55 } 56 57 void test9() { 58 lanes.check(OsmUtils.createPrimitive("way maxspeed=50 source:maxspeed=DE:fr:")) 59 assert lanes.errors.get(0).getMessage() == "Value of source:maxspeed is not correct" 60 } 61 }