Reputation: 1531
I am using data driven styling to style a Mapbox vector layer for a choropleth map. However, rather than getting the data strictly from the layer properties, I need to use statistic data from a separate object (pulled from our database). This separate object contains one statistic value for each polygon in the vector layer. The object maps the stat values to the vector layer polygons by a variable called "GEOID". In order to marry the polygon with its data, I would like to use a Mapbox expression to get the GEOID value from each polygon in the vector layer and pass this id to a separate function to get the statistic value for the polygon having this GEOID. Is this possible?
vectorLayer: {
id: "fooLayer",
type: "fill",
"source-layer": "foo-layer-dvf1ci",
paint: {
"fill-color": [
"rgba",
100,100,100,
this.getStatForDistrict(["get", "GEOID"])]
]
}
},
getStatForDistrict(districtId) {
console.log("districtId: " + districtId);
let alphaValue = fetchDataForThisDistrictFromDatabase(districtId)
return alphaValue;
},
I see that currently, I am passing ["get", "GEOID"] into the function getStatForDistrict when what I actually need is to pass the computed Mapbox expression.
More on Mapbox-GL's paint property: https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#paint-property
More on Mapbox-GL's expressions syntax: https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/
Upvotes: 1
Views: 1950
Reputation: 126527
No, there is not any way to call arbitrary functions from within expressions, which look up data that doesn't exist within the feature in question.
There are several ways to make choropleths. I'd suggest you start by looking at the mapbox-choropleth library.
Upvotes: -1