Skip to content

Commit 0293732

Browse files
committed
Refactored new geohash functions
1 parent 70330af commit 0293732

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

C/include/Geohash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
static const int GEOHASH_MAX_PRECISION = 12;
77

8-
uint64_t GeohashEncodeU64(double lat, double lon);
8+
uint64_t GeohashEncodeU64(double lat, double lon, int prec);
99
int GeohashComparePointsU64(double lon1, double lat1, double lon2, double lat2, int precision);
1010

1111
void GeohashDecode(const char* str,

C/src/Coordinates.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ CoordFilterByGeoHash(std::vector<geopoint_t> &lstSrc,
5151

5252
int idx = 0;
5353
for (auto ci = lstSrc.begin(); ci != lstSrc.end(); ++ci) {
54-
uint64_t gh = GeohashEncodeU64(ci->Latitude, ci->Longitude);
55-
gh >>= precision*5 + 4;
56-
54+
uint64_t gh = GeohashEncodeU64(ci->Latitude, ci->Longitude, precision);
5755
dctIter it = dctHashCount.find(gh);
5856
if (it == dctHashCount.end()) {
5957
AuxItem ni;

C/src/Geohash.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ static uint64_t interleave(uint64_t x, uint64_t y) {
2828
}
2929
///////////////////////////////////////////////////////
3030

31-
uint64_t GeohashEncodeU64(double lat, double lon) {
31+
uint64_t GeohashEncodeU64(double lat, double lon, int prec) {
3232
lat = lat/180.0 + 1.5;
3333
lon = lon/360.0 + 1.5;
3434
uint64_t ilat = *((uint64_t*)&lat);
@@ -37,17 +37,14 @@ uint64_t GeohashEncodeU64(double lat, double lon) {
3737
ilon >>= 20;
3838
ilat &= 0x00000000ffffffff;
3939
ilon &= 0x00000000ffffffff;
40-
41-
return interleave(ilat, ilon);
40+
return interleave(ilat, ilon) >> (GEOHASH_MAX_PRECISION-prec)*5;
4241
}
4342

4443
int GeohashComparePointsU64(double lon1, double lat1,
4544
double lon2, double lat2,
4645
int precision) {
4746
assert(precision >= 1 && precision <= GEOHASH_MAX_PRECISION);
48-
uint64_t gh1 = GeohashEncodeU64(lat1, lon1);
49-
uint64_t gh2 = GeohashEncodeU64(lat2, lon2);
50-
gh1 >>= precision*5 + 4;
51-
gh2 >>= precision*5 + 4;
47+
uint64_t gh1 = GeohashEncodeU64(lat1, lon1, precision);
48+
uint64_t gh2 = GeohashEncodeU64(lat2, lon2, precision);
5249
return gh1 - gh2;
5350
}

C/src/mainwindow.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,13 @@ static double filterDistanceRealTime(const std::vector<geopoint_t> &lst,
117117

118118
laGeo.Latitude = laGeo.Longitude = COORD_NOT_INITIALIZED;
119119

120-
*tgh0 = GeohashEncodeU64(pi->Latitude, pi->Longitude);
120+
*tgh0 = GeohashEncodeU64(pi->Latitude, pi->Longitude, prec);
121121
tmpGeo.Latitude = pi->Latitude;
122122
tmpGeo.Longitude = pi->Longitude;
123123
count = 1;
124124

125-
*tgh >>= prec*5 + 4;
126125
for (++pi; pi != lst.end(); ++pi) {
127-
*tgh = GeohashEncodeU64(pi->Latitude, pi->Longitude);
128-
*tgh >>= prec*5 + 4;
126+
*tgh = GeohashEncodeU64(pi->Latitude, pi->Longitude, prec);
129127

130128
//if (ppCompGeohash != ppReadGeohash)
131129
if (*tgh - *tgh0) {

C/tests/src/GeohashTest.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ typedef struct TestEncodeItem {
2020
const char *expected;
2121
} TestEncodeItem_t;
2222

23-
static const char *geohash_str(uint64_t geohash, int8_t prec) ;
23+
static const char *geohash_str(uint64_t geohash) ;
2424

2525
static void tesnEncodeU64() {
2626
double lat = 27.988056;
2727
double lon = 86.925278;
2828
uint64_t exp = 0xceb7f254240fd612;
29-
uint64_t act = GeohashEncodeU64(lat, lon);
29+
uint64_t act = GeohashEncodeU64(lat, lon, GEOHASH_MAX_PRECISION);
3030
assert(exp == act);
3131
//tuvz4p141zc1
32-
std::cout << geohash_str(act, GEOHASH_MAX_PRECISION) << std::endl;
32+
std::cout << geohash_str(act) << std::endl;
3333
}
3434
///////////////////////////////////////////////////////
3535

@@ -39,15 +39,13 @@ static const char base32Table[BASE32_COUNT] = {'0', '1', '2', '3', '4', '5', '6'
3939
'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r',
4040
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
4141

42-
const char *geohash_str(uint64_t geohash, int8_t prec) {
42+
const char *geohash_str(uint64_t geohash) {
4343
static char buff[GEOHASH_MAX_PRECISION+1] = {0};
44-
char *str = buff + prec + 1;
45-
int idx;
44+
char *str = buff + GEOHASH_MAX_PRECISION + 1;
4645
*str = 0;
47-
geohash >>= ((GEOHASH_MAX_PRECISION-prec) * 5) + 4; //cause we don't need last 4 bits
48-
while (prec--){
49-
idx = geohash & 0x1f;
50-
*--str = base32Table[idx];
46+
geohash >>= 4; //cause we don't need last 4 bits. that's strange, I thought we don't need first 4 bits %)
47+
while (geohash){
48+
*--str = base32Table[geohash & 0x1f];
5149
geohash >>= 5;
5250
}
5351
return str;
@@ -86,14 +84,14 @@ static void testEncode() {
8684
TestEncodeItem_t *tmp;
8785

8886
for (tmp = posTests; tmp->expected; ++tmp) {
89-
uint64_t geohash = GeohashEncodeU64(tmp->lat, tmp->lon);
90-
const char *geostr = geohash_str(geohash, tmp->precision);
87+
uint64_t geohash = GeohashEncodeU64(tmp->lat, tmp->lon, tmp->precision);
88+
const char *geostr = geohash_str(geohash);
9189
assert(strcmp(geostr, tmp->expected) == 0);
9290
}
9391

9492
for (tmp = negTests; tmp->expected; ++tmp) {
95-
uint64_t geohash = GeohashEncodeU64(tmp->lat, tmp->lon);
96-
const char *geostr = geohash_str(geohash, tmp->precision);
93+
uint64_t geohash = GeohashEncodeU64(tmp->lat, tmp->lon, tmp->precision);
94+
const char *geostr = geohash_str(geohash);
9795
assert(strcmp(geostr, tmp->expected) != 0);
9896
}
9997
}

0 commit comments

Comments
 (0)