Home > Back-end >  How could I replace fixed conditions to get clustering EPS for some calculation?
How could I replace fixed conditions to get clustering EPS for some calculation?

Time:01-28

I use a function of DBSCAN for clustering, that takes the EPS value as a parameter (radius that defines which points will be grouped together).

To set the EPS I use the following switch case:

    let eps = 0;
    switch (true) {
      case (zoom === 20):
        eps = 0.00001;
        break;
      case (zoom > 18):
        eps = 0.00002;
        break;
      case (zoom > 16):
        eps = 0.0003;
        break;
      case (zoom > 14):
        eps = 0.0014;
        break;
      case (zoom > 12):
        eps = 0.0024;
        break;
      case (zoom > 10):
        eps = 0.0082;
        break;
      case (zoom > 8):
        eps = 0.032;
        break;
      case (zoom > 6):
        eps = 0.2;
        break;
      case (zoom > 4):
        eps = 0.45;
        break;
      default:
        eps = 3;
    }

How could I replace this switch case with fixed values ​​and conditions for some calculation?

I use this method to get current map zoom:

const handleRegionChange = ({ longitudeDelta, latitudeDelta }: Region) => {
    const zoomLevel = Math.log(360 / Math.max(longitudeDelta, latitudeDelta)) / Math.LN2;

    if (zoomLevel !== zoom) {
      setZoom(Math.ceil(zoomLevel));
    }
};

this method always returns a value from 1 to 20.

CodePudding user response:

Use the convenient fact that the comparison operand decreases by 2 each time to calculate the index into a lookup array:

function getEpsFromZoom(zoom) {
  if (zoom >= 20) {
    return 0.00001;
  } else {
    const table = [ 3, 0.45, 0.2, 0.032, 0.0082, 0.0024, 0.0014, 0.0003, 0.00002 ];
    const index = Math.ceil(zoom/2);
    const clamp = Math.min(10, Math.max(2, index));
    return table[clamp - 2];
  }
}

Unfortunately 20 has to be handled separately here since its case condition is not a strict inequality like the others.

  • Related