public static GeoHash fromCoordinate(double latitude, double longitude, int precision) { GeoHash geoHash = new GeoHash(); geoHash.coordinate = new Coordinate(latitude, longitude); boolean isEvenBit = true; double[] latitudeRange = {-90, 90}; double[] longitudeRange = {-180, 180}; while (geoHash.significantBits < precision) { if (isEvenBit) { divideRangeEncode(geoHash, longitude, longitudeRange); } else { divideRangeEncode(geoHash, latitude, latitudeRange); } isEvenBit = !isEvenBit; } geoHash.bits <<= (MAX_PRECISION - precision); return geoHash; }
public static GeoHash fromLong(long longValue, int significantBits) { double[] latitudeRange = {-90.0, 90.0}; double[] longitudeRange = {-180.0, 180.0}; boolean isEvenBit = true; GeoHash geoHash = new GeoHash(); String binaryString = Long.toBinaryString(longValue); while (binaryString.length() < MAX_PRECISION) { binaryString = "0" + binaryString; } for (int j = 0; j < significantBits; j++) { if (isEvenBit) { divideRangeDecode(geoHash, longitudeRange, binaryString.charAt(j) != '0'); } else { divideRangeDecode(geoHash, latitudeRange, binaryString.charAt(j) != '0'); } isEvenBit = !isEvenBit; } double latitude = (latitudeRange[0] + latitudeRange[1]) / 2; double longitude = (longitudeRange[0] + longitudeRange[1]) / 2; geoHash.coordinate = new Coordinate(latitude, longitude); geoHash.bits <<= (MAX_PRECISION - geoHash.significantBits); return geoHash; }
private GeoHash recombineLatLonBitsToHash(long[] latBits, long[] lonBits) { GeoHash geoHash = new GeoHash(); boolean isEvenBit = false; latBits[0] <<= (MAX_PRECISION - latBits[1]); lonBits[0] <<= (MAX_PRECISION - lonBits[1]); double[] latitudeRange = {-90.0, 90.0}; double[] longitudeRange = {-180.0, 180.0}; for (int i = 0; i < latBits[1] + lonBits[1]; i++) { if (isEvenBit) { divideRangeDecode( geoHash, latitudeRange, (latBits[0] & FIRST_BIT_FLAGGED) == FIRST_BIT_FLAGGED); latBits[0] <<= 1; } else { divideRangeDecode( geoHash, longitudeRange, (lonBits[0] & FIRST_BIT_FLAGGED) == FIRST_BIT_FLAGGED); lonBits[0] <<= 1; } isEvenBit = !isEvenBit; } geoHash.bits <<= (MAX_PRECISION - geoHash.significantBits); geoHash.coordinate = getCenterCoordinate(latitudeRange, longitudeRange); return geoHash; }