modules/aggregation-layers/src/grid-layer/grid-aggregator.js
function _pointsToGridHashing(points = [], cellSize, getPosition) {
  // find the geometric center of sample points
  let latMin = Infinity;
  let latMax = -Infinity;
  let pLat;

  for (const pt of points) {
    pLat = getPosition(pt)[1];
    if (Number.isFinite(pLat)) {
      latMin = pLat < latMin ? pLat : latMin;
      latMax = pLat > latMax ? pLat : latMax;
    }
  }

  const centerLat = (latMin + latMax) / 2;

  const gridOffset = _calculateGridLatLonOffset(cellSize, centerLat);

  if (gridOffset.xOffset <= 0 || gridOffset.yOffset <= 0) {
    return {gridHash: {}, gridOffset};
  }

  // calculate count per cell
  const gridHash = {};
  for (const pt of points) {
    const [lng, lat] = getPosition(pt);

    if (Number.isFinite(lat) && Number.isFinite(lng)) {
      const latIdx = Math.floor((lat + 90) / gridOffset.yOffset);
      const lonIdx = Math.floor((lng + 180) / gridOffset.xOffset);
      const key = `${latIdx}-${lonIdx}`;

      gridHash[key] = gridHash[key] || {count: 0, points: []};
      gridHash[key].count += 1;
      gridHash[key].points.push(pt);
    }
  }

  return {gridHash, gridOffset};
}
function _pointsToGridHashing(points = [], cellSize, getPosition) {
  // find the geometric center of sample points
  let latMin = Infinity;
  let latMax = -Infinity;
  let pLat;

  for (const pt of points) {
    pLat = getPosition(pt)[1];
    if (Number.isFinite(pLat)) {
      latMin = pLat < latMin ? pLat : latMin;
      latMax = pLat > latMax ? pLat : latMax;
    }
  }

  const centerLat = (latMin + latMax) / 2;

  const gridOffset = _calculateGridLatLonOffset(cellSize, centerLat);

  if (gridOffset.xOffset <= 0 || gridOffset.yOffset <= 0) {
    return {gridHash: {}, gridOffset};
  }

  // calculate count per cell
  const gridHash = {};
  for (const pt of points) {
    const [lng, lat] = getPosition(pt);

    if (Number.isFinite(lat) && Number.isFinite(lng)) {
      const latIdx = Math.floor((lat + 90) / gridOffset.yOffset);
      const lonIdx = Math.floor((lng + 180) / gridOffset.xOffset);
      const key = `${latIdx}-${lonIdx}`;

      gridHash[key] = gridHash[key] || {count: 0, points: []};
      gridHash[key].count += 1;
      gridHash[key].points.push(pt);
    }
  }

  return {gridHash, gridOffset};
}
function _pointsToGridHashing ( points = [ ] , cellSize , getPosition ) { // find the geometric center of sample points let latMin = Infinity ; let latMax = - Infinity ; let pLat ; for ( const pt of points ) { pLat = getPosition ( pt ) [ 1 ] ; if ( Number . isFinite ( pLat ) ) { latMin = pLat < latMin ? pLat : latMin ; latMax = pLat > latMax ? pLat : latMax ; } } const centerLat = ( latMin + latMax ) / 2 ; const gridOffset = _calculateGridLatLonOffset ( cellSize , centerLat ) ; if ( gridOffset . xOffset <= 0 || gridOffset . yOffset <= 0 ) { return { gridHash : { } , gridOffset } ; } // calculate count per cell const gridHash = { } ; for ( const pt of points ) { const [ lng , lat ] = getPosition ( pt ) ; if ( Number . isFinite ( lat ) && Number . isFinite ( lng ) ) { const latIdx = Math . floor ( ( lat + 90 ) / gridOffset . yOffset ) ; const lonIdx = Math . floor ( ( lng + 180 ) / gridOffset . xOffset ) ; const key = ` ${ latIdx } ${ lonIdx } ` ; gridHash [ key ] = gridHash [ key ] || { count : 0 , points : [ ] } ; gridHash [ key ] . count += 1 ; gridHash [ key ] . points . push ( pt ) ; } } return { gridHash , gridOffset } ; }
Project points into each cell, return a hash table of cells
@param {Iterable} points
@param {number} cellSize - unit size in meters
@param {function} getPosition - position accessor
@returns {object} - grid hash and cell dimension
Project points into each cell return a hash table of cells