スタイル式リファレンス

スタイルルール内でフィーチャー選択、値の計算、マップの状態に応じた処理を行うために使用される式について説明します。式はJSON配列であり、フィーチャーのプロパティへのアクセス、現在のズームレベルの取得、条件分岐の実行などが可能です。

基本タイプ

array

array タイプは値タイプの配列を格納します。array値を作成するには、literal式を使用します。

例:

["literal", [1, 2] ] // returns an array of real numbers.
["literal", [1, "2", 3] ] // returns an array of numbers and strings

color

colorタイプにはレンダリング可能な色を格納します。color値は文字列として表されます。
サポートされている文字列形式は次のとおりです。

  • 16進形式の"#rgb"、"#rgba"、"#rrggbb"、"#rrggbbaa" (r、g、b、aの各値は[0-9, a-f]の範囲)。
  • CSSのカラー関数"rgb(r,g,b)" (r、g、bの各値は[0, 255]の範囲)。範囲外の値は範囲境界値に固定されます。

例:

"#00ff7f" -> opaque color with RGB channels 0, 255, 127 (SpringGreen)
"#f0f" -> opaque color with RGB channels 255, 0, 255 (Magenta)

dictionary

dictionary タイプはキー/値のペアを格納します。辞書は、get および has 式を使用してクエリできます。
dictionary値を作成するには、literal式を使用します。

例:

["literal", {"a": 1, "b": 2}]

vector2d

vector2d タイプは numbers の 2 次元ベクターを格納します。vector2d値を作成するには、make-vector式を使用します。

例:

["make-vector", 10, 20] // returns [10, 20]

vector3d

vector3d タイプは numbers の 3 次元ベクターを格納します。vector3d値を作成するには、make-vector式を使用します。

例:

["make-vector", 4.5, 5.5, 6.7] // returns [4.5, 5.5, 6.7]

vector4d

vector4d タイプは numbers の 4 次元ベクターを格納します。vector4d値を作成するには、make-vector式を使用します。

例:

Example: ["make-vector", -2, 4, 90, -234] //  return [-2, 4, 90, -234]

式

!

値がtrueに変換できる場合はfalseを返します。それ以外の場合はtrueを返します。

例:

["!", value]

!=

値が等しくない場合はtrueが返されます。サポートされるタイプはnumberとstringです。

例:

["!=", value, value]

!has

現在のfeatureまたは指定したdictionaryに指定されたプロパティがないかどうかを示すbooleanを返します。featureによって、道路やポリゴンなどのカスタマイズ可能なマップ項目がわかります。

["!has", name]
["!has", name, dictionary]

!has式はhas式の否定に相当します。

["!has", name] /* is same as */ ["!", ["has", name]]
["!has", name, dictionary] /* is same as */  ["!", ["has", name, object]]

<

最初の値が2つ目の値より小さい場合はtrueが返されます。

["<", number, number]

<=

最初の値が2つ目の値以下の場合はtrueが返されます。

["<=", number, number]

==

値が等しい場合はtrueが返されます。サポートされるタイプはnumberとstringです。

["==", value, value]

>

最初の値が2つ目の値より大きい場合はtrueが返されます。

[">", number, number]

>=

最初の値が2つ目の値以上の場合はtrueが返されます。

[">=", number, number]

all

すべてのサブ式がtrueと評価される場合、trueを返します。

["all", expression...] // boolean

例:

"definitions" : {
    "my_condition_1": true,
    "my_condition_2": false,
    "my_condition_3": true

}
["all",
    ["ref", "my_condition_1"],
    ["ref", "my_condition_2"],
    ["ref", "my_condition_3"]
] // returns false since my_condition_2 is false

any

いずれかのサブ式がtrueと評価される場合、trueを返します。

["any", expression...] // boolean

例:

"definitions" : {
    "my_condition_1": true,
    "my_condition_2": false,
    "my_condition_3": true

}
["any",
    ["ref", "my_condition_1"],
    ["ref", "my_condition_2"],
    ["ref", "my_condition_3"]
] // returns true since my_condition_1 is true

case

条件を順番に評価し、条件がtrueに評価される最初の結果を返します。trueと評価される条件がない場合はfallbackが返されます。

["case",
  condition1, result1,
  ...
  conditionN, resultN,
  fallback
]

例:

"definitions" : {
    "my_condition_1": false,
    "my_condition_2": false,
    "my_condition_3": true

}
["case",
    ["ref", "my_condition_1"],
    10,
    ["ref", "my_condition_2"],
    20,
    ["ref", "my_condition_3"],
    40,
    80 // fallback value
] // returns 40 since my_condition_3 is true

get

現在の feature または dictionary のプロパティ値を取得します。プロパティが存在しない場合はnullを返します。featureによって、道路やポリゴンなどのカスタマイズ可能なマップ項目がわかります。


["get", name] // `name` can be an expression, but that disables optimization and evaluation will be much slower.
["get", name, dictionary]

例:

get式の使用法を説明するために、レンダリングされたマップに「Roads」というレイヤーが含まれている場合を考えてみましょう。
レイヤー内の各フィーチャー (道路など) には、メートル単位で表されるプロパティ「road_width」が含まれます。
スタイル内の各フィーチャーの「road_width」プロパティを使用できるのが理想です。そのためには、
get 式を使用します。以下のスタイルでは、スタイル属性「width」を「road_width」プロパティの値に設定しています。このようにスタイル設定した各線の幅は
「road_width」プロパティで定義された幅になります。

{
  "styles": [
    {
      "layer": "Roads",
      "technique": "line",
      "attr": {
        "width": ["get", "road_width"] // returns the width in meter of the current road
        }
     }
  ]
}

