|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP Version 7 | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 2017 The PHP Group | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 3.01 of the PHP license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available through the world-wide-web at the following url: | |
| 10 | + | http://www.php.net/license/3_01.txt | |
| 11 | + | If you did not receive a copy of the PHP license and are unable to | |
| 12 | + | obtain it through the world-wide-web, please send a note to | |
| 13 | + | [email protected] so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | + | Authors: Emir Beganovic <[email protected]> | |
| 16 | + +----------------------------------------------------------------------+ |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <stdlib.h> |
| 20 | + |
| 21 | +#include "php.h" |
| 22 | +#include "geo_lat_long.h" |
| 23 | +#include "geohash.h" |
| 24 | + |
| 25 | +#define MAX_LAT 90.0 |
| 26 | +#define MIN_LAT -90.0 |
| 27 | + |
| 28 | +#define MAX_LONG 180.0 |
| 29 | +#define MIN_LONG -180.0 |
| 30 | + |
| 31 | +typedef struct interval_string { |
| 32 | + double high; |
| 33 | + double low; |
| 34 | +} interval_struct; |
| 35 | + |
| 36 | +static char char_map[32] = "0123456789bcdefghjkmnpqrstuvwxyz"; |
| 37 | +static size_t char_map_size = sizeof(char_map); |
| 38 | + |
| 39 | +char *php_geo_geohash_encode(double latitude, double longitude, int precision) |
| 40 | +{ |
| 41 | + char *hash; |
| 42 | + int steps; |
| 43 | + double coord, mid; |
| 44 | + int is_even = 1; |
| 45 | + unsigned int hash_char = 0; |
| 46 | + int i; |
| 47 | + interval_struct lat_interval = { MAX_LAT, MIN_LAT }; |
| 48 | + interval_struct lng_interval = { MAX_LONG, MIN_LONG }; |
| 49 | + interval_struct *interval; |
| 50 | + |
| 51 | + hash = (char*)safe_emalloc(precision, sizeof(char), 1); |
| 52 | + |
| 53 | + hash[precision] = '\0'; |
| 54 | + steps = precision * 5.0; |
| 55 | + |
| 56 | + for (i = 1; i <= steps; i++) { |
| 57 | + if (is_even) { |
| 58 | + interval = &lng_interval; |
| 59 | + coord = longitude; |
| 60 | + } else { |
| 61 | + interval = &lat_interval; |
| 62 | + coord = latitude; |
| 63 | + } |
| 64 | + |
| 65 | + mid = (interval->low + interval->high) / 2.0; |
| 66 | + hash_char = hash_char << 1; |
| 67 | + |
| 68 | + if (coord > mid) { |
| 69 | + interval->low = mid; |
| 70 | + hash_char |= 0x01; |
| 71 | + } else { |
| 72 | + interval->high = mid; |
| 73 | + } |
| 74 | + |
| 75 | + if (!(i % 5)) { |
| 76 | + hash[(i - 1) / 5] = char_map[hash_char]; |
| 77 | + hash_char = 0; |
| 78 | + } |
| 79 | + |
| 80 | + is_even = !is_even; |
| 81 | + } |
| 82 | + |
| 83 | + return hash; |
| 84 | +} |
| 85 | + |
| 86 | +static unsigned int index_for_char(char c, char *string, size_t string_amount) |
| 87 | +{ |
| 88 | + unsigned int index = -1; |
| 89 | + int i; |
| 90 | + |
| 91 | + for (i = 0; i < string_amount; i++) { |
| 92 | + if (c == string[i]) { |
| 93 | + index = i; |
| 94 | + break; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return index; |
| 99 | +} |
| 100 | + |
| 101 | +geo_lat_long php_geo_geohash_decode(char *hash, size_t char_amount) |
| 102 | +{ |
| 103 | + geo_lat_long coordinate; |
| 104 | + |
| 105 | + if (char_amount) { |
| 106 | + int charmap_index; |
| 107 | + double delta; |
| 108 | + int i, j; |
| 109 | + interval_struct lat_interval = { MAX_LAT, MIN_LAT }; |
| 110 | + interval_struct lng_interval = { MAX_LONG, MIN_LONG }; |
| 111 | + interval_struct *interval; |
| 112 | + |
| 113 | + int is_even = 1; |
| 114 | + |
| 115 | + for (i = 0; i < char_amount; i++) { |
| 116 | + charmap_index = index_for_char(hash[i], char_map, char_map_size); |
| 117 | + |
| 118 | + /* Interpret the last 5 bits of the integer */ |
| 119 | + for (j = 0; j < 5; j++) { |
| 120 | + interval = is_even ? &lng_interval : &lat_interval; |
| 121 | + |
| 122 | + delta = (interval->high - interval->low) / 2.0; |
| 123 | + |
| 124 | + if ((charmap_index << j) & 0x0010) { |
| 125 | + interval->low += delta; |
| 126 | + } else { |
| 127 | + interval->high -= delta; |
| 128 | + } |
| 129 | + |
| 130 | + is_even = !is_even; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + coordinate.x = lat_interval.high - ((lat_interval.high - lat_interval.low) / 2.0); |
| 135 | + coordinate.y = lng_interval.high - ((lng_interval.high - lng_interval.low) / 2.0); |
| 136 | + coordinate.z = 0; |
| 137 | + } |
| 138 | + |
| 139 | + return coordinate; |
| 140 | +} |
0 commit comments