Debian Codegraph @

plugins/functions/h3.c

raw symbols SCIP
1 /*
2 Copyright (C) 2024 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #ifdef GRN_EMBEDDED
20 # define GRN_PLUGIN_FUNCTION_TAG functions_h3
21 #endif
22
23 #include <groonga/plugin.h>
24
25 static grn_obj *
26 grn_h3_ensure_wgs84_geo_point(grn_ctx *ctx,
27 grn_obj *input,
28 grn_obj *buffer,
29 const char *tag,
30 const char *input_name)
31 {
32 if (grn_obj_is_bulk(ctx, input) &&
33 input->header.domain == GRN_DB_WGS84_GEO_POINT) {
34 return input;
35 }
36 if (grn_obj_is_text_family_bulk(ctx, input)) {
37 GRN_BULK_REWIND(buffer);
38 grn_rc rc = grn_obj_cast(ctx, input, buffer, false);
39 if (rc == GRN_SUCCESS) {
40 return buffer;
41 }
42 }
43
44 grn_obj inspected;
45 GRN_TEXT_INIT(&inspected, 0);
46 grn_inspect(ctx, &inspected, input);
47 GRN_PLUGIN_ERROR(ctx,
48 GRN_INVALID_ARGUMENT,
49 "%s %s must be WGS84GeoPoint: %.*s",
50 tag,
51 input_name,
52 (int)GRN_TEXT_LEN(&inspected),
53 GRN_TEXT_VALUE(&inspected));
54 GRN_OBJ_FIN(ctx, &inspected);
55 return NULL;
56 }
57
58 static grn_obj *
59 func_h3_grid_distance(grn_ctx *ctx,
60 int n_args,
61 grn_obj **args,
62 grn_user_data *user_data)
63 {
64 const char *tag = "h3_grid_distance():";
65
66 if (n_args != 3) {
67 GRN_PLUGIN_ERROR(ctx,
68 GRN_INVALID_ARGUMENT,
69 "%s wrong number of arguments (%d for 3)",
70 tag,
71 n_args);
72 return NULL;
73 }
74
75 grn_obj *distance_obj = NULL;
76
77 grn_obj *point1_obj = args[0];
78 grn_obj *point2_obj = args[1];
79 grn_obj *resolution_obj = args[2];
80
81 grn_obj point1_buffer;
82 GRN_WGS84_GEO_POINT_INIT(&point1_buffer, 0);
83 grn_obj point2_buffer;
84 GRN_WGS84_GEO_POINT_INIT(&point2_buffer, 0);
85
86 int32_t resolution =
87 grn_plugin_proc_get_value_int32(ctx, resolution_obj, -1, tag);
88
89 point1_obj = grn_h3_ensure_wgs84_geo_point(ctx,
90 point1_obj,
91 &point1_buffer,
92 tag,
93 "point1");
94 if (!point1_obj) {
95 goto exit;
96 }
97 point2_obj = grn_h3_ensure_wgs84_geo_point(ctx,
98 point2_obj,
99 &point2_buffer,
100 tag,
101 "point2");
102 if (!point2_obj) {
103 goto exit;
104 }
105
106 uint64_t h3_index1 = grn_h3_compute_cell(ctx,
107 GRN_GEO_POINT_VALUE_RAW(point1_obj),
108 resolution,
109 tag);
110 if (ctx->rc != GRN_SUCCESS) {
111 goto exit;
112 }
113 uint64_t h3_index2 = grn_h3_compute_cell(ctx,
114 GRN_GEO_POINT_VALUE_RAW(point2_obj),
115 resolution,
116 tag);
117 if (ctx->rc != GRN_SUCCESS) {
118 goto exit;
119 }
120 uint64_t distance =
121 grn_h3_compute_grid_distance(ctx, h3_index1, h3_index2, tag);
122 if (ctx->rc != GRN_SUCCESS) {
123 goto exit;
124 }
125
126 distance_obj = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_UINT64, 0);
127 if (!distance_obj) {
128 return NULL;
129 }
130 GRN_UINT64_SET(ctx, distance_obj, distance);
131
132 exit:
133 GRN_OBJ_FIN(ctx, &point1_buffer);
134 GRN_OBJ_FIN(ctx, &point2_buffer);
135
136 return distance_obj;
137 }
138
139 static grn_rc
140 applier_h3_grid_distance(grn_ctx *ctx, grn_applier_data *data)
141 {
142 const char *tag = "h3_grid_distance():";
143
144 grn_obj *table = grn_applier_data_get_table(ctx, data);
145 grn_obj *output_column = grn_applier_data_get_output_column(ctx, data);
146 size_t n_args;
147 grn_obj **args = grn_applier_data_get_args(ctx, data, &n_args);
148
149 if (n_args != 3) {
150 GRN_PLUGIN_ERROR(ctx,
151 GRN_INVALID_ARGUMENT,
152 "%s wrong number of arguments (%zu for 3)",
153 tag,
154 n_args);
155 return ctx->rc;
156 }
157
158 grn_obj *input_column = args[0];
159 grn_obj *point2_obj = args[1];
160 grn_obj *resolution_obj = args[2];
161
162 int32_t resolution =
163 grn_plugin_proc_get_value_int32(ctx, resolution_obj, -1, tag);
164
165 if (!(grn_obj_is_scalar_column(ctx, input_column) ||
166 grn_obj_is_scalar_accessor(ctx, input_column))) {
167 grn_obj inspected;
168 GRN_TEXT_INIT(&inspected, 0);
169 grn_inspect(ctx, &inspected, input_column);
170 GRN_PLUGIN_ERROR(
171 ctx,
172 GRN_INVALID_ARGUMENT,
173 "%s 1st argument must be a scalar column or accessor: %.*s",
174 tag,
175 (int)(GRN_TEXT_LEN(&inspected)),
176 GRN_TEXT_VALUE(&inspected));
177 GRN_OBJ_FIN(ctx, &inspected);
178 return ctx->rc;
179 }
180 grn_id input_column_range = grn_obj_get_range(ctx, input_column);
181 if (input_column_range != GRN_DB_WGS84_GEO_POINT) {
182 GRN_PLUGIN_ERROR(ctx,
183 GRN_INVALID_ARGUMENT,
184 "%s 1st argument's type must be WGS84GeoPoint: %s",
185 tag,
186 grn_type_id_to_string_builtin(ctx, input_column_range));
187 return ctx->rc;
188 }
189
190 grn_obj point2_buffer;
191 GRN_WGS84_GEO_POINT_INIT(&point2_buffer, 0);
192 point2_obj = grn_h3_ensure_wgs84_geo_point(ctx,
193 point2_obj,
194 &point2_buffer,
195 tag,
196 "point2");
197 uint64_t h3_index2 = grn_h3_compute_cell(ctx,
198 GRN_GEO_POINT_VALUE_RAW(point2_obj),
199 resolution,
200 tag);
201 GRN_OBJ_FIN(ctx, &point2_buffer);
202 if (ctx->rc != GRN_SUCCESS) {
203 return ctx->rc;
204 }
205
206 if (grn_table_size(ctx, table) == 0) {
207 return ctx->rc;
208 }
209
210 grn_obj point1_buffer;
211 GRN_WGS84_GEO_POINT_INIT(&point1_buffer, 0);
212 grn_obj distance_buffer;
213 GRN_UINT64_INIT(&distance_buffer, 0);
214 GRN_TABLE_EACH_BEGIN(ctx, table, cursor, id)
215 {
216 GRN_BULK_REWIND(&point1_buffer);
217 grn_obj_get_value(ctx, input_column, id, &point1_buffer);
218 if (GRN_BULK_VSIZE(&point1_buffer) != sizeof(grn_geo_point)) {
219 continue;
220 }
221 uint64_t h3_index1 =
222 grn_h3_compute_cell(ctx,
223 GRN_GEO_POINT_VALUE_RAW(&point1_buffer),
224 resolution,
225 tag);
226 if (ctx->rc != GRN_SUCCESS) {
227 break;
228 }
229 uint64_t distance_raw =
230 grn_h3_compute_grid_distance(ctx, h3_index1, h3_index2, tag);
231 if (ctx->rc != GRN_SUCCESS) {
232 break;
233 }
234 GRN_UINT64_SET(ctx, &distance_buffer, distance_raw);
235 grn_obj_set_value(ctx, output_column, id, &distance_buffer, GRN_OBJ_SET);
236 if (ctx->rc != GRN_SUCCESS) {
237 break;
238 }
239 }
240 GRN_TABLE_EACH_END(ctx, cursor);
241 GRN_OBJ_FIN(ctx, &point1_buffer);
242 GRN_OBJ_FIN(ctx, &distance_buffer);
243
244 return ctx->rc;
245 }
246
247 static grn_obj *
248 func_h3_in_grid_disk(grn_ctx *ctx,
249 int n_args,
250 grn_obj **args,
251 grn_user_data *user_data)
252 {
253 const char *tag = "h3_in_grid_disk():";
254 if (n_args != 4) {
255 GRN_PLUGIN_ERROR(ctx,
256 GRN_INVALID_ARGUMENT,
257 "%s wrong number of arguments (%d for 4)",
258 tag,
259 n_args);
260 return NULL;
261 }
262
263 grn_obj *in_obj = NULL;
264
265 grn_obj *target_obj = args[0];
266 grn_obj *origin_obj = args[1];
267 grn_obj *resolution_obj = args[2];
268 grn_obj *k_obj = args[3];
269
270 grn_obj target_buffer;
271 GRN_WGS84_GEO_POINT_INIT(&target_buffer, 0);
272 grn_obj origin_buffer;
273 GRN_WGS84_GEO_POINT_INIT(&origin_buffer, 0);
274 grn_obj h3_indices;
275 GRN_UINT64_INIT(&h3_indices, GRN_OBJ_VECTOR);
276
277 int32_t resolution =
278 grn_plugin_proc_get_value_int32(ctx, resolution_obj, -1, tag);
279 int32_t k = grn_plugin_proc_get_value_int32(ctx, k_obj, -1, tag);
280
281 target_obj = grn_h3_ensure_wgs84_geo_point(ctx,
282 target_obj,
283 &target_buffer,
284 tag,
285 "target");
286 if (!target_obj) {
287 goto exit;
288 }
289 uint64_t target_index =
290 grn_h3_compute_cell(ctx,
291 GRN_GEO_POINT_VALUE_RAW(target_obj),
292 resolution,
293 tag);
294 if (ctx->rc != GRN_SUCCESS) {
295 goto exit;
296 }
297 origin_obj = grn_h3_ensure_wgs84_geo_point(ctx,
298 origin_obj,
299 &origin_buffer,
300 tag,
301 "origin");
302 if (!origin_obj) {
303 goto exit;
304 }
305
306 if (grn_h3_compute_grid_disk(ctx,
307 GRN_GEO_POINT_VALUE_RAW(origin_obj),
308 resolution,
309 k,
310 &h3_indices,
311 tag) != GRN_SUCCESS) {
312 goto exit;
313 }
314 bool in = false;
315 size_t i;
316 size_t n_h3_indices = GRN_UINT64_VECTOR_SIZE(&h3_indices);
317 for (i = 0; i < n_h3_indices; i++) {
318 uint64_t h3_index = GRN_UINT64_VALUE_AT(&h3_indices, i);
319 if (h3_index == target_index) {
320 in = true;
321 break;
322 }
323 }
324
325 in_obj = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_BOOL, 0);
326 if (!in_obj) {
327 return NULL;
328 }
329 GRN_BOOL_SET(ctx, in_obj, in);
330
331 exit:
332 GRN_OBJ_FIN(ctx, &target_buffer);
333 GRN_OBJ_FIN(ctx, &origin_buffer);
334 GRN_OBJ_FIN(ctx, &h3_indices);
335
336 return in_obj;
337 }
338
339 static grn_rc
340 selector_h3_in_grid_disk(grn_ctx *ctx,
341 grn_obj *table,
342 grn_obj *index,
343 int n_args,
344 grn_obj **args,
345 grn_obj *res,
346 grn_operator op)
347 {
348 const char *tag = "h3_in_grid_disk():";
349
350 if ((n_args - 1) != 4) {
351 GRN_PLUGIN_ERROR(ctx,
352 GRN_INVALID_ARGUMENT,
353 "%s wrong number of arguments (%d for 4)",
354 tag,
355 (n_args - 1));
356 return ctx->rc;
357 }
358
359 grn_obj *origin_obj = args[2];
360 grn_obj *resolution_obj = args[3];
361 grn_obj *k_obj = args[4];
362
363 grn_obj origin_buffer;
364 GRN_WGS84_GEO_POINT_INIT(&origin_buffer, 0);
365
366 grn_obj h3_indices;
367 GRN_UINT64_INIT(&h3_indices, GRN_OBJ_VECTOR);
368
369 int32_t resolution =
370 grn_plugin_proc_get_value_int32(ctx, resolution_obj, -1, tag);
371 int32_t k = grn_plugin_proc_get_value_int32(ctx, k_obj, -1, tag);
372
373 origin_obj = grn_h3_ensure_wgs84_geo_point(ctx,
374 origin_obj,
375 &origin_buffer,
376 tag,
377 "origin");
378 if (!origin_obj) {
379 goto exit;
380 }
381 if (grn_h3_compute_grid_disk(ctx,
382 GRN_GEO_POINT_VALUE_RAW(origin_obj),
383 resolution,
384 k,
385 &h3_indices,
386 tag) != GRN_SUCCESS) {
387 goto exit;
388 }
389 grn_ii *ii = (grn_ii *)index;
390 grn_obj *lexicon = grn_ii_get_lexicon(ctx, ii);
391 size_t i;
392 size_t n_h3_indices = GRN_UINT64_VECTOR_SIZE(&h3_indices);
393 for (i = 0; i < n_h3_indices; i++) {
394 uint64_t h3_index = GRN_UINT64_VALUE_AT(&h3_indices, i);
395 if (h3_index == 0) {
396 continue;
397 }
398 grn_id id = grn_table_get(ctx, lexicon, &h3_index, sizeof(uint64_t));
399 if (id == GRN_ID_NIL) {
400 continue;
401 }
402 grn_ii_select_by_id(ctx, ii, id, (grn_hash *)res, op);
403 }
404 grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
405
406 exit:
407 GRN_OBJ_FIN(ctx, &origin_buffer);
408 GRN_OBJ_FIN(ctx, &h3_indices);
409
410 return ctx->rc;
411 }
412
413 grn_rc
414 GRN_PLUGIN_INIT(grn_ctx *ctx)
415 {
416 return ctx->rc;
417 }
418
419 grn_rc
420 GRN_PLUGIN_REGISTER(grn_ctx *ctx)
421 {
422 grn_rc rc = GRN_SUCCESS;
423
424 {
425 grn_obj *proc = grn_proc_create(ctx,
426 "h3_grid_distance",
427 -1,
428 GRN_PROC_FUNCTION,
429 func_h3_grid_distance,
430 NULL,
431 NULL,
432 0,
433 NULL);
434 grn_proc_set_applier(ctx, proc, applier_h3_grid_distance);
435 }
436
437 {
438 grn_obj *proc = grn_proc_create(ctx,
439 "h3_in_grid_disk",
440 -1,
441 GRN_PROC_FUNCTION,
442 func_h3_in_grid_disk,
443 NULL,
444 NULL,
445 0,
446 NULL);
447 grn_proc_set_selector(ctx, proc, selector_h3_in_grid_disk);
448 grn_proc_set_selector_operator(ctx, proc, GRN_OP_MATCH);
449 }
450
451 return rc;
452 }
453
454 grn_rc
455 GRN_PLUGIN_FIN(grn_ctx *ctx)
456 {
457 return GRN_SUCCESS;
458 }