次の例では、literal は 2 つのキー「a」と「b」を持つ dictionary を返します。get 式はキー「a」の値を返します。


["get", "a", ["literal", {"a": 1, "b": 2}]] // returns 1

has

現在のfeatureまたは指定したdictionaryに指定されたプロパティがあるかどうかを示すbooleanを返します。featureによって、道路やポリゴンなどのカスタマイズ可能なマップ項目がわかります。

["has", name] // `name` can be an expression, but that disables optimization and evaluation will be much slower.
["has", name, dictionary]

例:

再び、'Roads'というレイヤーを含むレンダリングされたマップを考えてみましょう。今回は、レイヤー内のすべてのフィーチャーがメートル単位で表された"road_width"プロパティを持っているわけではなく、持っているフィーチャーもあれば、持っていないフィーチャーもあります。各フィーチャーのスタイルで"road_width"プロパティを使用したい一方で、このプロパティを持たないフィーチャーにも対応する必要があります。has式を使用して、フィーチャーが"road_width"プロパティを持っているかどうかを確認します。このプロパティを持たないフィーチャーについては、デフォルトの線幅を設定します。

{
  "styles": [{
    "layer": "Roads",
    "technique": "line",
    "attr": {
      "width": ["case" ["has", "road_width"], // returns true if the current road has "road_width" property
        ["get", "road_width"], // if the current road has "road_width" property, we set line width to "road_width" value
        10
      ] // else we set the line width to 10 meters
    }
  }]
}

以下の例では、literalが"a"と"b"という2つのキーを持つdictionaryを返します。has式はtrueを返します。

["has", "a", ["literal", {"a": 1, "b": 2}]] // returns true

interpolate

別の停止変数に基づいて、指定された値の補間を作成します。
使用可能な補間タイプは["linear"]と["step"]です。

["interpolate", ["linear"], ["zoom"],
   stop1, value1,
   ...
   stopN, valueN,
]

例:

// ["zoom"] returns 9
["interpolate", ["linear"], ["zoom"], 8, 20, 10, 40] // returns 30

literal

json_documentで指定されたdictionaryまたはarrayを返します。

["literal", json_document]

例:

["literal", {"a": 1, "b": 2}] // returns a dictionary with keys "a" and "b"
["literal", [1, 2, 3]] // returns an array

make-vector

数値から2次元、3次元、または4次元のベクトルを構築します。

["make-vector", number, number]
["make-vector", number, number, number]
["make-vector", number, number, number, number]

例:

["make-vector", 10, 20] // returns [10, 20]
["make-vector", 4.5, 5.5, 6.7] // returns [4.5, 5.5, 6.7]
["make-vector", -2, 4, 90, -234] //  return [-2, 4, 90, -234]
["make-vector", 10, "20"] // returns null, does not support strings

match

valueをラベルと比較し、最初に一致したresultを返します。valueがどのラベルにも一致しない場合は、fallbackが返されます。labelは、number、string、またはそれらのarrayである必要があります。valueとlabelの型が異なる場合は、fallbackが返されます。

["match",
  value,
  label1, result1,
  ...
  labelN, resultN,
  fallback
]

例:

"definitions" : {
    "my_value": 10
}
["match",
    ["ref", "my_value"],
    20,
    "Value is 20", // result 1
    10,
    "Value is 10", // result 2
    "No match was found" // fallback value
] // returns "Value is 10"

なし

サブ式がtrueと評価されない場合、trueを返します。

["none", expression...]

例:

"definitions" : {
    "my_condition_1": true,
    "my_condition_2": false,
    "my_condition_3": true

}
["none",
    ["ref", "my_condition_1"],
    ["ref", "my_condition_2"],
    ["ref", "my_condition_3"]
] // returns false since my_condition_1 is true

pixel-world-scale

画面ピクセルをワールド単位(メートル)に変換します。

["pixel-world-scale", number]

例:

幅10ピクセルのポリラインをレンダリングする場合を考えてみましょう。ラインテクニックのwidth属性にはメートルが必要であることを考えると、幅10ピクセルのポリラインをレンダリングする最も簡単な方法は、pixel-world-scaleを使用することです。

{
  "styles": [
      {
        "layer": "MyRoads",
        "technique": "line",
        "attr": {
          "width": ["pixel-world-scale", 10.0] // returns the width in meter that is equivalent to 10 screen pixels. The line will be rendered with a width of 10 screen pixels
      }
    }
  ]
}

ref

値として評価できる値定義または式定義を参照します。定義が見つからない場合はnullを返します。

["ref", string]

例:


"definitions" : {
    "my_definition_1": 4,
    "my_definition_2": true,
    "my definition_3": ["+", 1, 2]

}
["ref", "my_definition_1"] // returns 4
["ref", "my_definition_2"] // returns true
["ref", "my_definition_3"] // returns 3
["ref", "my_definition_4"] // returns null, the definition cannot be found.

rgb

RGB コンポーネントから color を作成します。コンポーネントは、0 ~ 255 の整数である必要があります。範囲外の値は範囲境界値に固定されます。

["rgb", number, number, number]

例:

["rgb", 1, 2, 3] // returns opaque color with RGB channels 1, 2, 3

step

指定された区分関数を評価します。入力値が最初の区間の値より小さい場合、default valueを返します。それ以外の場合は、入力値以上の区間の値に対応する値を返します。

例:

["step", ["zoom"], default value, stop1, value1, ..., stopN, valueN]

zoom

現在のzoomレベルを取得します。

["zoom"]

次のステップ

マップスタイルをカスタマイズする際は、次のドキュメントを参照してください。


このページは役に立ちましたか?