uber / h3-js

h3-js provides a JavaScript version of H3, a hexagon-based geospatial indexing system.
https://uber.github.io/h3
Apache License 2.0
867 stars 78 forks source link

Count the number of points in the aggregated bins of H3-hexagons #162

Closed sharmapn closed 2 years ago

sharmapn commented 2 years ago

Hello,

I am focussing on the H3-JS example from https://observablehq.com/@nrabinowitz/h3-tutorial-using-point-layers?collection=@nrabinowitz/h3-tutorial

Is there a way to we can call a function that counts the number of points in each of the hexagons after the binning and embeds these as attributes within the geoJSON? I found this function on the above page and was wondering if it does exactly that?

function _countPoints(h3,h3Resolution,normalizeLayer){return(
function countPoints(geojson) {
  const layer = {};
  geojson.features.forEach(feature => {
    const [lng, lat] = feature.geometry.coordinates;
    const h3Index = h3.geoToH3(lat, lng, h3Resolution);
    layer[h3Index] = (layer[h3Index] || 0) + 1;
  });
  return normalizeLayer(layer, true);
}
)}
nrabinowitz commented 2 years ago

Yes, that's essentially what that function is doing. How you manage aggregation (count, sum of some property, etc) is entirely up to you, and depends on what your input data looks like. For example, if your input data is an array of [lng, lat] points, you might construct a map of {[h3Index]: count} and then use that map to construct the GeoJSON - that's essentially what the function above is doing. This is one approach to a standard GROUP BY operation - see e.g. _.groupBy from lodash.

sharmapn commented 1 year ago

Hello, In addition to the h3Resolution, I was hoping to integrate Buffer Radius in this calculation. I am fully using this example. Thus, the user can choose values for both: h3Resolution and Buffer Radius. https://observablehq.com/@nrabinowitz/h3-tutorial-using-point-layers?collection=@nrabinowitz/h3-tutorial

I would appreciate any guidance on this small question.

sharmapn commented 1 year ago

I mean at the end we need a geojson which has the features, together with the h3 cells, buffer radius and the number of points contained within.