2014-05-12 14:38:17 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
|
|
|
|
* Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
|
Multiple GEORADIUS bugs fixed.
By grepping the continuous integration errors log a number of GEORADIUS
tests failures were detected.
Fortunately when a GEORADIUS failure happens, the test suite logs enough
information in order to reproduce the problem: the PRNG seed,
coordinates and radius of the query.
By reproducing the issues, three different bugs were discovered and
fixed in this commit. This commit also improves the already good
reporting of the fuzzer and adds the failure vectors as regression
tests.
The issues found:
1. We need larger squares around the poles in order to cover the area
requested by the user. There were already checks in order to use a
smaller step (larger squares) but the limit set (+/- 67 degrees) is not
enough in certain edge cases, so 66 is used now.
2. Even near the equator, when the search area center is very near the
edge of the square, the north, south, west or ovest square may not be
able to fully cover the specified radius. Now a test is performed at the
edge of the initial guessed search area, and larger squares are used in
case the test fails.
3. Because of rounding errors between Redis and Tcl, sometimes the test
signaled false positives. This is now addressed.
Whenever possible the original code was improved a bit in other ways. A
debugging example stanza was added in order to make the next debugging
session simpler when the next bug is found.
2016-07-27 05:07:23 -04:00
|
|
|
* Copyright (c) 2015-2016, Salvatore Sanfilippo <antirez@gmail.com>.
|
2014-05-12 14:38:17 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
#include "geohash.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hashing works like this:
|
|
|
|
* Divide the world into 4 buckets. Label each one as such:
|
|
|
|
* -----------------
|
|
|
|
* | | |
|
|
|
|
* | | |
|
|
|
|
* | 0,1 | 1,1 |
|
|
|
|
* -----------------
|
|
|
|
* | | |
|
|
|
|
* | | |
|
|
|
|
* | 0,0 | 1,0 |
|
|
|
|
* -----------------
|
|
|
|
*/
|
|
|
|
|
2015-07-01 10:12:08 -04:00
|
|
|
/* Interleave lower bits of x and y, so the bits of x
|
|
|
|
* are in the even positions and bits from y in the odd;
|
2022-04-04 08:35:49 -04:00
|
|
|
* x and y must initially be less than 2**32 (4294967296).
|
2015-07-01 10:12:08 -04:00
|
|
|
* From: https://graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN
|
|
|
|
*/
|
|
|
|
static inline uint64_t interleave64(uint32_t xlo, uint32_t ylo) {
|
2015-07-09 05:27:53 -04:00
|
|
|
static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL,
|
|
|
|
0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
|
|
|
|
0x0000FFFF0000FFFFULL};
|
2015-07-01 10:12:08 -04:00
|
|
|
static const unsigned int S[] = {1, 2, 4, 8, 16};
|
|
|
|
|
|
|
|
uint64_t x = xlo;
|
|
|
|
uint64_t y = ylo;
|
|
|
|
|
|
|
|
x = (x | (x << S[4])) & B[4];
|
|
|
|
y = (y | (y << S[4])) & B[4];
|
|
|
|
|
|
|
|
x = (x | (x << S[3])) & B[3];
|
|
|
|
y = (y | (y << S[3])) & B[3];
|
|
|
|
|
|
|
|
x = (x | (x << S[2])) & B[2];
|
|
|
|
y = (y | (y << S[2])) & B[2];
|
|
|
|
|
|
|
|
x = (x | (x << S[1])) & B[1];
|
|
|
|
y = (y | (y << S[1])) & B[1];
|
|
|
|
|
|
|
|
x = (x | (x << S[0])) & B[0];
|
|
|
|
y = (y | (y << S[0])) & B[0];
|
|
|
|
|
|
|
|
return x | (y << 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reverse the interleave process
|
|
|
|
* derived from http://stackoverflow.com/questions/4909263
|
|
|
|
*/
|
|
|
|
static inline uint64_t deinterleave64(uint64_t interleaved) {
|
2015-07-09 05:27:53 -04:00
|
|
|
static const uint64_t B[] = {0x5555555555555555ULL, 0x3333333333333333ULL,
|
|
|
|
0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
|
|
|
|
0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL};
|
2015-07-01 10:12:08 -04:00
|
|
|
static const unsigned int S[] = {0, 1, 2, 4, 8, 16};
|
|
|
|
|
|
|
|
uint64_t x = interleaved;
|
|
|
|
uint64_t y = interleaved >> 1;
|
|
|
|
|
|
|
|
x = (x | (x >> S[0])) & B[0];
|
|
|
|
y = (y | (y >> S[0])) & B[0];
|
|
|
|
|
|
|
|
x = (x | (x >> S[1])) & B[1];
|
|
|
|
y = (y | (y >> S[1])) & B[1];
|
|
|
|
|
|
|
|
x = (x | (x >> S[2])) & B[2];
|
|
|
|
y = (y | (y >> S[2])) & B[2];
|
|
|
|
|
|
|
|
x = (x | (x >> S[3])) & B[3];
|
|
|
|
y = (y | (y >> S[3])) & B[3];
|
|
|
|
|
|
|
|
x = (x | (x >> S[4])) & B[4];
|
|
|
|
y = (y | (y >> S[4])) & B[4];
|
|
|
|
|
|
|
|
x = (x | (x >> S[5])) & B[5];
|
|
|
|
y = (y | (y >> S[5])) & B[5];
|
|
|
|
|
|
|
|
return x | (y << 32);
|
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
void geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range) {
|
2015-06-22 05:02:27 -04:00
|
|
|
/* These are constraints from EPSG:900913 / EPSG:3785 / OSGEO:41001 */
|
|
|
|
/* We can't geocode at the north/south pole. */
|
2015-07-06 12:39:25 -04:00
|
|
|
long_range->max = GEO_LONG_MAX;
|
|
|
|
long_range->min = GEO_LONG_MIN;
|
|
|
|
lat_range->max = GEO_LAT_MAX;
|
|
|
|
lat_range->min = GEO_LAT_MIN;
|
2015-06-22 05:02:27 -04:00
|
|
|
}
|
|
|
|
|
Multiple GEORADIUS bugs fixed.
By grepping the continuous integration errors log a number of GEORADIUS
tests failures were detected.
Fortunately when a GEORADIUS failure happens, the test suite logs enough
information in order to reproduce the problem: the PRNG seed,
coordinates and radius of the query.
By reproducing the issues, three different bugs were discovered and
fixed in this commit. This commit also improves the already good
reporting of the fuzzer and adds the failure vectors as regression
tests.
The issues found:
1. We need larger squares around the poles in order to cover the area
requested by the user. There were already checks in order to use a
smaller step (larger squares) but the limit set (+/- 67 degrees) is not
enough in certain edge cases, so 66 is used now.
2. Even near the equator, when the search area center is very near the
edge of the square, the north, south, west or ovest square may not be
able to fully cover the specified radius. Now a test is performed at the
edge of the initial guessed search area, and larger squares are used in
case the test fails.
3. Because of rounding errors between Redis and Tcl, sometimes the test
signaled false positives. This is now addressed.
Whenever possible the original code was improved a bit in other ways. A
debugging example stanza was added in order to make the next debugging
session simpler when the next bug is found.
2016-07-27 05:07:23 -04:00
|
|
|
int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range,
|
2015-06-25 12:05:45 -04:00
|
|
|
double longitude, double latitude, uint8_t step,
|
2015-06-22 05:02:27 -04:00
|
|
|
GeoHashBits *hash) {
|
2015-07-06 12:39:25 -04:00
|
|
|
/* Check basic arguments sanity. */
|
|
|
|
if (hash == NULL || step > 32 || step == 0 ||
|
|
|
|
RANGEPISZERO(lat_range) || RANGEPISZERO(long_range)) return 0;
|
|
|
|
|
|
|
|
/* Return an error when trying to index outside the supported
|
|
|
|
* constraints. */
|
2018-09-02 03:06:20 -04:00
|
|
|
if (longitude > GEO_LONG_MAX || longitude < GEO_LONG_MIN ||
|
|
|
|
latitude > GEO_LAT_MAX || latitude < GEO_LAT_MIN) return 0;
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
hash->bits = 0;
|
|
|
|
hash->step = step;
|
|
|
|
|
|
|
|
if (latitude < lat_range->min || latitude > lat_range->max ||
|
|
|
|
longitude < long_range->min || longitude > long_range->max) {
|
2015-06-22 05:02:27 -04:00
|
|
|
return 0;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-07-01 10:12:08 -04:00
|
|
|
double lat_offset =
|
|
|
|
(latitude - lat_range->min) / (lat_range->max - lat_range->min);
|
|
|
|
double long_offset =
|
|
|
|
(longitude - long_range->min) / (long_range->max - long_range->min);
|
|
|
|
|
|
|
|
/* convert to fixed point based on the step size */
|
2018-06-12 03:28:28 -04:00
|
|
|
lat_offset *= (1ULL << step);
|
|
|
|
long_offset *= (1ULL << step);
|
2015-07-01 10:12:08 -04:00
|
|
|
hash->bits = interleave64(lat_offset, long_offset);
|
2015-06-22 05:02:27 -04:00
|
|
|
return 1;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
int geohashEncodeType(double longitude, double latitude, uint8_t step, GeoHashBits *hash) {
|
2016-07-06 10:02:38 -04:00
|
|
|
GeoHashRange r[2] = {{0}};
|
2015-06-22 05:02:27 -04:00
|
|
|
geohashGetCoordRange(&r[0], &r[1]);
|
2015-06-25 12:05:45 -04:00
|
|
|
return geohashEncode(&r[0], &r[1], longitude, latitude, step, hash);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
int geohashEncodeWGS84(double longitude, double latitude, uint8_t step,
|
2015-06-22 05:02:27 -04:00
|
|
|
GeoHashBits *hash) {
|
2015-06-25 12:05:45 -04:00
|
|
|
return geohashEncodeType(longitude, latitude, step, hash);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
int geohashDecode(const GeoHashRange long_range, const GeoHashRange lat_range,
|
2014-05-12 14:38:17 -04:00
|
|
|
const GeoHashBits hash, GeoHashArea *area) {
|
|
|
|
if (HASHISZERO(hash) || NULL == area || RANGEISZERO(lat_range) ||
|
|
|
|
RANGEISZERO(long_range)) {
|
2015-06-22 05:02:27 -04:00
|
|
|
return 0;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
area->hash = hash;
|
2015-07-01 10:12:08 -04:00
|
|
|
uint8_t step = hash.step;
|
|
|
|
uint64_t hash_sep = deinterleave64(hash.bits); /* hash = [LAT][LONG] */
|
|
|
|
|
|
|
|
double lat_scale = lat_range.max - lat_range.min;
|
|
|
|
double long_scale = long_range.max - long_range.min;
|
|
|
|
|
|
|
|
uint32_t ilato = hash_sep; /* get lat part of deinterleaved hash */
|
|
|
|
uint32_t ilono = hash_sep >> 32; /* shift over to get long part of hash */
|
|
|
|
|
|
|
|
/* divide by 2**step.
|
|
|
|
* Then, for 0-1 coordinate, multiply times scale and add
|
|
|
|
to the min to get the absolute coordinate. */
|
|
|
|
area->latitude.min =
|
|
|
|
lat_range.min + (ilato * 1.0 / (1ull << step)) * lat_scale;
|
|
|
|
area->latitude.max =
|
|
|
|
lat_range.min + ((ilato + 1) * 1.0 / (1ull << step)) * lat_scale;
|
|
|
|
area->longitude.min =
|
|
|
|
long_range.min + (ilono * 1.0 / (1ull << step)) * long_scale;
|
|
|
|
area->longitude.max =
|
|
|
|
long_range.min + ((ilono + 1) * 1.0 / (1ull << step)) * long_scale;
|
|
|
|
|
2015-06-22 05:02:27 -04:00
|
|
|
return 1;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-22 05:02:27 -04:00
|
|
|
int geohashDecodeType(const GeoHashBits hash, GeoHashArea *area) {
|
2016-07-06 10:02:38 -04:00
|
|
|
GeoHashRange r[2] = {{0}};
|
2015-06-22 05:02:27 -04:00
|
|
|
geohashGetCoordRange(&r[0], &r[1]);
|
2014-05-12 14:38:17 -04:00
|
|
|
return geohashDecode(r[0], r[1], hash, area);
|
|
|
|
}
|
|
|
|
|
2015-06-22 05:02:27 -04:00
|
|
|
int geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area) {
|
|
|
|
return geohashDecodeType(hash, area);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
int geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy) {
|
|
|
|
if (!xy) return 0;
|
|
|
|
xy[0] = (area->longitude.min + area->longitude.max) / 2;
|
2020-04-09 05:48:29 -04:00
|
|
|
if (xy[0] > GEO_LONG_MAX) xy[0] = GEO_LONG_MAX;
|
|
|
|
if (xy[0] < GEO_LONG_MIN) xy[0] = GEO_LONG_MIN;
|
2015-06-25 12:05:45 -04:00
|
|
|
xy[1] = (area->latitude.min + area->latitude.max) / 2;
|
2020-04-09 05:48:29 -04:00
|
|
|
if (xy[1] > GEO_LAT_MAX) xy[1] = GEO_LAT_MAX;
|
|
|
|
if (xy[1] < GEO_LAT_MIN) xy[1] = GEO_LAT_MIN;
|
2015-06-22 05:02:27 -04:00
|
|
|
return 1;
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
int geohashDecodeToLongLatType(const GeoHashBits hash, double *xy) {
|
2016-07-06 10:02:38 -04:00
|
|
|
GeoHashArea area = {{0}};
|
2015-06-25 12:05:45 -04:00
|
|
|
if (!xy || !geohashDecodeType(hash, &area))
|
2015-06-22 05:02:27 -04:00
|
|
|
return 0;
|
2015-06-25 12:05:45 -04:00
|
|
|
return geohashDecodeAreaToLongLat(&area, xy);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:05:45 -04:00
|
|
|
int geohashDecodeToLongLatWGS84(const GeoHashBits hash, double *xy) {
|
|
|
|
return geohashDecodeToLongLatType(hash, xy);
|
2014-05-12 14:38:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void geohash_move_x(GeoHashBits *hash, int8_t d) {
|
|
|
|
if (d == 0)
|
|
|
|
return;
|
|
|
|
|
2015-07-09 05:27:53 -04:00
|
|
|
uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaULL;
|
|
|
|
uint64_t y = hash->bits & 0x5555555555555555ULL;
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2015-07-09 05:27:53 -04:00
|
|
|
uint64_t zz = 0x5555555555555555ULL >> (64 - hash->step * 2);
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
if (d > 0) {
|
|
|
|
x = x + (zz + 1);
|
|
|
|
} else {
|
|
|
|
x = x | zz;
|
|
|
|
x = x - (zz + 1);
|
|
|
|
}
|
|
|
|
|
2015-07-09 05:27:53 -04:00
|
|
|
x &= (0xaaaaaaaaaaaaaaaaULL >> (64 - hash->step * 2));
|
2014-05-12 14:38:17 -04:00
|
|
|
hash->bits = (x | y);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void geohash_move_y(GeoHashBits *hash, int8_t d) {
|
|
|
|
if (d == 0)
|
|
|
|
return;
|
|
|
|
|
2015-07-09 05:27:53 -04:00
|
|
|
uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaULL;
|
|
|
|
uint64_t y = hash->bits & 0x5555555555555555ULL;
|
2014-05-12 14:38:17 -04:00
|
|
|
|
2015-07-09 05:27:53 -04:00
|
|
|
uint64_t zz = 0xaaaaaaaaaaaaaaaaULL >> (64 - hash->step * 2);
|
2014-05-12 14:38:17 -04:00
|
|
|
if (d > 0) {
|
|
|
|
y = y + (zz + 1);
|
|
|
|
} else {
|
|
|
|
y = y | zz;
|
|
|
|
y = y - (zz + 1);
|
|
|
|
}
|
2015-07-09 05:27:53 -04:00
|
|
|
y &= (0x5555555555555555ULL >> (64 - hash->step * 2));
|
2014-05-12 14:38:17 -04:00
|
|
|
hash->bits = (x | y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors) {
|
|
|
|
neighbors->east = *hash;
|
|
|
|
neighbors->west = *hash;
|
|
|
|
neighbors->north = *hash;
|
|
|
|
neighbors->south = *hash;
|
|
|
|
neighbors->south_east = *hash;
|
|
|
|
neighbors->south_west = *hash;
|
|
|
|
neighbors->north_east = *hash;
|
|
|
|
neighbors->north_west = *hash;
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->east, 1);
|
|
|
|
geohash_move_y(&neighbors->east, 0);
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->west, -1);
|
|
|
|
geohash_move_y(&neighbors->west, 0);
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->south, 0);
|
|
|
|
geohash_move_y(&neighbors->south, -1);
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->north, 0);
|
|
|
|
geohash_move_y(&neighbors->north, 1);
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->north_west, -1);
|
|
|
|
geohash_move_y(&neighbors->north_west, 1);
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->north_east, 1);
|
|
|
|
geohash_move_y(&neighbors->north_east, 1);
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->south_east, 1);
|
|
|
|
geohash_move_y(&neighbors->south_east, -1);
|
|
|
|
|
|
|
|
geohash_move_x(&neighbors->south_west, -1);
|
|
|
|
geohash_move_y(&neighbors->south_west, -1);
|
|
|
|
}
|