Zone.byXY constructor

Zone.byXY(
  1. double x,
  2. double y,
  3. int level
)

Implementation

factory Zone.byXY(double x, double y, int level) {
  final h_size = calcHexSize(level);
  var h_x = x.round();
  var h_y = y.round();

  final h_lat = (h_k * h_x * (6 * h_size) + h_y * (6 * h_size * h_k)) / 2;
  final h_lon = (h_lat - h_y * (6 * h_size * h_k)) / h_k;

  final z_loc = xy2loc(h_lon, h_lat);
  var z_loc_x = z_loc.lon;
  final z_loc_y = z_loc.lat;
  final max_hsteps = math.pow(3, level + 2).toDouble();
  final hsteps = ((h_x - h_y)).abs().toDouble();

  if (hsteps == max_hsteps) {
    if (h_x > h_y) {
      final tmp = h_x;
      h_x = h_y;
      h_y = tmp;
    }
    z_loc_x = -180;
  }

  final h_code = StringBuffer();
  final code3_x = <int>[];
  final code3_y = <int>[];

  var mod_x = h_x;
  var mod_y = h_y;

  for (var i = 0; i <= level + 2; i++) {
    final h_pow = math.pow(3, level + 2 - i).round();
    final h_pow_half = (h_pow / 2).ceilToDouble();
    if (mod_x >= h_pow_half) {
      code3_x.add(2);
      mod_x -= h_pow;
    } else if (mod_x <= -h_pow_half) {
      code3_x.add(0);
      mod_x += h_pow;
    } else {
      code3_x.add(1);
    }

    if (mod_y >= h_pow_half) {
      code3_y.add(2);
      mod_y -= h_pow;
    } else if (mod_y <= -h_pow_half) {
      code3_y.add(0);
      mod_y += h_pow;
    } else {
      code3_y.add(1);
    }

    if (i == 2 && (z_loc_x == -180 || z_loc_x >= 0)) {
      if (code3_x[0] == 2 &&
          code3_y[0] == 1 &&
          code3_x[1] == code3_y[1] &&
          code3_x[2] == code3_y[2]) {
        code3_x[0] = 1;
        code3_y[0] = 2;
      } else if (code3_x[0] == 1 &&
          code3_y[0] == 0 &&
          code3_x[1] == code3_y[1] &&
          code3_x[2] == code3_y[2]) {
        code3_x[0] = 0;
        code3_y[0] = 1;
      }
    }
  }

  for (var i = 0; i < code3_x.length; i++) {
    final c3 = '${code3_x[i]}${code3_y[i]}';
    final c9 = '${int.parse(c3, radix: 3)}';
    h_code.write(c9);
  }

  final h_2 = h_code.toString().substring(3);
  final h_1 = int.parse(h_code.toString().substring(0, 3));
  final h_a1 = (h_1 / 30).floor();
  final h_a2 = h_1 % 30;

  return Zone(z_loc_y, z_loc_x, h_x, h_y, '${hKey[h_a1]}${hKey[h_a2]}$h_2');
}