-
Notifications
You must be signed in to change notification settings - Fork 1
/
Geohash.cs
145 lines (124 loc) · 4.15 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ViewModels
{
public class Geohash
{
private static int numbits = 5 * 5;
static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
static Dictionary<char, int> lookup = new Dictionary<char, int>();
static Geohash()
{
int i = 0;
foreach (char c in digits)
lookup[c] = i++;
}
public static double[] decode(string geohash)
{
string buffer = string.Empty;
foreach (char c in geohash.ToCharArray())
{
int i = lookup[c] + 32;
buffer += Convert.ToString(i, 2).Substring(1);
}
ObservableCollection<bool> lonset = new ObservableCollection<bool>();
ObservableCollection<bool> latset = new ObservableCollection<bool>();
for (int i = 0; i < 64; i++)
{
lonset.Add(false);
latset.Add(false);
}
//even bits
int j = 0;
for (int i = 0; i < numbits * 2; i += 2)
{
bool isSet = false;
if (i < buffer.Length)
isSet = buffer.ToCharArray()[i] == '1';
lonset.Insert(j++, isSet);
}
//odd bits
j = 0;
for (int i = 1; i < numbits * 2; i += 2)
{
bool isSet = false;
if (i < buffer.Length)
isSet = buffer.ToCharArray()[i] == '1';
latset.Insert(j++, isSet);
}
double lon = decode(lonset, -180, 180);
double lat = decode(latset, -90, 90);
return new double[] { lat, lon };
}
private static double decode(ObservableCollection<bool> lonset, double floor, double ceiling)
{
double mid = 0;
for (int i = 0; i < lonset.Count; i++)
{
mid = (floor + ceiling) / 2;
if (lonset[i])
floor = mid;
else
ceiling = mid;
}
return mid;
}
public static String encode(double lat, double lon)
{
ObservableCollection<bool> latbits = getBits(lat, -90, 90);
ObservableCollection<bool> lonbits = getBits(lon, -180, 180);
string buffer = string.Empty;
for (int i = 0; i < numbits; i++)
{
buffer += ((lonbits[i]) ? '1' : '0');
buffer += ((latbits[i]) ? '1' : '0');
}
return base32(Convert.ToInt64(buffer, 2));
}
private static ObservableCollection<bool> getBits(double lat, double floor, double ceiling)
{
ObservableCollection<bool> buffer = new ObservableCollection<bool>();
for (int i = 0; i < 64; i++)
{
buffer.Add(false);
}
for (int i = 0; i < numbits; i++)
{
double mid = (floor + ceiling) / 2;
if (lat >= mid)
{
buffer.Insert(i, true);
floor = mid;
}
else
{
ceiling = mid;
}
}
return buffer;
}
public static String base32(long i)
{
char[] buf = new char[54];
int charPos = 53;
bool negative = (i < 0);
if (!negative)
i = -i;
while (i <= -32)
{
buf[charPos--] = digits[(int)(-(i % 32))];
i /= 32;
}
buf[charPos] = digits[(int)(-i)];
if (negative)
buf[--charPos] = '-';
return new String(buf, charPos, (54 - charPos));
}
}
}