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_HELPER_HPP_ 33 #define GEOHASH_HELPER_HPP_ 34 35 #include <math.h> 36 #include "geohash.h" 37 38 #define GZERO(s) s.bits = s.step = 0; 39 #define GISZERO(s) (!s.bits && !s.step) 40 #define GISNOTZERO(s) (s.bits || s.step) 41 42 typedef uint64_t GeoHashFix52Bits; 43 typedef uint64_t GeoHashVarBits; 44 45 typedef struct { 46 GeoHashBits hash; 47 GeoHashArea area; 48 GeoHashNeighbors neighbors; 49 } GeoHashRadius; 50 51 int GeoHashBitsComparator(const GeoHashBits *a, const GeoHashBits *b); 52 uint8_t geohashEstimateStepsByRadius(double range_meters, double lat); 53 int geohashBoundingBox(double longitude, double latitude, double radius_meters, 54 double *bounds); 55 GeoHashRadius geohashGetAreasByRadius(double longitude, 56 double latitude, double radius_meters); 57 GeoHashRadius geohashGetAreasByRadiusWGS84(double longitude, double latitude, 58 double radius_meters); 59 GeoHashRadius geohashGetAreasByRadiusMercator(double longitude, double latitude, 60 double radius_meters); 61 GeoHashFix52Bits geohashAlign52Bits(const GeoHashBits hash); 62 double geohashGetDistance(double lon1d, double lat1d, 63 double lon2d, double lat2d); 64 int geohashGetDistanceIfInRadius(double x1, double y1, 65 double x2, double y2, double radius, 66 double *distance); 67 int geohashGetDistanceIfInRadiusWGS84(double x1, double y1, double x2, 68 double y2, double radius, 69 double *distance); 70 71 #endif /* GEOHASH_HELPER_HPP_ */ 72