|
| 1 | +/* |
| 2 | + * Copyright (c) 2013-2014, yinqiwen <yinqiwen@gmail.com> |
| 3 | + * Copyright (c) 2014, Matt Stancliff <matt@genges.com>. |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of Redis nor the names of its contributors may be used |
| 15 | + * to endorse or promote products derived from this software without |
| 16 | + * specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 22 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 28 | + * THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | +#include "geohash.h" |
| 31 | + |
| 32 | +/** |
| 33 | + * Hashing works like this: |
| 34 | + * Divide the world into 4 buckets. Label each one as such: |
| 35 | + * ----------------- |
| 36 | + * | | | |
| 37 | + * | | | |
| 38 | + * | 0,1 | 1,1 | |
| 39 | + * ----------------- |
| 40 | + * | | | |
| 41 | + * | | | |
| 42 | + * | 0,0 | 1,0 | |
| 43 | + * ----------------- |
| 44 | + */ |
| 45 | + |
| 46 | +bool geohashGetCoordRange(uint8_t coord_type, GeoHashRange *lat_range, |
| 47 | + GeoHashRange *long_range) { |
| 48 | + switch (coord_type) { |
| 49 | + case GEO_WGS84_TYPE: { |
| 50 | + /* These are constraints from EPSG:900913 / EPSG:3785 / OSGEO:41001 */ |
| 51 | + /* We can't geocode at the north/south pole. */ |
| 52 | + lat_range->max = 85.05112878; |
| 53 | + lat_range->min = -85.05112878; |
| 54 | + long_range->max = 180.0; |
| 55 | + long_range->min = -180.0; |
| 56 | + break; |
| 57 | + } |
| 58 | + case GEO_MERCATOR_TYPE: { |
| 59 | + lat_range->max = 20037726.37; |
| 60 | + lat_range->min = -20037726.37; |
| 61 | + long_range->max = 20037726.37; |
| 62 | + long_range->min = -20037726.37; |
| 63 | + break; |
| 64 | + } |
| 65 | + default: { return false; } |
| 66 | + } |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +bool geohashEncode(GeoHashRange *lat_range, GeoHashRange *long_range, |
| 71 | + double latitude, double longitude, uint8_t step, |
| 72 | + GeoHashBits *hash) { |
| 73 | + uint8_t i; |
| 74 | + |
| 75 | + if (NULL == hash || step > 32 || step == 0 || RANGEPISZERO(lat_range) || |
| 76 | + RANGEPISZERO(long_range)) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + hash->bits = 0; |
| 81 | + hash->step = step; |
| 82 | + |
| 83 | + if (latitude < lat_range->min || latitude > lat_range->max || |
| 84 | + longitude < long_range->min || longitude > long_range->max) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + for (i = 0; i < step; i++) { |
| 89 | + uint8_t lat_bit, long_bit; |
| 90 | + |
| 91 | + if (lat_range->max - latitude >= latitude - lat_range->min) { |
| 92 | + lat_bit = 0; |
| 93 | + lat_range->max = (lat_range->max + lat_range->min) / 2; |
| 94 | + } else { |
| 95 | + lat_bit = 1; |
| 96 | + lat_range->min = (lat_range->max + lat_range->min) / 2; |
| 97 | + } |
| 98 | + if (long_range->max - longitude >= longitude - long_range->min) { |
| 99 | + long_bit = 0; |
| 100 | + long_range->max = (long_range->max + long_range->min) / 2; |
| 101 | + } else { |
| 102 | + long_bit = 1; |
| 103 | + long_range->min = (long_range->max + long_range->min) / 2; |
| 104 | + } |
| 105 | + |
| 106 | + hash->bits <<= 1; |
| 107 | + hash->bits += long_bit; |
| 108 | + hash->bits <<= 1; |
| 109 | + hash->bits += lat_bit; |
| 110 | + } |
| 111 | + return true; |
| 112 | +} |
| 113 | + |
| 114 | +bool geohashEncodeType(uint8_t coord_type, double latitude, double longitude, |
| 115 | + uint8_t step, GeoHashBits *hash) { |
| 116 | + GeoHashRange r[2] = { { 0 } }; |
| 117 | + geohashGetCoordRange(coord_type, &r[0], &r[1]); |
| 118 | + return geohashEncode(&r[0], &r[1], latitude, longitude, step, hash); |
| 119 | +} |
| 120 | + |
| 121 | +bool geohashEncodeWGS84(double latitude, double longitude, uint8_t step, |
| 122 | + GeoHashBits *hash) { |
| 123 | + return geohashEncodeType(GEO_WGS84_TYPE, latitude, longitude, step, hash); |
| 124 | +} |
| 125 | + |
| 126 | +bool geohashEncodeMercator(double latitude, double longitude, uint8_t step, |
| 127 | + GeoHashBits *hash) { |
| 128 | + return geohashEncodeType(GEO_MERCATOR_TYPE, latitude, longitude, step, |
| 129 | + hash); |
| 130 | +} |
| 131 | + |
| 132 | +static inline uint8_t get_bit(uint64_t bits, uint8_t pos) { |
| 133 | + return (bits >> pos) & 0x01; |
| 134 | +} |
| 135 | + |
| 136 | +bool geohashDecode(const GeoHashRange lat_range, const GeoHashRange long_range, |
| 137 | + const GeoHashBits hash, GeoHashArea *area) { |
| 138 | + uint8_t i; |
| 139 | + |
| 140 | + if (HASHISZERO(hash) || NULL == area || RANGEISZERO(lat_range) || |
| 141 | + RANGEISZERO(long_range)) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + area->hash = hash; |
| 146 | + area->latitude.min = lat_range.min; |
| 147 | + area->latitude.max = lat_range.max; |
| 148 | + area->longitude.min = long_range.min; |
| 149 | + area->longitude.max = long_range.max; |
| 150 | + |
| 151 | + for (i = 0; i < hash.step; i++) { |
| 152 | + uint8_t lat_bit, long_bit; |
| 153 | + |
| 154 | + long_bit = get_bit(hash.bits, (hash.step - i) * 2 - 1); |
| 155 | + lat_bit = get_bit(hash.bits, (hash.step - i) * 2 - 2); |
| 156 | + |
| 157 | + if (lat_bit == 0) { |
| 158 | + area->latitude.max = (area->latitude.max + area->latitude.min) / 2; |
| 159 | + } else { |
| 160 | + area->latitude.min = (area->latitude.max + area->latitude.min) / 2; |
| 161 | + } |
| 162 | + |
| 163 | + if (long_bit == 0) { |
| 164 | + area->longitude.max = |
| 165 | + (area->longitude.max + area->longitude.min) / 2; |
| 166 | + } else { |
| 167 | + area->longitude.min = |
| 168 | + (area->longitude.max + area->longitude.min) / 2; |
| 169 | + } |
| 170 | + } |
| 171 | + return true; |
| 172 | +} |
| 173 | + |
| 174 | +bool geohashDecodeType(uint8_t coord_type, const GeoHashBits hash, |
| 175 | + GeoHashArea *area) { |
| 176 | + GeoHashRange r[2] = { { 0 } }; |
| 177 | + geohashGetCoordRange(coord_type, &r[0], &r[1]); |
| 178 | + return geohashDecode(r[0], r[1], hash, area); |
| 179 | +} |
| 180 | + |
| 181 | +bool geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area) { |
| 182 | + return geohashDecodeType(GEO_WGS84_TYPE, hash, area); |
| 183 | +} |
| 184 | + |
| 185 | +bool geohashDecodeMercator(const GeoHashBits hash, GeoHashArea *area) { |
| 186 | + return geohashDecodeType(GEO_MERCATOR_TYPE, hash, area); |
| 187 | +} |
| 188 | + |
| 189 | +bool geohashDecodeAreaToLatLong(const GeoHashArea *area, double *latlong) { |
| 190 | + double y, x; |
| 191 | + |
| 192 | + if (!latlong) |
| 193 | + return false; |
| 194 | + |
| 195 | + y = (area->latitude.min + area->latitude.max) / 2; |
| 196 | + x = (area->longitude.min + area->longitude.max) / 2; |
| 197 | + |
| 198 | + latlong[0] = y; |
| 199 | + latlong[1] = x; |
| 200 | + return true; |
| 201 | +} |
| 202 | + |
| 203 | +bool geohashDecodeToLatLongType(uint8_t coord_type, const GeoHashBits hash, |
| 204 | + double *latlong) { |
| 205 | + GeoHashArea area = { { 0 } }; |
| 206 | + if (!latlong || !geohashDecodeType(coord_type, hash, &area)) |
| 207 | + return false; |
| 208 | + return geohashDecodeAreaToLatLong(&area, latlong); |
| 209 | +} |
| 210 | + |
| 211 | +bool geohashDecodeToLatLongWGS84(const GeoHashBits hash, double *latlong) { |
| 212 | + return geohashDecodeToLatLongType(GEO_WGS84_TYPE, hash, latlong); |
| 213 | +} |
| 214 | + |
| 215 | +bool geohashDecodeToLatLongMercator(const GeoHashBits hash, double *latlong) { |
| 216 | + return geohashDecodeToLatLongType(GEO_MERCATOR_TYPE, hash, latlong); |
| 217 | +} |
| 218 | + |
| 219 | +static void geohash_move_x(GeoHashBits *hash, int8_t d) { |
| 220 | + if (d == 0) |
| 221 | + return; |
| 222 | + |
| 223 | + uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaLL; |
| 224 | + uint64_t y = hash->bits & 0x5555555555555555LL; |
| 225 | + |
| 226 | + uint64_t zz = 0x5555555555555555LL >> (64 - hash->step * 2); |
| 227 | + |
| 228 | + if (d > 0) { |
| 229 | + x = x + (zz + 1); |
| 230 | + } else { |
| 231 | + x = x | zz; |
| 232 | + x = x - (zz + 1); |
| 233 | + } |
| 234 | + |
| 235 | + x &= (0xaaaaaaaaaaaaaaaaLL >> (64 - hash->step * 2)); |
| 236 | + hash->bits = (x | y); |
| 237 | +} |
| 238 | + |
| 239 | +static void geohash_move_y(GeoHashBits *hash, int8_t d) { |
| 240 | + if (d == 0) |
| 241 | + return; |
| 242 | + |
| 243 | + uint64_t x = hash->bits & 0xaaaaaaaaaaaaaaaaLL; |
| 244 | + uint64_t y = hash->bits & 0x5555555555555555LL; |
| 245 | + |
| 246 | + uint64_t zz = 0xaaaaaaaaaaaaaaaaLL >> (64 - hash->step * 2); |
| 247 | + if (d > 0) { |
| 248 | + y = y + (zz + 1); |
| 249 | + } else { |
| 250 | + y = y | zz; |
| 251 | + y = y - (zz + 1); |
| 252 | + } |
| 253 | + y &= (0x5555555555555555LL >> (64 - hash->step * 2)); |
| 254 | + hash->bits = (x | y); |
| 255 | +} |
| 256 | + |
| 257 | +void geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors) { |
| 258 | + neighbors->east = *hash; |
| 259 | + neighbors->west = *hash; |
| 260 | + neighbors->north = *hash; |
| 261 | + neighbors->south = *hash; |
| 262 | + neighbors->south_east = *hash; |
| 263 | + neighbors->south_west = *hash; |
| 264 | + neighbors->north_east = *hash; |
| 265 | + neighbors->north_west = *hash; |
| 266 | + |
| 267 | + geohash_move_x(&neighbors->east, 1); |
| 268 | + geohash_move_y(&neighbors->east, 0); |
| 269 | + |
| 270 | + geohash_move_x(&neighbors->west, -1); |
| 271 | + geohash_move_y(&neighbors->west, 0); |
| 272 | + |
| 273 | + geohash_move_x(&neighbors->south, 0); |
| 274 | + geohash_move_y(&neighbors->south, -1); |
| 275 | + |
| 276 | + geohash_move_x(&neighbors->north, 0); |
| 277 | + geohash_move_y(&neighbors->north, 1); |
| 278 | + |
| 279 | + geohash_move_x(&neighbors->north_west, -1); |
| 280 | + geohash_move_y(&neighbors->north_west, 1); |
| 281 | + |
| 282 | + geohash_move_x(&neighbors->north_east, 1); |
| 283 | + geohash_move_y(&neighbors->north_east, 1); |
| 284 | + |
| 285 | + geohash_move_x(&neighbors->south_east, 1); |
| 286 | + geohash_move_y(&neighbors->south_east, -1); |
| 287 | + |
| 288 | + geohash_move_x(&neighbors->south_west, -1); |
| 289 | + geohash_move_y(&neighbors->south_west, -1); |
| 290 | +} |
0 commit comments