-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgeohash.c
61 lines (52 loc) · 1.49 KB
/
geohash.c
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
#include "geohash.h"
#include <stdlib.h>
#include <math.h>
struct RangeF {
double high;
double low;
};
#define SET_BIT(bits, mid, range, value, offset) \
mid = ((range)->high + (range)->low) / 2.0; \
if ((value) >= mid) { \
(range)->low = mid; \
(bits) |= (0x1 << (offset)); \
} else { \
(range)->high = mid; \
(bits) |= (0x0 << (offset)); \
}
static const char CHAR_MAP[32] = "0123456789bcdefghjkmnpqrstuvwxyz";
char* geohash_encode(double lat, double lon, int precision)
{
struct RangeF lat_range = { 90, -90 };
struct RangeF lon_range = { 180, -180 };
if (precision <= 0 || fabs(lat) > 90.0 || fabs(lon) > 180.0) {
return NULL;
}
char* hash = (char *)malloc(precision + 1);
if (hash == NULL) {
return NULL;
}
double val1 = lon;
struct RangeF *range1 = &lon_range;
double val2 = lat;
struct RangeF *range2 = &lat_range;
unsigned char bits = 0;
for (int i = 0; i < precision; i++) {
double mid;
bits = 0;
SET_BIT(bits, mid, range1, val1, 4);
SET_BIT(bits, mid, range2, val2, 3);
SET_BIT(bits, mid, range1, val1, 2);
SET_BIT(bits, mid, range2, val2, 1);
SET_BIT(bits, mid, range1, val1, 0);
hash[i] = CHAR_MAP[bits];
double val_tmp = val1;
val1 = val2;
val2 = val_tmp;
struct RangeF *range_tmp = range1;
range1 = range2;
range2 = range_tmp;
}
hash[precision] = '\0';
return hash;
}