Custom getColorValue fct to normalize variable by MAX of ALL hexagon cells
See original GitHub issueI am creating a custom function to get the color value and elevation value of the hexagon layer. I am facing an issue in calculating data for my custom function.
Let us take this example from documentation, with the added functionality of having a variable radius implemented through options (not shown below):
class MyHexagonLayer {
renderLayers() {
return new HexagonLayer({
id: 'hexagon-layer',
getColorValue: myFunction(),
data,
radius: 500
});
}
}
My data looks like this: index,lon,lat, Pharmacy, Restaurant
, with 1 or 0 as pharmacy,restaurants row values.
Now myFunction()
goal is to normalize the number of pharmacy & restaurant in each cell. For example:
- Hex Cell 1 has 5 pharmacies, 10 restaurants --> total point of interest of 15
- Hex Cell 2 has 2 pharmacies, 7 restaurants --> total point of interest of 9
- MAX (ALL HEX CELLS) = MAX (15,9) = 15
myFunction()
output should be a weighted sum of pharmacy and restaurants in each cell divided by the MAX number of point of interest of all cells (in this case Max =15). This is important because the hex radius is variable, therefore, the MAX of all cells is variable. An example of what the function should return weighted_sum / MAX_All_Hex_Cells:- Hex Cell 1: (weight_A x 5+weight_B x 10)/15
- Hex Cell 2: (weight_A x 2+weight_B x 7)/15
My issue is how do i compute the maximum number of points of ALL HEX CELLS (15 in example above), and not IN each cell. I thought to calculate the sum of all points counts in each hex cell, then find the maximum value of that sum across all cells:
function myFunction(points) {
//calculating weighted_sum
let weighted_sum = arraySum(points.map(p => p['Pharmacy']))*weight_A +
arraySum(points.map(p => p['Restaurant']))*weight_B;
// calculate MAX_All_Hex_Cells
let sumHexPOI = arraySum(points.map(p => p['Pharmacy'])) +
arraySum(points.map(p => p['Restaurant']));
let MAX_All_Hex_Cells = Math.max(sumHexPOI);
//this is not working, since i need to store each of the hex sumHexPOI in an array
and compute their maximum.
return weighted_sum/MAX_All_Hex_Cells;
}
function arraySum(array) {
return array.reduce((a,b) => a + b, 0);
}
I can get the sum of points of interest in each hexagon, however how do I store that into an array and do a Math.max to get the maximum?
Issue Analytics
- State:
- Created 3 years ago
- Comments:9
Top GitHub Comments
My suggestion is don’t mess those values. Internally they are only used to calculate color. The only place users will see them is in the tooltip. You can normalize the value before displaying it, by listening to
onSetColorDomain
.I ended up changing my approach. Thanks for the help!