1 /* 2 * Copyright (c) 2013-2014, yinqiwen <[email protected]> 3 * Copyright (c) 2014, Matt Stancliff <[email protected]>. 4 * Copyright (c) 2015, Salvatore Sanfilippo <[email protected]>. 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 #include <stdint.h> 38 39 #if defined(__cplusplus) 40 extern "C" { 41 #endif 42 43 #define HASHISZERO(r) (!(r).bits && !(r).step) 44 #define RANGEISZERO(r) (!(r).max && !(r).min) 45 #define RANGEPISZERO(r) (r == NULL || RANGEISZERO(*r)) 46 47 #define GEO_STEP_MAX 26 /* 26*2 = 52 bits. */ 48 49 /* Limits from EPSG:900913 / EPSG:3785 / OSGEO:41001 */ 50 #define GEO_LAT_MIN -85.05112878 51 #define GEO_LAT_MAX 85.05112878 52 #define GEO_LONG_MIN -180 53 #define GEO_LONG_MAX 180 54 55 typedef enum { 56 GEOHASH_NORTH = 0, 57 GEOHASH_EAST, 58 GEOHASH_WEST, 59 GEOHASH_SOUTH, 60 GEOHASH_SOUTH_WEST, 61 GEOHASH_SOUTH_EAST, 62 GEOHASH_NORT_WEST, 63 GEOHASH_NORT_EAST 64 } GeoDirection; 65 66 typedef struct { 67 uint64_t bits; 68 uint8_t step; 69 } GeoHashBits; 70 71 typedef struct { 72 double min; 73 double max; 74 } GeoHashRange; 75 76 typedef struct { 77 GeoHashBits hash; 78 GeoHashRange longitude; 79 GeoHashRange latitude; 80 } GeoHashArea; 81 82 typedef struct { 83 GeoHashBits north; 84 GeoHashBits east; 85 GeoHashBits west; 86 GeoHashBits south; 87 GeoHashBits north_east; 88 GeoHashBits south_east; 89 GeoHashBits north_west; 90 GeoHashBits south_west; 91 } GeoHashNeighbors; 92 93 /* 94 * 0:success 95 * -1:failed 96 */ 97 void geohashGetCoordRange(GeoHashRange *long_range, GeoHashRange *lat_range); 98 int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range, 99 double longitude, double latitude, uint8_t step, 100 GeoHashBits *hash); 101 int geohashEncodeType(double longitude, double latitude, 102 uint8_t step, GeoHashBits *hash); 103 int geohashEncodeWGS84(double longitude, double latitude, uint8_t step, 104 GeoHashBits *hash); 105 int geohashDecode(const GeoHashRange long_range, const GeoHashRange lat_range, 106 const GeoHashBits hash, GeoHashArea *area); 107 int geohashDecodeType(const GeoHashBits hash, GeoHashArea *area); 108 int geohashDecodeWGS84(const GeoHashBits hash, GeoHashArea *area); 109 int geohashDecodeAreaToLongLat(const GeoHashArea *area, double *xy); 110 int geohashDecodeToLongLatType(const GeoHashBits hash, double *xy); 111 int geohashDecodeToLongLatWGS84(const GeoHashBits hash, double *xy); 112 int geohashDecodeToLongLatMercator(const GeoHashBits hash, double *xy); 113 void geohashNeighbors(const GeoHashBits *hash, GeoHashNeighbors *neighbors); 114 115 #if defined(__cplusplus) 116 } 117 #endif 118 #endif /* GEOHASH_H_ */ 119