| 119 | } |
| 120 | |
| 121 | int geohashEncode(const GeoHashRange *long_range, const GeoHashRange *lat_range, |
| 122 | double longitude, double latitude, uint8_t step, |
| 123 | GeoHashBits *hash) { |
| 124 | /* Check basic arguments sanity. */ |
| 125 | if (hash == NULL || step > 32 || step == 0 || |
| 126 | RANGEPISZERO(lat_range) || RANGEPISZERO(long_range)) return 0; |
| 127 | |
| 128 | /* Return an error when trying to index outside the supported |
| 129 | * constraints. */ |
| 130 | if (longitude > GEO_LONG_MAX || longitude < GEO_LONG_MIN || |
| 131 | latitude > GEO_LAT_MAX || latitude < GEO_LAT_MIN) return 0; |
| 132 | |
| 133 | hash->bits = 0; |
| 134 | hash->step = step; |
| 135 | |
| 136 | if (latitude < lat_range->min || latitude > lat_range->max || |
| 137 | longitude < long_range->min || longitude > long_range->max) { |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | double lat_offset = |
| 142 | (latitude - lat_range->min) / (lat_range->max - lat_range->min); |
| 143 | double long_offset = |
| 144 | (longitude - long_range->min) / (long_range->max - long_range->min); |
| 145 | |
| 146 | /* convert to fixed point based on the step size */ |
| 147 | lat_offset *= (1ULL << step); |
| 148 | long_offset *= (1ULL << step); |
| 149 | hash->bits = interleave64(lat_offset, long_offset); |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | int geohashEncodeType(double longitude, double latitude, uint8_t step, GeoHashBits *hash) { |
| 154 | GeoHashRange r[2] = {{0}}; |
no test coverage detected