Class Functions


  • public final class Functions
    extends Object
    List of functions that can be used in MapCSS expressions. First parameter can be of type Environment (if needed). This is automatically filled in by JOSM and the user only sees the remaining arguments. When one of the user supplied arguments cannot be converted the expected type or is null, the function is not called and it returns null immediately. Add the annotation ExpressionFactory.NullableArguments to allow null arguments. Every method must be static.
    Since:
    15245 (extracted from ExpressionFactory)
    • Method Detail

      • eval

        public static Object eval​(Object o)
        Identity function for compatibility with MapCSS specification.
        Parameters:
        o - any object
        Returns:
        o unchanged
      • plus

        public static double plus​(double a,
                                  double b)
        Function associated to the numeric "+" operator.
        Parameters:
        a - the first operand
        b - the second operand
        Returns:
        Sum of arguments
        See Also:
        Float.sum(float, float)
      • minus

        public static double minus​(double a,
                                   double b)
        Function associated to the numeric "-" operator.
        Parameters:
        a - the first operand
        b - the second operand
        Returns:
        Subtraction of arguments
      • times

        public static double times​(double a,
                                   double b)
        Function associated to the numeric "*" operator.
        Parameters:
        a - the first operand
        b - the second operand
        Returns:
        Multiplication of arguments
      • divided_by

        public static double divided_by​(double a,
                                        double b)
        Function associated to the numeric "/" operator.
        Parameters:
        a - the first operand
        b - the second operand
        Returns:
        Division of arguments
      • mod

        public static float mod​(float a,
                                float b)
        Function associated to the math modulo "%" operator.
        Parameters:
        a - first value
        b - second value
        Returns:
        a mod b, e.g., mod(7, 5) = 2
      • list

        public static List<Objectlist​(Environment ignored,
                                        Object... args)
        Creates a list of values, e.g., for the dashes property.
        Parameters:
        ignored - The environment (ignored)
        args - The values to put in a list
        Returns:
        list of values
        See Also:
        Arrays.asList(Object[])
      • count

        public static Integer count​(List<?> lst)
        Returns the number of elements in a list.
        Parameters:
        lst - the list
        Returns:
        length of the list
      • get

        public static Object get​(List<?> lst,
                                 float n)
        Get the nth element of the list lst (counting starts at 0).
        Parameters:
        lst - list
        n - index
        Returns:
        nth element of the list, or null if index out of range
        Since:
        5699
      • split

        public static List<Stringsplit​(String sep,
                                         String toSplit)
        Splits string toSplit at occurrences of the separator string sep and returns a list of matches.
        Parameters:
        sep - separator string
        toSplit - string to split
        Returns:
        list of matches
        Since:
        5699
        See Also:
        String.split(String)
      • rgb

        public static Color rgb​(float r,
                                float g,
                                float b)
        Creates a color value with the specified amounts of red, green, blue (arguments from 0.0 to 1.0)
        Parameters:
        r - the red component
        g - the green component
        b - the blue component
        Returns:
        color matching the given components
        See Also:
        Color(float, float, float)
      • rgba

        public static Color rgba​(float r,
                                 float g,
                                 float b,
                                 float alpha)
        Creates a color value with the specified amounts of red, green, blue, alpha (arguments from 0.0 to 1.0)
        Parameters:
        r - the red component
        g - the green component
        b - the blue component
        alpha - the alpha component
        Returns:
        color matching the given components
        See Also:
        Color(float, float, float, float)
      • hsb_color

        public static Color hsb_color​(float h,
                                      float s,
                                      float b)
        Create color from hsb color model. (arguments form 0.0 to 1.0)
        Parameters:
        h - hue
        s - saturation
        b - brightness
        Returns:
        the corresponding color
      • html2color

        public static Color html2color​(String html)
        Creates a color value from an HTML notation, i.e., #rrggbb.
        Parameters:
        html - HTML notation
        Returns:
        color matching the given notation
      • color2html

        public static String color2html​(Color c)
        Computes the HTML notation (#rrggbb) for a color value).
        Parameters:
        c - color
        Returns:
        HTML notation matching the given color
      • red

        public static float red​(Color c)
        Get the value of the red color channel in the rgb color model
        Parameters:
        c - color
        Returns:
        the red color channel in the range [0;1]
        See Also:
        Color.getRed()
      • green

        public static float green​(Color c)
        Get the value of the green color channel in the rgb color model
        Parameters:
        c - color
        Returns:
        the green color channel in the range [0;1]
        See Also:
        Color.getGreen()
      • blue

        public static float blue​(Color c)
        Get the value of the blue color channel in the rgb color model
        Parameters:
        c - color
        Returns:
        the blue color channel in the range [0;1]
        See Also:
        Color.getBlue()
      • alpha

        public static float alpha​(Color c)
        Get the value of the alpha channel in the rgba color model
        Parameters:
        c - color
        Returns:
        the alpha channel in the range [0;1]
        See Also:
        Color.getAlpha()
      • prop

        public static Object prop​(Environment env,
                                  String key)
        Returns the value of the property key, e.g., prop("width").
        Parameters:
        env - the environment
        key - the property key
        Returns:
        the property value
      • prop

        public static Object prop​(Environment env,
                                  String key,
                                  String layer)
        Returns the value of the property key from layer layer.
        Parameters:
        env - the environment
        key - the property key
        layer - layer
        Returns:
        the property value
      • is_prop_set

        public static Boolean is_prop_set​(Environment env,
                                          String key)
        Determines whether property key is set.
        Parameters:
        env - the environment
        key - the property key
        Returns:
        true if the property is set, false otherwise
      • is_prop_set

        public static Boolean is_prop_set​(Environment env,
                                          String key,
                                          String layer)
        Determines whether property key is set on layer layer.
        Parameters:
        env - the environment
        key - the property key
        layer - layer
        Returns:
        true if the property is set, false otherwise
      • tag

        public static String tag​(Environment env,
                                 String key)
        Gets the value of the key key from the object in question.
        Parameters:
        env - the environment
        key - the OSM key
        Returns:
        the value for given key
      • parent_tag

        public static String parent_tag​(Environment env,
                                        String key)
        Gets the first non-null value of the key key from the object's parent(s).
        Parameters:
        env - the environment
        key - the OSM key
        Returns:
        first non-null value of the key key from the object's parent(s)
      • parent_tags

        public static List<Stringparent_tags​(Environment env,
                                               String key)
        Gets a list of all non-null values of the key key from the object's parent(s). The values are sorted according to AlphanumComparator.
        Parameters:
        env - the environment
        key - the OSM key
        Returns:
        a list of non-null values of the key key from the object's parent(s)
      • child_tag

        public static String child_tag​(Environment env,
                                       String key)
        Gets the value of the key key from the object's child.
        Parameters:
        env - the environment
        key - the OSM key
        Returns:
        the value of the key key from the object's child, or null if there is no child
      • parent_osm_id

        public static Long parent_osm_id​(Environment env)
        Returns the OSM id of the object's parent.

        Parent must be matched by child selector.

        Parameters:
        env - the environment
        Returns:
        the OSM id of the object's parent, if available, or null
        See Also:
        PrimitiveId.getUniqueId()
      • gpx_distance

        public static double gpx_distance​(Environment env)
        Returns the lowest distance between the OSM object and a GPX point

        Parameters:
        env - the environment
        Returns:
        the distance between the object and the closest gpx point or Double.MAX_VALUE
        Since:
        14802
      • has_tag_key

        public static boolean has_tag_key​(Environment env,
                                          String key)
        Determines whether the object has a tag with the given key.
        Parameters:
        env - the environment
        key - the OSM key
        Returns:
        true if the object has a tag with the given key, false otherwise
      • index

        public static Float index​(Environment env)
        Returns the index of node in parent way or member in parent relation.
        Parameters:
        env - the environment
        Returns:
        the index as float. Starts at 1
      • sort

        public static List<Stringsort​(Environment ignored,
                                        String... sortables)
        Sort an array of strings
        Parameters:
        ignored - The environment (ignored)
        sortables - The array to sort
        Returns:
        The sorted list
        Since:
        15279
      • sort_list

        public static List<Stringsort_list​(List<String> sortables)
        Sort a list of strings
        Parameters:
        sortables - The list to sort
        Returns:
        The sorted list
        Since:
        15279
      • uniq

        public static List<Stringuniq​(Environment ignored,
                                        String... values)
        Get unique values
        Parameters:
        ignored - The environment (ignored)
        values - A list of values that may have duplicates
        Returns:
        A list with no duplicates
        Since:
        15323
      • uniq_list

        public static List<Stringuniq_list​(List<String> values)
        Get unique values
        Parameters:
        values - A list of values that may have duplicates
        Returns:
        A list with no duplicates
        Since:
        15323
      • role

        public static String role​(Environment env)
        Returns the role of current object in parent relation, or role of child if current object is a relation.
        Parameters:
        env - the environment
        Returns:
        role of current object in parent relation, or role of child if current object is a relation
        See Also:
        Environment.getRole()
      • count_roles

        public static int count_roles​(Environment env,
                                      String... roles)
        Returns the number of primitives in a relation with the specified roles.
        Parameters:
        env - the environment
        roles - The roles to count in the relation
        Returns:
        The number of relation members with the specified role
        Since:
        15196
      • areasize

        public static Float areasize​(Environment env)
        Returns the area of a closed way or multipolygon in square meters or null.
        Parameters:
        env - the environment
        Returns:
        the area of a closed way or multipolygon in square meters or null
        See Also:
        Geometry.computeArea(IPrimitive)
      • waylength

        public static Float waylength​(Environment env)
        Returns the length of the way in metres or null.
        Parameters:
        env - the environment
        Returns:
        the length of the way in metres or null.
        See Also:
        Way.getLength()
      • not

        public static boolean not​(boolean b)
        Function associated to the logical "!" operator.
        Parameters:
        b - boolean value
        Returns:
        true if !b
      • greater_equal

        public static boolean greater_equal​(float a,
                                            float b)
        Function associated to the logical ">=" operator.
        Parameters:
        a - first value
        b - second value
        Returns:
        true if a >= b
      • less_equal

        public static boolean less_equal​(float a,
                                         float b)
        Function associated to the logical "<=" operator.
        Parameters:
        a - first value
        b - second value
        Returns:
        true if a <= b
      • greater

        public static boolean greater​(float a,
                                      float b)
        Function associated to the logical ">" operator.
        Parameters:
        a - first value
        b - second value
        Returns:
        true if a > b
      • less

        public static boolean less​(float a,
                                   float b)
        Function associated to the logical "<" operator.
        Parameters:
        a - first value
        b - second value
        Returns:
        true if a < b
      • degree_to_radians

        public static double degree_to_radians​(double degree)
        Converts an angle in degrees to radians.
        Parameters:
        degree - the angle in degrees
        Returns:
        the angle in radians
        See Also:
        Math.toRadians(double)
      • cardinal_to_radians

        public static Double cardinal_to_radians​(String cardinal)
        Converts an angle diven in cardinal directions to radians. The following values are supported: n, north, ne, northeast, e, east, se, southeast, s, south, sw, southwest, w, west, nw, northwest.
        Parameters:
        cardinal - the angle in cardinal directions.
        Returns:
        the angle in radians
        See Also:
        RotationAngle.parseCardinalRotation(String)
      • equal

        public static boolean equal​(Object a,
                                    Object b)
        Determines if the objects a and b are equal.
        Parameters:
        a - First object
        b - Second object
        Returns:
        true if objects are equal, false otherwise
        See Also:
        Object.equals(Object)
      • not_equal

        public static boolean not_equal​(Object a,
                                        Object b)
        Determines if the objects a and b are not equal.
        Parameters:
        a - First object
        b - Second object
        Returns:
        false if objects are equal, true otherwise
        See Also:
        Object.equals(Object)
      • JOSM_search

        public static Boolean JOSM_search​(Environment env,
                                          String searchStr)
        Determines whether the JOSM search with searchStr applies to the object.
        Parameters:
        env - the environment
        searchStr - the search string
        Returns:
        true if the JOSM search with searchStr applies to the object
        See Also:
        SearchCompiler
      • JOSM_pref

        public static String JOSM_pref​(Environment env,
                                       String key,
                                       String def)
        Obtains the JOSM'key Preferences string for key key, and defaults to def if that is null. If the default value can be converted to a Color, the NamedColorProperty is retrieved as string.
        Parameters:
        env - the environment
        key - Key in JOSM preference
        def - Default value
        Returns:
        value for key, or default value if not found
      • regexp_test

        public static boolean regexp_test​(String pattern,
                                          String target)
        Tests if string target matches pattern pattern
        Parameters:
        pattern - The regex expression
        target - The character sequence to be matched
        Returns:
        true if, and only if, the entire region sequence matches the pattern
        Since:
        5699
        See Also:
        Pattern.matches(String, CharSequence)
      • regexp_test

        public static boolean regexp_test​(String pattern,
                                          String target,
                                          String flags)
        Tests if string target matches pattern pattern
        Parameters:
        pattern - The regex expression
        target - The character sequence to be matched
        flags - a string that may contain "i" (case insensitive), "m" (multiline) and "s" ("dot all")
        Returns:
        true if, and only if, the entire region sequence matches the pattern
        Since:
        5699
        See Also:
        Pattern.CASE_INSENSITIVE, Pattern.DOTALL, Pattern.MULTILINE
      • regexp_match

        public static List<Stringregexp_match​(String pattern,
                                                String target,
                                                String flags)
        Tries to match string against pattern regexp and returns a list of capture groups in case of success. The first element (index 0) is the complete match (i.e. string). Further elements correspond to the bracketed parts of the regular expression.
        Parameters:
        pattern - The regex expression
        target - The character sequence to be matched
        flags - a string that may contain "i" (case insensitive), "m" (multiline) and "s" ("dot all")
        Returns:
        a list of capture groups if Matcher.matches(), or null.
        Since:
        5701
        See Also:
        Pattern.CASE_INSENSITIVE, Pattern.DOTALL, Pattern.MULTILINE
      • regexp_match

        public static List<Stringregexp_match​(String pattern,
                                                String target)
        Tries to match string against pattern regexp and returns a list of capture groups in case of success. The first element (index 0) is the complete match (i.e. string). Further elements correspond to the bracketed parts of the regular expression.
        Parameters:
        pattern - The regex expression
        target - The character sequence to be matched
        Returns:
        a list of capture groups if Matcher.matches(), or null.
        Since:
        5701
      • osm_id

        public static long osm_id​(Environment env)
        Returns the OSM id of the current object.
        Parameters:
        env - the environment
        Returns:
        the OSM id of the current object
        See Also:
        PrimitiveId.getUniqueId()
      • osm_user_name

        public static String osm_user_name​(Environment env)
        Returns the OSM user name who last touched the current object.
        Parameters:
        env - the environment
        Returns:
        the OSM user name who last touched the current object
        Since:
        15246
        See Also:
        IPrimitive.getUser()
      • osm_user_id

        public static long osm_user_id​(Environment env)
        Returns the OSM user id who last touched the current object.
        Parameters:
        env - the environment
        Returns:
        the OSM user id who last touched the current object
        Since:
        15246
        See Also:
        IPrimitive.getUser()
      • osm_version

        public static int osm_version​(Environment env)
        Returns the version number of the current object.
        Parameters:
        env - the environment
        Returns:
        the version number of the current object
        Since:
        15246
        See Also:
        IPrimitive.getVersion()
      • osm_changeset_id

        public static int osm_changeset_id​(Environment env)
        Returns the id of the changeset the current object was last uploaded to.
        Parameters:
        env - the environment
        Returns:
        the id of the changeset the current object was last uploaded to
        Since:
        15246
        See Also:
        IPrimitive.getChangesetId()
      • osm_timestamp

        public static int osm_timestamp​(Environment env)
        Returns the time of last modification to the current object, as timestamp.
        Parameters:
        env - the environment
        Returns:
        the time of last modification to the current object, as timestamp
        Since:
        15246
        See Also:
        IPrimitive.getRawTimestamp()
      • tr

        public static String tr​(Environment ignored,
                                String... args)
        Translates some text for the current locale. The first argument is the text to translate, and the subsequent arguments are parameters for the string indicated by {0}, {1}, …
        Parameters:
        ignored - The environment (ignored)
        args - arguments
        Returns:
        the translated string
      • substring

        public static String substring​(String s,
                                       float begin)
        Returns the substring of s starting at index begin (inclusive, 0-indexed).
        Parameters:
        s - The base string
        begin - The start index
        Returns:
        the substring
        See Also:
        String.substring(int)
      • substring

        public static String substring​(String s,
                                       float begin,
                                       float end)
        Returns the substring of s starting at index begin (inclusive) and ending at index end, (exclusive, 0-indexed).
        Parameters:
        s - The base string
        begin - The start index
        end - The end index
        Returns:
        the substring
        See Also:
        String.substring(int, int)
      • replace

        public static String replace​(String s,
                                     String target,
                                     String replacement)
        Replaces in s every target} substring by replacement.
        Parameters:
        s - The source string
        target - The sequence of char values to be replaced
        replacement - The replacement sequence of char values
        Returns:
        The resulting string
        See Also:
        String.replace(CharSequence, CharSequence)
      • title

        public static String title​(String str)
        Returns a title-cased version of the string where words start with an uppercase character and the remaining characters are lowercase Also known as "capitalize".
        Parameters:
        str - The source string
        Returns:
        The resulting string
        Since:
        17613
        See Also:
        Character.toTitleCase(char)
      • is_similar

        public static boolean is_similar​(String string1,
                                         String string2)
        Check if two strings are similar, but not identical, i.e., have a Levenshtein distance of 1 or 2.
        Parameters:
        string1 - first string to compare
        string2 - second string to compare
        Returns:
        true if the normalized strings are different but only a "little bit"
        Since:
        14371
        See Also:
        Utils.isSimilar(java.lang.String, java.lang.String)
      • URL_decode

        public static String URL_decode​(String s)
        Percent-decode a string. (See https://en.wikipedia.org/wiki/Percent-encoding) This is especially useful for wikipedia titles
        Parameters:
        s - url-encoded string
        Returns:
        the decoded string, or original in case of an error
        Since:
        11756
      • URL_encode

        public static String URL_encode​(String s)
        Percent-encode a string. (See https://en.wikipedia.org/wiki/Percent-encoding) This is especially useful for data urls, e.g. concat("data:image/svg+xml,", URL_encode("<svg>...</svg>"));
        Parameters:
        s - arbitrary string
        Returns:
        the encoded string
      • XML_encode

        public static String XML_encode​(String s)
        XML-encode a string. Escapes special characters in xml. Alternative to using <![CDATA[ ... ]]> blocks.
        Parameters:
        s - arbitrary string
        Returns:
        the encoded string
      • CRC32_checksum

        public static long CRC32_checksum​(String s)
        Calculates the CRC32 checksum from a string (based on RFC 1952).
        Parameters:
        s - the string
        Returns:
        long value from 0 to 2^32-1
      • is_right_hand_traffic

        public static boolean is_right_hand_traffic​(Environment env)
        check if there is right-hand traffic at the current location
        Parameters:
        env - the environment
        Returns:
        true if there is right-hand traffic
        Since:
        7193
      • print

        public static Object print​(Object o)
        Prints the object to the command line (for debugging purpose).
        Parameters:
        o - the object
        Returns:
        the same object, unchanged
      • println

        public static Object println​(Object o)
        Prints the object to the command line, with new line at the end (for debugging purpose).
        Parameters:
        o - the object
        Returns:
        the same object, unchanged
      • number_of_tags

        public static int number_of_tags​(Environment env)
        Get the number of tags for the current primitive.
        Parameters:
        env - the environment
        Returns:
        number of tags
      • setting

        public static Object setting​(Environment env,
                                     String key)
        Get value of a setting.
        Parameters:
        env - the environment
        key - setting key (given as layer identifier, e.g. setting::mykey {...})
        Returns:
        the value of the setting (calculated when the style is loaded)
      • center

        public static LatLon center​(Environment env)
        Returns the center of the environment OSM primitive.
        Parameters:
        env - the environment
        Returns:
        the center of the environment OSM primitive
        Since:
        11247
      • inside

        public static boolean inside​(Environment env,
                                     String codes)
        Determines if the object is inside territories matching given ISO3166 codes.
        Parameters:
        env - the environment
        codes - comma-separated list of ISO3166-1-alpha2 or ISO3166-2 country/subdivision codes
        Returns:
        true if the object is inside territory matching given ISO3166 codes
        Since:
        11247
      • outside

        public static boolean outside​(Environment env,
                                      String codes)
        Determines if the object is outside territories matching given ISO3166 codes.
        Parameters:
        env - the environment
        codes - comma-separated list of ISO3166-1-alpha2 or ISO3166-2 country/subdivision codes
        Returns:
        true if the object is outside territory matching given ISO3166 codes
        Since:
        11247
      • at

        public static boolean at​(Environment env,
                                 double lat,
                                 double lon)
        Determines if the object centroid lies at given lat/lon coordinates.
        Parameters:
        env - the environment
        lat - latitude, i.e., the north-south position in degrees
        lon - longitude, i.e., the east-west position in degrees
        Returns:
        true if the object centroid lies at given lat/lon coordinates
        Since:
        12514
      • to_boolean

        public static boolean to_boolean​(String value)
        Parses the string argument as a boolean.
        Parameters:
        value - the String containing the boolean representation to be parsed
        Returns:
        the boolean represented by the string argument
        Since:
        16110
        See Also:
        Boolean.parseBoolean(java.lang.String)
      • to_byte

        public static byte to_byte​(String value)
        Parses the string argument as a byte.
        Parameters:
        value - the String containing the byte representation to be parsed
        Returns:
        the byte represented by the string argument
        Since:
        16110
        See Also:
        Byte.parseByte(java.lang.String, int)
      • to_short

        public static short to_short​(String value)
        Parses the string argument as a short.
        Parameters:
        value - the String containing the short representation to be parsed
        Returns:
        the short represented by the string argument
        Since:
        16110
        See Also:
        Short.parseShort(java.lang.String, int)
      • to_int

        public static int to_int​(String value)
        Parses the string argument as an int.
        Parameters:
        value - the String containing the int representation to be parsed
        Returns:
        the int represented by the string argument
        Since:
        16110
        See Also:
        Integer.parseInt(java.lang.String, int)
      • to_long

        public static long to_long​(String value)
        Parses the string argument as a long.
        Parameters:
        value - the String containing the long representation to be parsed
        Returns:
        the long represented by the string argument
        Since:
        16110
        See Also:
        Long.parseLong(java.lang.String, int)
      • to_float

        public static float to_float​(String value)
        Parses the string argument as a float.
        Parameters:
        value - the String containing the float representation to be parsed
        Returns:
        the float represented by the string argument
        Since:
        16110
        See Also:
        Float.parseFloat(java.lang.String)
      • to_double

        public static double to_double​(String value)
        Parses the string argument as a double.
        Parameters:
        value - the String containing the double representation to be parsed
        Returns:
        the double represented by the string argument
        Since:
        16110
        See Also:
        Double.parseDouble(java.lang.String)