| 1 | /* |
| 2 | Copyright (C) 2024 Sutou Kouhei <[email protected]> |
| 3 | |
| 4 | This library is free software; you can redistribute it and/or |
| 5 | modify it under the terms of the GNU Lesser General Public |
| 6 | License as published by the Free Software Foundation; either |
| 7 | version 2.1 of the License, or (at your option) any later version. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #include "grn_ctx.h"cxx . . $ `<file>/lib/grn_ctx.h`/ |
| 20 | |
| 21 | #ifdef GRN_WITH_H3cxx . . $ `build/config.h:99:9`! |
| 22 | # ifdef GRN_WITH_H3_BUNDLED |
| 23 | # include <h3api.h> |
| 24 | # else |
| 25 | # include <h3/h3api.h> |
| 26 | # endif |
| 27 | #endif |
| 28 | |
| 29 | #ifdef GRN_WITH_H3cxx . . $ `build/config.h:99:9`! |
| 30 | static const char * |
| 31 | grn_h3_error_to_stringcxx . . $ grn_h3_error_to_string(81a3ae90da26a711).(H3Errorcxx . . $ H3Error# error) |
| 32 | { |
| 33 | switch (error) { |
| 34 | case E_SUCCESScxx . . $ E_SUCCESS.: |
| 35 | return "success"; |
| 36 | case E_FAILEDcxx . . $ E_FAILED.: |
| 37 | return "failed"; |
| 38 | case E_DOMAINcxx . . $ E_DOMAIN.: |
| 39 | return "outside of acceptable range"; |
| 40 | case E_LATLNG_DOMAINcxx . . $ E_LATLNG_DOMAIN.: |
| 41 | return "latitude or longitude is outside of acceptable range"; |
| 42 | case E_RES_DOMAINcxx . . $ E_RES_DOMAIN.: |
| 43 | return "resolution is outside of acceptable range"; |
| 44 | case E_CELL_INVALIDcxx . . $ E_CELL_INVALID.: |
| 45 | return "invalid cell"; |
| 46 | case E_DIR_EDGE_INVALIDcxx . . $ E_DIR_EDGE_INVALID.: |
| 47 | return "invalid directed edge"; |
| 48 | case E_UNDIR_EDGE_INVALIDcxx . . $ E_UNDIR_EDGE_INVALID.: |
| 49 | return "invalid undirected edge"; |
| 50 | case E_VERTEX_INVALIDcxx . . $ E_VERTEX_INVALID.: |
| 51 | return "invalid vertex"; |
| 52 | case E_PENTAGONcxx . . $ E_PENTAGON.: |
| 53 | return "pentagon distortion is encountered"; |
| 54 | case E_DUPLICATE_INPUTcxx . . $ E_DUPLICATE_INPUT.: |
| 55 | return "duplicate input is encountered"; |
| 56 | case E_NOT_NEIGHBORScxx . . $ E_NOT_NEIGHBORS.: |
| 57 | return "cells aren't neighbors"; |
| 58 | case E_RES_MISMATCHcxx . . $ E_RES_MISMATCH.: |
| 59 | return "cells have incompatible resolutions"; |
| 60 | case E_MEMORY_ALLOCcxx . . $ E_MEMORY_ALLOC.: |
| 61 | return "failed to allocate memory"; |
| 62 | case E_MEMORY_BOUNDScxx . . $ E_MEMORY_BOUNDS.: |
| 63 | return "bounds of provided memory are not large enough"; |
| 64 | case E_OPTION_INVALIDcxx . . $ E_OPTION_INVALID.: |
| 65 | return "invalid option"; |
| 66 | default: |
| 67 | return "unknown"; |
| 68 | } |
| 69 | } |
| 70 | #endif |
| 71 | |
| 72 | uint64_tcxx . . $ uint64_t# |
| 73 | grn_h3_compute_cellcxx . . $ grn_h3_compute_cell(20c0e0a6e42575f).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 74 | grn_geo_pointcxx . . $ grn_geo_point# *geo_point, |
| 75 | int32_tcxx . . $ int32_t# resolution, |
| 76 | const char *tag) |
| 77 | { |
| 78 | #ifdef GRN_WITH_H3cxx . . $ `build/config.h:99:9`! |
| 79 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 80 | |
| 81 | if (!(0 <= resolution && resolution <= 15)) { |
| 82 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 83 | "%s resolution must be in 0..15: %d", |
| 84 | tag, |
| 85 | resolution); |
| 86 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 87 | } |
| 88 | |
| 89 | LatLngcxx . . $ LatLng# lat_lng = { |
| 90 | .lat = GRN_GEO_MSEC2RADIANcxx . . $ `include/groonga/groonga.h:2958:9`!(geo_point->latitude), |
| 91 | .lng = GRN_GEO_MSEC2RADIANcxx . . $ `include/groonga/groonga.h:2958:9`!(geo_point->longitude), |
| 92 | }; |
| 93 | H3Indexcxx . . $ H3Index# h3_index = 0; |
| 94 | H3Errorcxx . . $ H3Error# error = latLngToCellcxx . . $ latLngToCell(8d564fea5a13b4c0).(&lat_lng, resolution, &h3_index); |
| 95 | if (error != E_SUCCESScxx . . $ E_SUCCESS.) { |
| 96 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 97 | "%s failed to convert a point to H3 index: %s(%u): %fx%f", |
| 98 | tag, |
| 99 | grn_h3_error_to_string(error), |
| 100 | error, |
| 101 | GRN_GEO_MSEC2DEGREEcxx . . $ `include/groonga/groonga.h:2953:9`!(geo_point->latitude), |
| 102 | GRN_GEO_MSEC2DEGREEcxx . . $ `include/groonga/groonga.h:2953:9`!(geo_point->longitude)); |
| 103 | } |
| 104 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(h3_index); |
| 105 | #else |
| 106 | GRN_API_ENTER; |
| 107 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "%s H3 isn't enabled", tag); |
| 108 | GRN_API_RETURN(0); |
| 109 | #endif |
| 110 | } |
| 111 | |
| 112 | grn_rccxx . . $ grn_rc# |
| 113 | grn_h3_compute_grid_diskcxx . . $ grn_h3_compute_grid_disk(19843f0bd71d5a02).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 114 | grn_geo_pointcxx . . $ grn_geo_point# *geo_point, |
| 115 | int32_tcxx . . $ int32_t# resolution, |
| 116 | int32_tcxx . . $ int32_t# k, |
| 117 | grn_objcxx . . $ grn_obj# *h3_indices, |
| 118 | const char *tag) |
| 119 | { |
| 120 | #ifdef GRN_WITH_H3cxx . . $ `build/config.h:99:9`! |
| 121 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 122 | |
| 123 | if (!(0 <= resolution && resolution <= 15)) { |
| 124 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 125 | "%s resolution must be in 0..15: %d", |
| 126 | tag, |
| 127 | resolution); |
| 128 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 129 | } |
| 130 | |
| 131 | if (k < 0) { |
| 132 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, "%s k must be 0 or larger: %d", tag, k); |
| 133 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 134 | } |
| 135 | |
| 136 | int64_tcxx . . $ int64_t# n_h3_indices = 0; |
| 137 | H3Errorcxx . . $ H3Error# error = maxGridDiskSizecxx . . $ maxGridDiskSize(2a43feef6d4aefe8).(k, &n_h3_indices); |
| 138 | if (error != E_SUCCESScxx . . $ E_SUCCESS.) { |
| 139 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 140 | "%s failed to compute the max number of indexes for grid disk: " |
| 141 | "%s(%u): k:<%d>", |
| 142 | tag, |
| 143 | grn_h3_error_to_string(error), |
| 144 | error, |
| 145 | k); |
| 146 | } |
| 147 | |
| 148 | H3Indexcxx . . $ H3Index# h3_index = grn_h3_compute_cellcxx . . $ grn_h3_compute_cell(20c0e0a6e42575f).(ctx, geo_point, resolution, tag); |
| 149 | if (ctx->rccxx . . $ _grn_ctx#rc. != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 150 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 151 | } |
| 152 | |
| 153 | size_t offset = GRN_BULK_VSIZEcxx . . $ `include/groonga/groonga.h:2642:9`!(h3_indices); |
| 154 | int64_tcxx . . $ int64_t# i; |
| 155 | for (i = 0; i < n_h3_indices; i++) { |
| 156 | GRN_UINT64_PUTcxx . . $ `include/groonga/groonga.h:3192:9`!(ctx, h3_indices, 0); |
| 157 | } |
| 158 | error = |
| 159 | gridDiskcxx . . $ gridDisk(c4dca036a0103f76).(h3_index, k, (H3Indexcxx . . $ H3Index# *)(GRN_BULK_HEADcxx . . $ `include/groonga/groonga.h:2648:9`!(h3_indices) + offset)); |
| 160 | if (error != E_SUCCESScxx . . $ E_SUCCESS.) { |
| 161 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 162 | "%s failed to compute grid disk: %s(%u): k:<%d>, geo_point:<%fx%f>(%d)", |
| 163 | tag, |
| 164 | grn_h3_error_to_string(error), |
| 165 | error, |
| 166 | k, |
| 167 | GRN_GEO_MSEC2DEGREEcxx . . $ `include/groonga/groonga.h:2953:9`!(geo_point->latitude), |
| 168 | GRN_GEO_MSEC2DEGREEcxx . . $ `include/groonga/groonga.h:2953:9`!(geo_point->longitude), |
| 169 | resolution); |
| 170 | } |
| 171 | |
| 172 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(ctx->rc); |
| 173 | #else |
| 174 | GRN_API_ENTER; |
| 175 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "%s H3 isn't enabled", tag); |
| 176 | GRN_API_RETURN(0); |
| 177 | #endif |
| 178 | } |
| 179 | |
| 180 | uint64_tcxx . . $ uint64_t# |
| 181 | grn_h3_compute_grid_distancecxx . . $ grn_h3_compute_grid_distance(794e02ec1926a47c).(grn_ctxcxx . . $ grn_ctx# *ctx, |
| 182 | uint64_tcxx . . $ uint64_t# h3_index1, |
| 183 | uint64_tcxx . . $ uint64_t# h3_index2, |
| 184 | const char *tag) |
| 185 | { |
| 186 | #ifdef GRN_WITH_H3cxx . . $ `build/config.h:99:9`! |
| 187 | GRN_API_ENTERcxx . . $ `lib/grn_ctx.h:41:9`!; |
| 188 | |
| 189 | int64_tcxx . . $ int64_t# distance; |
| 190 | H3Errorcxx . . $ H3Error# error = gridDistancecxx . . $ gridDistance(6f7c7839638880e7).(h3_index1, h3_index2, &distance); |
| 191 | if (error != E_SUCCESScxx . . $ E_SUCCESS.) { |
| 192 | LatLngcxx . . $ LatLng# lat_lng1; |
| 193 | cellToLatLngcxx . . $ cellToLatLng(105434d63509b0b5).(h3_index1, &lat_lng1); |
| 194 | LatLngcxx . . $ LatLng# lat_lng2; |
| 195 | cellToLatLngcxx . . $ cellToLatLng(105434d63509b0b5).(h3_index2, &lat_lng2); |
| 196 | ERRcxx . . $ `lib/grn_error.h:156:9`!(GRN_INVALID_ARGUMENT, |
| 197 | "%s failed to compute grid distance: %s(%u): %fx%f<->%fx%f", |
| 198 | tag, |
| 199 | grn_h3_error_to_string(error), |
| 200 | error, |
| 201 | radsToDegs(lat_lng1.lat), |
| 202 | radsToDegs(lat_lng1.lng), |
| 203 | radsToDegs(lat_lng2.lat), |
| 204 | radsToDegs(lat_lng2.lng)); |
| 205 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(0); |
| 206 | } |
| 207 | |
| 208 | GRN_API_RETURNcxx . . $ `lib/grn_ctx.h:56:9`!(distance); |
| 209 | #else |
| 210 | GRN_API_ENTER; |
| 211 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "%s H3 isn't enabled", tag); |
| 212 | GRN_API_RETURN(0); |
| 213 | #endif |
| 214 | } |