コード例 #1
0
ファイル: GeoHash.java プロジェクト: wenhao/geohash
 private static void divideRangeDecode(GeoHash geoHash, double[] range, boolean isOnBit) {
   double mid = (range[0] + range[1]) / 2;
   if (isOnBit) {
     geoHash.addOnBitToEnd();
     range[0] = mid;
   } else {
     geoHash.addOffBitToEnd();
     range[1] = mid;
   }
 }
コード例 #2
0
ファイル: GeoHash.java プロジェクト: wenhao/geohash
 private static void divideRangeEncode(GeoHash geoHash, double value, double[] range) {
   double mid = (range[0] + range[1]) / 2;
   if (value >= mid) {
     geoHash.addOnBitToEnd();
     range[0] = mid;
   } else {
     geoHash.addOffBitToEnd();
     range[1] = mid;
   }
 }
コード例 #3
0
ファイル: GeoHash.java プロジェクト: wenhao/geohash
  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;
  }
コード例 #4
0
ファイル: GeoHash.java プロジェクト: wenhao/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;
  }
コード例 #5
0
ファイル: GeoHash.java プロジェクト: wenhao/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;
  }
コード例 #6
0
ファイル: GeoHash.java プロジェクト: wenhao/geohash
 public List<GeoHash> getAdjacent() {
   GeoHash northern = getNorthernNeighbour();
   GeoHash eastern = getEasternNeighbour();
   GeoHash southern = getSouthernNeighbour();
   GeoHash western = getWesternNeighbour();
   return Arrays.asList(
       northern,
       northern.getEasternNeighbour(),
       eastern,
       southern.getEasternNeighbour(),
       southern,
       southern.getWesternNeighbour(),
       western,
       northern.getWesternNeighbour(),
       this);
 }