Changeset 19092 in josm


Ignore:
Timestamp:
2024-05-31T21:52:43+02:00 (8 months ago)
Author:
stoecker
Message:

fix #23702, see #23621 - improve length unit conversion

Location:
trunk
Files:
3 edited

Legend:

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

    r19089 r19092  
    11881188     * Convert a length unit to meters
    11891189     * <p>
    1190      * Tries to convert a length unit to meter value or returns a - when impossible
     1190     * Tries to convert a length unit to meter value or returns {@code null} when impossible
    11911191     * @param s arbitrary string representing a length
    11921192     * @return the length converted to meters
     
    12011201            Logging.debug(e);
    12021202        }
    1203         return "-";
     1203        return null;
    12041204    }
    12051205
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r19090 r19092  
    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|\")?$");
     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|\")?$");
    9595
    9696    private static final String DEFAULT_STRIP = "\uFEFF\u200B";
     
    20782078            else if ("mm".equals(m.group(2)))
    20792079                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;
    20802086            else if ("ft".equals(m.group(2)) || "'".equals(m.group(2)))
    20812087                v *= 0.3048;
     
    20862092            m = PATTERN_LENGTH2.matcher(s);
    20872093            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);
    20892097            }
    20902098        }
  • trunk/test/unit/org/openstreetmap/josm/tools/UtilsTest.java

    r19089 r19092  
    563563        assertEquals(1.5, Utils.unitToMeter("150cm"));
    564564        assertEquals(1.6, Utils.unitToMeter("1600.0mm"));
     565        assertEquals(1700, Utils.unitToMeter("1.7km"));
     566        assertEquals(-1800, Utils.unitToMeter("-1.8km"));
    565567        assertEquals(3.048, Utils.unitToMeter("10ft"));
    566568        assertEquals(6.096, Utils.unitToMeter("20'"));
    567569        assertEquals(2.54, Utils.unitToMeter("100in"));
    568570        assertEquals(5.08, Utils.unitToMeter("200\""));
     571        assertEquals(1852, Utils.unitToMeter("1nmi"));
     572        assertEquals(1609.344, Utils.unitToMeter("1mi"));
    569573        assertEquals(3.0734, Utils.unitToMeter("10ft1in"));
    570574        assertEquals(6.1468, Utils.unitToMeter("20'2\""));
     575        assertEquals(-6.1468, Utils.unitToMeter("-20'2\""));
    571576        assertThrows(IllegalArgumentException.class, () -> Utils.unitToMeter("Hallo"));
    572577    }
Note: See TracChangeset for help on using the changeset viewer.