| 1 | /* |
| 2 | Copyright (C) 2009-2016 Brazil |
| 3 | Copyright (C) 2025 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #pragma once |
| 21 | |
| 22 | #include "grn.h" |
| 23 | #include "grn_ii.h" |
| 24 | #include "grn_db.h" |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif |
| 29 | |
| 30 | #define GRN_GEO_RADIUS 6357303 |
| 31 | #define GRN_GEO_BES_C1 6334834 |
| 32 | #define GRN_GEO_BES_C2 6377397 |
| 33 | #define GRN_GEO_BES_C3 0.006674 |
| 34 | #define GRN_GEO_GRS_C1 6335439 |
| 35 | #define GRN_GEO_GRS_C2 6378137 |
| 36 | #define GRN_GEO_GRS_C3 0.006694 |
| 37 | |
| 38 | #define GRN_GEO_MAX_LATITUDE 324000000 /* 90 * 60 * 60 * 1000 */ |
| 39 | #define GRN_GEO_MAX_LONGITUDE (648000000 - 1) /* 180 * 60 * 60 * 1000 - 1 */ |
| 40 | #define GRN_GEO_MIN_LATITUDE -GRN_GEO_MAX_LATITUDE |
| 41 | #define GRN_GEO_MIN_LONGITUDE -GRN_GEO_MAX_LONGITUDE |
| 42 | |
| 43 | #define GRN_GEO_KEY_MAX_BITS 64 |
| 44 | |
| 45 | typedef enum { |
| 46 | GRN_GEO_APPROXIMATE_RECTANGLE, |
| 47 | GRN_GEO_APPROXIMATE_SPHERE, |
| 48 | GRN_GEO_APPROXIMATE_ELLIPSOID |
| 49 | } grn_geo_approximate_type; |
| 50 | |
| 51 | typedef enum { |
| 52 | GRN_GEO_CURSOR_ENTRY_STATUS_NONE = 0, |
| 53 | GRN_GEO_CURSOR_ENTRY_STATUS_TOP_INCLUDED = 1 << 0, |
| 54 | GRN_GEO_CURSOR_ENTRY_STATUS_BOTTOM_INCLUDED = 1 << 1, |
| 55 | GRN_GEO_CURSOR_ENTRY_STATUS_LEFT_INCLUDED = 1 << 2, |
| 56 | GRN_GEO_CURSOR_ENTRY_STATUS_RIGHT_INCLUDED = 1 << 3, |
| 57 | GRN_GEO_CURSOR_ENTRY_STATUS_LATITUDE_INNER = 1 << 4, |
| 58 | GRN_GEO_CURSOR_ENTRY_STATUS_LONGITUDE_INNER = 1 << 5 |
| 59 | } grn_geo_cursor_entry_status_flag; |
| 60 | |
| 61 | typedef enum { |
| 62 | GRN_GEO_AREA_NORTH_EAST, |
| 63 | GRN_GEO_AREA_NORTH_WEST, |
| 64 | GRN_GEO_AREA_SOUTH_WEST, |
| 65 | GRN_GEO_AREA_SOUTH_EAST, |
| 66 | GRN_GEO_AREA_LAST |
| 67 | } grn_geo_area_type; |
| 68 | |
| 69 | #define GRN_GEO_N_AREAS GRN_GEO_AREA_LAST |
| 70 | |
| 71 | typedef struct { |
| 72 | uint8_t key[sizeof(grn_geo_point)]; |
| 73 | int target_bit; |
| 74 | int status_flags; |
| 75 | } grn_geo_cursor_entry; |
| 76 | |
| 77 | typedef struct { |
| 78 | grn_geo_point top_left; |
| 79 | grn_geo_point bottom_right; |
| 80 | uint8_t top_left_key[sizeof(grn_geo_point)]; |
| 81 | uint8_t bottom_right_key[sizeof(grn_geo_point)]; |
| 82 | int current_entry; |
| 83 | grn_geo_cursor_entry entries[GRN_GEO_KEY_MAX_BITS]; |
| 84 | } grn_geo_cursor_area; |
| 85 | |
| 86 | typedef struct { |
| 87 | grn_db_obj obj; |
| 88 | grn_obj *pat; |
| 89 | grn_obj *index; |
| 90 | grn_geo_point top_left; |
| 91 | grn_geo_point bottom_right; |
| 92 | grn_geo_point center; |
| 93 | grn_geo_point current; |
| 94 | bool need_distance; |
| 95 | grn_table_cursor *pat_cursor; |
| 96 | grn_ii_cursor *ii_cursor; |
| 97 | int offset; |
| 98 | int rest; |
| 99 | int minimum_reduce_bit; |
| 100 | grn_geo_area_type current_area; |
| 101 | grn_geo_cursor_area areas[GRN_GEO_N_AREAS]; |
| 102 | } grn_geo_cursor_in_rectangle; |
| 103 | |
| 104 | grn_rc |
| 105 | grn_geo_cursor_close(grn_ctx *ctx, grn_obj *geo_cursor); |
| 106 | |
| 107 | grn_rc |
| 108 | grn_geo_resolve_approximate_type(grn_ctx *ctx, |
| 109 | grn_obj *type_name, |
| 110 | grn_geo_approximate_type *type); |
| 111 | |
| 112 | /** |
| 113 | * grn_geo_select_in_circle: |
| 114 | * @index: the index column for TokyoGeoPoint or WGS84GeoPpoint type. |
| 115 | * @center_point: the center point of the target circle. (ShortText, Text, |
| 116 | * LongText, TokyoGeoPoint or WGS84GeoPoint) |
| 117 | * @distance: the radius of the target circle (Int32, |
| 118 | * UInt32, Int64, UInt64 or Float) or the point |
| 119 | * on the circumference of the target circle. (ShortText, Text, LongText, |
| 120 | * TokyoGeoPoint or WGS84GeoPoint) |
| 121 | * @approximate_type: the approximate type to compute |
| 122 | * distance. |
| 123 | * @res: the table to store found record IDs. It must be |
| 124 | * GRN_TABLE_HASH_KEY type table. |
| 125 | * @op: the operator for matched records. |
| 126 | * |
| 127 | * It selects records that are in the circle specified by |
| 128 | * @center_point and @distance from @center_point. Records |
| 129 | * are searched by @index. Found records are added to @res |
| 130 | * table with @op operation. |
| 131 | **/ |
| 132 | grn_rc |
| 133 | grn_geo_select_in_circle(grn_ctx *ctx, |
| 134 | grn_obj *index, |
| 135 | grn_obj *center_point, |
| 136 | grn_obj *distance, |
| 137 | grn_geo_approximate_type approximate_type, |
| 138 | grn_obj *res, |
| 139 | grn_operator op); |
| 140 | |
| 141 | grn_rc |
| 142 | grn_selector_geo_in_circle(grn_ctx *ctx, |
| 143 | grn_obj *table, |
| 144 | grn_obj *index, |
| 145 | int nargs, |
| 146 | grn_obj **args, |
| 147 | grn_obj *res, |
| 148 | grn_operator op); |
| 149 | grn_rc |
| 150 | grn_selector_geo_in_rectangle(grn_ctx *ctx, |
| 151 | grn_obj *table, |
| 152 | grn_obj *index, |
| 153 | int nargs, |
| 154 | grn_obj **args, |
| 155 | grn_obj *res, |
| 156 | grn_operator op); |
| 157 | |
| 158 | GRN_API bool |
| 159 | grn_geo_in_circle(grn_ctx *ctx, |
| 160 | grn_obj *point, |
| 161 | grn_obj *center, |
| 162 | grn_obj *radius_or_point, |
| 163 | grn_geo_approximate_type approximate_type); |
| 164 | GRN_API bool |
| 165 | grn_geo_in_rectangle(grn_ctx *ctx, |
| 166 | grn_obj *point, |
| 167 | grn_obj *top_left, |
| 168 | grn_obj *bottom_right); |
| 169 | bool |
| 170 | grn_geo_in_rectangle_raw(grn_ctx *ctx, |
| 171 | grn_geo_point *point, |
| 172 | grn_geo_point *top_left, |
| 173 | grn_geo_point *bottom_right); |
| 174 | double |
| 175 | grn_geo_distance(grn_ctx *ctx, |
| 176 | grn_obj *point1, |
| 177 | grn_obj *point2, |
| 178 | grn_geo_approximate_type type); |
| 179 | grn_rc |
| 180 | grn_geo_distance_sorter(grn_ctx *ctx, grn_sorter_data *data); |
| 181 | GRN_API double |
| 182 | grn_geo_distance_rectangle(grn_ctx *ctx, grn_obj *point1, grn_obj *point2); |
| 183 | GRN_API double |
| 184 | grn_geo_distance_sphere(grn_ctx *ctx, grn_obj *point1, grn_obj *point2); |
| 185 | GRN_API double |
| 186 | grn_geo_distance_ellipsoid(grn_ctx *ctx, grn_obj *point1, grn_obj *point2); |
| 187 | double |
| 188 | grn_geo_distance_rectangle_raw(grn_ctx *ctx, |
| 189 | grn_geo_point *point1, |
| 190 | grn_geo_point *point2); |
| 191 | double |
| 192 | grn_geo_distance_sphere_raw(grn_ctx *ctx, |
| 193 | grn_geo_point *point1, |
| 194 | grn_geo_point *point2); |
| 195 | double |
| 196 | grn_geo_distance_ellipsoid_raw(grn_ctx *ctx, |
| 197 | grn_geo_point *point1, |
| 198 | grn_geo_point *point2, |
| 199 | int c1, |
| 200 | int c2, |
| 201 | double c3); |
| 202 | double |
| 203 | grn_geo_distance_ellipsoid_raw_tokyo(grn_ctx *ctx, |
| 204 | grn_geo_point *point1, |
| 205 | grn_geo_point *point2); |
| 206 | double |
| 207 | grn_geo_distance_ellipsoid_raw_wgs84(grn_ctx *ctx, |
| 208 | grn_geo_point *point1, |
| 209 | grn_geo_point *point2); |
| 210 | |
| 211 | #ifdef __cplusplus |
| 212 | } |
| 213 | #endif |