Class MBFunction
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Function category property, zoom, or property and zoom.static enum
Optional type, one of identity, exponential, interval, categorical. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMBFunction
(MBObjectParser parse, JSONObject json) MBFunction
(JSONObject json) -
Method Summary
Modifier and TypeMethodDescriptioncategory()
Programmatically look at the structure of the function and determine if it is a Zoom function, Property function or Zoom-and-property functions.color()
GeoToolsExpression
from json definition that evaluates to a color, used for properties such as 'color' and 'fill-color'.enumeration
(Class<? extends Enum<?>> enumeration) GeoToolsExpression
from json definition that evaluates to the provided Enum, used for properties such as 'line-cap' and 'text-transform'.font()
GeoToolsExpression
from json definition that evaluates to a font string, used for 'text-font'.GeoToolsExpression
from json definition.getBase()
(Optional) Number.A value to serve as a fallback function result when a value isn't otherwise available.If specified, the function will take the specified feature property as an input.JSONArray
getStops()
Functions are defined in terms of input and output values.getType()
Access the function 'type', or null if not specified.getTypeWithDefault
(Class<?> clazz) Return the function type, falling back to the default function type for the providedClass
if no function type is explicitly declared.boolean
Returns true if this function's stop values are all arrays.numeric()
GeoToolsExpression
from json definition that evaluates to a numeric, used for properties such as 'line-width' and 'opacity'.Splits an array function into multiple functions, one for each dimension in the function's stop value arrays.
-
Field Details
-
parse
-
json
protected final JSONObject json
-
-
Constructor Details
-
MBFunction
public MBFunction(JSONObject json) -
MBFunction
-
-
Method Details
-
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
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
Return the function type, falling back to the default function type for the providedClass
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
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
(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
GeoToolsExpression
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).MBFunction.FunctionType.IDENTITY
: input is directly converted to a color, providing a way to process attribute data into colors.MBFunction.FunctionType.CATEGORICAL
: selects stop equal to input valueMBFunction.FunctionType.INTERVAL
: selects stop less than numeric input valueMBFunction.FunctionType.EXPONENTIAL
: interpolates an output color between two stops
- Returns:
Function
(or identityExpression
for the provided json)
-
font
GeoToolsExpression
from json definition that evaluates to a font string, used for 'text-font'.Fonts only use interval functions
MBFunction.FunctionType.INTERVAL
: selects stop less than numeric input, and returns stop value as a number
- Returns:
- Expression providing font string (used for `text-font`)
-
numeric
GeoToolsExpression
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).MBFunction.FunctionType.IDENTITY
: input is directly converted to a numeric outputMBFunction.FunctionType.CATEGORICAL
: selects stop equal to input, and returns stop value as a numberMBFunction.FunctionType.INTERVAL
: selects stop less than numeric input, and returns stop value as a numberMBFunction.FunctionType.EXPONENTIAL
: interpolates a numeric output between two stops
- Returns:
Function
(or identityExpression
for the provided json)
-
function
GeoToolsExpression
from json definition.Delegates handling of Color, Number and Enum - for generic values (such as String) the following are available:
MBFunction.FunctionType.IDENTITY
: input is directly converted to a literalMBFunction.FunctionType.CATEGORICAL
: selects stop equal to input, and returns stop value as a literalMBFunction.FunctionType.INTERVAL
: selects stop less than numeric input, and returns stop value a literal
- Parameters:
clazz
- Type of data expected- Returns:
Function
(or identityExpression
for the provided json)
-
enumeration
GeoToolsExpression
from json definition that evaluates to the provided Enum, used for properties such as 'line-cap' and 'text-transform'.MBFunction.FunctionType.IDENTITY
: input is directly converted to an appropriate literalMBFunction.FunctionType.CATEGORICAL
: selects stop equal to input, and returns stop value as a literalMBFunction.FunctionType.INTERVAL
: selects stop less than numeric input, and returns stop value a literal
- Returns:
Function
(or identityExpression
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
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
-