Class MBFunction

Object
MBFunction

public class MBFunction extends Object
MBFunction json wrapper, allowing conversion of function to a GeoTools Expression.

As of v0.41.0, data expressions are the preferred method for styling features based on zoom level or the feature's properties.

Each function is evaluated according type: MBFunction.FunctionType.IDENTITY, MBFunction.FunctionType.INTERVAL, MBFunction.FunctionType.CATEGORICAL, MBFunction.FunctionType.EXPONENTIAL.

We have several methods that intelligently review getType() and produce the correct expression:

  • Field Details

    • parse

      protected final MBObjectParser parse
    • json

      protected final JSONObject json
  • Constructor Details

    • MBFunction

      public MBFunction(JSONObject json)
    • MBFunction

      public MBFunction(MBObjectParser parse, JSONObject json)
  • Method Details

    • getType

      public MBFunction.FunctionType getType()
      Access the function 'type', or null if not specified.

      Depending on the domain you are working with ( enumeration(Class), color(), enumeration(Class)} ) the default value to use is different. These functions check for null and use the appropriate setting.

      Returns:
      function type, or null if not defined.
    • getDefault

      public Object getDefault()
      A value to serve as a fallback function result when a value isn't otherwise available. It is used in the following circumstances:
      • In categorical functions, when the feature value does not match any of the stop domain values.
      • In property and zoom-and-property functions, when a feature does not contain a value for the specified property.
      • In identity functions, when the feature value is not valid for the style property (for example, if the function is being used for a circle-color property but the feature property value is not a string or not a valid color).
      • In interval or exponential property and zoom-and-property functions, when the feature value is not numeric.

      If no default is provided, the style property's default is used in these circumstances.

      Returns:
      The function's default value, or null if none was provided.
    • getTypeWithDefault

      public MBFunction.FunctionType getTypeWithDefault(Class<?> clazz)
      Return the function type, falling back to the default function type for the provided Class if no function type is explicitly declared. The parameter is necessary because different output classes will have different default function types.

      Examples (For a function with no explicitly declared type):

       getTypeWithDefault(String.class); // "interval" function type
       getTypeWithDefault(Number.class); // "exponential" function type
       
      Parameters:
      clazz - The class for which to return the default function type
      Returns:
      The function type, falling back to the default when the provided Class is the return type.
      See Also:
    • getProperty

      public String getProperty()
      If specified, the function will take the specified feature property as an input. See Zoom Functions and Property Functions for more information.
      Returns:
      property evaluated by function, optional (may be null for zoom functions).
    • category

      Programmatically look at the structure of the function and determine if it is a Zoom function, Property function or Zoom-and-property functions.
      Returns:
      Classify function as MBFunction.FunctionCategory.PROPERTY, MBFunction.FunctionCategory.ZOOM or both.
    • getStops

      public JSONArray getStops()
      Functions are defined in terms of input and output values. A set of one input value and one output value is known as a "stop."
      Returns:
      stops definition, optional may be null.
    • getBase

      public Number getBase()
      (Optional) Number. Default is 1. The exponential base of the interpolation curve. It controls the rate at which the function output increases. Higher values make the output increase more towards the high end of the range. With values close to 1 the output increases linearly.
      Returns:
      The exponential base of the interpolation curve.
    • color

      public Expression color()
      GeoTools Expression from json definition that evaluates to a color, used for properties such as 'color' and 'fill-color'.

      This is the same as numeric() except we can make some assumptions about the values (converting hex to color, looking up color names).

      If type is unspecified exponential is used as a default.
      Returns:
      Function (or identity Expression for the provided json)
    • font

      public Expression font()
      GeoTools Expression from json definition that evaluates to a font string, used for 'text-font'.

      Fonts only use interval functions

      Returns:
      Expression providing font string (used for `text-font`)
    • numeric

      public Expression numeric()
      GeoTools Expression from json definition that evaluates to a numeric, used for properties such as 'line-width' and 'opacity'.

      This is the same as color() except we can make some assumptions about the values (converting "50%" to 0.5).

      If type is unspecified exponential is used as a default.
      Returns:
      Function (or identity Expression for the provided json)
    • function

      public Expression function(Class<?> clazz)
      GeoTools Expression from json definition.

      Delegates handling of Color, Number and Enum - for generic values (such as String) the following are available:

      If type is unspecified interval is used as a default.
      Parameters:
      clazz - Type of data expected
      Returns:
      Function (or identity Expression for the provided json)
    • enumeration

      public Expression enumeration(Class<? extends Enum<?>> enumeration)
      GeoTools Expression from json definition that evaluates to the provided Enum, used for properties such as 'line-cap' and 'text-transform'. If type is unspecified internval is used as a default.
      Returns:
      Function (or identity Expression for the provided json)
    • isArrayFunction

      public boolean isArrayFunction()
      Returns true if this function's stop values are all arrays.

      For example, the following is an array function:

      
       "{'property':'temperature',
         'type':'exponential',
         'base':1.5,
         'stops': [
                // [stopkey, stopValueArray]
                   [0,       [0,10]],
                   [100,     [2,15]]
          ]
         }"
       
      Returns:
      true if this function's stop values are all arrays.
    • splitArrayFunction

      public List<MBFunction> splitArrayFunction() throws ParseException
      Splits an array function into multiple functions, one for each dimension in the function's stop value arrays.

      For example, for the following array function:

      
       "{'property':'temperature',
         'type':'exponential',
         'base':1.5,
         'stops': [
                // [stopkey, stopValueArray]
                   [0,       [0,10]],
                   [100,     [2,15]]
          ]
         }"
       

      This method would split the above function into the following two functions:

      "X" Function:

      
       "{'property':'temperature',
         'type':'exponential',
         'base':1.5,
         'stops': [
                [0,   0],
                [100, 2]
          ]
         }"
       

      And "Y" Function:

      
       "{'property':'temperature',
         'type':'exponential',
         'base':1.5,
         'stops': [
                [0,   10],
                [100, 15]
          ]
         }"
       
      Returns:
      A list of MBFunction, one for each dimension in the stop value array.
      Throws:
      ParseException