Debian Codegraph @

lib/geo.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2017 Brazil
3 Copyright (C) 2020-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 #include "grn_geo.h"
21 #include "grn_pat.h"
22 #include "grn_util.h"
23 #include "grn_posting.h"
24
25 #include <math.h>
26 #include <string.h>
27 #include <stdlib.h>
28
29 #ifndef MAX
30 # define MAX(a, b) ((a) > (b) ? (a) : (b))
31 #endif
32
33 #ifndef MIN
34 # define MIN(a, b) ((a) < (b) ? (a) : (b))
35 #endif
36
37 #define GRN_GEO_POINT_IN_NORTH_EAST(point) \
38 ((point)->latitude >= 0 && (point)->longitude >= 0)
39 #define GRN_GEO_POINT_IN_NORTH_WEST(point) \
40 ((point)->latitude >= 0 && (point)->longitude < 0)
41 #define GRN_GEO_POINT_IN_SOUTH_WEST(point) \
42 ((point)->latitude < 0 && (point)->longitude < 0)
43 #define GRN_GEO_POINT_IN_SOUTH_EAST(point) \
44 ((point)->latitude < 0 && (point)->longitude >= 0)
45
46 #define GRN_GEO_LONGITUDE_IS_WRAPPED(top_left, bottom_right) \
47 ((top_left)->longitude > 0 && (bottom_right)->longitude < 0)
48
49 typedef struct {
50 grn_id id;
51 double d;
52 } geo_entry;
53
54 typedef struct {
55 grn_geo_point key;
56 int key_size;
57 } mesh_entry;
58
59 typedef struct {
60 grn_obj *pat;
61 grn_obj top_left_point_buffer;
62 grn_obj bottom_right_point_buffer;
63 grn_geo_point *top_left;
64 grn_geo_point *bottom_right;
65 grn_geo_point center;
66 } in_rectangle_data;
67
68 typedef struct {
69 grn_geo_point min;
70 grn_geo_point max;
71 uint8_t rectangle_common_bit;
72 uint8_t rectangle_common_key[sizeof(grn_geo_point)];
73 } in_rectangle_area_data;
74
75 static uint8_t
76 compute_diff_bit(uint8_t *geo_key1, uint8_t *geo_key2)
77 {
78 uint8_t i;
79 uint8_t j;
80 uint8_t diff_bit = 0;
81
82 for (i = 0; i < sizeof(grn_geo_point); i++) {
83 if (geo_key1[i] != geo_key2[i]) {
84 diff_bit = 8;
85 for (j = 0; j < 8; j++) {
86 if ((geo_key1[i] & (1 << (7 - j))) != (geo_key2[i] & (1 << (7 - j)))) {
87 diff_bit = j;
88 break;
89 }
90 }
91 break;
92 }
93 }
94
95 uint8_t n_bits_per_byte = 8;
96 return (uint8_t)(i * n_bits_per_byte) + diff_bit;
97 }
98
99 static void
100 compute_min_and_max_key(uint8_t *key_base,
101 uint8_t diff_bit,
102 uint8_t *key_min,
103 uint8_t *key_max)
104 {
105 uint8_t diff_byte, diff_bit_mask;
106
107 diff_byte = diff_bit / 8;
108 diff_bit_mask = 0xff >> (diff_bit % 8);
109
110 if (diff_byte == sizeof(grn_geo_point)) {
111 if (key_min) {
112 grn_memcpy(key_min, key_base, diff_byte);
113 }
114 if (key_max) {
115 grn_memcpy(key_max, key_base, diff_byte);
116 }
117 } else {
118 if (key_min) {
119 grn_memcpy(key_min, key_base, diff_byte + 1);
120 key_min[diff_byte] &= ~diff_bit_mask;
121 memset(key_min + diff_byte + 1, 0, sizeof(grn_geo_point) - diff_byte - 1);
122 }
123
124 if (key_max) {
125 grn_memcpy(key_max, key_base, diff_byte + 1);
126 key_max[diff_byte] |= diff_bit_mask;
127 memset(key_max + diff_byte + 1,
128 0xff,
129 sizeof(grn_geo_point) - diff_byte - 1);
130 }
131 }
132 }
133
134 static void
135 compute_min_and_max(grn_geo_point *base_point,
136 uint8_t diff_bit,
137 grn_geo_point *geo_min,
138 grn_geo_point *geo_max)
139 {
140 uint8_t geo_key_base[sizeof(grn_geo_point)];
141 uint8_t geo_key_min[sizeof(grn_geo_point)];
142 uint8_t geo_key_max[sizeof(grn_geo_point)];
143
144 grn_gton(geo_key_base, base_point, sizeof(grn_geo_point));
145 compute_min_and_max_key(geo_key_base,
146 diff_bit,
147 geo_min ? geo_key_min : NULL,
148 geo_max ? geo_key_max : NULL);
149 if (geo_min) {
150 grn_ntog((uint8_t *)geo_min, geo_key_min, sizeof(grn_geo_point));
151 }
152 if (geo_max) {
153 grn_ntog((uint8_t *)geo_max, geo_key_max, sizeof(grn_geo_point));
154 }
155 }
156
157 /* #define GEO_DEBUG */
158
159 #ifdef GEO_DEBUG
160 # include <stdio.h>
161
162 static void
163 inspect_mesh(grn_ctx *ctx, grn_geo_point *key, int key_size, int n)
164 {
165 grn_geo_point min, max;
166
167 printf("mesh: %d:%d\n", n, key_size);
168
169 printf("key: ");
170 grn_p_geo_point(ctx, key);
171
172 compute_min_and_max(key, key_size, &min, &max);
173 printf("min: ");
174 grn_p_geo_point(ctx, &min);
175 printf("max: ");
176 grn_p_geo_point(ctx, &max);
177 }
178
179 static void
180 inspect_mesh_entry(grn_ctx *ctx, mesh_entry *entries, int n)
181 {
182 mesh_entry *entry;
183
184 entry = entries + n;
185 inspect_mesh(ctx, &(entry->key), entry->key_size, n);
186 }
187
188 static void
189 inspect_tid(grn_ctx *ctx, grn_id tid, grn_geo_point *point, double d)
190 {
191 printf("tid: %d:%g", tid, d);
192 grn_p_geo_point(ctx, point);
193 }
194
195 static void
196 inspect_key(grn_ctx *ctx, uint8_t *key)
197 {
198 int i;
199 for (i = 0; i < 8; i++) {
200 int j;
201 for (j = 0; j < 8; j++) {
202 printf("%d", (key[i] & (1 << (7 - j))) >> (7 - j));
203 }
204 printf(" ");
205 }
206 printf("\n");
207 }
208
209 static void
210 print_key_mark(grn_ctx *ctx, int target_bit)
211 {
212 int i;
213
214 for (i = 0; i < target_bit; i++) {
215 printf(" ");
216 if (i > 0 && i % 8 == 0) {
217 printf(" ");
218 }
219 }
220 if (i > 0 && i % 8 == 0) {
221 printf(" ");
222 }
223 printf("^\n");
224 }
225
226 static void
227 inspect_cursor_entry(grn_ctx *ctx, grn_geo_cursor_entry *entry)
228 {
229 grn_geo_point point;
230
231 printf("entry: ");
232 grn_ntog((uint8_t *)&point, entry->key, sizeof(grn_geo_point));
233 grn_p_geo_point(ctx, &point);
234 inspect_key(ctx, entry->key);
235 print_key_mark(ctx, entry->target_bit);
236
237 printf(" target bit: %d\n", entry->target_bit);
238
239 # define INSPECT_STATUS_FLAG(name) \
240 ((entry->status_flags & GRN_GEO_CURSOR_ENTRY_STATUS_##name) ? "true" \
241 : "false")
242
243 printf(" top included: %s\n", INSPECT_STATUS_FLAG(TOP_INCLUDED));
244 printf("bottom included: %s\n", INSPECT_STATUS_FLAG(BOTTOM_INCLUDED));
245 printf(" left included: %s\n", INSPECT_STATUS_FLAG(LEFT_INCLUDED));
246 printf(" right included: %s\n", INSPECT_STATUS_FLAG(RIGHT_INCLUDED));
247 printf(" latitude inner: %s\n", INSPECT_STATUS_FLAG(LATITUDE_INNER));
248 printf("longitude inner: %s\n", INSPECT_STATUS_FLAG(LONGITUDE_INNER));
249
250 # undef INSPECT_STATUS_FLAG
251 }
252
253 static void
254 inspect_cursor_entry_targets(grn_ctx *ctx,
255 grn_geo_cursor_entry *entry,
256 uint8_t *top_left_key,
257 uint8_t *bottom_right_key,
258 grn_geo_cursor_entry *next_entry0,
259 grn_geo_cursor_entry *next_entry1)
260 {
261 printf("entry: ");
262 inspect_key(ctx, entry->key);
263 printf("top-left: ");
264 inspect_key(ctx, top_left_key);
265 printf("bottom-right: ");
266 inspect_key(ctx, bottom_right_key);
267 printf("next-entry-0: ");
268 inspect_key(ctx, next_entry0->key);
269 printf("next-entry-1: ");
270 inspect_key(ctx, next_entry1->key);
271 printf(" ");
272 print_key_mark(ctx, entry->target_bit + 1);
273 }
274 #else
275 # define inspect_mesh(...)
276 # define inspect_mesh_entry(...)
277 # define inspect_tid(...)
278 # define inspect_key(...)
279 # define print_key_mark(...)
280 # define inspect_cursor_entry(...)
281 # define inspect_cursor_entry_targets(...)
282 #endif
283
284 static int
285 grn_geo_table_sort_detect_far_point(grn_ctx *ctx,
286 grn_obj *table,
287 grn_obj *index,
288 grn_pat *pat,
289 geo_entry *entries,
290 grn_pat_cursor *pc,
291 int n,
292 bool accessorp,
293 grn_geo_point *base_point,
294 double *d_far,
295 uint8_t *diff_bit)
296 {
297 int i = 0;
298 uint8_t diff_bit_prev;
299 uint8_t diff_bit_current;
300 grn_id tid;
301 geo_entry *ep, *p;
302 double d;
303 uint8_t geo_key_prev[sizeof(grn_geo_point)];
304 uint8_t geo_key_curr[sizeof(grn_geo_point)];
305 grn_geo_point point;
306
307 *d_far = 0.0;
308 grn_gton(geo_key_curr, base_point, sizeof(grn_geo_point));
309 *diff_bit = sizeof(grn_geo_point) * 8;
310 diff_bit_current = sizeof(grn_geo_point) * 8;
311 grn_memcpy(&point, base_point, sizeof(grn_geo_point));
312 ep = entries;
313 inspect_mesh(ctx, &point, *diff_bit, -1);
314 while ((tid = grn_pat_cursor_next(ctx, pc))) {
315 grn_ii_cursor *ic =
316 grn_ii_cursor_open(ctx, (grn_ii *)index, tid, 0, 0, 1, 0);
317 if (ic) {
318 grn_posting *posting;
319 grn_gton(geo_key_prev, &point, sizeof(grn_geo_point));
320 grn_pat_get_key(ctx, pat, tid, &point, sizeof(grn_geo_point));
321 grn_gton(geo_key_curr, &point, sizeof(grn_geo_point));
322 d = grn_geo_distance_rectangle_raw(ctx, base_point, &point);
323 inspect_tid(ctx, tid, &point, d);
324
325 diff_bit_prev = diff_bit_current;
326 diff_bit_current = compute_diff_bit(geo_key_curr, geo_key_prev);
327 #ifdef GEO_DEBUG
328 printf("diff: %d:%d:%d\n", *diff_bit, diff_bit_prev, diff_bit_current);
329 #endif
330 if ((diff_bit_current % 2) == 1) {
331 diff_bit_current--;
332 }
333 if (diff_bit_current < diff_bit_prev && *diff_bit > diff_bit_current) {
334 if (i == n) {
335 grn_ii_cursor_close(ctx, ic);
336 break;
337 }
338 *diff_bit = diff_bit_current;
339 }
340
341 if (d > *d_far) {
342 *d_far = d;
343 }
344 while ((posting = grn_ii_cursor_next(ctx, ic))) {
345 grn_id rid =
346 accessorp ? grn_table_get(ctx, table, &posting->rid, sizeof(grn_id))
347 : posting->rid;
348 if (rid) {
349 for (p = ep; entries < p && p[-1].d > d; p--) {
350 p->id = p[-1].id;
351 p->d = p[-1].d;
352 }
353 p->id = rid;
354 p->d = d;
355 if (i < n) {
356 ep++;
357 i++;
358 }
359 }
360 }
361 grn_ii_cursor_close(ctx, ic);
362 }
363 }
364
365 return i;
366 }
367
368 typedef enum {
369 MESH_LEFT_TOP,
370 MESH_RIGHT_TOP,
371 MESH_RIGHT_BOTTOM,
372 MESH_LEFT_BOTTOM
373 } mesh_position;
374
375 /*
376 meshes should have
377 86 >= spaces when include_base_point_hash == false,
378 87 >= spaces when include_base_point_hash == true.
379 */
380 static int
381 grn_geo_get_meshes_for_circle(grn_ctx *ctx,
382 grn_geo_point *base_point,
383 double d_far,
384 uint8_t diff_bit,
385 bool include_base_point_mesh,
386 mesh_entry *meshes)
387 {
388 double d;
389 int n_meshes;
390 int lat_diff, lng_diff;
391 mesh_position position;
392 grn_geo_point geo_base, geo_min, geo_max;
393
394 compute_min_and_max(base_point, diff_bit - 2, &geo_min, &geo_max);
395
396 lat_diff = (geo_max.latitude - geo_min.latitude + 1) / 2;
397 lng_diff = (geo_max.longitude - geo_min.longitude + 1) / 2;
398 geo_base.latitude = geo_min.latitude + lat_diff;
399 geo_base.longitude = geo_min.longitude + lng_diff;
400 if (base_point->latitude >= geo_base.latitude) {
401 if (base_point->longitude >= geo_base.longitude) {
402 position = MESH_RIGHT_TOP;
403 } else {
404 position = MESH_LEFT_TOP;
405 }
406 } else {
407 if (base_point->longitude >= geo_base.longitude) {
408 position = MESH_RIGHT_BOTTOM;
409 } else {
410 position = MESH_LEFT_BOTTOM;
411 }
412 }
413 /*
414 base_point: b
415 geo_min: i
416 geo_max: a
417 geo_base: x: must be at the left bottom in the top right mesh.
418
419 e.g.: base_point is at the left bottom mesh case:
420 +------+------+
421 | | a|
422 | |x |
423 ^+------+------+
424 || | |
425 lng_diff || b | |
426 \/i------+------+
427 <------>
428 lat_diff
429
430 grn_min + lat_diff -> the right mesh.
431 grn_min + lng_diff -> the top mesh.
432 */
433 #ifdef GEO_DEBUG
434 grn_p_geo_point(ctx, base_point);
435 printf("base: ");
436 grn_p_geo_point(ctx, &geo_base);
437 printf("min: ");
438 grn_p_geo_point(ctx, &geo_min);
439 printf("max: ");
440 grn_p_geo_point(ctx, &geo_max);
441 printf("diff: %d (%d, %d)\n", diff_bit, lat_diff, lng_diff);
442 switch (position) {
443 case MESH_LEFT_TOP:
444 printf("position: left-top\n");
445 break;
446 case MESH_RIGHT_TOP:
447 printf("position: right-top\n");
448 break;
449 case MESH_RIGHT_BOTTOM:
450 printf("position: right-bottom\n");
451 break;
452 case MESH_LEFT_BOTTOM:
453 printf("position: left-bottom\n");
454 break;
455 }
456 #endif
457
458 n_meshes = 0;
459
460 #define add_mesh(lat_diff_, lng_diff_, key_size_) \
461 do { \
462 meshes[n_meshes].key.latitude = geo_base.latitude + (lat_diff_); \
463 meshes[n_meshes].key.longitude = geo_base.longitude + (lng_diff_); \
464 meshes[n_meshes].key_size = key_size_; \
465 n_meshes++; \
466 } while (0)
467
468 if (include_base_point_mesh || position != MESH_LEFT_TOP) {
469 add_mesh(0, -lng_diff, diff_bit);
470 }
471 if (include_base_point_mesh || position != MESH_RIGHT_TOP) {
472 add_mesh(0, 0, diff_bit);
473 }
474 if (include_base_point_mesh || position != MESH_RIGHT_BOTTOM) {
475 add_mesh(-lat_diff, 0, diff_bit);
476 }
477 if (include_base_point_mesh || position != MESH_LEFT_BOTTOM) {
478 add_mesh(-lat_diff, -lng_diff, diff_bit);
479 }
480
481 /*
482 b: base_point
483 x: geo_base
484 0-83: sub meshes. 0-83 are added order.
485
486 j: -5 -4 -3 -2 -1 0 1 2 3 4
487 +---+---+---+---+---+---+---+---+---+---+
488 |74 |75 |76 |77 |78 |79 |80 |81 |82 |83 | 4
489 +---+---+---+---+---+---+---+---+---+---+
490 |64 |65 |66 |67 |68 |69 |70 |71 |72 |73 | 3
491 +---+---+---+---+---+---+---+---+---+---+
492 |54 |55 |56 |57 |58 |59 |60 |61 |62 |63 | 2
493 +---+---+---+---+---+---+---+---+---+---+
494 |48 |49 |50 | b | |51 |52 |53 | 1
495 +---+---+---+ | +---+---+---+
496 |42 |43 |44 | |x |45 |46 |47 | 0
497 +---+---+---+-------+-------+---+---+---+
498 |36 |37 |38 | | |39 |40 |41 | -1
499 +---+---+---+ base meshes +---+---+---+
500 |30 |31 |32 | | |33 |34 |35 | -2
501 +---+---+---+---+---+---+---+---+---+---+
502 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 | -3
503 +---+---+---+---+---+---+---+---+---+---+
504 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 | -4
505 +---+---+---+---+---+---+---+---+---+---+
506 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -5
507 +---+---+---+---+---+---+---+---+---+---+
508 i
509 */
510 {
511 int i, j, n_sub_meshes, lat, lat_min, lat_max, lng, lng_min, lng_max;
512 n_sub_meshes = 0;
513 for (i = -5; i < 5; i++) {
514 lat_min = ((lat_diff + 1) / 2) * i;
515 lat_max = ((lat_diff + 1) / 2) * (i + 1) - 1;
516 for (j = -5; j < 5; j++) {
517 if (-3 < i && i < 2 && -3 < j && j < 2) {
518 continue;
519 }
520 lng_min = ((lng_diff + 1) / 2) * j;
521 lng_max = ((lng_diff + 1) / 2) * (j + 1) - 1;
522 if (base_point->latitude <= geo_base.latitude + lat_min) {
523 lat = geo_base.latitude + lat_min;
524 } else if (geo_base.latitude + lat_max < base_point->latitude) {
525 lat = geo_base.latitude + lat_max;
526 } else {
527 lat = base_point->latitude;
528 }
529 if (base_point->longitude <= geo_base.longitude + lng_min) {
530 lng = geo_base.longitude + lng_min;
531 } else if (geo_base.longitude + lng_max < base_point->longitude) {
532 lng = geo_base.longitude + lng_max;
533 } else {
534 lng = base_point->longitude;
535 }
536 meshes[n_meshes].key.latitude = lat;
537 meshes[n_meshes].key.longitude = lng;
538 d = grn_geo_distance_rectangle_raw(ctx,
539 base_point,
540 &(meshes[n_meshes].key));
541 if (d < d_far) {
542 #ifdef GEO_DEBUG
543 printf("sub-mesh: %d: (%d,%d): (%d,%d;%d,%d)\n",
544 n_sub_meshes,
545 base_point->latitude,
546 base_point->longitude,
547 geo_base.latitude + lat_min,
548 geo_base.latitude + lat_max,
549 geo_base.longitude + lng_min,
550 geo_base.longitude + lng_max);
551 grn_p_geo_point(ctx, &(meshes[n_meshes].key));
552 #endif
553 meshes[n_meshes].key_size = diff_bit + 2;
554 n_meshes++;
555 }
556 n_sub_meshes++;
557 }
558 }
559 }
560
561 #undef add_mesh
562
563 return n_meshes;
564 }
565
566 static int
567 grn_geo_table_sort_collect_points(grn_ctx *ctx,
568 grn_obj *table,
569 grn_obj *index,
570 grn_pat *pat,
571 geo_entry *entries,
572 int n_entries,
573 int n,
574 bool accessorp,
575 grn_geo_point *base_point,
576 double d_far,
577 uint8_t diff_bit)
578 {
579 int n_meshes;
580 mesh_entry meshes[86];
581 geo_entry *ep, *p;
582
583 n_meshes = grn_geo_get_meshes_for_circle(ctx,
584 base_point,
585 d_far,
586 diff_bit,
587 false,
588 meshes);
589
590 ep = entries + n_entries;
591 while (n_meshes--) {
592 grn_id tid;
593 grn_pat_cursor *pc =
594 grn_pat_cursor_open(ctx,
595 pat,
596 &(meshes[n_meshes].key),
597 (uint32_t)(meshes[n_meshes].key_size),
598 NULL,
599 0,
600 0,
601 -1,
602 GRN_CURSOR_PREFIX | GRN_CURSOR_SIZE_BY_BIT);
603 inspect_mesh_entry(ctx, meshes, n_meshes);
604 if (pc) {
605 while ((tid = grn_pat_cursor_next(ctx, pc))) {
606 grn_ii_cursor *ic =
607 grn_ii_cursor_open(ctx, (grn_ii *)index, tid, 0, 0, 1, 0);
608 if (ic) {
609 double d;
610 grn_geo_point pos;
611 grn_posting *posting;
612 grn_pat_get_key(ctx, pat, tid, &pos, sizeof(grn_geo_point));
613 d = grn_geo_distance_rectangle_raw(ctx, base_point, &pos);
614 inspect_tid(ctx, tid, &pos, d);
615 while ((posting = grn_ii_cursor_next(ctx, ic))) {
616 grn_id rid =
617 accessorp
618 ? grn_table_get(ctx, table, &posting->rid, sizeof(grn_id))
619 : posting->rid;
620 if (rid) {
621 for (p = ep; entries < p && p[-1].d > d; p--) {
622 p->id = p[-1].id;
623 p->d = p[-1].d;
624 }
625 p->id = rid;
626 p->d = d;
627 if (n_entries < n) {
628 ep++;
629 n_entries++;
630 }
631 }
632 }
633 grn_ii_cursor_close(ctx, ic);
634 }
635 }
636 grn_pat_cursor_close(ctx, pc);
637 }
638 }
639 return n_entries;
640 }
641
642 static inline grn_obj *
643 find_geo_sort_index(grn_ctx *ctx, grn_obj *key)
644 {
645 grn_obj *index = NULL;
646
647 if (GRN_ACCESSORP(key)) {
648 grn_accessor *accessor = (grn_accessor *)key;
649 if (accessor->action != GRN_ACCESSOR_GET_KEY) {
650 return NULL;
651 }
652 if (!(DB_OBJ(accessor->obj)->id & GRN_OBJ_TMP_OBJECT)) {
653 return NULL;
654 }
655 if (accessor->obj->header.type != GRN_TABLE_HASH_KEY) {
656 return NULL;
657 }
658 if (!accessor->next) {
659 return NULL;
660 }
661 grn_column_index(ctx, accessor->next->obj, GRN_OP_LESS, &index, 1, NULL);
662 } else {
663 grn_column_index(ctx, key, GRN_OP_LESS, &index, 1, NULL);
664 }
665
666 return index;
667 }
668
669 static inline int
670 grn_geo_table_sort_by_distance(grn_ctx *ctx,
671 grn_obj *table,
672 grn_obj *index,
673 grn_pat *pat,
674 grn_pat_cursor *pc,
675 bool accessorp,
676 grn_geo_point *base_point,
677 int offset,
678 int limit,
679 grn_obj *result)
680 {
681 int n_entries = 0, e = offset + limit;
682 geo_entry *entries;
683
684 if ((entries = GRN_MALLOC(sizeof(geo_entry) * (size_t)(e + 1)))) {
685 int n;
686 uint8_t diff_bit;
687 double d_far;
688 geo_entry *ep;
689 bool need_not_indexed_records;
690 grn_hash *indexed_records = NULL;
691
692 n = grn_geo_table_sort_detect_far_point(ctx,
693 table,
694 index,
695 pat,
696 entries,
697 pc,
698 e,
699 accessorp,
700 base_point,
701 &d_far,
702 &diff_bit);
703 if (diff_bit > 0) {
704 n = grn_geo_table_sort_collect_points(ctx,
705 table,
706 index,
707 pat,
708 entries,
709 n,
710 e,
711 accessorp,
712 base_point,
713 d_far,
714 diff_bit);
715 }
716 need_not_indexed_records = offset + limit > n;
717 if (need_not_indexed_records) {
718 indexed_records = grn_hash_create(ctx,
719 NULL,
720 sizeof(grn_id),
721 0,
722 GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY);
723 }
724 for (ep = entries + offset; n_entries < limit && ep < entries + n;
725 n_entries++, ep++) {
726 grn_id *sorted_id;
727 if (!grn_array_add(ctx, (grn_array *)result, (void **)&sorted_id)) {
728 if (indexed_records) {
729 grn_hash_close(ctx, indexed_records);
730 indexed_records = NULL;
731 }
732 break;
733 }
734 *sorted_id = ep->id;
735 if (indexed_records) {
736 grn_hash_add(ctx,
737 indexed_records,
738 &(ep->id),
739 sizeof(grn_id),
740 NULL,
741 NULL);
742 }
743 }
744 GRN_FREE(entries);
745 if (indexed_records) {
746 GRN_TABLE_EACH(ctx, table, GRN_ID_NIL, GRN_ID_MAX, id, NULL, NULL, NULL, {
747 if (!grn_hash_get(ctx, indexed_records, &id, sizeof(grn_id), NULL)) {
748 grn_id *sorted_id;
749 if (grn_array_add(ctx, (grn_array *)result, (void **)&sorted_id)) {
750 *sorted_id = id;
751 }
752 n_entries++;
753 if (n_entries == limit) {
754 break;
755 }
756 };
757 });
758 grn_hash_close(ctx, indexed_records);
759 }
760 }
761
762 return n_entries;
763 }
764
765 int
766 grn_geo_table_sort(grn_ctx *ctx,
767 grn_obj *table,
768 int offset,
769 int limit,
770 grn_obj *result,
771 grn_obj *column,
772 grn_obj *geo_point)
773 {
774 grn_obj *index;
775 int i = 0;
776
777 GRN_API_ENTER;
778
779 if (offset < 0 || limit < 0) {
780 unsigned int size;
781 grn_rc rc;
782 size = grn_table_size(ctx, table);
783 rc = grn_output_range_normalize(ctx, (int)size, &offset, &limit);
784 if (rc != GRN_SUCCESS) {
785 ERR(rc,
786 "[sort][geo] failed to normalize offset and limit: "
787 "offset:%d limit:%d table-size:%u",
788 offset,
789 limit,
790 size);
791 GRN_API_RETURN(i);
792 }
793 }
794
795 if ((index = find_geo_sort_index(ctx, column))) {
796 grn_id tid;
797 grn_pat *pat = (grn_pat *)grn_ctx_at(ctx, index->header.domain);
798 if (!pat) {
799 char index_name[GRN_TABLE_MAX_KEY_SIZE];
800 int index_name_size;
801 char lexicon_name[GRN_TABLE_MAX_KEY_SIZE];
802 int lexicon_name_size;
803 index_name_size =
804 grn_obj_name(ctx, index, index_name, GRN_TABLE_MAX_KEY_SIZE);
805 lexicon_name_size = grn_table_get_key(ctx,
806 grn_ctx_db(ctx),
807 index->header.domain,
808 lexicon_name,
809 GRN_TABLE_MAX_KEY_SIZE);
810 ERR(GRN_OBJECT_CORRUPT,
811 "[sort][geo] lexicon is broken: <%.*s>: <%.*s>(%d)",
812 index_name_size,
813 index_name,
814 lexicon_name_size,
815 lexicon_name,
816 index->header.domain);
817 GRN_API_RETURN(i);
818 }
819 grn_id domain = pat->obj.header.domain;
820 grn_pat_cursor *pc =
821 grn_pat_cursor_open(ctx,
822 pat,
823 NULL,
824 0,
825 GRN_BULK_HEAD(geo_point),
826 (uint32_t)GRN_BULK_VSIZE(geo_point),
827 0,
828 -1,
829 GRN_CURSOR_PREFIX);
830 if (pc) {
831 if (domain != GRN_DB_TOKYO_GEO_POINT &&
832 domain != GRN_DB_WGS84_GEO_POINT) {
833 int e = offset + limit;
834 while (i < e && (tid = grn_pat_cursor_next(ctx, pc))) {
835 grn_ii_cursor *ic =
836 grn_ii_cursor_open(ctx, (grn_ii *)index, tid, 0, 0, 1, 0);
837 if (ic) {
838 grn_posting *posting;
839 while (i < e && (posting = grn_ii_cursor_next(ctx, ic))) {
840 if (offset <= i) {
841 grn_id *v;
842 if (!grn_array_add(ctx, (grn_array *)result, (void **)&v)) {
843 break;
844 }
845 *v = posting->rid;
846 }
847 i++;
848 }
849 grn_ii_cursor_close(ctx, ic);
850 }
851 }
852 } else {
853 grn_geo_point *base_point = (grn_geo_point *)GRN_BULK_HEAD(geo_point);
854 i = grn_geo_table_sort_by_distance(ctx,
855 table,
856 index,
857 pat,
858 pc,
859 GRN_ACCESSORP(column),
860 base_point,
861 offset,
862 limit,
863 result);
864 }
865 grn_pat_cursor_close(ctx, pc);
866 }
867 }
868 GRN_API_RETURN(i);
869 }
870
871 grn_rc
872 grn_geo_resolve_approximate_type(grn_ctx *ctx,
873 grn_obj *type_name,
874 grn_geo_approximate_type *type)
875 {
876 grn_rc rc;
877 grn_obj approximate_type;
878
879 GRN_TEXT_INIT(&approximate_type, 0);
880 rc = grn_obj_cast(ctx, type_name, &approximate_type, false);
881 if (rc == GRN_SUCCESS) {
882 const char *name;
883 size_t size;
884 name = GRN_TEXT_VALUE(&approximate_type);
885 size = GRN_TEXT_LEN(&approximate_type);
886 if ((strncmp("rectangle", name, size) == 0) ||
887 (strncmp("rect", name, size) == 0)) {
888 *type = GRN_GEO_APPROXIMATE_RECTANGLE;
889 } else if ((strncmp("sphere", name, size) == 0) ||
890 (strncmp("sphr", name, size) == 0)) {
891 *type = GRN_GEO_APPROXIMATE_SPHERE;
892 } else if ((strncmp("ellipsoid", name, size) == 0) ||
893 (strncmp("ellip", name, size) == 0)) {
894 *type = GRN_GEO_APPROXIMATE_ELLIPSOID;
895 } else {
896 ERR(GRN_INVALID_ARGUMENT,
897 "geo distance approximate type must be one of "
898 "[rectangle, rect, sphere, sphr, ellipsoid, ellip]"
899 ": <%.*s>",
900 (int)size,
901 name);
902 }
903 }
904 GRN_OBJ_FIN(ctx, &approximate_type);
905
906 return rc;
907 }
908
909 typedef double (*grn_geo_distance_raw_func)(grn_ctx *ctx,
910 grn_geo_point *point1,
911 grn_geo_point *point2);
912
913 grn_rc
914 grn_selector_geo_in_circle(grn_ctx *ctx,
915 grn_obj *table,
916 grn_obj *index,
917 int nargs,
918 grn_obj **args,
919 grn_obj *res,
920 grn_operator op)
921 {
922 const char *tag = "[geo-in-circle]";
923 grn_selector_data *data = grn_selector_data_get(ctx);
924 grn_geo_approximate_type type = GRN_GEO_APPROXIMATE_RECTANGLE;
925
926 if (!(nargs == 4 || nargs == 5)) {
927 ERR(GRN_INVALID_ARGUMENT,
928 "%s requires 3 or 4 arguments but was <%d> arguments",
929 tag,
930 nargs - 1);
931 return ctx->rc;
932 }
933
934 if (nargs == 5) {
935 grn_obj *type_name = NULL;
936 if (args[4]->header.type == GRN_TABLE_HASH_KEY) {
937 grn_obj *options = args[4];
938 grn_rc rc = grn_selector_data_parse_options(ctx,
939 data,
940 options,
941 tag,
942 "type",
943 GRN_PROC_OPTION_VALUE_RAW,
944 &type_name,
945 NULL);
946 if (rc != GRN_SUCCESS) {
947 return rc;
948 }
949 } else {
950 type_name = args[4];
951 }
952 if (type_name) {
953 grn_rc rc = grn_geo_resolve_approximate_type(ctx, type_name, &type);
954 if (rc != GRN_SUCCESS) {
955 return rc;
956 }
957 }
958 }
959
960 {
961 grn_obj *center_point, *distance;
962 center_point = args[2];
963 distance = args[3];
964 grn_geo_select_in_circle(ctx, index, center_point, distance, type, res, op);
965 }
966
967 return ctx->rc;
968 }
969
970 static grn_geo_distance_raw_func
971 grn_geo_resolve_distance_raw_func(grn_ctx *ctx,
972 grn_geo_approximate_type approximate_type,
973 grn_id domain)
974 {
975 grn_geo_distance_raw_func distance_raw_func = NULL;
976
977 switch (approximate_type) {
978 case GRN_GEO_APPROXIMATE_RECTANGLE:
979 distance_raw_func = grn_geo_distance_rectangle_raw;
980 break;
981 case GRN_GEO_APPROXIMATE_SPHERE:
982 distance_raw_func = grn_geo_distance_sphere_raw;
983 break;
984 case GRN_GEO_APPROXIMATE_ELLIPSOID:
985 if (domain == GRN_DB_WGS84_GEO_POINT) {
986 distance_raw_func = grn_geo_distance_ellipsoid_raw_wgs84;
987 } else {
988 distance_raw_func = grn_geo_distance_ellipsoid_raw_tokyo;
989 }
990 break;
991 default:
992 break;
993 }
994
995 return distance_raw_func;
996 }
997
998 grn_rc
999 grn_geo_select_in_circle(grn_ctx *ctx,
1000 grn_obj *index,
1001 grn_obj *center_point,
1002 grn_obj *distance,
1003 grn_geo_approximate_type approximate_type,
1004 grn_obj *res,
1005 grn_operator op)
1006 {
1007 grn_id domain;
1008 double center_longitude, center_latitude;
1009 double d;
1010 grn_obj *pat, *point_on_circle = NULL, center_point_, point_on_circle_;
1011 grn_geo_point *center, on_circle;
1012 grn_geo_distance_raw_func distance_raw_func;
1013 pat = grn_ctx_at(ctx, index->header.domain);
1014 if (!pat) {
1015 char index_name[GRN_TABLE_MAX_KEY_SIZE];
1016 int index_name_size;
1017 char lexicon_name[GRN_TABLE_MAX_KEY_SIZE];
1018 int lexicon_name_size;
1019 index_name_size =
1020 grn_obj_name(ctx, index, index_name, GRN_TABLE_MAX_KEY_SIZE);
1021 lexicon_name_size = grn_table_get_key(ctx,
1022 grn_ctx_db(ctx),
1023 index->header.domain,
1024 lexicon_name,
1025 GRN_TABLE_MAX_KEY_SIZE);
1026 ERR(GRN_OBJECT_CORRUPT,
1027 "geo_in_circle(): lexicon is broken: <%.*s>: <%.*s>(%d)",
1028 index_name_size,
1029 index_name,
1030 lexicon_name_size,
1031 lexicon_name,
1032 index->header.domain);
1033 goto exit;
1034 }
1035 domain = pat->header.domain;
1036 if (domain != GRN_DB_TOKYO_GEO_POINT && domain != GRN_DB_WGS84_GEO_POINT) {
1037 char name[GRN_TABLE_MAX_KEY_SIZE];
1038 int name_size = 0;
1039 grn_obj *domain_object;
1040 domain_object = grn_ctx_at(ctx, domain);
1041 if (domain_object) {
1042 name_size =
1043 grn_obj_name(ctx, domain_object, name, GRN_TABLE_MAX_KEY_SIZE);
1044 grn_obj_unlink(ctx, domain_object);
1045 } else {
1046 grn_strcpy(name, GRN_TABLE_MAX_KEY_SIZE, "(null)");
1047 name_size = (int)strlen(name);
1048 }
1049 ERR(GRN_INVALID_ARGUMENT,
1050 "geo_in_circle(): index table must be "
1051 "TokyoGeoPoint or WGS84GeoPoint key type table: <%.*s>",
1052 name_size,
1053 name);
1054 goto exit;
1055 }
1056
1057 if (center_point->header.domain != domain) {
1058 GRN_OBJ_INIT(&center_point_, GRN_BULK, 0, domain);
1059 if (grn_obj_cast(ctx, center_point, &center_point_, false)) {
1060 goto exit;
1061 }
1062 center_point = &center_point_;
1063 }
1064 center = GRN_GEO_POINT_VALUE_RAW(center_point);
1065 center_longitude = GRN_GEO_MSEC2RADIAN(center->longitude);
1066 center_latitude = GRN_GEO_MSEC2RADIAN(center->latitude);
1067
1068 distance_raw_func =
1069 grn_geo_resolve_distance_raw_func(ctx, approximate_type, domain);
1070 if (!distance_raw_func) {
1071 ERR(GRN_INVALID_ARGUMENT,
1072 "unknown approximate type: <%d>",
1073 approximate_type);
1074 goto exit;
1075 }
1076
1077 switch (distance->header.domain) {
1078 case GRN_DB_INT32:
1079 d = GRN_INT32_VALUE(distance);
1080 on_circle.latitude =
1081 center->latitude + GRN_GEO_RADIAN2MSEC(d / (double)GRN_GEO_RADIUS);
1082 on_circle.longitude = center->longitude;
1083 break;
1084 case GRN_DB_UINT32:
1085 d = GRN_UINT32_VALUE(distance);
1086 on_circle.latitude =
1087 center->latitude + GRN_GEO_RADIAN2MSEC(d / (double)GRN_GEO_RADIUS);
1088 on_circle.longitude = center->longitude;
1089 break;
1090 case GRN_DB_INT64:
1091 d = (double)GRN_INT64_VALUE(distance);
1092 on_circle.latitude =
1093 center->latitude + GRN_GEO_RADIAN2MSEC(d / (double)GRN_GEO_RADIUS);
1094 on_circle.longitude = center->longitude;
1095 break;
1096 case GRN_DB_UINT64:
1097 d = (double)GRN_UINT64_VALUE(distance);
1098 on_circle.latitude =
1099 center->latitude + GRN_GEO_RADIAN2MSEC(d / (double)GRN_GEO_RADIUS);
1100 on_circle.longitude = center->longitude;
1101 break;
1102 case GRN_DB_FLOAT32:
1103 d = GRN_FLOAT32_VALUE(distance);
1104 on_circle.latitude =
1105 center->latitude + GRN_GEO_RADIAN2MSEC(d / (double)GRN_GEO_RADIUS);
1106 on_circle.longitude = center->longitude;
1107 break;
1108 case GRN_DB_FLOAT:
1109 d = GRN_FLOAT_VALUE(distance);
1110 on_circle.latitude =
1111 center->latitude + GRN_GEO_RADIAN2MSEC(d / (double)GRN_GEO_RADIUS);
1112 on_circle.longitude = center->longitude;
1113 break;
1114 case GRN_DB_SHORT_TEXT:
1115 case GRN_DB_TEXT:
1116 case GRN_DB_LONG_TEXT:
1117 GRN_OBJ_INIT(&point_on_circle_, GRN_BULK, 0, domain);
1118 if (grn_obj_cast(ctx, distance, &point_on_circle_, false)) {
1119 goto exit;
1120 }
1121 point_on_circle = &point_on_circle_;
1122 /* fallthru */
1123 case GRN_DB_TOKYO_GEO_POINT:
1124 case GRN_DB_WGS84_GEO_POINT:
1125 if (!point_on_circle) {
1126 if (domain != distance->header.domain) { /* todo */
1127 goto exit;
1128 }
1129 point_on_circle = distance;
1130 }
1131 GRN_GEO_POINT_VALUE(point_on_circle,
1132 on_circle.latitude,
1133 on_circle.longitude);
1134 d = distance_raw_func(ctx, center, &on_circle);
1135 if (point_on_circle == &point_on_circle_) {
1136 grn_obj_unlink(ctx, point_on_circle);
1137 }
1138 break;
1139 default:
1140 goto exit;
1141 }
1142 {
1143 grn_selector_data *data = grn_selector_data_get(ctx);
1144 const bool use_selector_data =
1145 (data && (grn_selector_data_have_score_column(ctx, data) ||
1146 grn_selector_data_have_tags_column(ctx, data)));
1147
1148 int n_meshes;
1149 uint8_t diff_bit;
1150 double d_far;
1151 mesh_entry meshes[87];
1152 uint8_t geo_key1[sizeof(grn_geo_point)];
1153 uint8_t geo_key2[sizeof(grn_geo_point)];
1154
1155 d_far = grn_geo_distance_rectangle_raw(ctx, center, &on_circle);
1156 grn_gton(geo_key1, center, sizeof(grn_geo_point));
1157 grn_gton(geo_key2, &on_circle, sizeof(grn_geo_point));
1158 diff_bit = compute_diff_bit(geo_key1, geo_key2);
1159 #ifdef GEO_DEBUG
1160 printf("center point: ");
1161 grn_p_geo_point(ctx, center);
1162 printf("point on circle: ");
1163 grn_p_geo_point(ctx, &on_circle);
1164 printf("diff: %d\n", diff_bit);
1165 #endif
1166 if ((diff_bit % 2) == 1) {
1167 diff_bit--;
1168 }
1169 n_meshes =
1170 grn_geo_get_meshes_for_circle(ctx, center, d_far, diff_bit, true, meshes);
1171 while (n_meshes--) {
1172 grn_table_cursor *tc;
1173 tc = grn_table_cursor_open(ctx,
1174 pat,
1175 &(meshes[n_meshes].key),
1176 (unsigned int)(meshes[n_meshes].key_size),
1177 NULL,
1178 0,
1179 0,
1180 -1,
1181 GRN_CURSOR_PREFIX | GRN_CURSOR_SIZE_BY_BIT);
1182 inspect_mesh_entry(ctx, meshes, n_meshes);
1183 if (tc) {
1184 grn_id tid;
1185 grn_geo_point point;
1186 while ((tid = grn_table_cursor_next(ctx, tc))) {
1187 double point_distance;
1188 grn_table_get_key(ctx, pat, tid, &point, sizeof(grn_geo_point));
1189 point_distance = distance_raw_func(ctx, &point, center);
1190 if (point_distance <= d) {
1191 inspect_tid(ctx, tid, &point, point_distance);
1192 if (use_selector_data) {
1193 grn_selector_data_on_token_found(ctx,
1194 data,
1195 index,
1196 tid,
1197 point_distance);
1198 } else {
1199 grn_ii_at(ctx, (grn_ii *)index, tid, (grn_hash *)res, op);
1200 }
1201 }
1202 }
1203 grn_table_cursor_close(ctx, tc);
1204 }
1205 }
1206 }
1207 exit:
1208 grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
1209 return ctx->rc;
1210 }
1211
1212 grn_rc
1213 grn_selector_geo_in_rectangle(grn_ctx *ctx,
1214 grn_obj *table,
1215 grn_obj *index,
1216 int nargs,
1217 grn_obj **args,
1218 grn_obj *res,
1219 grn_operator op)
1220 {
1221 const char *tag = "[geo-in-rectangle]";
1222 grn_selector_data *data = grn_selector_data_get(ctx);
1223
1224 if (!(nargs == 4 || nargs == 5)) {
1225 ERR(GRN_INVALID_ARGUMENT,
1226 "%s requires 3 or 4 arguments but was <%d> arguments",
1227 tag,
1228 nargs - 1);
1229 return ctx->rc;
1230 }
1231
1232 if (nargs == 5) {
1233 if (args[4]->header.type != GRN_TABLE_HASH_KEY) {
1234 grn_obj inspected;
1235 GRN_TEXT_INIT(&inspected, 0);
1236 grn_inspect(ctx, &inspected, args[4]);
1237 ERR(GRN_INVALID_ARGUMENT,
1238 "%s the 4th argument must be options: %.*s",
1239 tag,
1240 (int)GRN_TEXT_LEN(&inspected),
1241 GRN_TEXT_VALUE(&inspected));
1242 GRN_OBJ_FIN(ctx, &inspected);
1243 return ctx->rc;
1244 }
1245 grn_obj *options = args[4];
1246 grn_rc rc = grn_selector_data_parse_options(ctx, data, options, tag, NULL);
1247 if (rc != GRN_SUCCESS) {
1248 return rc;
1249 }
1250 }
1251
1252 grn_obj *top_left_point, *bottom_right_point;
1253 top_left_point = args[2];
1254 bottom_right_point = args[3];
1255 grn_geo_select_in_rectangle(ctx,
1256 index,
1257 top_left_point,
1258 bottom_right_point,
1259 res,
1260 op);
1261 return ctx->rc;
1262 }
1263
1264 static void
1265 in_rectangle_data_fill(grn_ctx *ctx,
1266 grn_obj *index,
1267 grn_obj *top_left_point,
1268 grn_obj *bottom_right_point,
1269 const char *process_name,
1270 in_rectangle_data *data)
1271 {
1272 grn_id domain;
1273 const char *domain_name;
1274
1275 data->pat = grn_ctx_at(ctx, index->header.domain);
1276 if (!data->pat) {
1277 char index_name[GRN_TABLE_MAX_KEY_SIZE];
1278 int index_name_size;
1279 char lexicon_name[GRN_TABLE_MAX_KEY_SIZE];
1280 int lexicon_name_size;
1281 index_name_size =
1282 grn_obj_name(ctx, index, index_name, GRN_TABLE_MAX_KEY_SIZE);
1283 lexicon_name_size = grn_table_get_key(ctx,
1284 grn_ctx_db(ctx),
1285 index->header.domain,
1286 lexicon_name,
1287 GRN_TABLE_MAX_KEY_SIZE);
1288 ERR(GRN_OBJECT_CORRUPT,
1289 "%s: lexicon lexicon is broken: <%.*s>: <%.*s>(%d)",
1290 process_name,
1291 index_name_size,
1292 index_name,
1293 lexicon_name_size,
1294 lexicon_name,
1295 index->header.domain);
1296 return;
1297 }
1298
1299 domain = data->pat->header.domain;
1300 if (domain != GRN_DB_TOKYO_GEO_POINT && domain != GRN_DB_WGS84_GEO_POINT) {
1301 char name[GRN_TABLE_MAX_KEY_SIZE];
1302 int name_size = 0;
1303 grn_obj *domain_object;
1304 domain_object = grn_ctx_at(ctx, domain);
1305 if (domain_object) {
1306 name_size =
1307 grn_obj_name(ctx, domain_object, name, GRN_TABLE_MAX_KEY_SIZE);
1308 grn_obj_unlink(ctx, domain_object);
1309 } else {
1310 grn_strcpy(name, GRN_TABLE_MAX_KEY_SIZE, "(null)");
1311 name_size = (int)strlen(name);
1312 }
1313 ERR(GRN_INVALID_ARGUMENT,
1314 "%s: index table must be "
1315 "TokyoGeoPoint or WGS84GeoPoint key type table: <%.*s>",
1316 process_name,
1317 name_size,
1318 name);
1319 return;
1320 }
1321
1322 if (domain == GRN_DB_TOKYO_GEO_POINT) {
1323 domain_name = "TokyoGeoPoint";
1324 } else {
1325 domain_name = "WGS84GeoPoint";
1326 }
1327
1328 if (top_left_point->header.domain != domain) {
1329 grn_obj_reinit(ctx, &(data->top_left_point_buffer), domain, GRN_BULK);
1330 if (grn_obj_cast(ctx,
1331 top_left_point,
1332 &(data->top_left_point_buffer),
1333 false)) {
1334 ERR(GRN_INVALID_ARGUMENT,
1335 "%s: failed to cast to %s: <%.*s>",
1336 process_name,
1337 domain_name,
1338 (int)GRN_TEXT_LEN(top_left_point),
1339 GRN_TEXT_VALUE(top_left_point));
1340 return;
1341 }
1342 top_left_point = &(data->top_left_point_buffer);
1343 }
1344 data->top_left = GRN_GEO_POINT_VALUE_RAW(top_left_point);
1345
1346 if (bottom_right_point->header.domain != domain) {
1347 grn_obj_reinit(ctx, &(data->bottom_right_point_buffer), domain, GRN_BULK);
1348 if (grn_obj_cast(ctx,
1349 bottom_right_point,
1350 &(data->bottom_right_point_buffer),
1351 false)) {
1352 ERR(GRN_INVALID_ARGUMENT,
1353 "%s: failed to cast to %s: <%.*s>",
1354 process_name,
1355 domain_name,
1356 (int)GRN_TEXT_LEN(bottom_right_point),
1357 GRN_TEXT_VALUE(bottom_right_point));
1358 return;
1359 }
1360 bottom_right_point = &(data->bottom_right_point_buffer);
1361 }
1362 data->bottom_right = GRN_GEO_POINT_VALUE_RAW(bottom_right_point);
1363
1364 data->center.latitude =
1365 data->top_left->latitude -
1366 (abs(data->top_left->latitude - data->bottom_right->latitude) / 2);
1367 data->center.longitude =
1368 data->bottom_right->longitude -
1369 (abs(data->bottom_right->longitude - data->top_left->longitude) / 2);
1370 }
1371
1372 static void
1373 in_rectangle_data_validate(grn_ctx *ctx,
1374 const char *process_name,
1375 in_rectangle_data *data)
1376 {
1377 grn_geo_point *top_left, *bottom_right;
1378
1379 top_left = data->top_left;
1380 bottom_right = data->bottom_right;
1381
1382 if (top_left->latitude >= GRN_GEO_MAX_LATITUDE) {
1383 ERR(GRN_INVALID_ARGUMENT,
1384 "%s: top left point's latitude is too big: "
1385 "<%d>(max:%d): (%d,%d) (%d,%d)",
1386 process_name,
1387 GRN_GEO_MAX_LATITUDE,
1388 top_left->latitude,
1389 top_left->latitude,
1390 top_left->longitude,
1391 bottom_right->latitude,
1392 bottom_right->longitude);
1393 return;
1394 }
1395
1396 if (top_left->latitude <= GRN_GEO_MIN_LATITUDE) {
1397 ERR(GRN_INVALID_ARGUMENT,
1398 "%s: top left point's latitude is too small: "
1399 "<%d>(min:%d): (%d,%d) (%d,%d)",
1400 process_name,
1401 GRN_GEO_MIN_LATITUDE,
1402 top_left->latitude,
1403 top_left->latitude,
1404 top_left->longitude,
1405 bottom_right->latitude,
1406 bottom_right->longitude);
1407 return;
1408 }
1409
1410 if (top_left->longitude >= GRN_GEO_MAX_LONGITUDE) {
1411 ERR(GRN_INVALID_ARGUMENT,
1412 "%s: top left point's longitude is too big: "
1413 "<%d>(max:%d): (%d,%d) (%d,%d)",
1414 process_name,
1415 GRN_GEO_MAX_LONGITUDE,
1416 top_left->longitude,
1417 top_left->latitude,
1418 top_left->longitude,
1419 bottom_right->latitude,
1420 bottom_right->longitude);
1421 return;
1422 }
1423
1424 if (top_left->longitude <= GRN_GEO_MIN_LONGITUDE) {
1425 ERR(GRN_INVALID_ARGUMENT,
1426 "%s: top left point's longitude is too small: "
1427 "<%d>(min:%d): (%d,%d) (%d,%d)",
1428 process_name,
1429 GRN_GEO_MIN_LONGITUDE,
1430 top_left->longitude,
1431 top_left->latitude,
1432 top_left->longitude,
1433 bottom_right->latitude,
1434 bottom_right->longitude);
1435 return;
1436 }
1437
1438 if (bottom_right->latitude >= GRN_GEO_MAX_LATITUDE) {
1439 ERR(GRN_INVALID_ARGUMENT,
1440 "%s: bottom right point's latitude is too big: "
1441 "<%d>(max:%d): (%d,%d) (%d,%d)",
1442 process_name,
1443 GRN_GEO_MAX_LATITUDE,
1444 bottom_right->latitude,
1445 top_left->latitude,
1446 top_left->longitude,
1447 bottom_right->latitude,
1448 bottom_right->longitude);
1449 return;
1450 }
1451
1452 if (bottom_right->latitude <= GRN_GEO_MIN_LATITUDE) {
1453 ERR(GRN_INVALID_ARGUMENT,
1454 "%s: bottom right point's latitude is too small: "
1455 "<%d>(min:%d): (%d,%d) (%d,%d)",
1456 process_name,
1457 GRN_GEO_MIN_LATITUDE,
1458 bottom_right->latitude,
1459 top_left->latitude,
1460 top_left->longitude,
1461 bottom_right->latitude,
1462 bottom_right->longitude);
1463 return;
1464 }
1465
1466 if (bottom_right->longitude >= GRN_GEO_MAX_LONGITUDE) {
1467 ERR(GRN_INVALID_ARGUMENT,
1468 "%s: bottom right point's longitude is too big: "
1469 "<%d>(max:%d): (%d,%d) (%d,%d)",
1470 process_name,
1471 GRN_GEO_MAX_LONGITUDE,
1472 bottom_right->longitude,
1473 top_left->latitude,
1474 top_left->longitude,
1475 bottom_right->latitude,
1476 bottom_right->longitude);
1477 return;
1478 }
1479
1480 if (bottom_right->longitude <= GRN_GEO_MIN_LONGITUDE) {
1481 ERR(GRN_INVALID_ARGUMENT,
1482 "%s: bottom right point's longitude is too small: "
1483 "<%d>(min:%d): (%d,%d) (%d,%d)",
1484 process_name,
1485 GRN_GEO_MIN_LONGITUDE,
1486 bottom_right->longitude,
1487 top_left->latitude,
1488 top_left->longitude,
1489 bottom_right->latitude,
1490 bottom_right->longitude);
1491 return;
1492 }
1493 }
1494
1495 static void
1496 in_rectangle_area_data_compute(grn_ctx *ctx,
1497 grn_geo_point *top_left,
1498 grn_geo_point *bottom_right,
1499 in_rectangle_area_data *data)
1500 {
1501 int latitude_distance, longitude_distance;
1502 uint8_t diff_bit;
1503 grn_geo_point base;
1504 grn_geo_point *geo_point_input;
1505 uint8_t geo_key_input[sizeof(grn_geo_point)];
1506 uint8_t geo_key_base[sizeof(grn_geo_point)];
1507 uint8_t geo_key_top_left[sizeof(grn_geo_point)];
1508 uint8_t geo_key_bottom_right[sizeof(grn_geo_point)];
1509
1510 latitude_distance = top_left->latitude - bottom_right->latitude;
1511 longitude_distance = bottom_right->longitude - top_left->longitude;
1512 if (latitude_distance > longitude_distance) {
1513 geo_point_input = bottom_right;
1514 base.latitude = bottom_right->latitude;
1515 base.longitude = bottom_right->longitude - longitude_distance;
1516 } else {
1517 geo_point_input = top_left;
1518 base.latitude = top_left->latitude - latitude_distance;
1519 base.longitude = top_left->longitude;
1520 }
1521 grn_gton(geo_key_input, geo_point_input, sizeof(grn_geo_point));
1522 grn_gton(geo_key_base, &base, sizeof(grn_geo_point));
1523 diff_bit = compute_diff_bit(geo_key_input, geo_key_base);
1524 compute_min_and_max(&base, diff_bit, &(data->min), &(data->max));
1525
1526 grn_gton(geo_key_top_left, top_left, sizeof(grn_geo_point));
1527 grn_gton(geo_key_bottom_right, bottom_right, sizeof(grn_geo_point));
1528 data->rectangle_common_bit =
1529 compute_diff_bit(geo_key_top_left, geo_key_bottom_right) - 1;
1530 compute_min_and_max_key(geo_key_top_left,
1531 data->rectangle_common_bit + 1,
1532 data->rectangle_common_key,
1533 NULL);
1534
1535 #ifdef GEO_DEBUG
1536 printf("base: ");
1537 grn_p_geo_point(ctx, &base);
1538 printf("min: ");
1539 grn_p_geo_point(ctx, &(data->min));
1540 printf("max: ");
1541 grn_p_geo_point(ctx, &(data->max));
1542 printf("top-left: ");
1543 grn_p_geo_point(ctx, top_left);
1544 printf("bottom-right: ");
1545 grn_p_geo_point(ctx, bottom_right);
1546 printf("rectangle-common-bit:%10d\n", data->rectangle_common_bit);
1547 printf("distance(latitude): %10d\n", latitude_distance);
1548 printf("distance(longitude): %10d\n", longitude_distance);
1549 #endif
1550 }
1551
1552 static grn_rc
1553 in_rectangle_data_prepare(grn_ctx *ctx,
1554 grn_obj *index,
1555 grn_obj *top_left_point,
1556 grn_obj *bottom_right_point,
1557 const char *process_name,
1558 in_rectangle_data *data)
1559 {
1560 in_rectangle_data_fill(ctx,
1561 index,
1562 top_left_point,
1563 bottom_right_point,
1564 process_name,
1565 data);
1566 if (ctx->rc != GRN_SUCCESS) {
1567 goto exit;
1568 }
1569
1570 in_rectangle_data_validate(ctx, process_name, data);
1571 if (ctx->rc != GRN_SUCCESS) {
1572 goto exit;
1573 }
1574
1575 exit:
1576 return ctx->rc;
1577 }
1578
1579 #define SAME_BIT_P(a, b, n_bit) \
1580 ((((uint8_t *)(a))[(n_bit) / 8] & (1 << (7 - ((n_bit) % 8)))) == \
1581 (((uint8_t *)(b))[(n_bit) / 8] & (1 << (7 - ((n_bit) % 8)))))
1582
1583 #define CURSOR_ENTRY_UPDATE_STATUS(entry, name, other_key) \
1584 do { \
1585 if (SAME_BIT_P((entry)->key, (other_key), (entry)->target_bit)) { \
1586 (entry)->status_flags |= GRN_GEO_CURSOR_ENTRY_STATUS_##name; \
1587 } else { \
1588 (entry)->status_flags &= ~GRN_GEO_CURSOR_ENTRY_STATUS_##name; \
1589 } \
1590 } while (0)
1591
1592 #define CURSOR_ENTRY_CHECK_STATUS(entry, name) \
1593 ((entry)->status_flags & GRN_GEO_CURSOR_ENTRY_STATUS_##name)
1594 #define CURSOR_ENTRY_IS_INNER(entry) \
1595 (((entry)->status_flags & (GRN_GEO_CURSOR_ENTRY_STATUS_LATITUDE_INNER | \
1596 GRN_GEO_CURSOR_ENTRY_STATUS_LONGITUDE_INNER)) == \
1597 (GRN_GEO_CURSOR_ENTRY_STATUS_LATITUDE_INNER | \
1598 GRN_GEO_CURSOR_ENTRY_STATUS_LONGITUDE_INNER))
1599 #define CURSOR_ENTRY_INCLUDED_IN_LATITUDE_DIRECTION(entry) \
1600 ((entry)->status_flags & (GRN_GEO_CURSOR_ENTRY_STATUS_LATITUDE_INNER | \
1601 GRN_GEO_CURSOR_ENTRY_STATUS_TOP_INCLUDED | \
1602 GRN_GEO_CURSOR_ENTRY_STATUS_BOTTOM_INCLUDED))
1603 #define CURSOR_ENTRY_INCLUDED_IN_LONGITUDE_DIRECTION(entry) \
1604 ((entry)->status_flags & (GRN_GEO_CURSOR_ENTRY_STATUS_LONGITUDE_INNER | \
1605 GRN_GEO_CURSOR_ENTRY_STATUS_LEFT_INCLUDED | \
1606 GRN_GEO_CURSOR_ENTRY_STATUS_RIGHT_INCLUDED))
1607
1608 #define SET_N_BIT(a, n_bit) \
1609 (((uint8_t *)(a))[((n_bit) / 8)] ^= (1 << (7 - ((n_bit) % 8))))
1610
1611 #define N_BIT(a, n_bit) \
1612 ((((uint8_t *)(a))[((n_bit) / 8)] & (1 << (7 - ((n_bit) % 8)))) >> \
1613 (1 << (7 - ((n_bit) % 8))))
1614
1615 static bool
1616 extract_rectangle_in_area(grn_ctx *ctx,
1617 grn_geo_area_type area_type,
1618 const grn_geo_point *top_left,
1619 const grn_geo_point *bottom_right,
1620 grn_geo_point *area_top_left,
1621 grn_geo_point *area_bottom_right)
1622 {
1623 bool out_of_area = false;
1624 bool cover_all_areas = false;
1625
1626 if ((GRN_GEO_POINT_IN_NORTH_WEST(top_left) &&
1627 GRN_GEO_POINT_IN_SOUTH_EAST(bottom_right)) ||
1628 (GRN_GEO_POINT_IN_NORTH_EAST(top_left) &&
1629 GRN_GEO_POINT_IN_SOUTH_WEST(bottom_right))) {
1630 cover_all_areas = true;
1631 }
1632
1633 switch (area_type) {
1634 case GRN_GEO_AREA_NORTH_EAST:
1635 if (cover_all_areas || GRN_GEO_POINT_IN_NORTH_EAST(top_left) ||
1636 GRN_GEO_POINT_IN_NORTH_EAST(bottom_right)) {
1637 area_top_left->latitude = MAX(top_left->latitude, 0);
1638 area_bottom_right->latitude = MAX(bottom_right->latitude, 0);
1639 if (GRN_GEO_LONGITUDE_IS_WRAPPED(top_left, bottom_right)) {
1640 area_top_left->longitude = top_left->longitude;
1641 area_bottom_right->longitude = GRN_GEO_MAX_LONGITUDE;
1642 } else {
1643 area_top_left->longitude = MAX(top_left->longitude, 0);
1644 area_bottom_right->longitude = MAX(bottom_right->longitude, 0);
1645 }
1646 } else {
1647 out_of_area = true;
1648 }
1649 break;
1650 case GRN_GEO_AREA_NORTH_WEST:
1651 if (cover_all_areas || GRN_GEO_POINT_IN_NORTH_WEST(top_left) ||
1652 GRN_GEO_POINT_IN_NORTH_WEST(bottom_right)) {
1653 area_top_left->latitude = MAX(top_left->latitude, 0);
1654 area_bottom_right->latitude = MAX(bottom_right->latitude, 0);
1655 if (GRN_GEO_LONGITUDE_IS_WRAPPED(top_left, bottom_right)) {
1656 area_top_left->longitude = GRN_GEO_MIN_LONGITUDE;
1657 area_bottom_right->longitude = bottom_right->longitude;
1658 } else {
1659 area_top_left->longitude = MIN(top_left->longitude, -1);
1660 area_bottom_right->longitude = MIN(bottom_right->longitude, -1);
1661 }
1662 } else {
1663 out_of_area = true;
1664 }
1665 break;
1666 case GRN_GEO_AREA_SOUTH_WEST:
1667 if (cover_all_areas || GRN_GEO_POINT_IN_SOUTH_WEST(top_left) ||
1668 GRN_GEO_POINT_IN_SOUTH_WEST(bottom_right)) {
1669 area_top_left->latitude = MIN(top_left->latitude, -1);
1670 area_bottom_right->latitude = MIN(bottom_right->latitude, -1);
1671 if (GRN_GEO_LONGITUDE_IS_WRAPPED(top_left, bottom_right)) {
1672 area_top_left->longitude = GRN_GEO_MIN_LONGITUDE;
1673 area_bottom_right->longitude = bottom_right->longitude;
1674 } else {
1675 area_top_left->longitude = MIN(top_left->longitude, -1);
1676 area_bottom_right->longitude = MIN(bottom_right->longitude, -1);
1677 }
1678 } else {
1679 out_of_area = true;
1680 }
1681 break;
1682 case GRN_GEO_AREA_SOUTH_EAST:
1683 if (cover_all_areas || GRN_GEO_POINT_IN_SOUTH_EAST(top_left) ||
1684 GRN_GEO_POINT_IN_SOUTH_EAST(bottom_right)) {
1685 area_top_left->latitude = MIN(top_left->latitude, -1);
1686 area_bottom_right->latitude = MIN(bottom_right->latitude, -1);
1687 if (GRN_GEO_LONGITUDE_IS_WRAPPED(top_left, bottom_right)) {
1688 area_top_left->longitude = top_left->longitude;
1689 area_bottom_right->longitude = GRN_GEO_MAX_LONGITUDE;
1690 } else {
1691 area_top_left->longitude = MAX(top_left->longitude, 0);
1692 area_bottom_right->longitude = MAX(bottom_right->longitude, 0);
1693 }
1694 } else {
1695 out_of_area = true;
1696 }
1697 break;
1698 default:
1699 out_of_area = true;
1700 break;
1701 }
1702
1703 return out_of_area;
1704 }
1705
1706 static void
1707 grn_geo_cursor_area_init(grn_ctx *ctx,
1708 grn_geo_cursor_area *area,
1709 grn_geo_area_type area_type,
1710 const grn_geo_point *top_left,
1711 const grn_geo_point *bottom_right)
1712 {
1713 grn_geo_point area_top_left, area_bottom_right;
1714 in_rectangle_area_data data;
1715 grn_geo_cursor_entry *entry;
1716 bool out_of_area;
1717
1718 out_of_area = extract_rectangle_in_area(ctx,
1719 area_type,
1720 top_left,
1721 bottom_right,
1722 &area_top_left,
1723 &area_bottom_right);
1724 if (out_of_area) {
1725 area->current_entry = -1;
1726 return;
1727 }
1728
1729 area->current_entry = 0;
1730 grn_memcpy(&(area->top_left), &area_top_left, sizeof(grn_geo_point));
1731 grn_memcpy(&(area->bottom_right), &area_bottom_right, sizeof(grn_geo_point));
1732 grn_gton(area->top_left_key, &area_top_left, sizeof(grn_geo_point));
1733 grn_gton(area->bottom_right_key, &area_bottom_right, sizeof(grn_geo_point));
1734
1735 entry = &(area->entries[area->current_entry]);
1736 in_rectangle_area_data_compute(ctx,
1737 &area_top_left,
1738 &area_bottom_right,
1739 &data);
1740 entry->target_bit = data.rectangle_common_bit;
1741 grn_memcpy(entry->key, data.rectangle_common_key, sizeof(grn_geo_point));
1742 entry->status_flags = GRN_GEO_CURSOR_ENTRY_STATUS_TOP_INCLUDED |
1743 GRN_GEO_CURSOR_ENTRY_STATUS_BOTTOM_INCLUDED |
1744 GRN_GEO_CURSOR_ENTRY_STATUS_LEFT_INCLUDED |
1745 GRN_GEO_CURSOR_ENTRY_STATUS_RIGHT_INCLUDED;
1746 if (data.min.latitude == area_bottom_right.latitude &&
1747 data.max.latitude == area_top_left.latitude) {
1748 entry->status_flags |= GRN_GEO_CURSOR_ENTRY_STATUS_LATITUDE_INNER;
1749 }
1750 if (data.min.longitude == area_top_left.longitude &&
1751 data.max.longitude == area_bottom_right.longitude) {
1752 entry->status_flags |= GRN_GEO_CURSOR_ENTRY_STATUS_LONGITUDE_INNER;
1753 }
1754 }
1755
1756 grn_obj *
1757 grn_geo_cursor_open_in_rectangle(grn_ctx *ctx,
1758 grn_obj *index,
1759 grn_obj *top_left_point,
1760 grn_obj *bottom_right_point,
1761 int offset,
1762 int limit)
1763 {
1764 grn_geo_cursor_in_rectangle *cursor = NULL;
1765 in_rectangle_data data;
1766
1767 GRN_API_ENTER;
1768 GRN_VOID_INIT(&(data.top_left_point_buffer));
1769 GRN_VOID_INIT(&(data.bottom_right_point_buffer));
1770 if (in_rectangle_data_prepare(ctx,
1771 index,
1772 top_left_point,
1773 bottom_right_point,
1774 "geo_in_rectangle()",
1775 &data)) {
1776 goto exit;
1777 }
1778
1779 cursor = GRN_CALLOC(sizeof(grn_geo_cursor_in_rectangle));
1780 if (!cursor) {
1781 ERR(GRN_NO_MEMORY_AVAILABLE,
1782 "[geo][cursor][in-rectangle] failed to allocate memory for geo cursor");
1783 goto exit;
1784 }
1785
1786 cursor->pat = data.pat;
1787 grn_obj_refer(ctx, cursor->pat);
1788 cursor->index = index;
1789 grn_obj_refer(ctx, cursor->index);
1790 cursor->top_left = *(data.top_left);
1791 cursor->bottom_right = *(data.bottom_right);
1792 cursor->center = data.center;
1793 cursor->need_distance = false;
1794 cursor->pat_cursor = NULL;
1795 cursor->ii_cursor = NULL;
1796 cursor->offset = offset;
1797 cursor->rest = limit;
1798
1799 cursor->current_area = GRN_GEO_AREA_NORTH_EAST;
1800 {
1801 grn_geo_area_type area_type;
1802 const grn_geo_point *top_left = &(cursor->top_left);
1803 const grn_geo_point *bottom_right = &(cursor->bottom_right);
1804 for (area_type = GRN_GEO_AREA_NORTH_EAST; area_type < GRN_GEO_AREA_LAST;
1805 area_type++) {
1806 grn_geo_cursor_area_init(ctx,
1807 &(cursor->areas[area_type]),
1808 area_type,
1809 top_left,
1810 bottom_right);
1811 }
1812 }
1813 {
1814 char minimum_reduce_bit_env[GRN_ENV_BUFFER_SIZE];
1815 cursor->minimum_reduce_bit = 0;
1816 grn_getenv("GRN_GEO_IN_RECTANGLE_MINIMUM_REDUCE_BIT",
1817 minimum_reduce_bit_env,
1818 GRN_ENV_BUFFER_SIZE);
1819 if (minimum_reduce_bit_env[0]) {
1820 cursor->minimum_reduce_bit = atoi(minimum_reduce_bit_env);
1821 }
1822 if (cursor->minimum_reduce_bit < 1) {
1823 cursor->minimum_reduce_bit = 1;
1824 }
1825 }
1826 GRN_DB_OBJ_SET_TYPE(cursor, GRN_CURSOR_COLUMN_GEO_INDEX);
1827 {
1828 grn_obj *db;
1829 grn_id id;
1830 db = grn_ctx_db(ctx);
1831 id = grn_obj_register(ctx, db, NULL, 0);
1832 DB_OBJ(cursor)->header.domain = GRN_ID_NIL;
1833 DB_OBJ(cursor)->range = GRN_ID_NIL;
1834 grn_db_obj_init(ctx, db, id, DB_OBJ(cursor));
1835 }
1836
1837 exit:
1838 grn_obj_unlink(ctx, &(data.top_left_point_buffer));
1839 grn_obj_unlink(ctx, &(data.bottom_right_point_buffer));
1840 GRN_API_RETURN((grn_obj *)cursor);
1841 }
1842
1843 static void
1844 grn_geo_cursor_in_rectangle_set_need_distance(grn_ctx *ctx,
1845 grn_obj *cursor,
1846 bool need_distance)
1847 {
1848 grn_geo_cursor_in_rectangle *cursor_in_rectangle =
1849 (grn_geo_cursor_in_rectangle *)cursor;
1850 cursor_in_rectangle->need_distance = need_distance;
1851 }
1852
1853 static inline bool
1854 grn_geo_cursor_entry_next_push(grn_ctx *ctx,
1855 grn_geo_cursor_in_rectangle *cursor,
1856 grn_geo_cursor_entry *entry)
1857 {
1858 grn_geo_cursor_entry *next_entry;
1859 grn_geo_point entry_base;
1860 grn_table_cursor *pat_cursor;
1861 bool pushed = false;
1862
1863 grn_ntog((uint8_t *)(&entry_base), entry->key, sizeof(grn_geo_point));
1864 pat_cursor =
1865 grn_table_cursor_open(ctx,
1866 cursor->pat,
1867 &entry_base,
1868 (unsigned int)(entry->target_bit + 1),
1869 NULL,
1870 0,
1871 0,
1872 -1,
1873 GRN_CURSOR_PREFIX | GRN_CURSOR_SIZE_BY_BIT);
1874 if (pat_cursor) {
1875 if (grn_table_cursor_next(ctx, pat_cursor)) {
1876 grn_geo_cursor_area *area;
1877 area = &(cursor->areas[cursor->current_area]);
1878 next_entry = &(area->entries[++area->current_entry]);
1879 grn_memcpy(next_entry, entry, sizeof(grn_geo_cursor_entry));
1880 pushed = true;
1881 }
1882 grn_table_cursor_close(ctx, pat_cursor);
1883 }
1884
1885 return pushed;
1886 }
1887
1888 static inline bool
1889 grn_geo_cursor_entry_next(grn_ctx *ctx,
1890 grn_geo_cursor_in_rectangle *cursor,
1891 grn_geo_cursor_entry *entry)
1892 {
1893 uint8_t *top_left_key;
1894 uint8_t *bottom_right_key;
1895 int max_target_bit = GRN_GEO_KEY_MAX_BITS - cursor->minimum_reduce_bit;
1896 grn_geo_cursor_area *area = NULL;
1897
1898 while (cursor->current_area < GRN_GEO_AREA_LAST) {
1899 area = &(cursor->areas[cursor->current_area]);
1900 if (area->current_entry >= 0) {
1901 break;
1902 }
1903 cursor->current_area++;
1904 area = NULL;
1905 }
1906
1907 if (!area) {
1908 return false;
1909 }
1910
1911 top_left_key = area->top_left_key;
1912 bottom_right_key = area->bottom_right_key;
1913 grn_memcpy(entry,
1914 &(area->entries[area->current_entry--]),
1915 sizeof(grn_geo_cursor_entry));
1916 while (true) {
1917 grn_geo_cursor_entry next_entry0, next_entry1;
1918 bool pushed = false;
1919
1920 /*
1921 top_left_key: tl
1922 bottom_right_key: br
1923
1924 e.g.: top_left_key is at the top left sub mesh and
1925 bottom_right_key is at the bottom right sub mesh.
1926 top_left_key is also at the top left - bottom right
1927 sub-sub mesh and
1928 bottom_right_key is at the bottom right - bottom left
1929 sub-sub mesh.
1930
1931 ^latitude +----+----+----+----+
1932 | 1 |1010|1011|1110|1111|
1933 | | | | | |
1934 | 1 +----+----+----+----+
1935 \/ 0 |1000|1001|1100|1101|
1936 | | tl | | |
1937 +----+----+----+----+
1938 1 |0010|0011|0110|0111|
1939 | | | | |
1940 0 +----+----+----+----+
1941 0 |0000|0001|0100|0101|
1942 | | | br | |
1943 +----+----+----+----+
1944 0 1 0 1
1945 |-------| |-------|
1946 0 1
1947 <------>
1948 longitude
1949
1950 entry.target_bit + 1 -> next_entry0
1951 entry.target_bit + 1 and entry.key ^ (entry.target_bit + 1) in bit
1952 -> next_entry1
1953
1954 entry: represents the biggest mesh.
1955 (1010, 1011, 1110, 1111,
1956 1000, 1001, 1100, 1101,
1957 0010, 0011, 0110, 0111,
1958 0000, 0001, 0100, 0101)
1959 next_entry0: represents bottom sub-mesh.
1960 (0010, 0011, 0110, 0111,
1961 0000, 0001, 0100, 0101)
1962 next_entry1: represents top sub-mesh.
1963 (1010, 1011, 1110, 1111,
1964 1000, 1001, 1100, 1101)
1965
1966 entry->status_flags = TOP_INCLUDED |
1967 BOTTOM_INCLUDED |
1968 LEFT_INCLUDED |
1969 RIGHT_INCLUDED
1970 next_entry0->status_flags = BOTTOM_INCLUDED |
1971 LEFT_INCLUDED |
1972 RIGHT_INCLUDED
1973 next_entry1->status_flags = TOP_INCLUDED |
1974 LEFT_INCLUDED |
1975 RIGHT_INCLUDED
1976
1977 Both next_entry1 and next_entry0 are pushed to the stack in cursor.
1978 */
1979
1980 #ifdef GEO_DEBUG
1981 inspect_cursor_entry(ctx, entry);
1982 #endif
1983
1984 if (entry->target_bit >= max_target_bit) {
1985 #ifdef GEO_DEBUG
1986 printf("%d: force stopping to reduce a mesh\n", entry->target_bit);
1987 #endif
1988 break;
1989 }
1990
1991 if (CURSOR_ENTRY_IS_INNER(entry)) {
1992 #ifdef GEO_DEBUG
1993 printf("%d: inner entries\n", entry->target_bit);
1994 #endif
1995 break;
1996 }
1997
1998 grn_memcpy(&next_entry0, entry, sizeof(grn_geo_cursor_entry));
1999 next_entry0.target_bit++;
2000 grn_memcpy(&next_entry1, entry, sizeof(grn_geo_cursor_entry));
2001 next_entry1.target_bit++;
2002 SET_N_BIT(next_entry1.key, next_entry1.target_bit);
2003
2004 #ifdef GEO_DEBUG
2005 inspect_cursor_entry_targets(ctx,
2006 entry,
2007 top_left_key,
2008 bottom_right_key,
2009 &next_entry0,
2010 &next_entry1);
2011 #endif
2012
2013 if ((entry->target_bit + 1) % 2 == 0) {
2014 if (CURSOR_ENTRY_CHECK_STATUS(entry, TOP_INCLUDED)) {
2015 CURSOR_ENTRY_UPDATE_STATUS(&next_entry0, TOP_INCLUDED, top_left_key);
2016 CURSOR_ENTRY_UPDATE_STATUS(&next_entry1, TOP_INCLUDED, top_left_key);
2017 }
2018 if (CURSOR_ENTRY_CHECK_STATUS(entry, BOTTOM_INCLUDED)) {
2019 CURSOR_ENTRY_UPDATE_STATUS(&next_entry0,
2020 BOTTOM_INCLUDED,
2021 bottom_right_key);
2022 CURSOR_ENTRY_UPDATE_STATUS(&next_entry1,
2023 BOTTOM_INCLUDED,
2024 bottom_right_key);
2025 }
2026
2027 if (CURSOR_ENTRY_CHECK_STATUS(entry, TOP_INCLUDED) &&
2028 !CURSOR_ENTRY_CHECK_STATUS(entry, BOTTOM_INCLUDED) &&
2029 CURSOR_ENTRY_CHECK_STATUS(&next_entry1, TOP_INCLUDED)) {
2030 next_entry0.status_flags |= GRN_GEO_CURSOR_ENTRY_STATUS_LATITUDE_INNER;
2031 } else if (!CURSOR_ENTRY_CHECK_STATUS(entry, TOP_INCLUDED) &&
2032 CURSOR_ENTRY_CHECK_STATUS(entry, BOTTOM_INCLUDED) &&
2033 CURSOR_ENTRY_CHECK_STATUS(&next_entry0, BOTTOM_INCLUDED)) {
2034 next_entry1.status_flags |= GRN_GEO_CURSOR_ENTRY_STATUS_LATITUDE_INNER;
2035 }
2036
2037 if (CURSOR_ENTRY_INCLUDED_IN_LATITUDE_DIRECTION(&next_entry1)) {
2038 if (grn_geo_cursor_entry_next_push(ctx, cursor, &next_entry1)) {
2039 pushed = true;
2040 #ifdef GEO_DEBUG
2041 printf("%d: latitude: push 1\n", next_entry1.target_bit);
2042 #endif
2043 }
2044 }
2045 if (CURSOR_ENTRY_INCLUDED_IN_LATITUDE_DIRECTION(&next_entry0)) {
2046 if (grn_geo_cursor_entry_next_push(ctx, cursor, &next_entry0)) {
2047 pushed = true;
2048 #ifdef GEO_DEBUG
2049 printf("%d: latitude: push 0\n", next_entry0.target_bit);
2050 #endif
2051 }
2052 }
2053 } else {
2054 if (CURSOR_ENTRY_CHECK_STATUS(entry, RIGHT_INCLUDED)) {
2055 CURSOR_ENTRY_UPDATE_STATUS(&next_entry0,
2056 RIGHT_INCLUDED,
2057 bottom_right_key);
2058 CURSOR_ENTRY_UPDATE_STATUS(&next_entry1,
2059 RIGHT_INCLUDED,
2060 bottom_right_key);
2061 }
2062 if (CURSOR_ENTRY_CHECK_STATUS(entry, LEFT_INCLUDED)) {
2063 CURSOR_ENTRY_UPDATE_STATUS(&next_entry0, LEFT_INCLUDED, top_left_key);
2064 CURSOR_ENTRY_UPDATE_STATUS(&next_entry1, LEFT_INCLUDED, top_left_key);
2065 }
2066
2067 if (CURSOR_ENTRY_CHECK_STATUS(entry, LEFT_INCLUDED) &&
2068 !CURSOR_ENTRY_CHECK_STATUS(entry, RIGHT_INCLUDED) &&
2069 CURSOR_ENTRY_CHECK_STATUS(&next_entry0, LEFT_INCLUDED)) {
2070 next_entry1.status_flags |= GRN_GEO_CURSOR_ENTRY_STATUS_LONGITUDE_INNER;
2071 } else if (!CURSOR_ENTRY_CHECK_STATUS(entry, LEFT_INCLUDED) &&
2072 CURSOR_ENTRY_CHECK_STATUS(entry, RIGHT_INCLUDED) &&
2073 CURSOR_ENTRY_CHECK_STATUS(&next_entry1, RIGHT_INCLUDED)) {
2074 next_entry0.status_flags |= GRN_GEO_CURSOR_ENTRY_STATUS_LONGITUDE_INNER;
2075 }
2076
2077 if (CURSOR_ENTRY_INCLUDED_IN_LONGITUDE_DIRECTION(&next_entry1)) {
2078 if (grn_geo_cursor_entry_next_push(ctx, cursor, &next_entry1)) {
2079 pushed = true;
2080 #ifdef GEO_DEBUG
2081 printf("%d: longitude: push 1\n", next_entry1.target_bit);
2082 #endif
2083 }
2084 }
2085 if (CURSOR_ENTRY_INCLUDED_IN_LONGITUDE_DIRECTION(&next_entry0)) {
2086 if (grn_geo_cursor_entry_next_push(ctx, cursor, &next_entry0)) {
2087 pushed = true;
2088 #ifdef GEO_DEBUG
2089 printf("%d: longitude: push 0\n", next_entry0.target_bit);
2090 #endif
2091 }
2092 }
2093 }
2094
2095 if (pushed) {
2096 #ifdef GEO_DEBUG
2097 int i;
2098
2099 printf("%d: pushed\n", entry->target_bit);
2100 printf("stack:\n");
2101 for (i = area->current_entry; i >= 0; i--) {
2102 grn_geo_cursor_entry *stack_entry;
2103 stack_entry = &(area->entries[i]);
2104 printf("%2d: ", i);
2105 inspect_key(ctx, stack_entry->key);
2106 printf(" ");
2107 print_key_mark(ctx, stack_entry->target_bit);
2108 }
2109 #endif
2110 grn_memcpy(entry,
2111 &(area->entries[area->current_entry--]),
2112 sizeof(grn_geo_cursor_entry));
2113 #ifdef GEO_DEBUG
2114 printf("%d: pop entry\n", entry->target_bit);
2115 #endif
2116 } else {
2117 break;
2118 }
2119 }
2120
2121 #ifdef GEO_DEBUG
2122 printf("found:\n");
2123 inspect_cursor_entry(ctx, entry);
2124 #endif
2125
2126 return true;
2127 }
2128
2129 typedef bool (*grn_geo_cursor_callback)(grn_ctx *ctx,
2130 grn_posting *posting,
2131 double distance,
2132 void *user_data);
2133
2134 static void
2135 grn_geo_cursor_each(grn_ctx *ctx,
2136 grn_obj *geo_cursor,
2137 grn_geo_cursor_callback callback,
2138 void *user_data)
2139 {
2140 grn_geo_cursor_in_rectangle *cursor;
2141 grn_obj *pat;
2142 grn_table_cursor *pat_cursor;
2143 grn_ii *ii;
2144 grn_ii_cursor *ii_cursor;
2145 grn_posting *posting = NULL;
2146 grn_geo_point *current, *top_left, *bottom_right;
2147 grn_id index_id;
2148
2149 cursor = (grn_geo_cursor_in_rectangle *)geo_cursor;
2150 if (cursor->rest == 0) {
2151 return;
2152 }
2153
2154 pat = cursor->pat;
2155 pat_cursor = cursor->pat_cursor;
2156 ii = (grn_ii *)(cursor->index);
2157 ii_cursor = cursor->ii_cursor;
2158 current = &(cursor->current);
2159 top_left = &(cursor->top_left);
2160 bottom_right = &(cursor->bottom_right);
2161
2162 while (true) {
2163 if (!pat_cursor) {
2164 grn_geo_cursor_entry entry;
2165 grn_geo_point entry_base;
2166 if (!grn_geo_cursor_entry_next(ctx, cursor, &entry)) {
2167 cursor->rest = 0;
2168 return;
2169 }
2170 grn_ntog((uint8_t *)(&entry_base), entry.key, sizeof(grn_geo_point));
2171 if (!(cursor->pat_cursor = pat_cursor = grn_table_cursor_open(
2172 ctx,
2173 pat,
2174 &entry_base,
2175 (unsigned int)(entry.target_bit + 1),
2176 NULL,
2177 0,
2178 0,
2179 -1,
2180 GRN_CURSOR_PREFIX | GRN_CURSOR_SIZE_BY_BIT))) {
2181 cursor->rest = 0;
2182 return;
2183 }
2184 #ifdef GEO_DEBUG
2185 inspect_mesh(ctx, &entry_base, entry.target_bit, 0);
2186 #endif
2187 }
2188
2189 while (ii_cursor || (index_id = grn_table_cursor_next(ctx, pat_cursor))) {
2190 if (!ii_cursor) {
2191 grn_table_get_key(ctx, pat, index_id, current, sizeof(grn_geo_point));
2192 if (grn_geo_in_rectangle_raw(ctx, current, top_left, bottom_right)) {
2193 inspect_tid(ctx, index_id, current, 0);
2194 if (!(cursor->ii_cursor = ii_cursor =
2195 grn_ii_cursor_open(ctx,
2196 ii,
2197 index_id,
2198 GRN_ID_NIL,
2199 GRN_ID_MAX,
2200 (int)(ii->n_elements),
2201 0))) {
2202 continue;
2203 }
2204 } else {
2205 continue;
2206 }
2207 }
2208
2209 while ((posting = grn_ii_cursor_next(ctx, ii_cursor))) {
2210 if (cursor->offset == 0) {
2211 double distance = 0.0;
2212 if (cursor->need_distance) {
2213 distance = grn_geo_distance_rectangle_raw(ctx,
2214 &(cursor->center),
2215 &(cursor->current));
2216 }
2217 bool keep_each = callback(ctx, posting, distance, user_data);
2218 if (cursor->rest > 0) {
2219 if (--(cursor->rest) == 0) {
2220 keep_each = false;
2221 }
2222 }
2223 if (!keep_each) {
2224 return;
2225 }
2226 } else {
2227 cursor->offset--;
2228 }
2229 }
2230 grn_ii_cursor_close(ctx, ii_cursor);
2231 cursor->ii_cursor = ii_cursor = NULL;
2232 }
2233 grn_table_cursor_close(ctx, pat_cursor);
2234 cursor->pat_cursor = pat_cursor = NULL;
2235 }
2236 }
2237
2238 static bool
2239 grn_geo_cursor_next_callback(grn_ctx *ctx,
2240 grn_posting *posting,
2241 double distance,
2242 void *user_data)
2243 {
2244 grn_posting **return_posting = user_data;
2245 *return_posting = posting;
2246 return false;
2247 }
2248
2249 grn_posting *
2250 grn_geo_cursor_next(grn_ctx *ctx, grn_obj *geo_cursor)
2251 {
2252 grn_posting *posting = NULL;
2253 grn_geo_cursor_each(ctx, geo_cursor, grn_geo_cursor_next_callback, &posting);
2254 return posting;
2255 }
2256
2257 grn_rc
2258 grn_geo_cursor_close(grn_ctx *ctx, grn_obj *geo_cursor)
2259 {
2260 grn_geo_cursor_in_rectangle *cursor;
2261
2262 if (!geo_cursor) {
2263 return GRN_INVALID_ARGUMENT;
2264 }
2265
2266 cursor = (grn_geo_cursor_in_rectangle *)geo_cursor;
2267 if (cursor->pat) {
2268 grn_obj_unlink(ctx, cursor->pat);
2269 }
2270 if (cursor->index) {
2271 grn_obj_unlink(ctx, cursor->index);
2272 }
2273 if (cursor->pat_cursor) {
2274 grn_table_cursor_close(ctx, cursor->pat_cursor);
2275 }
2276 if (cursor->ii_cursor) {
2277 grn_ii_cursor_close(ctx, cursor->ii_cursor);
2278 }
2279 GRN_FREE(cursor);
2280
2281 return GRN_SUCCESS;
2282 }
2283
2284 typedef struct {
2285 grn_selector_data *selector_data;
2286 bool use_distance;
2287 grn_hash *res;
2288 grn_operator op;
2289 } grn_geo_select_in_rectangle_data;
2290
2291 static bool
2292 grn_geo_select_in_rectangle_callback(grn_ctx *ctx,
2293 grn_posting *posting,
2294 double distance,
2295 void *user_data)
2296 {
2297 grn_geo_select_in_rectangle_data *data = user_data;
2298 grn_posting_internal add_posting = *((grn_posting_internal *)posting);
2299 if (data->use_distance) {
2300 add_posting.weight_float =
2301 (float)((add_posting.weight_float + 1.0) * distance);
2302 } else {
2303 add_posting.weight_float += 1.0f;
2304 }
2305 grn_ii_posting_add_float(ctx,
2306 (grn_posting *)(&add_posting),
2307 data->res,
2308 data->op);
2309 return ctx->rc == GRN_SUCCESS;
2310 }
2311
2312 grn_rc
2313 grn_geo_select_in_rectangle(grn_ctx *ctx,
2314 grn_obj *index,
2315 grn_obj *top_left_point,
2316 grn_obj *bottom_right_point,
2317 grn_obj *res,
2318 grn_operator op)
2319 {
2320 grn_obj *cursor;
2321
2322 grn_geo_select_in_rectangle_data data;
2323 data.selector_data = grn_selector_data_get(ctx);
2324 data.use_distance =
2325 (data.selector_data &&
2326 grn_selector_data_have_score_column(ctx, data.selector_data));
2327 data.res = (grn_hash *)res;
2328 data.op = op;
2329 cursor = grn_geo_cursor_open_in_rectangle(ctx,
2330 index,
2331 top_left_point,
2332 bottom_right_point,
2333 0,
2334 -1);
2335 if (cursor) {
2336 grn_geo_cursor_in_rectangle_set_need_distance(ctx,
2337 cursor,
2338 data.use_distance);
2339 grn_geo_cursor_each(ctx,
2340 cursor,
2341 grn_geo_select_in_rectangle_callback,
2342 &data);
2343 grn_obj_unlink(ctx, cursor);
2344 grn_ii_resolve_sel_and(ctx, (grn_hash *)res, op);
2345 }
2346
2347 return ctx->rc;
2348 }
2349
2350 static grn_rc
2351 geo_point_get(grn_ctx *ctx, grn_obj *pat, int flags, grn_geo_point *geo_point)
2352 {
2353 grn_rc rc = GRN_SUCCESS;
2354 grn_id id;
2355 grn_table_cursor *cursor = NULL;
2356
2357 cursor = grn_table_cursor_open(ctx,
2358 pat,
2359 NULL,
2360 0,
2361 NULL,
2362 0,
2363 0,
2364 1,
2365 GRN_CURSOR_BY_KEY | flags);
2366 if (!cursor) {
2367 rc = ctx->rc;
2368 goto exit;
2369 }
2370
2371 id = grn_table_cursor_next(ctx, cursor);
2372 if (id == GRN_ID_NIL) {
2373 rc = GRN_END_OF_DATA;
2374 } else {
2375 void *key;
2376 int key_size;
2377 key_size = grn_table_cursor_get_key(ctx, cursor, &key);
2378 grn_memcpy(geo_point, key, (size_t)key_size);
2379 }
2380
2381 exit:
2382 if (cursor) {
2383 grn_table_cursor_close(ctx, cursor);
2384 }
2385 return rc;
2386 }
2387
2388 uint32_t
2389 grn_geo_estimate_size_in_rectangle(grn_ctx *ctx,
2390 grn_obj *index,
2391 grn_obj *top_left_point,
2392 grn_obj *bottom_right_point)
2393 {
2394 uint32_t n = 0;
2395 unsigned int total_records;
2396 grn_rc rc;
2397 in_rectangle_data data;
2398
2399 GRN_VOID_INIT(&(data.top_left_point_buffer));
2400 GRN_VOID_INIT(&(data.bottom_right_point_buffer));
2401 if (in_rectangle_data_prepare(ctx,
2402 index,
2403 top_left_point,
2404 bottom_right_point,
2405 "grn_geo_estimate_in_rectangle()",
2406 &data)) {
2407 goto exit;
2408 }
2409
2410 total_records = grn_table_size(ctx, data.pat);
2411 if (total_records > 0) {
2412 grn_geo_point min, max;
2413 int select_latitude_distance, select_longitude_distance;
2414 int total_latitude_distance, total_longitude_distance;
2415 double select_ratio;
2416 double estimated_n_records;
2417 in_rectangle_area_data area_data;
2418
2419 rc = geo_point_get(ctx, data.pat, GRN_CURSOR_ASCENDING, &min);
2420 if (!rc) {
2421 rc = geo_point_get(ctx, data.pat, GRN_CURSOR_DESCENDING, &max);
2422 }
2423 if (rc) {
2424 if (rc == GRN_END_OF_DATA) {
2425 n = total_records;
2426 rc = GRN_SUCCESS;
2427 }
2428 goto exit;
2429 }
2430
2431 in_rectangle_area_data_compute(ctx,
2432 data.top_left,
2433 data.bottom_right,
2434 &area_data);
2435 select_latitude_distance =
2436 abs(area_data.max.latitude - area_data.min.latitude);
2437 select_longitude_distance =
2438 abs(area_data.max.longitude - area_data.min.longitude);
2439 total_latitude_distance = abs(max.latitude - min.latitude);
2440 total_longitude_distance = abs(max.longitude - min.longitude);
2441
2442 select_ratio = 1.0;
2443 if (select_latitude_distance < total_latitude_distance) {
2444 select_ratio *=
2445 ((double)select_latitude_distance / (double)total_latitude_distance);
2446 }
2447 if (select_longitude_distance < total_longitude_distance) {
2448 select_ratio *=
2449 ((double)select_longitude_distance / (double)total_longitude_distance);
2450 }
2451 estimated_n_records = ceil(total_records * select_ratio);
2452 n = (uint32_t)estimated_n_records;
2453 }
2454
2455 exit:
2456 grn_obj_unlink(ctx, &(data.top_left_point_buffer));
2457 grn_obj_unlink(ctx, &(data.bottom_right_point_buffer));
2458 return n;
2459 }
2460
2461 int
2462 grn_geo_estimate_in_rectangle(grn_ctx *ctx,
2463 grn_obj *index,
2464 grn_obj *top_left_point,
2465 grn_obj *bottom_right_point)
2466 {
2467 uint32_t size;
2468
2469 size = grn_geo_estimate_size_in_rectangle(ctx,
2470 index,
2471 top_left_point,
2472 bottom_right_point);
2473 if (ctx->rc != GRN_SUCCESS) {
2474 return -1;
2475 }
2476
2477 return (int)size;
2478 }
2479
2480 bool
2481 grn_geo_in_circle(grn_ctx *ctx,
2482 grn_obj *point,
2483 grn_obj *center,
2484 grn_obj *radius_or_point,
2485 grn_geo_approximate_type approximate_type)
2486 {
2487 bool r = false;
2488 grn_obj center_, radius_or_point_;
2489 grn_id domain = point->header.domain;
2490 if (domain == GRN_DB_TOKYO_GEO_POINT || domain == GRN_DB_WGS84_GEO_POINT) {
2491 grn_geo_distance_raw_func distance_raw_func;
2492 double d;
2493 if (center->header.domain != domain) {
2494 GRN_OBJ_INIT(&center_, GRN_BULK, 0, domain);
2495 if (grn_obj_cast(ctx, center, &center_, false)) {
2496 goto exit;
2497 }
2498 center = &center_;
2499 }
2500
2501 distance_raw_func =
2502 grn_geo_resolve_distance_raw_func(ctx, approximate_type, domain);
2503 if (!distance_raw_func) {
2504 ERR(GRN_INVALID_ARGUMENT,
2505 "unknown approximate type: <%d>",
2506 approximate_type);
2507 goto exit;
2508 }
2509 d = distance_raw_func(ctx,
2510 GRN_GEO_POINT_VALUE_RAW(point),
2511 GRN_GEO_POINT_VALUE_RAW(center));
2512 switch (radius_or_point->header.domain) {
2513 case GRN_DB_INT32:
2514 r = d <= GRN_INT32_VALUE(radius_or_point);
2515 break;
2516 case GRN_DB_UINT32:
2517 r = d <= GRN_UINT32_VALUE(radius_or_point);
2518 break;
2519 case GRN_DB_INT64:
2520 r = d <= GRN_INT64_VALUE(radius_or_point);
2521 break;
2522 case GRN_DB_UINT64:
2523 r = d <= GRN_UINT64_VALUE(radius_or_point);
2524 break;
2525 case GRN_DB_FLOAT32:
2526 r = d <= GRN_FLOAT32_VALUE(radius_or_point);
2527 break;
2528 case GRN_DB_FLOAT:
2529 r = d <= GRN_FLOAT_VALUE(radius_or_point);
2530 break;
2531 case GRN_DB_SHORT_TEXT:
2532 case GRN_DB_TEXT:
2533 case GRN_DB_LONG_TEXT:
2534 GRN_OBJ_INIT(&radius_or_point_, GRN_BULK, 0, domain);
2535 if (grn_obj_cast(ctx, radius_or_point, &radius_or_point_, false)) {
2536 goto exit;
2537 }
2538 radius_or_point = &radius_or_point_;
2539 /* fallthru */
2540 case GRN_DB_TOKYO_GEO_POINT:
2541 case GRN_DB_WGS84_GEO_POINT:
2542 if (domain != radius_or_point->header.domain) { /* todo */
2543 goto exit;
2544 }
2545 r = d <= distance_raw_func(ctx,
2546 GRN_GEO_POINT_VALUE_RAW(radius_or_point),
2547 GRN_GEO_POINT_VALUE_RAW(center));
2548 break;
2549 default:
2550 goto exit;
2551 }
2552 } else {
2553 /* todo */
2554 }
2555 exit:
2556 return r;
2557 }
2558
2559 bool
2560 grn_geo_in_rectangle_raw(grn_ctx *ctx,
2561 grn_geo_point *point,
2562 grn_geo_point *top_left,
2563 grn_geo_point *bottom_right)
2564 {
2565 if (point->latitude > top_left->latitude) {
2566 return false;
2567 }
2568 if (point->latitude < bottom_right->latitude) {
2569 return false;
2570 }
2571
2572 if (GRN_GEO_LONGITUDE_IS_WRAPPED(top_left, bottom_right)) {
2573 if (point->longitude >= top_left->longitude) {
2574 return true;
2575 }
2576 if (point->longitude <= bottom_right->longitude) {
2577 return true;
2578 }
2579 return false;
2580 } else {
2581 if (point->longitude < top_left->longitude) {
2582 return false;
2583 }
2584 if (point->longitude > bottom_right->longitude) {
2585 return false;
2586 }
2587 return true;
2588 }
2589 }
2590
2591 bool
2592 grn_geo_in_rectangle(grn_ctx *ctx,
2593 grn_obj *point,
2594 grn_obj *top_left,
2595 grn_obj *bottom_right)
2596 {
2597 bool r = false;
2598 grn_obj top_left_, bottom_right_;
2599 grn_id domain = point->header.domain;
2600 if (domain == GRN_DB_TOKYO_GEO_POINT || domain == GRN_DB_WGS84_GEO_POINT) {
2601 if (top_left->header.domain != domain) {
2602 GRN_OBJ_INIT(&top_left_, GRN_BULK, 0, domain);
2603 if (grn_obj_cast(ctx, top_left, &top_left_, false)) {
2604 goto exit;
2605 }
2606 top_left = &top_left_;
2607 }
2608 if (bottom_right->header.domain != domain) {
2609 GRN_OBJ_INIT(&bottom_right_, GRN_BULK, 0, domain);
2610 if (grn_obj_cast(ctx, bottom_right, &bottom_right_, false)) {
2611 goto exit;
2612 }
2613 bottom_right = &bottom_right_;
2614 }
2615 r = grn_geo_in_rectangle_raw(ctx,
2616 GRN_GEO_POINT_VALUE_RAW(point),
2617 GRN_GEO_POINT_VALUE_RAW(top_left),
2618 GRN_GEO_POINT_VALUE_RAW(bottom_right));
2619 } else {
2620 /* todo */
2621 }
2622 exit:
2623 return r;
2624 }
2625
2626 typedef enum {
2627 LONGITUDE_SHORT,
2628 LONGITUDE_LONG,
2629 } distance_type;
2630
2631 typedef enum {
2632 QUADRANT_1ST,
2633 QUADRANT_2ND,
2634 QUADRANT_3RD,
2635 QUADRANT_4TH,
2636 QUADRANT_1ST_TO_2ND,
2637 QUADRANT_1ST_TO_3RD,
2638 QUADRANT_1ST_TO_4TH,
2639 QUADRANT_2ND_TO_1ST,
2640 QUADRANT_2ND_TO_3RD,
2641 QUADRANT_2ND_TO_4TH,
2642 QUADRANT_3RD_TO_1ST,
2643 QUADRANT_3RD_TO_2ND,
2644 QUADRANT_3RD_TO_4TH,
2645 QUADRANT_4TH_TO_1ST,
2646 QUADRANT_4TH_TO_2ND,
2647 QUADRANT_4TH_TO_3RD,
2648 } quadrant_type;
2649
2650 static distance_type
2651 geo_longitude_distance_type(int start_longitude, int end_longitude)
2652 {
2653 int diff_longitude;
2654 int east_to_west;
2655 int west_to_east;
2656 if (start_longitude >= 0) {
2657 diff_longitude = abs(start_longitude - end_longitude);
2658 } else {
2659 diff_longitude = abs(end_longitude - start_longitude);
2660 }
2661 east_to_west = start_longitude > 0 && end_longitude < 0;
2662 west_to_east = start_longitude < 0 && end_longitude > 0;
2663 if (start_longitude != end_longitude && (east_to_west || west_to_east) &&
2664 diff_longitude > 180 * GRN_GEO_RESOLUTION) {
2665 return LONGITUDE_LONG;
2666 } else {
2667 return LONGITUDE_SHORT;
2668 }
2669 }
2670
2671 static inline quadrant_type
2672 geo_quadrant_type(grn_geo_point *point1, grn_geo_point *point2)
2673 {
2674 #define QUADRANT_1ST_WITH_AXIS(point) \
2675 (point->longitude >= 0) && (point->latitude >= 0)
2676 #define QUADRANT_2ND_WITH_AXIS(point) \
2677 (point->longitude <= 0) && (point->latitude >= 0)
2678 #define QUADRANT_3RD_WITH_AXIS(point) \
2679 (point->longitude <= 0) && (point->latitude <= 0)
2680 #define QUADRANT_4TH_WITH_AXIS(point) \
2681 (point->longitude >= 0) && (point->latitude <= 0)
2682
2683 if (QUADRANT_1ST_WITH_AXIS(point1) && QUADRANT_1ST_WITH_AXIS(point2)) {
2684 return QUADRANT_1ST;
2685 } else if (QUADRANT_2ND_WITH_AXIS(point1) && QUADRANT_2ND_WITH_AXIS(point2)) {
2686 return QUADRANT_2ND;
2687 } else if (QUADRANT_3RD_WITH_AXIS(point1) && QUADRANT_3RD_WITH_AXIS(point2)) {
2688 return QUADRANT_3RD;
2689 } else if (QUADRANT_4TH_WITH_AXIS(point1) && QUADRANT_4TH_WITH_AXIS(point2)) {
2690 return QUADRANT_4TH;
2691 } else {
2692 if (point1->longitude > 0 && point2->longitude < 0 &&
2693 point1->latitude >= 0 && point2->latitude >= 0) {
2694 return QUADRANT_1ST_TO_2ND;
2695 } else if (point1->longitude < 0 && point2->longitude > 0 &&
2696 point1->latitude >= 0 && point2->latitude >= 0) {
2697 return QUADRANT_2ND_TO_1ST;
2698 } else if (point1->longitude < 0 && point2->longitude > 0 &&
2699 point1->latitude <= 0 && point2->latitude <= 0) {
2700 return QUADRANT_3RD_TO_4TH;
2701 } else if (point1->longitude > 0 && point2->longitude < 0 &&
2702 point1->latitude <= 0 && point2->latitude <= 0) {
2703 return QUADRANT_4TH_TO_3RD;
2704 } else if (point1->longitude >= 0 && point2->longitude >= 0 &&
2705 point1->latitude > 0 && point2->latitude < 0) {
2706 return QUADRANT_1ST_TO_4TH;
2707 } else if (point1->longitude >= 0 && point2->longitude >= 0 &&
2708 point1->latitude < 0 && point2->latitude > 0) {
2709 return QUADRANT_4TH_TO_1ST;
2710 } else if (point1->longitude <= 0 && point2->longitude <= 0 &&
2711 point1->latitude > 0 && point2->latitude < 0) {
2712 return QUADRANT_2ND_TO_3RD;
2713 } else if (point1->longitude <= 0 && point2->longitude <= 0 &&
2714 point1->latitude < 0 && point2->latitude > 0) {
2715 return QUADRANT_3RD_TO_2ND;
2716 } else if (point1->longitude >= 0 && point2->longitude <= 0 &&
2717 point1->latitude > 0 && point2->latitude < 0) {
2718 return QUADRANT_1ST_TO_3RD;
2719 } else if (point1->longitude <= 0 && point2->longitude >= 0 &&
2720 point1->latitude < 0 && point2->latitude > 0) {
2721 return QUADRANT_3RD_TO_1ST;
2722 } else if (point1->longitude <= 0 && point2->longitude >= 0 &&
2723 point1->latitude > 0 && point2->latitude < 0) {
2724 return QUADRANT_2ND_TO_4TH;
2725 } else if (point1->longitude >= 0 && point2->longitude <= 0 &&
2726 point1->latitude < 0 && point2->latitude > 0) {
2727 return QUADRANT_4TH_TO_2ND;
2728 } else {
2729 /* FIXME */
2730 return QUADRANT_1ST;
2731 }
2732 }
2733 #undef QUADRANT_1ST_WITH_AXIS
2734 #undef QUADRANT_2ND_WITH_AXIS
2735 #undef QUADRANT_3RD_WITH_AXIS
2736 #undef QUADRANT_4TH_WITH_AXIS
2737 }
2738
2739 static inline double
2740 geo_distance_rectangle_square_root(double start_longitude,
2741 double start_latitude,
2742 double end_longitude,
2743 double end_latitude)
2744 {
2745 double diff_longitude;
2746 double x, y;
2747
2748 diff_longitude = end_longitude - start_longitude;
2749 x = diff_longitude * cos((start_latitude + end_latitude) * 0.5);
2750 y = end_latitude - start_latitude;
2751 return sqrt((x * x) + (y * y));
2752 }
2753
2754 static inline double
2755 geo_distance_rectangle_short_dist_type(
2756 quadrant_type quad_type, double lng1, double lat1, double lng2, double lat2)
2757 {
2758 double distance;
2759 double longitude_delta, latitude_delta;
2760
2761 if (quad_type == QUADRANT_1ST_TO_4TH || quad_type == QUADRANT_4TH_TO_1ST ||
2762 quad_type == QUADRANT_2ND_TO_3RD || quad_type == QUADRANT_3RD_TO_2ND) {
2763 longitude_delta = lng2 - lng1;
2764 if (longitude_delta > 0 || longitude_delta < 0) {
2765 if (lat2 > lat1) {
2766 distance = geo_distance_rectangle_square_root(lng1, lat1, lng2, lat2) *
2767 GRN_GEO_RADIUS;
2768 } else {
2769 distance = geo_distance_rectangle_square_root(lng2, lat2, lng1, lat1) *
2770 GRN_GEO_RADIUS;
2771 }
2772 } else {
2773 latitude_delta = fabs(lat1) + fabs(lat2);
2774 distance = sqrt(latitude_delta * latitude_delta) * GRN_GEO_RADIUS;
2775 }
2776 } else if (quad_type == QUADRANT_1ST_TO_3RD ||
2777 quad_type == QUADRANT_2ND_TO_4TH) {
2778 distance = geo_distance_rectangle_square_root(lng1, lat1, lng2, lat2) *
2779 GRN_GEO_RADIUS;
2780 } else if (quad_type == QUADRANT_3RD_TO_1ST ||
2781 quad_type == QUADRANT_4TH_TO_2ND) {
2782 distance = geo_distance_rectangle_square_root(lng2, lat2, lng1, lat1) *
2783 GRN_GEO_RADIUS;
2784 } else if (quad_type == QUADRANT_1ST_TO_2ND ||
2785 quad_type == QUADRANT_2ND_TO_1ST ||
2786 quad_type == QUADRANT_3RD_TO_4TH ||
2787 quad_type == QUADRANT_4TH_TO_3RD) {
2788 if (lat2 > lat1) {
2789 distance = geo_distance_rectangle_square_root(lng1, lat1, lng2, lat2) *
2790 GRN_GEO_RADIUS;
2791 } else if (lat2 < lat1) {
2792 distance = geo_distance_rectangle_square_root(lng2, lat2, lng1, lat1) *
2793 GRN_GEO_RADIUS;
2794 } else {
2795 longitude_delta = lng2 - lng1;
2796 distance = longitude_delta * cos(lat1);
2797 distance = sqrt(distance * distance) * GRN_GEO_RADIUS;
2798 }
2799 } else {
2800 distance = geo_distance_rectangle_square_root(lng1, lat1, lng2, lat2) *
2801 GRN_GEO_RADIUS;
2802 }
2803 return distance;
2804 }
2805
2806 static inline double
2807 geo_distance_rectangle_long_dist_type(
2808 quadrant_type quad_type, double lng1, double lat1, double lng2, double lat2)
2809 {
2810 #define M_2PI 6.28318530717958647692
2811
2812 double distance;
2813
2814 if (quad_type == QUADRANT_1ST_TO_2ND || quad_type == QUADRANT_4TH_TO_3RD) {
2815 if (lat1 > lat2) {
2816 distance =
2817 geo_distance_rectangle_square_root(lng2 + M_2PI, lat2, lng1, lat1) *
2818 GRN_GEO_RADIUS;
2819 } else {
2820 distance =
2821 geo_distance_rectangle_square_root(lng1, lat1, lng2 + M_2PI, lat2) *
2822 GRN_GEO_RADIUS;
2823 }
2824 } else if (quad_type == QUADRANT_2ND_TO_1ST ||
2825 quad_type == QUADRANT_3RD_TO_4TH) {
2826 if (lat1 > lat2) {
2827 distance =
2828 geo_distance_rectangle_square_root(lng2, lat2, lng1 + M_2PI, lat1) *
2829 GRN_GEO_RADIUS;
2830 } else {
2831 distance =
2832 geo_distance_rectangle_square_root(lng1 + M_2PI, lat1, lng2, lat2) *
2833 GRN_GEO_RADIUS;
2834 }
2835 } else if (quad_type == QUADRANT_1ST_TO_3RD) {
2836 distance =
2837 geo_distance_rectangle_square_root(lng2 + M_2PI, lat2, lng1, lat1) *
2838 GRN_GEO_RADIUS;
2839 } else if (quad_type == QUADRANT_3RD_TO_1ST) {
2840 distance =
2841 geo_distance_rectangle_square_root(lng1 + M_2PI, lat1, lng2, lat2) *
2842 GRN_GEO_RADIUS;
2843 } else if (quad_type == QUADRANT_2ND_TO_4TH) {
2844 distance =
2845 geo_distance_rectangle_square_root(lng2, lat2, lng1 + M_2PI, lat1) *
2846 GRN_GEO_RADIUS;
2847 } else if (quad_type == QUADRANT_4TH_TO_2ND) {
2848 distance =
2849 geo_distance_rectangle_square_root(lng1, lat1, lng2 + M_2PI, lat2) *
2850 GRN_GEO_RADIUS;
2851 } else {
2852 if (lng1 > lng2) {
2853 distance =
2854 geo_distance_rectangle_square_root(lng1, lat1, lng2 + M_2PI, lat2) *
2855 GRN_GEO_RADIUS;
2856 } else {
2857 distance =
2858 geo_distance_rectangle_square_root(lng2, lat2, lng1 + M_2PI, lat1) *
2859 GRN_GEO_RADIUS;
2860 }
2861 }
2862 return distance;
2863 #undef M_2PI
2864 }
2865
2866 double
2867 grn_geo_distance_rectangle_raw(grn_ctx *ctx,
2868 grn_geo_point *point1,
2869 grn_geo_point *point2)
2870 {
2871
2872 double lng1, lat1, lng2, lat2, distance;
2873 distance_type dist_type;
2874 quadrant_type quad_type;
2875
2876 lat1 = GRN_GEO_MSEC2RADIAN(point1->latitude);
2877 lng1 = GRN_GEO_MSEC2RADIAN(point1->longitude);
2878 lat2 = GRN_GEO_MSEC2RADIAN(point2->latitude);
2879 lng2 = GRN_GEO_MSEC2RADIAN(point2->longitude);
2880 quad_type = geo_quadrant_type(point1, point2);
2881 if (quad_type <= QUADRANT_4TH) {
2882 distance = geo_distance_rectangle_square_root(lng1, lat1, lng2, lat2) *
2883 GRN_GEO_RADIUS;
2884 } else {
2885 dist_type =
2886 geo_longitude_distance_type(point1->longitude, point2->longitude);
2887 if (dist_type == LONGITUDE_SHORT) {
2888 distance = geo_distance_rectangle_short_dist_type(quad_type,
2889 lng1,
2890 lat1,
2891 lng2,
2892 lat2);
2893 } else {
2894 distance = geo_distance_rectangle_long_dist_type(quad_type,
2895 lng1,
2896 lat1,
2897 lng2,
2898 lat2);
2899 }
2900 }
2901 return distance;
2902 }
2903
2904 double
2905 grn_geo_distance_sphere_raw(grn_ctx *ctx,
2906 grn_geo_point *point1,
2907 grn_geo_point *point2)
2908 {
2909 double lng1, lat1, lng2, lat2, x, y;
2910
2911 lat1 = GRN_GEO_MSEC2RADIAN(point1->latitude);
2912 lng1 = GRN_GEO_MSEC2RADIAN(point1->longitude);
2913 lat2 = GRN_GEO_MSEC2RADIAN(point2->latitude);
2914 lng2 = GRN_GEO_MSEC2RADIAN(point2->longitude);
2915 x = sin(fabs(lng2 - lng1) * 0.5);
2916 y = sin(fabs(lat2 - lat1) * 0.5);
2917 return asin(sqrt((y * y) + cos(lat1) * cos(lat2) * x * x)) * 2 *
2918 GRN_GEO_RADIUS;
2919 }
2920
2921 double
2922 grn_geo_distance_ellipsoid_raw(grn_ctx *ctx,
2923 grn_geo_point *point1,
2924 grn_geo_point *point2,
2925 int c1,
2926 int c2,
2927 double c3)
2928 {
2929 double lng1, lat1, lng2, lat2, p, q, r, m, n, x, y;
2930
2931 lat1 = GRN_GEO_MSEC2RADIAN(point1->latitude);
2932 lng1 = GRN_GEO_MSEC2RADIAN(point1->longitude);
2933 lat2 = GRN_GEO_MSEC2RADIAN(point2->latitude);
2934 lng2 = GRN_GEO_MSEC2RADIAN(point2->longitude);
2935 p = (lat1 + lat2) * 0.5;
2936 q = (1 - c3 * sin(p) * sin(p));
2937 r = sqrt(q);
2938 m = c1 / (q * r);
2939 n = c2 / r;
2940 x = n * cos(p) * fabs(lng1 - lng2);
2941 y = m * fabs(lat1 - lat2);
2942 return sqrt((x * x) + (y * y));
2943 }
2944
2945 double
2946 grn_geo_distance_ellipsoid_raw_tokyo(grn_ctx *ctx,
2947 grn_geo_point *point1,
2948 grn_geo_point *point2)
2949 {
2950 return grn_geo_distance_ellipsoid_raw(ctx,
2951 point1,
2952 point2,
2953 GRN_GEO_BES_C1,
2954 GRN_GEO_BES_C2,
2955 GRN_GEO_BES_C3);
2956 }
2957
2958 double
2959 grn_geo_distance_ellipsoid_raw_wgs84(grn_ctx *ctx,
2960 grn_geo_point *point1,
2961 grn_geo_point *point2)
2962 {
2963 return grn_geo_distance_ellipsoid_raw(ctx,
2964 point1,
2965 point2,
2966 GRN_GEO_GRS_C1,
2967 GRN_GEO_GRS_C2,
2968 GRN_GEO_GRS_C3);
2969 }
2970
2971 double
2972 grn_geo_distance(grn_ctx *ctx,
2973 grn_obj *point1,
2974 grn_obj *point2,
2975 grn_geo_approximate_type type)
2976 {
2977 double d = 0.0;
2978
2979 switch (type) {
2980 case GRN_GEO_APPROXIMATE_RECTANGLE:
2981 d = grn_geo_distance_rectangle(ctx, point1, point2);
2982 break;
2983 case GRN_GEO_APPROXIMATE_SPHERE:
2984 d = grn_geo_distance_sphere(ctx, point1, point2);
2985 break;
2986 case GRN_GEO_APPROXIMATE_ELLIPSOID:
2987 d = grn_geo_distance_ellipsoid(ctx, point1, point2);
2988 break;
2989 default:
2990 ERR(GRN_INVALID_ARGUMENT, "unknown approximate type: <%d>", type);
2991 break;
2992 }
2993 return d;
2994 }
2995
2996 grn_rc
2997 grn_geo_distance_sorter(grn_ctx *ctx, grn_sorter_data *data)
2998 {
2999 const char *tag = "geo_distance():";
3000
3001 size_t n_args;
3002 grn_obj **args = grn_sorter_data_get_args(ctx, data, &n_args);
3003 if (!(n_args == 2)) {
3004 ERR(GRN_INVALID_ARGUMENT,
3005 "%s wrong number of arguments (%u for 2)",
3006 tag,
3007 (uint32_t)n_args);
3008 return ctx->rc;
3009 }
3010
3011 grn_obj *table = grn_sorter_data_get_table(ctx, data);
3012 size_t offset = grn_sorter_data_get_offset(ctx, data);
3013 size_t limit = grn_sorter_data_get_limit(ctx, data);
3014 grn_obj *result = grn_sorter_data_get_result(ctx, data);
3015
3016 grn_obj *column = args[0];
3017 grn_obj *point = args[1];
3018
3019 grn_id column_range = grn_obj_get_range(ctx, column);
3020 if (!(column_range == GRN_DB_TOKYO_GEO_POINT ||
3021 column_range == GRN_DB_WGS84_GEO_POINT)) {
3022 ERR(GRN_INVALID_ARGUMENT,
3023 "%s 1st argument's type must be TokyoGeoPoint or WGS84GeoPoint: %s(%u)",
3024 tag,
3025 grn_type_id_to_string_builtin(ctx, column_range),
3026 column_range);
3027 return ctx->rc;
3028 }
3029
3030 grn_obj casted_point;
3031 GRN_VALUE_FIX_SIZE_INIT(&casted_point, 0, column_range);
3032 grn_rc rc = grn_obj_cast(ctx, point, &casted_point, false);
3033 if (rc != GRN_SUCCESS) {
3034 grn_obj inspected;
3035 GRN_TEXT_INIT(&inspected, 0);
3036 grn_inspect(ctx, &inspected, point);
3037 ERR(GRN_INVALID_ARGUMENT,
3038 "%s 2nd argument must's %s: <%.*s>",
3039 tag,
3040 grn_type_id_to_string_builtin(ctx, column_range),
3041 (int)GRN_TEXT_LEN(&inspected),
3042 GRN_TEXT_VALUE(&inspected));
3043 GRN_OBJ_FIN(ctx, &inspected);
3044 GRN_OBJ_FIN(ctx, &casted_point);
3045 return ctx->rc;
3046 }
3047
3048 grn_geo_table_sort(ctx, table, offset, limit, result, column, &casted_point);
3049 GRN_OBJ_FIN(ctx, &casted_point);
3050 return ctx->rc;
3051 }
3052
3053 double
3054 grn_geo_distance_rectangle(grn_ctx *ctx, grn_obj *point1, grn_obj *point2)
3055 {
3056 double d = 0;
3057 bool point1_initialized = false;
3058 bool point2_initialized = false;
3059 grn_obj point1_, point2_;
3060 grn_id domain1 = point1->header.domain;
3061 grn_id domain2 = point2->header.domain;
3062 if (domain1 == GRN_DB_TOKYO_GEO_POINT || domain1 == GRN_DB_WGS84_GEO_POINT) {
3063 if (domain1 != domain2) {
3064 GRN_OBJ_INIT(&point2_, GRN_BULK, 0, domain1);
3065 point2_initialized = true;
3066 if (grn_obj_cast(ctx, point2, &point2_, false)) {
3067 goto exit;
3068 }
3069 point2 = &point2_;
3070 }
3071 } else if (domain2 == GRN_DB_TOKYO_GEO_POINT ||
3072 domain2 == GRN_DB_WGS84_GEO_POINT) {
3073 GRN_OBJ_INIT(&point1_, GRN_BULK, 0, domain2);
3074 point1_initialized = true;
3075 if (grn_obj_cast(ctx, point1, &point1_, false)) {
3076 goto exit;
3077 }
3078 point1 = &point1_;
3079 } else if ((GRN_DB_SHORT_TEXT <= domain1 && domain1 <= GRN_DB_LONG_TEXT) &&
3080 (GRN_DB_SHORT_TEXT <= domain2 && domain2 <= GRN_DB_LONG_TEXT)) {
3081 GRN_OBJ_INIT(&point1_, GRN_BULK, 0, GRN_DB_WGS84_GEO_POINT);
3082 point1_initialized = true;
3083 if (grn_obj_cast(ctx, point1, &point1_, false)) {
3084 goto exit;
3085 }
3086 point1 = &point1_;
3087
3088 GRN_OBJ_INIT(&point2_, GRN_BULK, 0, GRN_DB_WGS84_GEO_POINT);
3089 point2_initialized = true;
3090 if (grn_obj_cast(ctx, point2, &point2_, false)) {
3091 goto exit;
3092 }
3093 point2 = &point2_;
3094 } else {
3095 goto exit;
3096 }
3097 d = grn_geo_distance_rectangle_raw(ctx,
3098 GRN_GEO_POINT_VALUE_RAW(point1),
3099 GRN_GEO_POINT_VALUE_RAW(point2));
3100 exit:
3101 if (point1_initialized) {
3102 GRN_OBJ_FIN(ctx, &point1_);
3103 }
3104 if (point2_initialized) {
3105 GRN_OBJ_FIN(ctx, &point2_);
3106 }
3107 return d;
3108 }
3109
3110 double
3111 grn_geo_distance_sphere(grn_ctx *ctx, grn_obj *point1, grn_obj *point2)
3112 {
3113 double d = 0;
3114 bool point2_initialized = false;
3115 grn_obj point2_;
3116 grn_id domain = point1->header.domain;
3117 if (domain == GRN_DB_TOKYO_GEO_POINT || domain == GRN_DB_WGS84_GEO_POINT) {
3118 if (point2->header.domain != domain) {
3119 GRN_OBJ_INIT(&point2_, GRN_BULK, 0, domain);
3120 point2_initialized = true;
3121 if (grn_obj_cast(ctx, point2, &point2_, false)) {
3122 goto exit;
3123 }
3124 point2 = &point2_;
3125 }
3126 d = grn_geo_distance_sphere_raw(ctx,
3127 GRN_GEO_POINT_VALUE_RAW(point1),
3128 GRN_GEO_POINT_VALUE_RAW(point2));
3129 } else {
3130 /* todo */
3131 }
3132 exit:
3133 if (point2_initialized) {
3134 GRN_OBJ_FIN(ctx, &point2_);
3135 }
3136 return d;
3137 }
3138
3139 double
3140 grn_geo_distance_ellipsoid(grn_ctx *ctx, grn_obj *point1, grn_obj *point2)
3141 {
3142 double d = 0;
3143 bool point2_initialized = false;
3144 grn_obj point2_;
3145 grn_id domain = point1->header.domain;
3146 if (domain == GRN_DB_TOKYO_GEO_POINT || domain == GRN_DB_WGS84_GEO_POINT) {
3147 if (point2->header.domain != domain) {
3148 GRN_OBJ_INIT(&point2_, GRN_BULK, 0, domain);
3149 point2_initialized = true;
3150 if (grn_obj_cast(ctx, point2, &point2_, false)) {
3151 goto exit;
3152 }
3153 point2 = &point2_;
3154 }
3155 if (domain == GRN_DB_TOKYO_GEO_POINT) {
3156 d = grn_geo_distance_ellipsoid_raw_tokyo(ctx,
3157 GRN_GEO_POINT_VALUE_RAW(point1),
3158 GRN_GEO_POINT_VALUE_RAW(point2));
3159 } else {
3160 d = grn_geo_distance_ellipsoid_raw_wgs84(ctx,
3161 GRN_GEO_POINT_VALUE_RAW(point1),
3162 GRN_GEO_POINT_VALUE_RAW(point2));
3163 }
3164 } else {
3165 /* todo */
3166 }
3167 exit:
3168 if (point2_initialized) {
3169 GRN_OBJ_FIN(ctx, &point2_);
3170 }
3171 return d;
3172 }