Example #1
0
  @Test
  public void testDecode() {
    // for all lat/lon pairs check decoded point is in the same bbox as the
    // geohash formed by encoder
    for (GeoHash gh : RandomGeohashes.fullRange()) {
      BoundingBox bbox = gh.getBoundingBox();
      GeoHash decodedHash = GeoHash.fromGeohashString(gh.toBase32());
      WGS84Point decodedCenter = decodedHash.getBoundingBoxCenterPoint();

      assertTrue(
          "bbox " + bbox + " should contain the decoded center value " + decodedCenter,
          bbox.contains(decodedCenter));
      BoundingBox decodedBoundingBox = decodedHash.getBoundingBox();
      assertEquals(bbox, decodedBoundingBox);
      assertEquals(gh, decodedHash);
      assertEquals(gh.toBase32(), decodedHash.toBase32());
    }
  }
Example #2
0
  @Test
  public void testStepsBetween() {
    GeoHash bl = GeoHash.withBitPrecision(37.7, -122.52, 35);
    GeoHash ur = GeoHash.withBitPrecision(37.84, -122.35, 35);

    long steps = GeoHash.stepsBetween(bl, bl);
    assertEquals(steps, 0);

    steps = GeoHash.stepsBetween(bl, bl.next(4));
    assertEquals(steps, 4);

    BoundingBoxGeoHashIterator iter =
        new BoundingBoxGeoHashIterator(new TwoGeoHashBoundingBox(bl, ur));
    int count = 0;
    while (iter.hasNext()) {
      iter.next();
      count++;
    }
    assertEquals(12875, count);

    int allHashes = 0;
    int inBbox = 1;
    int latMore = 0;
    int lonMore = 0;
    int bothMore = 0;
    int latLess = 0;
    int lonLess = 0;
    int bothLess = 0;
    int latLessLonMore = 0;
    int latMoreLonLess = 0;
    GeoHash idx = bl;
    BoundingBox iterBbox = iter.getBoundingBox().getBoundingBox();
    while (idx.compareTo(ur) < 0) {
      idx = idx.next();
      allHashes++;
      if (iterBbox.contains(idx.getPoint())) {
        inBbox++;
      }
      boolean latIsMore = false;
      boolean latIsLess = false;
      if (idx.getPoint().getLatitude() > iterBbox.getMaxLat()) {
        latIsMore = true;
        latMore++;
      } else if (idx.getPoint().getLatitude() < iterBbox.getMinLat()) {
        latIsLess = true;
        latLess++;
      }
      if (idx.getPoint().getLongitude() > iterBbox.getMaxLon()) {
        lonMore++;
        if (latIsMore) {
          bothMore++;
        }
        if (latIsLess) {
          latLessLonMore++;
        }
      } else if (idx.getPoint().getLongitude() < iterBbox.getMinLon()) {
        lonLess++;
        if (latIsLess) {
          bothLess++;
        }
        if (latIsMore) {
          latMoreLonLess++;
        }
      }
    }

    // Just trying to understand where these GeoHashes are with regard to
    // their bounding box.
    steps = GeoHash.stepsBetween(bl, ur);
    assertEquals(48472, steps);
    assertEquals(steps, allHashes);
    assertEquals(count, inBbox);
    assertEquals(14938, latMore);
    assertEquals(640, lonMore);
    assertEquals(0, bothMore);
    assertEquals(7680, latLess);
    assertEquals(24391, lonLess);
    assertEquals(0, bothLess);
    assertEquals(240, latLessLonMore);
    assertEquals(11811, latMoreLonLess);
    assertEquals(
        steps,
        lonLess + latLess + latMore + lonMore + inBbox - latLessLonMore - latMoreLonLess - 1);
  }