@Test public void testEqualsAndHashCode() { GeoHash hash1 = GeoHash.withBitPrecision(30, 30, 24); GeoHash hash2 = GeoHash.withBitPrecision(30, 30, 24); GeoHash hash3 = GeoHash.withBitPrecision(30, 30, 10); assertTrue(hash1.equals(hash2) && hash2.equals(hash1)); assertFalse(hash1.equals(hash3) && hash3.equals(hash1)); assertEquals(hash1.hashCode(), hash2.hashCode()); assertFalse(hash1.hashCode() == hash3.hashCode()); }
@Test public void testGetLongitudeBits() { hash = GeoHash.withBitPrecision(30, 30, 16); long[] longitudeBits = hash.getRightAlignedLongitudeBits(); assertEquals(0x95l, longitudeBits[0]); assertEquals(8, longitudeBits[1]); }
@Test public void testNext() { double lat = 37.7; double lon = -122.52; GeoHash hash = GeoHash.withBitPrecision(lat, lon, 10); GeoHash next = hash.next(); assertTrue(hash.compareTo(next) < 0); }
@Test public void testConstructorWithBitPrecision() { GeoHash hash1 = GeoHash.withBitPrecision(45, 120, 20); assertEquals(hash1.significantBits, 20); System.out.println(hash1); System.out.println(hash1.toBase32()); GeoHash hash2 = GeoHash.withBitPrecision(45, 120, 55); assertEquals(hash2.significantBits, 55); System.out.println(hash2); System.out.println(hash2.toBase32()); assertTrue(hash2.within(hash1)); // this should match Dave Troys Codebase. This is also his maximum // accuracy (12 5-nibbles). GeoHash hash3 = GeoHash.withBitPrecision(20, 31, 60); assertEquals("sew1c2vs2q5r", hash3.toBase32()); System.out.println("--------------"); String hash4 = new GeoHash().geoHashStringWithCharacterPrecision(45, 120, 64); System.out.println(hash4); }
@Test public void testNextPrev() { double lat = 37.7; double lon = -122.52; GeoHash hash = GeoHash.withBitPrecision(lat, lon, 35); GeoHash next = hash.next(2); assertTrue(hash.compareTo(next) < 0); GeoHash prev1 = next.prev(); GeoHash prev2 = prev1.next(-1); assertTrue(prev1.compareTo(next) < 0); System.out.println("hash: " + hash.toBase32()); System.out.println("next: " + next.toBase32()); System.out.println("prev1: " + prev1.toBase32()); System.out.println("prev2: " + prev2.toBase32()); assertTrue(prev2.compareTo(prev1) < 0); assertTrue(prev2.compareTo(hash) == 0); }
private void checkMoveAroundStrip(String direction) throws Exception { for (int bits = 2; bits < 16; bits++) { double randomLatitude = (rand.nextDouble() - 0.5) * 180; double randomLongitude = (rand.nextDouble() - 0.5) * 360; // this divides the range by 2^bits GeoHash hash = GeoHash.withBitPrecision(randomLatitude, randomLongitude, bits); Method method = hash.getClass().getDeclaredMethod("get" + direction + "Neighbour"); GeoHash result = hash; // moving this direction 2^bits times should yield the same hash // again for (int i = 0; i < Math.pow(2, bits); i++) { result = (GeoHash) method.invoke(result); } assertEquals(hash, result); } }
@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); }
@Test public void testGetCharacterPrecisionWorksWhenPrecisionIsMultipleOfFive() throws Exception { GeoHash hash = GeoHash.withBitPrecision(37.7, -122.52, 60); int precision = hash.getCharacterPrecision(); assertEquals(precision, 12); }
@Test(expected = IllegalStateException.class) public void testGetCharacterPrecisionThrows() throws Exception { GeoHash hash = GeoHash.withBitPrecision(37.7, -122.52, 32); hash.getCharacterPrecision(); }
@Test public void testSimpleWithin() { GeoHash hash = GeoHash.withBitPrecision(70, -120, 8); GeoHash inside = GeoHash.withBitPrecision(74, -130, 64); assertWithin(inside, hash); }
@Test public void testLatLonBoundingBoxes() { hash = GeoHash.withBitPrecision(40, 120, 10); System.out.println(hash.toBase32()); printBoundingBox(hash); }