Changeset 14484 in josm for trunk/src/org
- Timestamp:
- 2018-12-02T02:32:13+01:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/osm/OsmUtils.java
r14238 r14484 113 113 114 114 /** 115 * Creates a new OSM primitive according to the given assertion. Originally written for unit tests, 115 * Creates a new OSM primitive around (0,0) according to the given assertion. Originally written for unit tests, 116 116 * this can also be used in another places like validation of local MapCSS validator rules. 117 117 * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail") … … 121 121 */ 122 122 public static OsmPrimitive createPrimitive(String assertion) { 123 return createPrimitive(assertion, LatLon.ZERO); 124 } 125 126 /** 127 * Creates a new OSM primitive according to the given assertion. Originally written for unit tests, 128 * this can also be used in another places like validation of local MapCSS validator rules. 129 * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail") 130 * @param around the coordinate at which the primitive will be located 131 * @return a new OSM primitive according to the given assertion 132 * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it 133 * @since 14484 134 */ 135 public static OsmPrimitive createPrimitive(String assertion, LatLon around) { 123 136 CheckParameterUtil.ensureParameterNotNull(assertion, "assertion"); 124 137 final String[] x = assertion.split("\\s+", 2); 125 138 final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0]) 126 ? new Node( LatLon.ZERO)139 ? new Node(around) 127 140 : "w".equals(x[0]) || "way".equals(x[0]) || /*for MapCSS related usage*/ "area".equals(x[0]) 128 ? new Way()141 ? newWay(around) 129 142 : "r".equals(x[0]) || "relation".equals(x[0]) 130 ? new 143 ? newRelation(around) 131 144 : null; 132 145 if (p == null) { … … 139 152 } 140 153 return p; 154 } 155 156 private static Node newNode(LatLon around) { 157 return new Node(around); 158 } 159 160 private static Way newWay(LatLon around) { 161 Way w = new Way(); 162 w.addNode(newNode(new LatLon(around.lat()+0.1, around.lon()))); 163 w.addNode(newNode(new LatLon(around.lat()-0.1, around.lon()))); 164 return w; 165 } 166 167 private static Relation newRelation(LatLon around) { 168 Relation r = new Relation(); 169 r.addMember(new RelationMember(null, newNode(around))); 170 return r; 141 171 } 142 172 -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r14481 r14484 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Rectangle; 6 7 import java.io.BufferedReader; 7 8 import java.io.IOException; … … 9 10 import java.io.Reader; 10 11 import java.io.StringReader; 12 import java.lang.reflect.Method; 11 13 import java.text.MessageFormat; 12 14 import java.util.ArrayList; … … 35 37 import org.openstreetmap.josm.command.DeleteCommand; 36 38 import org.openstreetmap.josm.command.SequenceCommand; 39 import org.openstreetmap.josm.data.coor.LatLon; 37 40 import org.openstreetmap.josm.data.osm.DataSet; 38 41 import org.openstreetmap.josm.data.osm.INode; … … 53 56 import org.openstreetmap.josm.gui.mappaint.mapcss.Condition; 54 57 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.ClassCondition; 58 import org.openstreetmap.josm.gui.mappaint.mapcss.ConditionFactory.ExpressionCondition; 55 59 import org.openstreetmap.josm.gui.mappaint.mapcss.Expression; 60 import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory.Functions; 61 import org.openstreetmap.josm.gui.mappaint.mapcss.ExpressionFactory.ParameterFunction; 56 62 import org.openstreetmap.josm.gui.mappaint.mapcss.Instruction; 63 import org.openstreetmap.josm.gui.mappaint.mapcss.LiteralExpression; 57 64 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule; 58 65 import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSRule.Declaration; … … 73 80 import org.openstreetmap.josm.spi.preferences.Config; 74 81 import org.openstreetmap.josm.tools.CheckParameterUtil; 82 import org.openstreetmap.josm.tools.DefaultGeoProperty; 83 import org.openstreetmap.josm.tools.GeoProperty; 84 import org.openstreetmap.josm.tools.GeoPropertyIndex; 75 85 import org.openstreetmap.josm.tools.I18n; 76 86 import org.openstreetmap.josm.tools.JosmRuntimeException; 77 87 import org.openstreetmap.josm.tools.Logging; 78 88 import org.openstreetmap.josm.tools.MultiMap; 89 import org.openstreetmap.josm.tools.Territories; 79 90 import org.openstreetmap.josm.tools.Utils; 80 91 … … 958 969 } 959 970 971 private static Method getFunctionMethod(String method) { 972 try { 973 return Functions.class.getDeclaredMethod(method, Environment.class, String.class); 974 } catch (NoSuchMethodException | SecurityException e) { 975 Logging.error(e); 976 return null; 977 } 978 } 979 980 private static Optional<String> getFirstInsideCountry(TagCheck check, Method insideMethod) { 981 return check.rule.selectors.stream() 982 .filter(s -> s instanceof GeneralSelector) 983 .flatMap(s -> ((GeneralSelector) s).getConditions().stream()) 984 .filter(c -> c instanceof ExpressionCondition) 985 .map(c -> ((ExpressionCondition) c).getExpression()) 986 .filter(c -> c instanceof ParameterFunction) 987 .map(c -> (ParameterFunction) c) 988 .filter(c -> c.getMethod().equals(insideMethod)) 989 .flatMap(c -> c.getArgs().stream()) 990 .filter(e -> e instanceof LiteralExpression) 991 .map(e -> ((LiteralExpression) e).getLiteral()) 992 .filter(l -> l instanceof String) 993 .map(l -> (String) l) 994 .findFirst(); 995 } 996 997 private static LatLon getLocation(TagCheck check, Method insideMethod) { 998 Optional<String> inside = getFirstInsideCountry(check, insideMethod); 999 if (inside.isPresent()) { 1000 GeoPropertyIndex<Boolean> index = Territories.getGeoPropertyIndex(inside.get()); 1001 if (index != null) { 1002 GeoProperty<Boolean> prop = index.getGeoProperty(); 1003 if (prop instanceof DefaultGeoProperty) { 1004 Rectangle bounds = ((DefaultGeoProperty) prop).getArea().getBounds(); 1005 return new LatLon(bounds.getCenterY(), bounds.getCenterX()); 1006 } 1007 } 1008 } 1009 return LatLon.ZERO; 1010 } 1011 960 1012 /** 961 1013 * Checks that rule assertions are met for the given set of TagChecks. … … 966 1018 public Set<String> checkAsserts(final Collection<TagCheck> schecks) { 967 1019 Set<String> assertionErrors = new LinkedHashSet<>(); 1020 final Method insideMethod = getFunctionMethod("inside"); 968 1021 final DataSet ds = new DataSet(); 969 1022 for (final TagCheck check : schecks) { … … 971 1024 for (final Map.Entry<String, Boolean> i : check.assertions.entrySet()) { 972 1025 Logging.debug("- Assertion: {0}", i); 973 final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey()); 1026 final OsmPrimitive p = OsmUtils.createPrimitive(i.getKey(), getLocation(check, insideMethod)); 974 1027 // Build minimal ordered list of checks to run to test the assertion 975 1028 List<Set<TagCheck>> checksToRun = new ArrayList<>(); -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ConditionFactory.java
r14436 r14484 872 872 } 873 873 874 /** 875 * Returns the expression. 876 * @return the expression 877 * @since 14484 878 */ 879 public final Expression getExpression() { 880 return e; 881 } 882 874 883 @Override 875 884 public boolean applies(Environment env) { -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java
r14371 r14484 1306 1306 } 1307 1307 1308 /** 1309 * Returns the method. 1310 * @return the method 1311 * @since 14484 1312 */ 1313 public final Method getMethod() { 1314 return m; 1315 } 1316 1317 /** 1318 * Returns the arguments. 1319 * @return the arguments 1320 * @since 14484 1321 */ 1322 public final List<Expression> getArgs() { 1323 return args; 1324 } 1325 1308 1326 @Override 1309 1327 public Object evaluate(Environment env) { -
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/LiteralExpression.java
r10300 r14484 23 23 } 24 24 25 /** 26 * Returns the literal. 27 * @return the literal 28 * @since 14484 29 */ 30 public final Object getLiteral() { 31 return literal; 32 } 33 25 34 @Override 26 35 public Object evaluate(Environment env) { -
trunk/src/org/openstreetmap/josm/tools/DefaultGeoProperty.java
r11361 r14484 60 60 } 61 61 62 /** 63 * Returns the area. 64 * @return the area 65 * @since 14484 66 */ 67 public final Area getArea() { 68 return area; 69 } 62 70 } -
trunk/src/org/openstreetmap/josm/tools/GeoPropertyIndex.java
r14008 r14484 48 48 public T get(LatLon ll) { 49 49 return lastLevelUsed.get(ll); 50 } 51 52 /** 53 * Returns the geo property. 54 * @return the geo property 55 * @since 14484 56 */ 57 public final GeoProperty<T> getGeoProperty() { 58 return geoProp; 50 59 } 51 60 -
trunk/src/org/openstreetmap/josm/tools/Territories.java
r14035 r14484 51 51 52 52 /** 53 * Determine, if a point is inside a territory with the given the ISO3166-1 53 * Returns the {@link GeoPropertyIndex} for the given ISO3166-1 or ISO3166-2 code. 54 * @param code the ISO3166-1 or ISO3166-2 code 55 * @return the {@link GeoPropertyIndex} for the given {@code code} 56 * @since 14484 57 */ 58 public static GeoPropertyIndex<Boolean> getGeoPropertyIndex(String code) { 59 return iso3166Cache.get(code); 60 } 61 62 /** 63 * Determine, if a point is inside a territory with the given ISO3166-1 54 64 * or ISO3166-2 code. 55 65 *
Note:
See TracChangeset
for help on using the changeset viewer.