-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeoHash.cs
98 lines (74 loc) · 3.29 KB
/
GeoHash.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using com.surfm.firebase.geofire.util;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace com.surfm.firebase.geofire.core {
public class GeoHash {
private readonly string geoHash;
// The default precision of a geohash
private static readonly int DEFAULT_PRECISION = 10;
// The maximal precision of a geohash
public static readonly int MAX_PRECISION = 22;
// The maximal number of bits precision for a geohash
public static readonly int MAX_PRECISION_BITS = MAX_PRECISION * Base32Utils.BITS_PER_BASE32_CHAR;
public GeoHash(double latitude, double longitude) : this(latitude, longitude, DEFAULT_PRECISION) {
}
public GeoHash(GeoLocation location): this(location.latitude, location.longitude, DEFAULT_PRECISION) {
}
public GeoHash(double latitude, double longitude, int precision) {
if (precision < 1) {
throw new System.Exception("Precision of GeoHash must be larger than zero!");
}
if (precision > MAX_PRECISION) {
throw new System.Exception("Precision of a GeoHash must be less than " + (MAX_PRECISION + 1) + "!");
}
if (!GeoLocation.coordinatesValid(latitude, longitude)) {
throw new System.Exception(string.Format("Not valid location coordinates: [{0}, {1}]", latitude, longitude));
}
double[] longitudeRange = { -180, 180 };
double[] latitudeRange = { -90, 90 };
char[] buffer = new char[precision];
for (int i = 0; i < precision; i++) {
int hashValue = 0;
for (int j = 0; j < Base32Utils.BITS_PER_BASE32_CHAR; j++) {
bool even = (((i * Base32Utils.BITS_PER_BASE32_CHAR) + j) % 2) == 0;
double val = even ? longitude : latitude;
double[] range = even ? longitudeRange : latitudeRange;
double mid = (range[0] + range[1]) / 2;
if (val > mid) {
hashValue = (hashValue << 1) + 1;
range[0] = mid;
} else {
hashValue = (hashValue << 1);
range[1] = mid;
}
}
buffer[i] = Base32Utils.valueToBase32Char(hashValue);
}
this.geoHash = new string(buffer);
}
public GeoHash(string hash) {
if (hash.Length == 0 || !Base32Utils.isValidBase32string(hash)) {
throw new System.Exception("Not a valid geoHash: " + hash);
}
this.geoHash = hash;
}
public string getGeoHashString() {
return this.geoHash;
}
public override bool Equals(object o) {
if (this == o) return true;
if (o == null ||GetType() != o.GetType()) return false;
GeoHash other = (GeoHash)o;
return this.geoHash.Equals(other.geoHash);
}
public override string ToString() {
return "GeoHash{" +
"geoHash='" + geoHash + '\'' +
'}';
}
public override int GetHashCode() {
return this.geoHash.GetHashCode();
}
}
}