-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo-hashing.cpp
140 lines (123 loc) · 3.5 KB
/
geo-hashing.cpp
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
/*************************************************************************
> File Name: geo-hashing.cpp
> Author: chenshaoyi
> Mail: [email protected]
> Created Time: 一 12/26 10:06:20 2016
************************************************************************/
#include<iostream>
#include<map>
#include<string>
using namespace std;
struct Position{
double latitude;
double longitude;
};
class GeoHasher{
private:
map<int, char> binary2char_dict_;
map<char, int> char2binary_dict_;
public:
GeoHasher();
//precision is the length of hashed string
string Encode(Position& pos, int precision);
Position Decode(string& hashstr);
};
GeoHasher::GeoHasher(){
for(int i=0; i<10; i++){
binary2char_dict_[i] = '0' + i;
char2binary_dict_['0'+i] = i;
}
char c;
int cnt=10;
for(int i=0; i<26; i++){
c = 'a' + i;
if('a'==c||'i'==c||'l'==c||'o'==c)
continue;
binary2char_dict_[cnt] = c;
char2binary_dict_[c] = cnt;
cnt ++;
}
}
string GeoHasher::Encode(Position& pos, int precision){
int length = precision * 5;
string hashstr="";
double latitude_range[2] ={-90.0, 90.0};
double longitude_range[2] ={-180.0, 180.0};
double mid;
int bits=0;
for(int i=0; i<length; i++){
if(i%2==1){ // latitude
mid = (latitude_range[0] + latitude_range[1])/2;
if(pos.latitude<mid){
bits <<= 1;
latitude_range[1] = mid;
}else{
bits <<= 1;
bits |= 1;
latitude_range[0] = mid;
}
}else{// longitude
mid = (longitude_range[0] + longitude_range[1])/2;
if(pos.longitude<mid){
bits <<= 1;
longitude_range[1] = mid;
}else{
bits <<= 1;
bits |= 1;
longitude_range[0] = mid;
}
}
if((i+1)%5==0){
hashstr += binary2char_dict_[bits];
bits = 0;
}
}
return hashstr;
}
Position GeoHasher::Decode(string& hashstr){
Position pos;
double latitude_range[2] ={-90.0, 90.0};
double longitude_range[2] ={-180.0, 180.0};
double mid;
int bits=0;
int bit;
int islatitude=false;
for(int i=0; i<hashstr.size(); i++){
bits = char2binary_dict_[hashstr[i]];
for(int j=4; j>=0; j--){
bit = (bits>>j)&1;
if(islatitude){
mid = (latitude_range[0] + latitude_range[1])/2;
if(bit) latitude_range[0] = mid;
else latitude_range[1] = mid;
}else{
mid = (longitude_range[0] + longitude_range[1])/2;
if(bit) longitude_range[0] = mid;
else longitude_range[1] = mid;
}
islatitude = !islatitude;
}
}
pos.latitude = (latitude_range[0]+latitude_range[1])/2;
pos.longitude = (longitude_range[0]+longitude_range[1])/2;
return pos;
}
int main(){
GeoHasher hasher;
Position pos;
string hashstr;
int m, n;
cin >> m >> n;
while(m--){
cin >> pos.latitude >> pos.longitude;
cout << hasher.Encode(pos, 10) << endl;
}
cout.setf(ios::fixed);
cout.precision(6);
while(n--){
cin >> hashstr;
pos = hasher.Decode(hashstr);
cout << pos.latitude << " " << pos.longitude << endl;
}
return 0;
}