MapCSS/0.2/eval
From OpenStreetMap Wiki
This page contains proposed MapCSS eval() syntax.
Contents
Data types
EIAS: Everything is a string.
When used in different contexts, it may be considered as:
- string
- String is enclosed in "quotation marks". It can be:
- concatenated using . operator.
- checked for being a number using num() function. In case it can't be converted, "" (empty string) is returned.
- converted to a number using metric() or zmetric() function. These functions should return (heuristically?) parsed width/height in map units. E.g. on "2 meters in 1 pixel" scale metric("3m") should return 1.5. zmetric() does the same for elevations. In the case it can't be converted, "" (empty string) should be returned.
- number. May be enclosed in "quotation marks".
- Can be:
- converted to string using str() function (actually does nothing, as any number is a string)
- multiplied/divided/substracted/added using usual *, /, -, +
- square-rooted using sqrt()
- integer part is returned using int() (ceil/floor/round? - no, what it says – integer part) function
- Consequences:
- "2" + 4 == 6;
- "2" == 2;
- But:
- 2/0 == "";
- none / "" (empty string)
- none is a constant equal to empty string
- in any number operation on none, it's equal to 0.
- if none is the final evaluation result, it's considered that a property isn't applied at all.
- Consequences:
- 2."" == 2;
- 2+"" == 2;
- 2/"" == "";
- none == "";
- boolean
- expression is considered "false" if it equals to "0", "no", "false" or "" (empty string);
- expression is considered "true" otherwise.
- can be turned to a canonical form using boolean() function
- canonical form for "true" is "true", canonical form for "false" is "false"
- Consequences:
- boolean("yes") == boolean("true")
Operations
- Comparison operators: ==, <>, !=, <, >, <=, >=. Perform canonization for numbers prior to comparison, so "2" == "02". Implicit canonization doesn't convert numeric value to boolean and vice versa, however.
- String comparison operators: eq, ne: "2" ne "02", "2" eq "2".
Functions
Operators
Operator | Description | Example | JOSM | pgm |
---|---|---|---|---|
== | Equal comparison | "2" == "2" => "true", "3" == "2" => "false", "a" == "a" => "true" | + | + |
== | Equal comparison with canonization for numbers | "2" == "02" => "true", "3" == "2" => "false", "a" == "a" => "true" | - | + |
!= | Not equal comparison | "2" != "02" => "false", "3" != "2" => "true", "a" != "a" => "false" | + | + |
!= | Not equal comparison with canonization for numbers | "2" != "02" => "false", "3" != "2" => "true", "a" != "a" => "false" | - | + |
<> | Not equal comparison with canonization for numbers | "2" <> "02" => "false", "3" <> "2" => "true" | + | + |
<, <=, >, >= | Numeric greater / lower comparison | "3" > "2" => "true" | + | + |
eq | String equality | "2" eq "02" => "false", "3" eq "2" => "false", "a" eq "a" => "true" | - | + |
ne | String not equality | "2" ne "02" => "true", "3" ne "2" => "true", "a" ne "a" => "false" | - | + |
&& | Logical and | ("a" == "a") && ("2" == "3") => "false" | + | + |
|| | Logical or | ("a" == "a") || ("2" == "3") => "true" | + | + |
! | Logical not | !("a" == "a") => "false | + | + |
General functions
Function | Description | Example | JOSM | pgm |
---|---|---|---|---|
tag(key:string) | returns value of a tag, described by key. In the case there's no such tag, returns none | tag("amenity") => "restaurant" | + | + |
prop(key:string) | returns value of a property, described by key. In the case there's no such property, returns none | prop("color") => "#ff0000" | + | + |
cond(expr, if, else) | returns if if expr is true, else otherwise. | cond("true", "yes", "no") => "yes" | + | + |
any(k1, k2, ...) | returns first k that's not none | any("", "foo", "bar") => "foo" | +^{[1]} | + |
- ↑ before 7164: use coalesce() instead
"Type check / conversion" functions
Function | Description | Example | JOSM | pgm |
---|---|---|---|---|
num(value) | Returns the parsed value as number or none | num("4.5 ") => 4.5 | - | + |
str(value) | convert to string (actually does nothing, as any number is a string) | str(4.5) => "4.5" | - | + |
boolean(value) | turn value to canonical form; "0", "no", "false", "" => "false"; every other value => "true" | boolean("something") => "true" | - | + |
Numeric functions
Function | Description | Example | JOSM | pgm |
---|---|---|---|---|
int(value) | integer part is returned using int() (ceil/floor/round? - no, what it says – integer part) function | int(-5.6) => -5 | - | + |
max(x1, x2, ...) | returns maximum | min(3, 5, "") => 5 | + | + |
min(x1, x2, ...) | returns minimum | min(3, 5, "") => 3 | + | + |
metric(value:string) | Convert parsed (metric) length in map units (pixels) | metric("3m") => 1.5 (when scale is 2m = 1px) | - | +^{[1]} |
sqrt(value:float) | returns square root of value | sqrt(4) => 2 | + | + |
- ↑ not geodesic, using 900913 projection as coordinate system
String functions
Function | Description | Example | JOSM | pgm |
---|---|---|---|---|
concat(value, value, ...) | concatenate strings together | concat("amenity: ", tag("amenity")) => "amenity: restaurant" | + | + |
value . value . ... | concatenate strings together | "amenity: " . tag("amenity") => "amenity: restaurant" | - | + |