Как раскрасить отдельные полигоны, нарисованные с помощью mapbox-gl draw?
Я читаю файл GeoJSON и импортирую полигоны (и другие материалы) в mapbox-gl draw, используя draw.set(geoJSON)
, Как мне раскрасить отдельные полигоны по атрибуту в свойствах объекта. Пример:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
//"id": "the most unique id in the world",
"properties": {
"class_id": 1
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
79.30961608886719,
61.57192958204744
],
[
79.34309005737303,
61.57192958204744
],
[
79.34309005737303,
61.57871162332267
],
[
79.30961608886719,
61.57871162332267
],
[
79.30961608886719,
61.57192958204744
]
]
]
}
},
{
"type": "Feature",
"properties": {
"class_id": 2
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
79.35201644897461,
61.58271478278019
],
[
79.35115814208984,
61.573972521656835
],
[
79.38188552856444,
61.57192958204744
],
[
79.35201644897461,
61.58271478278019
]
]
]
}
},
}
Идея состоит в том, что мы окрашиваем class_id
= 1 как красный, class_id
= 2 как синий, и class_id
= 3 как зеленый. Как мы это делаем?
1 ответ
Решение
Вам нужно установить userProperties
верно для свойств a feature will be available for styling
, И использовать префикс user
,
И использовать case expression
:
var Draw = new MapboxDraw({
userProperties: true,
styles: [{
'id': 'gl-draw-polygon-fill-inactive',
'type': 'fill',
'filter': ['all', ['==', 'active', 'false'],
['==', '$type', 'Polygon'],
['!=', 'mode', 'static']
],
'paint': {
'fill-color': [
"case",
['==', ['get', "user_class_id"], 1], "#00ff00",
['==', ['get', "user_class_id"], 2], "#0000ff",
'#ff0000'
],
'fill-outline-color': '#3bb2d0',
'fill-opacity': 0.5
}
}...