2014-05-12 14:38:17 -04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
|
|
|
|
* Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
|
2015-06-29 10:34:02 -04:00
|
|
|
* Copyright (c) 2015, 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef GEOHASH_H_
|
|
|
|
#define GEOHASH_H_
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2015-06-22 05:02:27 -04:00
|
|
|
#include <stdint.h>
|
2014-05-12 14:38:17 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HASHISZERO(r) (!(r).bits && !(r).step)
|
|
|
|
#define RANGEISZERO(r) (!(r).max && !(r).min)
|
|
|
|
#define RANGEPISZERO(r) (r == NULL || RANGEISZERO(*r))
|
|
|
|
|
2015-07-06 12:39:25 -04:00
|
|
|
#define GEO_STEP_MAX 26 /* 26*2 = 52 bits. */
|
|
|
|
|
|
|
|
/* Limits from EPSG:900913 / EPSG:3785 / OSGEO:41001 */
|
|
|
|
#define GEO_LAT_MIN -85.05112878
|
|
|
|
#define GEO_LAT_MAX 85.05112878
|
|
|
|
#define GEO_LONG_MIN -180
|
|
|
|
#define GEO_LONG_MAX 180
|
2014-05-12 14:38:17 -04:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
GEOHASH_NORTH = 0,
|
|
|
|
GEOHASH_EAST,
|
|
|
|
GEOHASH_WEST,
|
|
|
|
GEOHASH_SOUTH,
|
|
|
|
GEOHASH_SOUTH_WEST,
|
|
|
|
GEOHASH_SOUTH_EAST,
|
|
|
|
GEOHASH_NORT_WEST,
|
|
|
|
GEOHASH_NORT_EAST
|
|
|
|
} GeoDirection;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint64_t bits;
|
|
|
|
uint8_t step;
|
|
|
|
} GeoHashBits;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
double min;
|
2015-06-25 12:05:45 -04:00
|
|
|
double max;
|
2014-05-12 14:38:17 -04:00
|
|
|
} GeoHashRange;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GeoHashBits hash;
|
|
|
|
GeoHashRange longitude;
|
2015-06-25 12:05:45 -04:00
|
|
|
GeoHashRange latitude;
|
2014-05-12 14:38:17 -04:00
|
|
|
} GeoHashArea;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
GeoHashBits north;
|
|
|
|
GeoHashBits east;
|
|
|
|
GeoHashBits west;
|
|
|
|
GeoHashBits south;
|
|
|
|
GeoHashBits north_east;
|
|
|
|
GeoHashBits south_east;
|
|
|
|
GeoHashBits north_west;
|
|
|
|
GeoHashBits south_west;
|
|
|
|
} GeoHashNeighbors;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 0:success
|
|
|
|
* -1:failed
|
|
|
|
*/
|
2015-06-25 12:05:45 -04:00
|
|
|
void geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range);
|
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-06-25 12:05:45 -04:00
|
|
|
int geohashEncodeType(double longitude, double latitude,
|
2015-06-22 05:02:27 -04:00
|
|
|
uint8_t step, GeoHashBits *hash);
|
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
|
|
|
int geohashDecode(const GeoHashRange long_range, const GeoHashRange lat_range,
|
2015-06-22 05:02:27 -04:00
|
|
|
const GeoHashBits hash, GeoHashArea *area);
|
|
|
|
int geohashDecodeType(const GeoHashBits hash, GeoHashArea *area);
|
|
|
|
int geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area);
|
2015-06-25 12:05:45 -04:00
|
|
|
int geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy);
|
|
|
|
int geohashDecodeToLongLatType(const GeoHashBits hash, double *xy);
|
|
|
|
int geohashDecodeToLongLatWGS84(const GeoHashBits hash, double *xy);
|
|
|
|
int geohashDecodeToLongLatMercator(const GeoHashBits hash, double *xy);
|
2014-05-12 14:38:17 -04:00
|
|
|
void geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors);
|
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* GEOHASH_H_ */
|