Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapbox heatmap by point value

There is an example of the heatmap https://www.mapbox.com/mapbox-gl-js/example/heatmap/ by the number of markers/points on the area. But is there a way to display a heatmap by average pins/markers values? For example if I have 5 pins and their average prop value speed=3 then it will be shown as green cluster/heatmap and if their av. prop val is 6 then it will be red cluster/heatmap.

I found that "clusterAggregates" property can help, but can't find any example of using it.

Thanks

like image 478
SERG Avatar asked Aug 24 '17 09:08

SERG


Video Answer


1 Answers

I'll leave my way to do so. Old question, which is sometimes risen, but there are no nice sollution, so... Turf's hexgrid (http://turfjs.org/docs/#hexGrid) can help:

const hexagons = hexGrid(bbox, size);
const collection = // collection of your points;

const hexagonsWithin = collect(hexagons, collection, "propertyToAgretateFrom", "propertyToAggregateIn");
const notEmptyHexagonValues = hexagonsWithin.features.filter(({ properties }) => properties.propertyToAggregateIn.length !== 0);
const notEmptyHexagons = {
    "type": "FeatureCollection",
    "features": notEmptyHexagonValues,
};
// at this point you're having not empty hexagons as a geojson, which you can add to the map

collect is another method from turf, whatcollection should be you can look up in the docs, because it's changing a lot.

The general idea behind is to "divide" visible part of map (bbox) into hexagons by hexGrid method and and aggregate some properties that you need from every marker inside of every hexagon you'll have into the array, so you can get an average value, for example. And assign a color based on it.

Let's say we have feature.properties.propertyToAgretateFrom as 4 and 5 in two markers. After the aggregation, if these markers were inside one polygon, you'll have it feature.properties.propertyToAggregateIn: [4, 5] - this feature is polygon. From this on you can do pretty much everything you want.

like image 56
RomanistHere Avatar answered Oct 19 '22 01:10

RomanistHere