summaryrefslogtreecommitdiff
path: root/deps
diff options
context:
space:
mode:
authorMatt Stancliff <matt@genges.com>2014-05-12 14:38:17 -0400
committerantirez <antirez@gmail.com>2015-06-22 09:07:13 +0200
commit7f4ac3d19c28e0a7a608fe94411e92bc59097e11 (patch)
tree03da237a4b47c189462d79ca915fa828a11bf680 /deps
parent821a986643717018cad8af9f35cba49818e60294 (diff)
downloadredis-7f4ac3d19c28e0a7a608fe94411e92bc59097e11.tar.gz
[In-Progress] Add Geo Commands
Current todo: - replace functions in zset.{c,h} with a new unified Redis zset access API. Once we get the zset interface fixed, we can squash relevant commits in this branch and have one nice commit to merge into unstable. This commit adds: - Geo commands - Tests; runnable with: ./runtest --single unit/geo - Geo helpers in deps/geohash-int/ - src/geo.{c,h} and src/geojson.{c,h} implementing geo commands - Updated build configurations to get everything working - TEMPORARY: src/zset.{c,h} implementing zset score and zset range reading without writing to client output buffers. - Modified linkage of one t_zset.c function for use in zset.c Conflicts: src/Makefile src/redis.c
Diffstat (limited to 'deps')
-rw-r--r--deps/Makefile7
-rw-r--r--deps/geohash-int/Makefile23
-rw-r--r--deps/geohash-int/geohash.c290
-rw-r--r--deps/geohash-int/geohash.h120
-rw-r--r--deps/geohash-int/geohash_helper.c279
-rw-r--r--deps/geohash-int/geohash_helper.h78
6 files changed, 797 insertions, 0 deletions
diff --git a/deps/Makefile b/deps/Makefile
index 71f6d3a2c..10ae6e790 100644
--- a/deps/Makefile
+++ b/deps/Makefile
@@ -36,6 +36,7 @@ distclean:
-(cd hiredis && $(MAKE) clean) > /dev/null || true
-(cd linenoise && $(MAKE) clean) > /dev/null || true
-(cd lua && $(MAKE) clean) > /dev/null || true
+ -(cd geohash-int && $(MAKE) clean) > /dev/null || true
-(cd jemalloc && [ -f Makefile ] && $(MAKE) distclean) > /dev/null || true
-(rm -f .make-*)
@@ -81,3 +82,9 @@ jemalloc: .make-prerequisites
cd jemalloc && $(MAKE) CFLAGS="$(JEMALLOC_CFLAGS)" LDFLAGS="$(JEMALLOC_LDFLAGS)" lib/libjemalloc.a
.PHONY: jemalloc
+
+geohash-int: .make-prerequisites
+ @printf '%b %b\n' $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR)
+ cd geohash-int && $(MAKE)
+
+.PHONY: geohash-int
diff --git a/deps/geohash-int/Makefile b/deps/geohash-int/Makefile
new file mode 100644
index 000000000..bf9eaebb8
--- /dev/null
+++ b/deps/geohash-int/Makefile
@@ -0,0 +1,23 @@
+STD=
+WARN= -Wall
+OPT= -Ofast
+
+R_CFLAGS= $(STD) $(WARN) $(OPT) $(DEBUG) $(CFLAGS)
+R_LDFLAGS= $(LDFLAGS)
+DEBUG= -g
+
+R_CC=$(CC) $(R_CFLAGS)
+R_LD=$(CC) $(R_LDFLAGS)
+
+all: geohash.o geohash_helper.o
+
+.PHONY: all
+
+geohash.o: geohash.h geohash.c
+geohash_helper.o: geohash.h geohash_helper.h geohash_helper.c
+
+.c.o:
+ $(R_CC) -c $<
+
+clean:
+ rm -f *.o
diff --git a/deps/geohash-int/geohash.c b/deps/geohash-int/geohash.c
new file mode 100644
index 000000000..9212736bf
--- /dev/null
+++ b/deps/geohash-int/geohash.c
@@ -0,0 +1,290 @@
+/*
+ * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
+ * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
+ * 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 |
+ * -----------------
+ */
+
+bool geohashGetCoordRange(uint8_t coord_type, GeoHashRange *lat_range,
+ GeoHashRange *long_range) {
+ switch (coord_type) {
+ case GEO_WGS84_TYPE: {
+ /* These are constraints from EPSG:900913 / EPSG:3785 / OSGEO:41001 */
+ /* We can't geocode at the north/south pole. */
+ lat_range->max = 85.05112878;
+ lat_range->min = -85.05112878;
+ long_range->max = 180.0;
+ long_range->min = -180.0;
+ break;
+ }
+ case GEO_MERCATOR_TYPE: {
+ lat_range->max = 20037726.37;
+ lat_range->min = -20037726.37;
+ long_range->max = 20037726.37;
+ long_range->min = -20037726.37;
+ break;
+ }
+ default: { return false; }
+ }
+ return true;
+}
+
+bool geohashEncode(GeoHashRange *lat_range, GeoHashRange *long_range,
+ double latitude, double longitude, uint8_t step,
+ GeoHashBits *hash) {
+ uint8_t i;
+
+ if (NULL == hash || step > 32 || step == 0 || RANGEPISZERO(lat_range) ||
+ RANGEPISZERO(long_range)) {
+ return false;
+ }
+
+ hash->bits = 0;
+ hash->step = step;
+
+ if (latitude < lat_range->min || latitude > lat_range->max ||
+ longitude < long_range->min || longitude > long_range->max) {
+ return false;
+ }
+
+ for (i = 0; i < step; i++) {
+ uint8_t lat_bit, long_bit;
+
+ if (lat_range->max - latitude >= latitude - lat_range->min) {
+ lat_bit = 0;
+ lat_range->max = (lat_range->max + lat_range->min) / 2;
+ } else {
+ lat_bit = 1;
+ lat_range->min = (lat_range->max + lat_range->min) / 2;
+ }
+ if (long_range->max - longitude >= longitude - long_range->min) {
+ long_bit = 0;
+ long_range->max = (long_range->max + long_range->min) / 2;
+ } else {
+ long_bit = 1;
+ long_range->min = (long_range->max + long_range->min) / 2;
+ }
+
+ hash->bits <<= 1;
+ hash->bits += long_bit;
+ hash->bits <<= 1;
+ hash->bits += lat_bit;
+ }
+ return true;
+}
+
+bool geohashEncodeType(uint8_t coord_type, double latitude, double longitude,
+ uint8_t step, GeoHashBits *hash) {
+ GeoHashRange r[2] = { { 0 } };
+ geohashGetCoordRange(coord_type, &r[0], &r[1]);
+ return geohashEncode(&r[0], &r[1], latitude, longitude, step, hash);
+}
+
+bool geohashEncodeWGS84(double latitude, double longitude, uint8_t step,
+ GeoHashBits *hash) {
+ return geohashEncodeType(GEO_WGS84_TYPE, latitude, longitude, step, hash);
+}
+
+bool geohashEncodeMercator(double latitude, double longitude, uint8_t step,
+ GeoHashBits *hash) {
+ return geohashEncodeType(GEO_MERCATOR_TYPE, latitude, longitude, step,
+ hash);
+}
+
+static inline uint8_t get_bit(uint64_t bits, uint8_t pos) {
+ return (bits >> pos) & 0x01;
+}
+
+bool geohashDecode(const GeoHashRange lat_range, const GeoHashRange long_range,
+ const GeoHashBits hash, GeoHashArea *area) {
+ uint8_t i;
+
+ if (HASHISZERO(hash) || NULL == area || RANGEISZERO(lat_range) ||
+ RANGEISZERO(long_range)) {
+ return false;
+ }
+
+ area->hash = hash;
+ area->latitude.min = lat_range.min;
+ area->latitude.max = lat_range.max;
+ area->longitude.min = long_range.min;
+ area->longitude.max = long_range.max;
+
+ for (i = 0; i < hash.step; i++) {
+ uint8_t lat_bit, long_bit;
+
+ long_bit = get_bit(hash.bits, (hash.step - i) * 2 - 1);
+ lat_bit = get_bit(hash.bits, (hash.step - i) * 2 - 2);
+
+ if (lat_bit == 0) {
+ area->latitude.max = (area->latitude.max + area->latitude.min) / 2;
+ } else {
+ area->latitude.min = (area->latitude.max + area->latitude.min) / 2;
+ }
+
+ if (long_bit == 0) {
+ area->longitude.max =
+ (area->longitude.max + area->longitude.min) / 2;
+ } else {
+ area->longitude.min =
+ (area->longitude.max + area->longitude.min) / 2;
+ }
+ }
+ return true;
+}
+
+bool geohashDecodeType(uint8_t coord_type, const GeoHashBits hash,
+ GeoHashArea *area) {
+ GeoHashRange r[2] = { { 0 } };
+ geohashGetCoordRange(coord_type, &r[0], &r[1]);
+ return geohashDecode(r[0], r[1], hash, area);
+}
+
+bool geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area) {
+ return geohashDecodeType(GEO_WGS84_TYPE, hash, area);
+}
+
+bool geohashDecodeMercator(const GeoHashBits hash, GeoHashArea *area) {
+ return geohashDecodeType(GEO_MERCATOR_TYPE, hash, area);
+}
+
+bool geohashDecodeAreaToLatLong(const GeoHashArea *area, double *latlong) {
+ double y, x;
+
+ if (!latlong)
+ return false;
+
+ y = (area->latitude.min + area->latitude.max) / 2;
+ x = (area->longitude.min + area->longitude.max) / 2;
+
+ latlong[0] = y;
+ latlong[1] = x;
+ return true;
+}
+
+bool geohashDecodeToLatLongType(uint8_t coord_type, const GeoHashBits hash,
+ double *latlong) {
+ GeoHashArea area = { { 0 } };
+ if (!latlong || !geohashDecodeType(coord_type, hash, &area))
+ return false;
+ return geohashDecodeAreaToLatLong(&area, latlong);
+}
+
+bool geohashDecodeToLatLongWGS84(const GeoHashBits hash, double *latlong) {
+ return geohashDecodeToLatLongType(GEO_WGS84_TYPE, hash, latlong);
+}
+
+bool geohashDecodeToLatLongMercator(const GeoHashBits hash, double *latlong) {
+ return geohashDecodeToLatLongType(GEO_MERCATOR_TYPE, hash, latlong);
+}
+
+static void geohash_move_x(GeoHashBits *hash, int8_t d) {
+ if (d == 0)
+ return;
+
+ uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaLL;
+ uint64_t y = hash->bits & 0x5555555555555555LL;
+
+ uint64_t zz = 0x5555555555555555LL >> (64 - hash->step * 2);
+
+ if (d > 0) {
+ x = x + (zz + 1);
+ } else {
+ x = x | zz;
+ x = x - (zz + 1);
+ }
+
+ x &= (0xaaaaaaaaaaaaaaaaLL >> (64 - hash->step * 2));
+ hash->bits = (x | y);
+}
+
+static void geohash_move_y(GeoHashBits *hash, int8_t d) {
+ if (d == 0)
+ return;
+
+ uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaLL;
+ uint64_t y = hash->bits & 0x5555555555555555LL;
+
+ uint64_t zz = 0xaaaaaaaaaaaaaaaaLL >> (64 - hash->step * 2);
+ if (d > 0) {
+ y = y + (zz + 1);
+ } else {
+ y = y | zz;
+ y = y - (zz + 1);
+ }
+ y &= (0x5555555555555555LL >> (64 - hash->step * 2));
+ 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);
+}
diff --git a/deps/geohash-int/geohash.h b/deps/geohash-int/geohash.h
new file mode 100644
index 000000000..30fc17144
--- /dev/null
+++ b/deps/geohash-int/geohash.h
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
+ * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
+ * 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>
+#include <stdbool.h>
+#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))
+
+#define GEO_WGS84_TYPE 1
+#define GEO_MERCATOR_TYPE 2
+
+#define GEO_STEP_MAX 26
+
+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 max;
+ double min;
+} GeoHashRange;
+
+typedef struct {
+ GeoHashBits hash;
+ GeoHashRange latitude;
+ GeoHashRange longitude;
+} 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
+ */
+bool geohashGetCoordRange(uint8_t coord_type, GeoHashRange *lat_range,
+ GeoHashRange *long_range);
+bool geohashEncode(GeoHashRange *lat_range, GeoHashRange *long_range,
+ double latitude, double longitude, uint8_t step,
+ GeoHashBits *hash);
+bool geohashEncodeType(uint8_t coord_type, double latitude, double longitude,
+ uint8_t step, GeoHashBits *hash);
+bool geohashEncodeMercator(double latitude, double longitude, uint8_t step,
+ GeoHashBits *hash);
+bool geohashEncodeWGS84(double latitude, double longitude, uint8_t step,
+ GeoHashBits *hash);
+bool geohashDecode(const GeoHashRange lat_range, const GeoHashRange long_range,
+ const GeoHashBits hash, GeoHashArea *area);
+bool geohashDecodeType(uint8_t coord_type, const GeoHashBits hash,
+ GeoHashArea *area);
+bool geohashDecodeMercator(const GeoHashBits hash, GeoHashArea *area);
+bool geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area);
+bool geohashDecodeAreaToLatLong(const GeoHashArea *area, double *latlong);
+bool geohashDecodeToLatLongType(uint8_t coord_type, const GeoHashBits hash,
+ double *latlong);
+bool geohashDecodeToLatLongWGS84(const GeoHashBits hash, double *latlong);
+bool geohashDecodeToLatLongMercator(const GeoHashBits hash, double *latlong);
+void geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors);
+
+#if defined(__cplusplus)
+}
+#endif
+#endif /* GEOHASH_H_ */
diff --git a/deps/geohash-int/geohash_helper.c b/deps/geohash-int/geohash_helper.c
new file mode 100644
index 000000000..010ab070b
--- /dev/null
+++ b/deps/geohash-int/geohash_helper.c
@@ -0,0 +1,279 @@
+/*
+ * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
+ * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
+ * 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.
+ */
+
+/* This is a C++ to C conversion from the ardb project.
+ * This file started out as:
+ * https://github.com/yinqiwen/ardb/blob/d42503/src/geo/geohash_helper.cpp
+ */
+
+#include "geohash_helper.h"
+
+#define D_R (M_PI / 180.0)
+#define R_MAJOR 6378137.0
+#define R_MINOR 6356752.3142
+#define RATIO (R_MINOR / R_MAJOR)
+#define ECCENT (sqrt(1.0 - (RATIO *RATIO)))
+#define COM (0.5 * ECCENT)
+
+/// @brief The usual PI/180 constant
+const double DEG_TO_RAD = 0.017453292519943295769236907684886;
+/// @brief Earth's quatratic mean radius for WGS-84
+const double EARTH_RADIUS_IN_METERS = 6372797.560856;
+
+const double MERCATOR_MAX = 20037726.37;
+const double MERCATOR_MIN = -20037726.37;
+
+static inline double deg_rad(double ang) { return ang * D_R; }
+static inline double rad_deg(double ang) { return ang / D_R; }
+
+double mercator_y(double lat) {
+ lat = fmin(89.5, fmax(lat, -89.5));
+ double phi = deg_rad(lat);
+ double sinphi = sin(phi);
+ double con = ECCENT * sinphi;
+ con = pow((1.0 - con) / (1.0 + con), COM);
+ double ts = tan(0.5 * (M_PI * 0.5 - phi)) / con;
+ return 0 - R_MAJOR * log(ts);
+}
+
+double mercator_x(double lon) { return R_MAJOR * deg_rad(lon); }
+double merc_lon(double x) { return rad_deg(x) / R_MAJOR; }
+
+double merc_lat(double y) {
+ double ts = exp(-y / R_MAJOR);
+ double phi = M_PI_2 - 2 * atan(ts);
+ double dphi = 1.0;
+ int i;
+ for (i = 0; fabs(dphi) > 0.000000001 && i < 15; i++) {
+ double con = ECCENT * sin(phi);
+ dphi =
+ M_PI_2 - 2 * atan(ts * pow((1.0 - con) / (1.0 + con), COM)) - phi;
+ phi += dphi;
+ }
+ return rad_deg(phi);
+}
+
+/* You must *ONLY* estimate steps when you are encoding.
+ * If you are decoding, always decode to GEO_STEP_MAX (26). */
+uint8_t geohashEstimateStepsByRadius(double range_meters) {
+ uint8_t step = 1;
+ while (range_meters > 0 && range_meters < MERCATOR_MAX) {
+ range_meters *= 2;
+ step++;
+ }
+ step--;
+ if (!step)
+ step = 26; /* if range = 0, give max resolution */
+ return step > 26 ? 26 : step;
+}
+
+double geohashGetXWGS84(double x) { return merc_lon(x); }
+double geohashGetYWGS84(double y) { return merc_lat(y); }
+
+double geohashGetXMercator(double longtitude) {
+ if (longtitude > 180 || longtitude < -180) {
+ return longtitude;
+ }
+ return mercator_x(longtitude);
+}
+double geohashGetYMercator(double latitude) {
+ if (latitude > 90 || latitude < -90) {
+ return latitude;
+ }
+ return mercator_y(latitude);
+}
+
+int geohashBitsComparator(const GeoHashBits *a, const GeoHashBits *b) {
+ /* If step not equal, compare on step. Else, compare on bits. */
+ return a->step != b->step ? a->step - b->step : a->bits - b->bits;
+}
+
+bool geohashBoundingBox(double latitude, double longitude, double radius_meters,
+ double *bounds) {
+ if (!bounds)
+ return false;
+
+ double latr, lonr;
+ latr = deg_rad(latitude);
+ lonr = deg_rad(longitude);
+
+ double distance = radius_meters / EARTH_RADIUS_IN_METERS;
+ double min_latitude = latr - distance;
+ double max_latitude = latr + distance;
+
+ /* Note: we're being lazy and not accounting for coordinates near poles */
+ double min_longitude, max_longitude;
+ double difference_longitude = asin(sin(distance) / cos(latr));
+ min_longitude = lonr - difference_longitude;
+ max_longitude = lonr + difference_longitude;
+
+ bounds[0] = rad_deg(min_latitude);
+ bounds[1] = rad_deg(min_longitude);
+ bounds[2] = rad_deg(max_latitude);
+ bounds[3] = rad_deg(max_longitude);
+
+ return true;
+}
+
+GeoHashRadius geohashGetAreasByRadius(uint8_t coord_type, double latitude,
+ double longitude, double radius_meters) {
+ GeoHashRange lat_range, long_range;
+ GeoHashRadius radius = { { 0 } };
+ GeoHashBits hash = { 0 };
+ GeoHashNeighbors neighbors = { { 0 } };
+ GeoHashArea area = { { 0 } };
+ double delta_longitude, delta_latitude;
+ double min_lat, max_lat, min_lon, max_lon;
+ int steps;
+
+ if (coord_type == GEO_WGS84_TYPE) {
+ double bounds[4];
+ geohashBoundingBox(latitude, longitude, radius_meters, bounds);
+ min_lat = bounds[0];
+ min_lon = bounds[1];
+ max_lat = bounds[2];
+ max_lon = bounds[3];
+ } else {
+ delta_latitude = delta_longitude = radius_meters;
+ min_lat = latitude - delta_latitude;
+ max_lat = latitude + delta_latitude;
+ min_lon = longitude - delta_longitude;
+ max_lon = longitude + delta_longitude;
+ }
+
+ steps = geohashEstimateStepsByRadius(radius_meters);
+
+ geohashGetCoordRange(coord_type, &lat_range, &long_range);
+ geohashEncode(&lat_range, &long_range, latitude, longitude, steps, &hash);
+ geohashNeighbors(&hash, &neighbors);
+ geohashDecode(lat_range, long_range, hash, &area);
+
+ if (area.latitude.min < min_lat) {
+ GZERO(neighbors.south);
+ GZERO(neighbors.south_west);
+ GZERO(neighbors.south_east);
+ }
+ if (area.latitude.max > max_lat) {
+ GZERO(neighbors.north);
+ GZERO(neighbors.north_east);
+ GZERO(neighbors.north_west);
+ }
+ if (area.longitude.min < min_lon) {
+ GZERO(neighbors.west);
+ GZERO(neighbors.south_west);
+ GZERO(neighbors.north_west);
+ }
+ if (area.longitude.max > max_lon) {
+ GZERO(neighbors.east);
+ GZERO(neighbors.south_east);
+ GZERO(neighbors.north_east);
+ }
+ radius.hash = hash;
+ radius.neighbors = neighbors;
+ radius.area = area;
+ return radius;
+}
+
+GeoHashRadius geohashGetAreasByRadiusWGS84(double latitude, double longitude,
+ double radius_meters) {
+ return geohashGetAreasByRadius(GEO_WGS84_TYPE, latitude, longitude,
+ radius_meters);
+}
+
+GeoHashRadius geohashGetAreasByRadiusMercator(double latitude, double longitude,
+ double radius_meters) {
+ return geohashGetAreasByRadius(GEO_MERCATOR_TYPE, latitude, longitude,
+ radius_meters);
+}
+
+GeoHashFix52Bits geohashAlign52Bits(const GeoHashBits hash) {
+ uint64_t bits = hash.bits;
+ bits <<= (52 - hash.step * 2);
+ return bits;
+}
+
+/* calculate distance using haversin great circle distance formula */
+double distanceEarth(double lat1d, double lon1d, double lat2d, double lon2d) {
+ double lat1r, lon1r, lat2r, lon2r, u, v;
+ lat1r = deg_rad(lat1d);
+ lon1r = deg_rad(lon1d);
+ lat2r = deg_rad(lat2d);
+ lon2r = deg_rad(lon2d);
+ u = sin((lat2r - lat1r) / 2);
+ v = sin((lon2r - lon1r) / 2);
+ return 2.0 * EARTH_RADIUS_IN_METERS *
+ asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
+}
+
+bool geohashGetDistanceIfInRadius(uint8_t coord_type, double x1, double y1,
+ double x2, double y2, double radius,
+ double *distance) {
+ if (coord_type == GEO_WGS84_TYPE) {
+ *distance = distanceEarth(y1, x1, y2, x2);
+ if (*distance > radius) {
+ return false;
+ }
+ } else {
+ double xx = (x1 - x2) * (x1 - x2);
+ double yy = (y1 - y2) * (y1 - y2);
+ double dd = xx + yy;
+ *distance = dd;
+ if (dd > (radius * radius)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool geohashGetDistanceIfInRadiusWGS84(double x1, double y1, double x2,
+ double y2, double radius,
+ double *distance) {
+ return geohashGetDistanceIfInRadius(GEO_WGS84_TYPE, x1, y1, x2, y2, radius,
+ distance);
+}
+
+bool geohashGetDistanceSquaredIfInRadiusMercator(double x1, double y1,
+ double x2, double y2,
+ double radius,
+ double *distance) {
+ return geohashGetDistanceIfInRadius(GEO_MERCATOR_TYPE, x1, y1, x2, y2,
+ radius, distance);
+}
+
+bool geohashVerifyCoordinates(uint8_t coord_type, double x, double y) {
+ GeoHashRange lat_range, long_range;
+ geohashGetCoordRange(coord_type, &lat_range, &long_range);
+
+ if (x < long_range.min || x > long_range.max || y < lat_range.min ||
+ y > lat_range.max) {
+ return false;
+ }
+ return true;
+}
diff --git a/deps/geohash-int/geohash_helper.h b/deps/geohash-int/geohash_helper.h
new file mode 100644
index 000000000..b72e51442
--- /dev/null
+++ b/deps/geohash-int/geohash_helper.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
+ * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
+ * 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_HELPER_HPP_
+#define GEOHASH_HELPER_HPP_
+
+#include <math.h>
+#include <stdbool.h>
+#include "geohash.h"
+
+#define GZERO(s) s.bits = s.step = 0;
+#define GISZERO(s) (!s.bits && !s.step)
+#define GISNOTZERO(s) (s.bits || s.step)
+
+typedef uint64_t GeoHashFix52Bits;
+typedef uint64_t GeoHashVarBits;
+
+typedef struct {
+ GeoHashBits hash;
+ GeoHashArea area;
+ GeoHashNeighbors neighbors;
+} GeoHashRadius;
+
+int GeoHashBitsComparator(const GeoHashBits *a, const GeoHashBits *b);
+uint8_t geohashEstimateStepsByRadius(double range_meters);
+bool geohashBoundingBox(double latitude, double longitude, double radius_meters,
+ double *bounds);
+GeoHashRadius geohashGetAreasByRadius(uint8_t coord_type, double latitude,
+ double longitude, double radius_meters);
+GeoHashRadius geohashGetAreasByRadiusWGS84(double latitude, double longitude,
+ double radius_meters);
+GeoHashRadius geohashGetAreasByRadiusMercator(double latitude, double longitude,
+ double radius_meters);
+GeoHashFix52Bits geohashAlign52Bits(const GeoHashBits hash);
+double geohashGetXMercator(double longtitude);
+double geohashGetYMercator(double latitude);
+double geohashGetXWGS84(double x);
+double geohashGetYWGS84(double y);
+bool geohashVerifyCoordinates(uint8_t coord_type, double x, double y);
+bool geohashGetDistanceIfInRadius(uint8_t coord_type, double x1, double y1,
+ double x2, double y2, double radius,
+ double *distance);
+bool geohashGetDistanceIfInRadiusWGS84(double x1, double y1, double x2,
+ double y2, double radius,
+ double *distance);
+bool geohashGetDistanceSquaredIfInRadiusMercator(double x1, double y1,
+ double x2, double y2,
+ double radius,
+ double *distance);
+
+#endif /* GEOHASH_HELPER_HPP_ */