Changeset 19092 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java
r19089 r19092 1188 1188 * Convert a length unit to meters 1189 1189 * <p> 1190 * Tries to convert a length unit to meter value or returns a -when impossible1190 * Tries to convert a length unit to meter value or returns {@code null} when impossible 1191 1191 * @param s arbitrary string representing a length 1192 1192 * @return the length converted to meters … … 1201 1201 Logging.debug(e); 1202 1202 } 1203 return "-";1203 return null; 1204 1204 } 1205 1205 -
trunk/src/org/openstreetmap/josm/tools/Utils.java
r19090 r19092 91 91 private static final Pattern REMOVE_DIACRITICS = Pattern.compile("\\p{InCombiningDiacriticalMarks}+"); 92 92 93 private static final Pattern PATTERN_LENGTH = Pattern.compile("^(\\d+(?:\\.\\d+)?)(cm|m m|m|ft|in|'|\")?$");94 private static final Pattern PATTERN_LENGTH2 = Pattern.compile("^(\\d+(?:\\.\\d+)?)(ft|')(\\d+(?:\\.\\d+)?)(in|\")?$"); 93 private static final Pattern PATTERN_LENGTH = Pattern.compile("^(-?\\d+(?:\\.\\d+)?)(cm|mi|mm|m|ft|km|nmi|in|'|\")?$"); 94 private static final Pattern PATTERN_LENGTH2 = Pattern.compile("^(-?)(\\d+(?:\\.\\d+)?)(ft|')(\\d+(?:\\.\\d+)?)(in|\")?$"); 95 95 96 96 private static final String DEFAULT_STRIP = "\uFEFF\u200B"; … … 2078 2078 else if ("mm".equals(m.group(2))) 2079 2079 v *= 0.001; 2080 else if ("km".equals(m.group(2))) 2081 v *= 1000.0; 2082 else if ("nmi".equals(m.group(2))) 2083 v *= 1852.0; 2084 else if ("mi".equals(m.group(2))) 2085 v *= 1609.344; 2080 2086 else if ("ft".equals(m.group(2)) || "'".equals(m.group(2))) 2081 2087 v *= 0.3048; … … 2086 2092 m = PATTERN_LENGTH2.matcher(s); 2087 2093 if (m.matches()) { 2088 return Double.valueOf(m.group(1))*0.3048+Double.valueOf(m.group(3))*0.0254; 2094 /* NOTE: we assume -a'b" means -(a'+b") and not (-a')+b" - because of such issues SI units have been invented 2095 and have been adapted by the majority of the world */ 2096 return (Double.valueOf(m.group(2))*0.3048+Double.valueOf(m.group(4))*0.0254)*(m.group(1).isEmpty()?1.0:-1.0); 2089 2097 } 2090 2098 } -
trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java
r19089 r19092 563 563 assertEquals(1.5, Utils.unitToMeter("150cm")); 564 564 assertEquals(1.6, Utils.unitToMeter("1600.0mm")); 565 assertEquals(1700, Utils.unitToMeter("1.7km")); 566 assertEquals(-1800, Utils.unitToMeter("-1.8km")); 565 567 assertEquals(3.048, Utils.unitToMeter("10ft")); 566 568 assertEquals(6.096, Utils.unitToMeter("20'")); 567 569 assertEquals(2.54, Utils.unitToMeter("100in")); 568 570 assertEquals(5.08, Utils.unitToMeter("200\"")); 571 assertEquals(1852, Utils.unitToMeter("1nmi")); 572 assertEquals(1609.344, Utils.unitToMeter("1mi")); 569 573 assertEquals(3.0734, Utils.unitToMeter("10ft1in")); 570 574 assertEquals(6.1468, Utils.unitToMeter("20'2\"")); 575 assertEquals(-6.1468, Utils.unitToMeter("-20'2\"")); 571 576 assertThrows(IllegalArgumentException.class, () -> Utils.unitToMeter("Hallo")); 572 577 }
Note:
See TracChangeset
for help on using the changeset viewer.