Changeset 19089 in josm for trunk/src


Ignore:
Timestamp:
2024-05-30T23:44:58+02:00 (4 months ago)
Author:
stoecker
Message:

see #23621 - add unit conversion for length values

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r18972 r19089  
    255255        FACTORY_MAP.put("URL_encode", Factory.of(String.class, Functions::URL_encode));
    256256        FACTORY_MAP.put("XML_encode", Factory.of(String.class, Functions::XML_encode));
     257        FACTORY_MAP.put("siunit_length", Factory.of(String.class, Functions::siunit_length));
    257258        FACTORY_MAP.put("abs", Factory.of(Math::acos));
    258259        FACTORY_MAP.put("acos", Factory.of(Math::acos));
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Functions.java

    r18876 r19089  
    11861186
    11871187    /**
     1188     * Convert a length unit to meters
     1189     * <p>
     1190     * Tries to convert a length unit to meter value or returns a - when impossible
     1191     * @param s arbitrary string representing a length
     1192     * @return the length converted to meters
     1193     * @since 19089
     1194     */
     1195    public static String siunit_length(String s) {
     1196        if (s == null)
     1197            return null;
     1198        try {
     1199            return Utils.unitToMeter(s).toString();
     1200        } catch (IllegalArgumentException e) {
     1201            Logging.debug(e);
     1202        }
     1203        return "-";
     1204    }
     1205
     1206    /**
    11881207     * Calculates the CRC32 checksum from a string (based on RFC 1952).
    11891208     * @param s the string
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r19080 r19089  
    9191    private static final Pattern REMOVE_DIACRITICS = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
    9292
     93    private static final Pattern PATTERN_LENGTH = Pattern.compile("^(\\d+(?:\\.\\d+)?)(cm|mm|m|ft|in|'|\")?$");
     94    private static final Pattern PATTERN_LENGTH2 = Pattern.compile("^(\\d+(?:\\.\\d+)?)(ft|')(\\d+(?:\\.\\d+)?)(in|\")?$");
     95
    9396    private static final String DEFAULT_STRIP = "\uFEFF\u200B";
    9497
     
    15261529     * @return byte array of data in input stream (empty if stream is null)
    15271530     * @throws IOException if any I/O error occurs
    1528      * @deprecated since xxx -- use {@link InputStream#readAllBytes()} instead
     1531     * @deprecated since 19089 -- use {@link InputStream#readAllBytes()} instead
    15291532     */
    15301533    @Deprecated
     
    20592062        return string == null ? null : string.intern();
    20602063    }
     2064
     2065    /**
     2066     * Convert a length unit to meters
     2067     * @param s arbitrary string representing a length
     2068     * @return the length converted to meters
     2069     * @since 19089
     2070     */
     2071    public static Double unitToMeter(String s) throws IllegalArgumentException {
     2072        s = s.replaceAll(" ", "").replaceAll(",", ".");
     2073        Matcher m = PATTERN_LENGTH.matcher(s);
     2074        if (m.matches()) {
     2075            Double v = Double.valueOf(m.group(1));
     2076            if ("cm".equals(m.group(2)))
     2077                v *= 0.01;
     2078            else if ("mm".equals(m.group(2)))
     2079                v *= 0.001;
     2080            else if ("ft".equals(m.group(2)) || "'".equals(m.group(2)))
     2081                v *= 0.3048;
     2082            else if ("in".equals(m.group(2)) || "\"".equals(m.group(2)))
     2083                v *= 0.0254;
     2084            return v;
     2085        } else {
     2086            m = PATTERN_LENGTH2.matcher(s);
     2087            if (m.matches()) {
     2088                return Double.valueOf(m.group(1))*0.3048+Double.valueOf(m.group(3))*0.0254;
     2089            }
     2090        }
     2091        throw new IllegalArgumentException("Invalid length value: " + s);
     2092    }
    20612093}
Note: See TracChangeset for help on using the changeset viewer.