Debian Codegraph @

lib/h3.c

raw symbols SCIP
1 /*
2 Copyright (C) 2024 Sutou Kouhei <[email protected]>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "grn_ctx.h"
20
21 #ifdef GRN_WITH_H3
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_H3
30 static const char *
31 grn_h3_error_to_string(H3Error error)
32 {
33 switch (error) {
34 case E_SUCCESS:
35 return "success";
36 case E_FAILED:
37 return "failed";
38 case E_DOMAIN:
39 return "outside of acceptable range";
40 case E_LATLNG_DOMAIN:
41 return "latitude or longitude is outside of acceptable range";
42 case E_RES_DOMAIN:
43 return "resolution is outside of acceptable range";
44 case E_CELL_INVALID:
45 return "invalid cell";
46 case E_DIR_EDGE_INVALID:
47 return "invalid directed edge";
48 case E_UNDIR_EDGE_INVALID:
49 return "invalid undirected edge";
50 case E_VERTEX_INVALID:
51 return "invalid vertex";
52 case E_PENTAGON:
53 return "pentagon distortion is encountered";
54 case E_DUPLICATE_INPUT:
55 return "duplicate input is encountered";
56 case E_NOT_NEIGHBORS:
57 return "cells aren't neighbors";
58 case E_RES_MISMATCH:
59 return "cells have incompatible resolutions";
60 case E_MEMORY_ALLOC:
61 return "failed to allocate memory";
62 case E_MEMORY_BOUNDS:
63 return "bounds of provided memory are not large enough";
64 case E_OPTION_INVALID:
65 return "invalid option";
66 default:
67 return "unknown";
68 }
69 }
70 #endif
71
72 uint64_t
73 grn_h3_compute_cell(grn_ctx *ctx,
74 grn_geo_point *geo_point,
75 int32_t resolution,
76 const char *tag)
77 {
78 #ifdef GRN_WITH_H3
79 GRN_API_ENTER;
80
81 if (!(0 <= resolution && resolution <= 15)) {
82 ERR(GRN_INVALID_ARGUMENT,
83 "%s resolution must be in 0..15: %d",
84 tag,
85 resolution);
86 GRN_API_RETURN(ctx->rc);
87 }
88
89 LatLng lat_lng = {
90 .lat = GRN_GEO_MSEC2RADIAN(geo_point->latitude),
91 .lng = GRN_GEO_MSEC2RADIAN(geo_point->longitude),
92 };
93 H3Index h3_index = 0;
94 H3Error error = latLngToCell(&lat_lng, resolution, &h3_index);
95 if (error != E_SUCCESS) {
96 ERR(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_MSEC2DEGREE(geo_point->latitude),
102 GRN_GEO_MSEC2DEGREE(geo_point->longitude));
103 }
104 GRN_API_RETURN(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_rc
113 grn_h3_compute_grid_disk(grn_ctx *ctx,
114 grn_geo_point *geo_point,
115 int32_t resolution,
116 int32_t k,
117 grn_obj *h3_indices,
118 const char *tag)
119 {
120 #ifdef GRN_WITH_H3
121 GRN_API_ENTER;
122
123 if (!(0 <= resolution && resolution <= 15)) {
124 ERR(GRN_INVALID_ARGUMENT,
125 "%s resolution must be in 0..15: %d",
126 tag,
127 resolution);
128 GRN_API_RETURN(ctx->rc);
129 }
130
131 if (k < 0) {
132 ERR(GRN_INVALID_ARGUMENT, "%s k must be 0 or larger: %d", tag, k);
133 GRN_API_RETURN(ctx->rc);
134 }
135
136 int64_t n_h3_indices = 0;
137 H3Error error = maxGridDiskSize(k, &n_h3_indices);
138 if (error != E_SUCCESS) {
139 ERR(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 H3Index h3_index = grn_h3_compute_cell(ctx, geo_point, resolution, tag);
149 if (ctx->rc != GRN_SUCCESS) {
150 GRN_API_RETURN(ctx->rc);
151 }
152
153 size_t offset = GRN_BULK_VSIZE(h3_indices);
154 int64_t i;
155 for (i = 0; i < n_h3_indices; i++) {
156 GRN_UINT64_PUT(ctx, h3_indices, 0);
157 }
158 error =
159 gridDisk(h3_index, k, (H3Index *)(GRN_BULK_HEAD(h3_indices) + offset));
160 if (error != E_SUCCESS) {
161 ERR(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_MSEC2DEGREE(geo_point->latitude),
168 GRN_GEO_MSEC2DEGREE(geo_point->longitude),
169 resolution);
170 }
171
172 GRN_API_RETURN(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_t
181 grn_h3_compute_grid_distance(grn_ctx *ctx,
182 uint64_t h3_index1,
183 uint64_t h3_index2,
184 const char *tag)
185 {
186 #ifdef GRN_WITH_H3
187 GRN_API_ENTER;
188
189 int64_t distance;
190 H3Error error = gridDistance(h3_index1, h3_index2, &distance);
191 if (error != E_SUCCESS) {
192 LatLng lat_lng1;
193 cellToLatLng(h3_index1, &lat_lng1);
194 LatLng lat_lng2;
195 cellToLatLng(h3_index2, &lat_lng2);
196 ERR(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_RETURN(0);
206 }
207
208 GRN_API_RETURN(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 }