aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/src/geohash.h
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/src/geohash.h')
-rw-r--r--examples/redis-unstable/src/geohash.h135
1 files changed, 135 insertions, 0 deletions
diff --git a/examples/redis-unstable/src/geohash.h b/examples/redis-unstable/src/geohash.h
new file mode 100644
index 0000000..19fa5a1
--- /dev/null
+++ b/examples/redis-unstable/src/geohash.h
@@ -0,0 +1,135 @@
1/*
2 * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com>
3 * Copyright (c) 2014, Matt Stancliff <matt@genges.com>.
4 * Copyright (c) 2015-current, Redis Ltd.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of Redis nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef GEOHASH_H_
33#define GEOHASH_H_
34
35#include <stddef.h>
36#include <stdint.h>
37
38#if defined(__cplusplus)
39extern "C" {
40#endif
41
42#define HASHISZERO(r) (!(r).bits && !(r).step)
43#define RANGEISZERO(r) (!(r).max && !(r).min)
44#define RANGEPISZERO(r) (r == NULL || RANGEISZERO(*r))
45
46#define GEO_STEP_MAX 26 /* 26*2 = 52 bits. */
47
48/* Limits from EPSG:900913 / EPSG:3785 / OSGEO:41001 */
49#define GEO_LAT_MIN -85.05112878
50#define GEO_LAT_MAX 85.05112878
51#define GEO_LONG_MIN -180
52#define GEO_LONG_MAX 180
53
54typedef enum {
55 GEOHASH_NORTH = 0,
56 GEOHASH_EAST,
57 GEOHASH_WEST,
58 GEOHASH_SOUTH,
59 GEOHASH_SOUTH_WEST,
60 GEOHASH_SOUTH_EAST,
61 GEOHASH_NORT_WEST,
62 GEOHASH_NORT_EAST
63} GeoDirection;
64
65typedef struct {
66 uint64_t bits;
67 uint8_t step;
68} GeoHashBits;
69
70typedef struct {
71 double min;
72 double max;
73} GeoHashRange;
74
75typedef struct {
76 GeoHashBits hash;
77 GeoHashRange longitude;
78 GeoHashRange latitude;
79} GeoHashArea;
80
81typedef struct {
82 GeoHashBits north;
83 GeoHashBits east;
84 GeoHashBits west;
85 GeoHashBits south;
86 GeoHashBits north_east;
87 GeoHashBits south_east;
88 GeoHashBits north_west;
89 GeoHashBits south_west;
90} GeoHashNeighbors;
91
92#define CIRCULAR_TYPE 1
93#define RECTANGLE_TYPE 2
94typedef struct {
95 int type; /* search type */
96 double xy[2]; /* search center point, xy[0]: lon, xy[1]: lat */
97 double conversion; /* km: 1000 */
98 double bounds[4]; /* bounds[0]: min_lon, bounds[1]: min_lat
99 * bounds[2]: max_lon, bounds[3]: max_lat */
100 union {
101 /* CIRCULAR_TYPE */
102 double radius;
103 /* RECTANGLE_TYPE */
104 struct {
105 double height;
106 double width;
107 } r;
108 } t;
109} GeoShape;
110
111/*
112 * 0:success
113 * -1:failed
114 */
115void geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range);
116int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range,
117 double longitude, double latitude, uint8_t step,
118 GeoHashBits *hash);
119int geohashEncodeType(double longitude, double latitude,
120 uint8_t step, GeoHashBits *hash);
121int geohashEncodeWGS84(double longitude, double latitude, uint8_t step,
122 GeoHashBits *hash);
123int geohashDecode(const GeoHashRange long_range, const GeoHashRange lat_range,
124 const GeoHashBits hash, GeoHashArea *area);
125int geohashDecodeType(const GeoHashBits hash, GeoHashArea *area);
126int geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area);
127int geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy);
128int geohashDecodeToLongLatType(const GeoHashBits hash, double *xy);
129int geohashDecodeToLongLatWGS84(const GeoHashBits hash, double *xy);
130void geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors);
131
132#if defined(__cplusplus)
133}
134#endif
135#endif /* GEOHASH_H_ */