blib/lib/Geo/H3/FFI.pm

filter: Statementstmt 12.7% 18 / 141 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 24.0% 6 / 25 Podpod n/a totaltotal 12.7% 24 / 188 CC 11 SCAR 23.3
CC
11
Cov
11%
geoToH3Wrapper
42.0
compactWrapper
12.0
kRingWrapper
6.0
CRAP 10.2
↑ Summary
1 package Geo::H3::FFI;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use FFI::CheckLib qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use FFI::Platypus qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use FFI::C qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 our $PACKAGE = __PACKAGE__;
10 our $VERSION = '0.03';
11  
12 my $lib = FFI::CheckLib::find_lib_or_die(lib => 'h3');
13 my $ffi = FFI::Platypus->new(api => 1, lib => $lib);
14 FFI::C->ffi($ffi); #Beware: Class setting
15  
16 $ffi->type('uint64_t[]' => 'uint64_t_array');
17 $ffi->type('int[]' => 'int_array');
18 package Geo::H3::FFI::Struct::H3Index {FFI::C->struct(h3_index_t => [index => 'uint64_t' ])};
19 package Geo::H3::FFI::Array::H3Index {FFI::C->array (array_h3_index_t => [h3_index_t => 255 ])};
20 package Geo::H3::FFI::Struct::GeoCoord {FFI::C->struct(geo_coord_t => [lat => 'double', lon => 'double' ])};
21 package Geo::H3::FFI::Array::GeoCoord {FFI::C->array (array_geo_coord_t => [geo_coord_t => 10 ])};
22 package Geo::H3::FFI::Struct::GeoBoundary {FFI::C->struct(geo_boundary_t => [num_verts => 'int' , verts => 'array_geo_coord_t'])};
23 #package Geo::H3::FFI::Array::Int {FFI::C->array (array_int_t => [int => 19 ])};
24 #package Geo::H3::FFI::Struct::ArrayIntStruct {FFI::C->struct(array_int_struct_t=> ['array' => 'array_int_t' ])};
25  
26  
27 sub _oowrapper {
28 0 ▶ my $xs = shift;
Subroutine _oowrapper: not called CC=1 SCAR=6.9
29 0 my $self = shift;
30 0 return $xs->(@_);
31 }
32  
33 =head1 NAME
34  
35 Geo::H3::FFI - Perl FFI binding to H3 library functions
36  
37 =head1 SYNOPSIS
38  
39 use Geo::H3::FFI;
40  
41 =head1 DESCRIPTION
42  
43 Perl FFI binding to H3 library functions
44  
45 =head1 CONSTRUCTORS
46  
47 =head2 new
48  
49 my $gh3 = Geo::H3::FFI->new;
50  
51 =head2 geo
52  
53 Returns a GeoCoord struct
54  
55 my $geo = $gh3->geo; #empty struct #isa Geo::H3::FFI::Struct::GeoCoord
56 my $geo = $gh3->geo(lat=>$lat_rad, lon=>$lon_rad); #isa Geo::H3::FFI::Struct::GeoCoord
57  
58 =cut
59  
60 sub geo {
61 0 ▶ my $self = shift;
Subroutine geo: not called CC=1 SCAR=6.9
62 0 my %hash = @_;
63 0 return Geo::H3::FFI::Struct::GeoCoord->new(\%hash);
64 }
65  
66 =head2 gb
67  
68 Returns a GeoBoundary struct
69  
70 my $gb = $gh3->gb; #empty struct #isa Geo::H3::FFI::Struct::GeoBoundary
71  
72 =cut
73  
74 sub gb {
75 0 ▶ my $self = shift;
Subroutine gb: not called CC=1 SCAR=6.9
76 0 my %hash = @_;
77 0 return Geo::H3::FFI::Struct::GeoBoundary->new(\%hash);
78 }
79  
80 =head1 Indexing Functions
81  
82 These function are used for finding the H3 index containing coordinates, and for finding the center and boundary of H3 indexes.
83  
84 =head2 geoToH3
85  
86 Indexes the location at the specified resolution, returning the index of the cell containing the location.
87  
88 my $geo = $gh3->geo(lat=>$lat_rad, lon=>$lon_rad); #isa Geo::H3::FFI::Struct::GeoCoord
89 my $resolution = 8; #isa Int in (0 .. 15)
90 my $index = $gh3->geoToH3($geo, $resolution); #isa Int64
91  
92 Returns 0 on error.
93  
94 =cut
95  
96 #H3Index geoToH3(const GeoCoord *g, int res);
97 $ffi->attach(geoToH3 => ['geo_coord_t', 'int'] => 'uint64_t' => \&_oowrapper);
98  
99 =head2 geoToH3Wrapper
100  
101 my $index = $gh3->geoToH3Wrapper(lat=>$lat_rad, lon=>$lon_rad, resolution=>$resolution);
102 my $index = $gh3->geoToH3Wrapper(lat=>$lat, lon=>$lon, resolution=>$resolution, uom=>"deg");
103  
104 =cut
105  
106 sub geoToH3Wrapper {
107 0 ▶ my $self = shift;
Subroutine geoToH3Wrapper: not called CC=6 SCAR=37.4
108 0 my %input = @_;
109 0 ▶ my $lat = $input{'lat'}; die unless defined $lat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lat 0 0 0
110 0 ▶ my $lon = $input{'lon'}; die unless defined $lon;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lon 0 0 0
111 0 ▶ my $uom = $input{'uom'} || 'rad';
Condition0%0/2
$input{'uom'} || 'rad'
l!l
00
MC/DC0%0/1
$input{'uom'} || 'rad'
$input{'uom'}
Truth table0%0/2
A
A= $input{'uom'}
Aresult
11
00
112 0 ▶ if ($uom eq "deg") {
Branch: 1 branch
BranchTrueFalseTotal
if ($uom eq "deg") 0 0 0
113 0 $lat = $self->degsToRads($lat);
114 0 $lon = $self->degsToRads($lon);
115 }
116 0 ▶ my $res = $input{'resolution'} || 0;
Condition0%0/2
$input{'resolution'} || 0
l!l
00
MC/DC0%0/1
$input{'resolution'} || 0
$input{'resolution'}
Truth table0%0/2
A
A= $input{'resolution'}
Aresult
11
00
117 0 my $geo = $self->geo(lat=>$lat, lon=>$lon); #isa Geo::H3::FFI::Struct::GeoCoord
118 0 my $index = $self->geoToH3($geo, $res); #isa Int64
119 0 return $index;
120 }
121  
122 =head2 h3ToGeo
123  
124 Finds the centroid of the index.
125  
126 my $geo = $gh3->geo; #isa Geo::H3::FFI::Struct::GeoCoord
127 $gh3->h3ToGeo($index, $geo);
128 my $lat = $geo->lat; #isa Float in radians
129 my $lon = $geo->lon; #isa Float in radians
130  
131 =cut
132  
133 #void h3ToGeo(H3Index h3, GeoCoord *g);
134 $ffi->attach(h3ToGeo => ['uint64_t', 'geo_coord_t'] => 'void' => \&_oowrapper);
135  
136 =head2 h3ToGeoWrapper
137  
138 my $geo = h3ToGeoWrapper($index); #isa Geo::H3::FFI::Struct::GeoCoord
139 my $lat = $geo->lat; #isa Float in radians
140 my $lon = $geo->lon; #isa Float in radians
141  
142 =cut
143  
144 sub h3ToGeoWrapper {
145 0 ▶ my $self = shift;
Subroutine h3ToGeoWrapper: not called CC=1 SCAR=6.9
146 0 my $index = shift;
147 0 my $geo = $self->geo; #isa Geo::H3::FFI::Struct::GeoCoord
148 0 $self->h3ToGeo($index, $geo);
149 0 return $geo;
150 }
151  
152 =head2 h3ToGeoBoundary
153  
154 Finds the boundary of the index.
155  
156 my $gb = $gh3->gb; #isa empty Geo::H3::FFI::Struct::GeoBoundary
157 $gh3->h3ToGeoBoundary($index, $gb); #populates $gb
158 my $num_verts = $gb->num_verts; #isa Int
159 my $vert0 = $gb->verts->[0]; #isa Geo::H3::FFI::Struct::GeoCord
160  
161 =cut
162  
163 #void h3ToGeoBoundary(H3Index h3, GeoBoundary *gp);
164 $ffi->attach(h3ToGeoBoundary => ['uint64_t', 'geo_boundary_t'] => 'void' => \&_oowrapper);
165  
166 =head2 h3ToGeoBoundaryWrapper
167  
168 my $GeoBoundary = $gh3->h3ToGeoBoundaryWrapper($index); #isa Geo::H3::FFI::Struct::GeoBoundary
169  
170 =cut
171  
172 sub h3ToGeoBoundaryWrapper {
173 0 ▶ my $self = shift;
Subroutine h3ToGeoBoundaryWrapper: not called CC=1 SCAR=6.9
174 0 my $index = shift;
175 0 my $gb = $self->gb;
176 0 $self->h3ToGeoBoundary($index, $gb);
177 0 return $gb;
178 }
179  
180 =head1 Index Inspection Functions
181  
182 These functions provide metadata about an H3 index, such as its resolution or base cell, and provide utilities for converting into and out of the 64-bit representation of an H3 index.
183  
184 =head2 h3GetResolution
185  
186 Returns the resolution of the index.
187  
188 my $resolution = $gh3->h3GetResolution($index); #isa Int
189  
190 =cut
191  
192 #int h3GetResolution(H3Index h);
193 $ffi->attach(h3GetResolution => ['uint64_t'] => 'int' => \&_oowrapper);
194  
195 =head2 h3GetBaseCell
196  
197 Returns the base cell number of the index.
198  
199 my $baseCell = h3GetBaseCell($index);
200  
201 =cut
202  
203 #int h3GetBaseCell(H3Index h);
204 $ffi->attach(h3GetBaseCell => ['uint64_t'] => 'int' => \&_oowrapper);
205  
206 =head2 stringToH3
207  
208 Converts the string representation to H3Index (uint64_t) representation.
209  
210 Returns 0 on error.
211  
212 my $index = $gh3->stringToH3($string, length($string));
213  
214 =cut
215  
216 #H3Index stringToH3(const char *str);
217 $ffi->attach(stringToH3 => ['string', 'size_t'] => 'uint64_t' => \&_oowrapper);
218  
219 =head2 stringToH3Wrapper
220  
221 my $index = $gh3->stringToH3Wrapper($string);
222  
223 =cut
224  
225 sub stringToH3Wrapper {
226 0 ▶ my $self = shift;
Subroutine stringToH3Wrapper: not called CC=1 SCAR=6.9
227 0 my $string = shift;
228 0 my $index = $self->stringToH3($string, length($string));
229 0 return $index;
230 }
231  
232 =head2 h3ToString
233  
234 Converts the H3Index representation of the index to the string representation. str must be at least of length 17.
235  
236 my $size = 17; #Must be 17 for API to work
237 my $string = "\000" x $size;
238 $gh3->h3ToString($index, $string, $size);
239 $string =~ s/\000+\Z//;
240  
241 =cut
242  
243 #void h3ToString(H3Index h, char *str, size_t sz);
244 $ffi->attach(h3ToString => ['uint64_t', 'string', 'size_t'] => 'void' => \&_oowrapper);
245  
246 =head2 h3ToStringWrapper
247  
248 my $string = $gh3->h3ToStringWrapper($index);
249  
250 =cut
251  
252 sub h3ToStringWrapper {
253 0 ▶ my $self = shift;
Subroutine h3ToStringWrapper: not called CC=1 SCAR=6.9
254 0 my $index = shift;
255 0 my $size = 17; #Must be 17 for API to work
256 0 my $string = "\000" x $size;
257 0 $self->h3ToString($index, $string, $size);
258 0 $string =~ s/\000+\Z//;
259 0 return $string;
260 }
261  
262 =head2 h3IsValid
263  
264 Returns non-zero if this is a valid H3 index.
265  
266 my isValid = $gh3->h3IsValid($index);
267  
268 =cut
269  
270 #int h3IsValid(H3Index h);
271 $ffi->attach(h3IsValid => ['uint64_t'] => 'int' => \&_oowrapper);
272  
273 =head2 h3IsResClassIII
274  
275 Returns non-zero if this index has a resolution with Class III orientation.
276  
277 my $isRC3 = $gh3->h3IsResClassIII($index);
278  
279 =cut
280  
281 #int h3IsResClassIII(H3Index h);
282 $ffi->attach(h3IsResClassIII => ['uint64_t'] => 'int' => \&_oowrapper);
283  
284 =head2 h3IsPentagon
285  
286 Returns non-zero if this index represents a pentagonal cell.
287  
288 my $isPentagon = $gh3->h3IsPentagon($index);
289  
290 =cut
291  
292 #int h3IsPentagon(H3Index h);
293 $ffi->attach(h3IsPentagon => ['uint64_t'] => 'int' => \&_oowrapper);
294  
295 =head2 h3GetFaces
296  
297 Find all icosahedron faces intersected by a given H3 index and places them in the array out. out must be at least of length maxFaceCount(h).
298  
299 Faces are represented as integers from 0-19, inclusive. The array is sparse, and empty (no intersection) array values are represented by -1.
300
301 my @array = (-1,-1,-1,-1,-1);
302 $gh3->h3GetFaces($index, \@array); #sets values into initialized array
303  
304 =cut
305  
306 #void h3GetFaces(H3Index h, int* out);
307 $ffi->attach(h3GetFaces => ['uint64_t', 'int_array'] => 'void' => \&_oowrapper);
308  
309 =head2 h3GetFacesWrapper
310  
311 my $array_ref = $gh3->h3GetFacesWrapper($index);
312  
313 =cut
314  
315 sub h3GetFacesWrapper {
316 0 ▶ my $self = shift;
Subroutine h3GetFacesWrapper: not called CC=1 SCAR=6.9
317 0 my $index = shift;
318 0 my $size = $self->maxFaceCount($index);
319 0 my @array = (-1) x $size;
320 0 $self->h3GetFaces($index, \@array);
321 0 return [grep {$_ > -1} @array];
322 }
323  
324 =head2 maxFaceCount
325  
326 Returns the maximum number of icosahedron faces the given H3 index may intersect.
327  
328 my $count = $gh3->maxFaceCount($index);
329  
330 =cut
331  
332 #int maxFaceCount(H3Index h3);
333 $ffi->attach(maxFaceCount => ['uint64_t'] => 'int' => \&_oowrapper);
334  
335 =head1 Grid traversal functions
336  
337 Grid traversal allows finding cells in the vicinity of an origin cell, and determining how to traverse the grid from one cell to another.
338  
339 =head2 kRing
340  
341 k-rings produces indices within k distance of the origin index.
342  
343 k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indices, and so on.
344  
345 Output is placed in the provided array in no particular order. Elements of the output array may be left zero, as can happen when crossing a pentagon.
346  
347 my $size = $gh3->maxKringSize($k);
348 my @array = (-1) x $size;
349 $self->kRing($index, $k, \@array);
350  
351 =cut
352  
353 #void kRing(H3Index origin, int k, H3Index* out);
354 $ffi->attach(kRing => ['uint64_t', 'int', 'uint64_t_array'] => 'void' => \&_oowrapper);
355  
356 =head2 kRingWrapper
357  
358 Returns an array reference of H3 indices with the k distance of the origin index.
359  
360 my $aref = $gh3->kRingWrapper($index, $k); #ias ARRAY of H3 Indexes
361  
362 =cut
363  
364 sub kRingWrapper {
365 0 ▶ my $self = shift;
Subroutine kRingWrapper: not called CC=2 SCAR=17.9
366 0 my $index = shift;
367 0 my $k = shift;
368 0 my $size = $self->maxKringSize($k);
369 0 my @array = (-1) x $size;
370 0 $self->kRing($index, $k, \@array);
371 0 ▶ return [grep {$_ > 0 && $_ < 18446744073709551615} @array];
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0 and $_ < '18446744073709551615' 0 0 0
372 }
373  
374 =head2 maxKringSize
375  
376 Maximum number of indices that result from the kRing algorithm with the given k.
377  
378 my $size = $gh3->maxKringSize($k);
379  
380 =cut
381  
382 #int maxKringSize(int k);
383 $ffi->attach(maxKringSize => ['int'] => 'int' => \&_oowrapper);
384  
385 =head2 kRingDistances
386  
387 k-rings produces indices within k distance of the origin index.
388  
389 k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indices, and so on.
390  
391 Output is placed in the provided array in no particular order. Elements of the output array may be left zero, as can happen when crossing a pentagon.
392  
393 my $size = $gh3->maxKringSize($k);
394 my @array = (-1) x $size;
395 my @dist = (-1) x $size;
396 my %hash = ();
397 $gh3->kRingDistances($index, $k, \@array, \@dist);
398  
399 =cut
400  
401 #void kRingDistances(H3Index origin, int k, H3Index* out, int* distances);
402 $ffi->attach(kRingDistances => ['uint64_t', 'int', 'uint64_t_array', 'int_array'] => 'void' => \&_oowrapper);
403  
404 =head2 kRingDistancesWrapper
405  
406 Returns a hash reference where the keys are the H3 index and values are the k distance for the given index and k value.
407  
408 my $href = $gh3->kRingDistancesWrapper($index, $k); #isa HASH
409  
410 =cut
411  
412 sub kRingDistancesWrapper {
413 0 ▶ my $self = shift;
Subroutine kRingDistancesWrapper: not called CC=1 SCAR=6.9
414 0 my $index = shift;
415 0 my $k = shift;
416 0 my $size = $self->maxKringSize($k);
417 0 my @array = (-1) x $size;
418 0 my @dist = (-1) x $size;
419 0 my %hash = ();
420 0 $self->kRingDistances($index, $k, \@array, \@dist);
421 0 @hash{@array} = @dist; #hash slice assignment
422 0 delete $hash{'18446744073709551615'};
423 0 return \%hash;
424 }
425  
426 =head2 hexRange
427  
428 hexRange produces indexes within k distance of the origin index. Output behavior is undefined when one of the indexes returned by this function is a pentagon or is in the pentagon distortion area.
429  
430 k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indexes, and so on.
431  
432 Output is placed in the provided array in order of increasing distance from the origin.
433  
434 Returns 0 if no pentagonal distortion is encountered.
435  
436 my $return = $gh3->hexRange($index, $k, \@out);
437  
438 =cut
439  
440 #int hexRange(H3Index origin, int k, H3Index* out);
441 $ffi->attach(hexRange => ['uint64_t', 'int', 'uint64_t_array'] => 'int' => \&_oowrapper);
442  
443 =head2 hexRangeWrapper
444  
445 my @indexes = $gh3->hexRangeWrapper($index, $k);
446  
447 =cut
448  
449 sub hexRangeWrapper {
450 0 ▶ my $self = shift;
Subroutine hexRangeWrapper: not called CC=1 SCAR=6.9
451 0 my $index = shift;
452 0 my $k = shift;
453 0 my $size = $self->maxHexRangeSize($k);
454 0 my @array = (-1) x $size;
455 0 my $return = $self->hexRange($index, $k, \@array); #What is $return
456 0 return \@array;
457 }
458  
459 =head2 maxHexRangeSize
460  
461 my $size = $gh3->maxHexRangeSize($k);
462  
463 =cut
464  
465 0 ▶sub maxHexRangeSize {shift->maxKringSize(@_)};
Subroutine maxHexRangeSize: not called CC=1 SCAR=6.9
466  
467 =head2 hexRangeDistances
468  
469 hexRange produces indexes within k distance of the origin index. Output behavior is undefined when one of the indexes returned by this function is a pentagon or is in the pentagon distortion area.
470  
471 k-ring 0 is defined as the origin index, k-ring 1 is defined as k-ring 0 and all neighboring indexes, and so on.
472  
473 Output is placed in the provided array in order of increasing distance from the origin. The distances in hexagons is placed in the distances array at the same offset.
474  
475 Returns 0 if no pentagonal distortion is encountered.
476  
477 my $return = $gh3->hexRangeDistances($index, $k, \@indexes, \@distances);
478  
479 =cut
480  
481 #int hexRangeDistances(H3Index origin, int k, H3Index* out, int* distances);
482 $ffi->attach(hexRangeDistances => ['uint64_t', 'int', 'uint64_t_array', 'int_array'] => 'int' => \&_oowrapper);
483  
484 =head2 hexRangeDistancesWrapper
485  
486 my $href = $gh3->hexRangeDistancesWrapper($index, $k);
487  
488 =cut
489  
490 sub hexRangeDistancesWrapper {
491 0 ▶ my $self = shift;
Subroutine hexRangeDistancesWrapper: not called CC=1 SCAR=6.9
492 0 my $index = shift;
493 0 my $k = shift;
494 0 my $size = $self->maxHexRangeSize($k);
495 0 my @array = (-1) x $size;
496 0 my @dist = (-1) x $size;
497 0 my %hash = ();
498 0 $self->hexRangeDistances($index, $k, \@array, \@dist);
499 0 @hash{@array} = @dist; #hash slice assignment
500 0 delete $hash{'18446744073709551615'};
501 0 return \%hash;
502 }
503  
504 =head2 hexRanges
505  
506 hexRanges takes an array of input hex IDs and a max k-ring and returns an array of hexagon IDs sorted first by the original hex IDs and then by the k-ring (0 to max), with no guaranteed sorting within each k-ring group.
507  
508 Returns 0 if no pentagonal distortion was encountered. Otherwise, output is undefined
509  
510 =cut
511  
512 #int hexRanges(H3Index* h3Set, int length, int k, H3Index* out);
513 $ffi->attach(hexRanges => ['uint64_t_array', 'int', 'int', 'uint64_t_array'] => 'int' => \&_oowrapper);
514  
515 =head2 hexRing
516  
517 Produces the hollow hexagonal ring centered at origin with sides of length k.
518  
519 Returns 0 if no pentagonal distortion was encountered.
520  
521 my $distortion = $gh3->hexRing($index, $k, \@ring);
522  
523 =cut
524  
525 #int hexRing(H3Index origin, int k, H3Index* out);
526 $ffi->attach(hexRing => ['uint64_t', 'int', 'uint64_t_array'] => 'int' => \&_oowrapper);
527  
528 =head2 hexRingWrapper
529  
530 my $aref = $gh3->hexRingWrapper($index, $k);
531  
532 =cut
533  
534 sub hexRingWrapper {
535 0 ▶ my $self = shift;
Subroutine hexRingWrapper: not called CC=2 SCAR=17.9
536 0 my $index = shift;
537 0 my $k = shift;
538 0 my $size = $self->maxHexRingSize($k);
539 0 my @array = (-1) x $size;
540 0 my $distortion = $self->hexRing($index, $k, \@array); #What is $return
541 0 ▶ warn("Error: Package: $PACKAGE, Method: hexRingWrapper, Distrortion: $distortion") if $distortion;
Branch: 1 branch
BranchTrueFalseTotal
if $distortion 0 0 0
542 0 return \@array;
543 }
544  
545 =head2 maxHexRingSize
546  
547 my $size = $gh3->maxHexRingSize($k);
548  
549 =cut
550  
551 sub maxHexRingSize {
552 0 ▶ my $self = shift;
Subroutine maxHexRingSize: not called CC=2 SCAR=17.9
553 0 my $k = shift;
554 0 ▶ return $k == 0 ? 1 : $k * 6; #See: https://www.rubydoc.info/gems/h3/3.2.0/H3%2FTraversal:max_hex_ring_size
Branch: 1 branch
BranchTrueFalseTotal
$k == 0 ? : 0 0 0
555 }
556  
557 =head2 h3Line
558  
559 Given two H3 indexes, return the line of indexes between them (inclusive).
560  
561 This function may fail to find the line between two indexes, for example if they are very far apart. It may also fail when finding distances for indexes on opposite sides of a pentagon.
562  
563 Notes:
564  
565 - The specific output of this function should not be considered stable across library versions. The only guarantees the library provides are that the line length will be h3Distance(start, end) + 1 and that every index in the line will be a neighbor of the preceding index.
566  
567 - Lines are drawn in grid space, and may not correspond exactly to either Cartesian lines or great arcs.
568  
569 =cut
570  
571 #int h3Line(H3Index start, H3Index end, H3Index* out);
572 $ffi->attach(h3Line => ['uint64_t', 'uint64_t', 'uint64_t_array'] => 'int' => \&_oowrapper);
573  
574 =head2 h3LineWrapper
575  
576 my $aref = $gh3->h3LineWrapper($start, $end);
577  
578 =cut
579  
580 sub h3LineWrapper {
581 0 ▶ my $self = shift;
Subroutine h3LineWrapper: not called CC=1 SCAR=6.9
582 0 my $start = shift;
583 0 my $end = shift;
584 0 my $size = $self->h3LineSize($start, $end);
585 0 my @array = (-1) x $size;
586 0 my $int = $self->h3Line($start, $end, \@array); #what is int???
587 0 return \@array;
588 }
589  
590 =head2 h3LineSize
591  
592 Number of indexes in a line from the start index to the end index, to be used for allocating memory. Returns a negative number if the line cannot be computed.
593  
594 =cut
595  
596 #int h3LineSize(H3Index start, H3Index end);
597 $ffi->attach(h3LineSize => ['uint64_t', 'uint64_t'] => 'int' => \&_oowrapper);
598  
599 =head2 h3Distance
600  
601 Returns the distance in grid cells between the two indexes.
602  
603 Returns a negative number if finding the distance failed. Finding the distance can fail because the two indexes are not comparable (different resolutions), too far apart, or are separated by pentagonal distortion. This is the same set of limitations as the local IJ coordinate space functions.
604  
605 =cut
606  
607 #int h3Distance(H3Index origin, H3Index h3);
608 $ffi->attach(h3Distance => ['uint64_t', 'uint64_t'] => 'int' => \&_oowrapper);
609  
610 =head2 experimentalH3ToLocalIj
611  
612 Produces local IJ coordinates for an H3 index anchored by an origin.
613  
614 This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.
615  
616 =cut
617  
618 #int experimentalH3ToLocalIj(H3Index origin, H3Index h3, CoordIJ *out);
619  
620  
621 =head2 experimentalLocalIjToH3
622  
623 Produces an H3 index from local IJ coordinates anchored by an origin.
624  
625 This function is experimental, and its output is not guaranteed to be compatible across different versions of H3.
626  
627 =cut
628  
629 #int experimentalLocalIjToH3(H3Index origin, const CoordIJ *ij, H3Index *out);
630  
631 =head1 Hierarchical grid functions
632  
633 These functions permit moving between resolutions in the H3 grid system. The functions produce parent (coarser) or children (finer) cells.
634  
635 =head2 h3ToParent
636  
637 Returns the parent (coarser) index containing h.
638  
639 my $parent = $gh3->h3ToParent($index, $resolution);
640  
641 =cut
642  
643 #H3Index h3ToParent(H3Index h, int parentRes);
644 $ffi->attach(h3ToParent => ['uint64_t', 'int'] => 'uint64_t' => \&_oowrapper);
645  
646 =head2 h3ToChildren
647  
648 Populates children with the indexes contained by h at resolution childRes. children must be an array of at least size maxH3ToChildrenSize(h, childRes).
649  
650 my $size = $gh3->maxH3ToChildrenSize($index, $res);
651 my @array = (-1) x $size;
652 $gh3->h3ToChildren($index, $res, \@array);
653  
654 =cut
655  
656 #void h3ToChildren(H3Index h, int childRes, H3Index *children);
657 $ffi->attach(h3ToChildren => ['uint64_t', 'int', 'uint64_t_array'] => 'void' => \&_oowrapper);
658  
659 =head2 h3ToChildrenWrapper
660  
661 my $aref = $gh3->h3ToChildrenWrapper($index, $resoultion);
662  
663 =cut
664  
665 sub h3ToChildrenWrapper {
666 0 ▶ my $self = shift;
Subroutine h3ToChildrenWrapper: not called CC=1 SCAR=6.9
667 0 my $index = shift;
668 0 my $res = shift;
669 0 my $size = $self->maxH3ToChildrenSize($index, $res);
670 0 my @array = (-1) x $size;
671 0 $self->h3ToChildren($index, $res, \@array);
672 0 return \@array;
673 }
674  
675 =head2 maxH3ToChildrenSize
676  
677 my $size = $gh3->maxH3ToChildrenSize($index, $res);
678  
679 =cut
680  
681 #int maxH3ToChildrenSize(H3Index h, int childRes);
682 $ffi->attach(maxH3ToChildrenSize => ['uint64_t', 'int'] => 'int' => \&_oowrapper);
683  
684 =head2 h3ToCenterChild
685  
686 Returns the center child (finer) index contained by h at resolution childRes.
687  
688 =cut
689  
690 #H3Index h3ToCenterChild(H3Index h, int childRes);
691 $ffi->attach(h3ToCenterChild => ['uint64_t', 'int'] => 'uint64_t' => \&_oowrapper);
692  
693 =head2 compact
694  
695 Compacts the set h3Set of indexes as best as possible, into the array compactedSet. compactedSet must be at least the size of h3Set in case the set cannot be compacted.
696  
697 Returns 0 on success.
698  
699 =cut
700  
701 #int compact(const H3Index *h3Set, H3Index *compactedSet, const int numHexes);
702 $ffi->attach(compact => ['uint64_t_array', 'uint64_t_array', 'int'] => 'int' => \&_oowrapper);
703  
704 =head2 compactWrapper
705  
706 my $aref = $gh3->compactWrapper(\@indexes);
707  
708 =cut
709  
710 sub compactWrapper {
711 0 ▶ my $self = shift;
Subroutine compactWrapper: not called CC=3 SCAR=24.8
712 0 my $in = shift;
713 0 my $out = [map {-1} @$in];
714 0 my $size = scalar(@$in);
715 0 my $error = $self->compact($in, $out, $size);
716 0 ▶ die(qq{Error: Package $PACKAGE method compact returned error code "$error"}) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
717 0 ▶ return [grep {$_ > 0 and $_ < 18446744073709551615} @$out];
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0 and $_ < '18446744073709551615' 0 0 0
718 }
719  
720 =head2 uncompact
721  
722 Uncompacts the set compactedSet of indexes to the resolution res. h3Set must be at least of size maxUncompactSize(compactedSet, numHexes, res).
723  
724 Returns 0 on success.
725  
726 =cut
727  
728 #int uncompact(const H3Index *compactedSet, const int numHexes, H3Index *h3Set, const int maxHexes, const int res);
729 $ffi->attach(uncompact => ['uint64_t*', 'int', 'uint64_t*', 'int', 'int'] => 'int' => \&_oowrapper);
730  
731 =head2 maxUncompactSize
732  
733 Returns the size of the array needed by uncompact.
734  
735 =cut
736  
737 #int maxUncompactSize(const H3Index *compactedSet, const int numHexes, const int res)
738 $ffi->attach(maxUncompactSize => ['uint64_t*', 'int', 'int'] => 'int' => \&_oowrapper);
739  
740 =head1 Region functions
741  
742 These functions convert H3 indexes to and from polygonal areas.
743  
744 =head2 polyfill
745  
746 polyfill takes a given GeoJSON-like data structure and preallocated, zeroed memory, and fills it with the hexagons that are contained by the GeoJSON-like data structure.
747  
748 Containment is determined by the cells' centroids. A partioning using the GeoJSON-like data structure, where polygons cover an area without overlap, will result in a partitioning in the H3 grid, where cells cover the same area without overlap.
749  
750 =cut
751  
752 #void polyfill(const GeoPolygon* geoPolygon, int res, H3Index* out);
753  
754 =head2 maxPolyfillSize
755  
756 maxPolyfillSize returns the number of hexagons to allocate space for when performing a polyfill on the given GeoJSON-like data structure.
757  
758 =cut
759  
760 #int maxPolyfillSize(const GeoPolygon* geoPolygon, int res);
761  
762 =head2 h3SetToLinkedGeo
763  
764 Create a LinkedGeoPolygon describing the outline(s) of a set of hexagons. Polygon outlines will follow GeoJSON MultiPolygon order: Each polygon will have one outer loop, which is first in the list, followed by any holes.
765  
766 It is the responsibility of the caller to call destroyLinkedPolygon on the populated linked geo structure, or the memory for that structure will not be freed.
767  
768 It is expected that all hexagons in the set have the same resolution and that the set contains no duplicates. Behavior is undefined if duplicates or multiple resolutions are present, and the algorithm may produce unexpected or invalid output.
769  
770 =cut
771  
772 #void h3SetToLinkedGeo(const H3Index* h3Set, const int numHexes, LinkedGeoPolygon* out);
773  
774 =head2 h3SetToMultiPolygon
775  
776 =cut
777  
778 #void h3SetToMultiPolygon(const H3Index* h3Set, const int numHexes, MultiPolygon* out);
779  
780 =head2 destroyLinkedPolygon
781  
782 Free all allocated memory for a linked geo structure. The caller is responsible for freeing memory allocated to the input polygon struct.
783  
784 =cut
785  
786 =head1 Unidirectional edge functions
787  
788 Unidirectional edges allow encoding the directed edge from one cell to a neighboring cell.
789  
790 =head2 h3IndexesAreNeighbors
791  
792 Returns whether or not the provided H3Indexes are neighbors.
793  
794 Returns 1 if the indexes are neighbors, 0 otherwise.
795  
796 =cut
797  
798 #int h3IndexesAreNeighbors(H3Index origin, H3Index destination);
799 $ffi->attach(h3IndexesAreNeighbors => ['uint64_t', 'uint64_t'] => 'int' => \&_oowrapper);
800  
801 =head2 getH3UnidirectionalEdge
802  
803 Returns a unidirectional edge H3 index based on the provided origin and destination.
804  
805 Returns 0 on error.
806  
807 =cut
808  
809 #H3Index getH3UnidirectionalEdge(H3Index origin, H3Index destination);
810 $ffi->attach(getH3UnidirectionalEdge => ['uint64_t', 'uint64_t'] => 'uint64_t' => \&_oowrapper);
811  
812 =head2 h3UnidirectionalEdgeIsValid
813  
814 Determines if the provided H3Index is a valid unidirectional edge index.
815  
816 Returns 1 if it is a unidirectional edge H3Index, otherwise 0.
817  
818 =cut
819  
820 #int h3UnidirectionalEdgeIsValid(H3Index edge);
821 $ffi->attach(h3UnidirectionalEdgeIsValid => ['uint64_t'] => 'int' => \&_oowrapper);
822  
823 =head2 getOriginH3IndexFromUnidirectionalEdge
824  
825 Returns the origin hexagon from the unidirectional edge H3Index.
826  
827 =cut
828  
829 #H3Index getOriginH3IndexFromUnidirectionalEdge(H3Index edge);
830 $ffi->attach(getOriginH3IndexFromUnidirectionalEdge => ['uint64_t'] => 'uint64_t' => \&_oowrapper);
831  
832 =head2 getDestinationH3IndexFromUnidirectionalEdge
833  
834 Returns the destination hexagon from the unidirectional edge H3Index.
835  
836 =cut
837  
838 #H3Index getDestinationH3IndexFromUnidirectionalEdge(H3Index edge);
839 $ffi->attach(getDestinationH3IndexFromUnidirectionalEdge => ['uint64_t'] => 'uint64_t' => \&_oowrapper);
840  
841 =head2 getH3IndexesFromUnidirectionalEdge
842  
843 Returns the origin, destination pair of hexagon IDs for the given edge ID, which are placed at originDestination[0] and originDestination[1] respectively.
844  
845 =cut
846  
847 #void getH3IndexesFromUnidirectionalEdge(H3Index edge, H3Index* originDestination);
848  
849 =head2 getH3UnidirectionalEdgesFromHexagon
850  
851 Provides all of the unidirectional edges from the current H3Index. edges must be of length 6, and the number of undirectional edges placed in the array may be less than 6.
852  
853 =cut
854  
855 #void getH3UnidirectionalEdgesFromHexagon(H3Index origin, H3Index* edges);
856  
857 =head2 getH3UnidirectionalEdgeBoundary
858  
859 Provides the coordinates defining the unidirectional edge.
860  
861 =cut
862  
863 #void getH3UnidirectionalEdgeBoundary(H3Index edge, GeoBoundary* gb);
864 $ffi->attach(getH3UnidirectionalEdgeBoundary => ['uint64_t', 'geo_boundary_t'] => 'void' => \&_oowrapper);
865  
866 =head1 Miscellaneous H3 functions
867  
868 These functions include descriptions of the H3 grid system.
869  
870 =head2 degsToRads
871  
872 Converts degrees to radians.
873  
874 =cut
875  
876 #double degsToRads(double degrees);
877 $ffi->attach(degsToRads => ['double'] => 'double' => \&_oowrapper);
878  
879 =head2 radsToDegs
880  
881 Converts radians to degrees.
882  
883 =cut
884  
885 #double radsToDegs(double radians);
886 $ffi->attach(radsToDegs => ['double'] => 'double' => \&_oowrapper);
887  
888 =head2 hexAreaKm2
889  
890 Average hexagon area in square kilometers at the given resolution.
891  
892 =cut
893  
894 #double hexAreaKm2(int res);
895 $ffi->attach(hexAreaKm2 => ['int'] => 'double' => \&_oowrapper);
896  
897 =head2 hexAreaM2
898  
899 Average hexagon area in square meters at the given resolution.
900  
901 =cut
902  
903 #double hexAreaM2(int res);
904 $ffi->attach(hexAreaM2 => ['int'] => 'double' => \&_oowrapper);
905  
906 =head2 cellAreaM2
907  
908 Exact area of specific cell in square meters.
909  
910 =cut
911  
912 #double cellAreaM2(H3Index h);
913 $ffi->attach(cellAreaM2 => ['uint64_t'] => 'double' => \&_oowrapper);
914  
915 =head2 cellAreaRads2
916  
917 Exact area of specific cell in square radians.
918  
919 =cut
920  
921 #double cellAreaRads2(H3Index h);
922 $ffi->attach(cellAreaRads2 => ['uint64_t'] => 'double' => \&_oowrapper);
923  
924 =head2 edgeLengthKm
925  
926 Average hexagon edge length in kilometers at the given resolution.
927  
928 =cut
929  
930 #double edgeLengthKm(int res);
931 $ffi->attach(edgeLengthKm=> ['int'] => 'double' => \&_oowrapper);
932  
933 =head2 edgeLengthM
934  
935 Average hexagon edge length in meters at the given resolution.
936  
937 =cut
938  
939 #double edgeLengthM(int res);
940 $ffi->attach(edgeLengthM=> ['int'] => 'double' => \&_oowrapper);
941  
942 =head2 exactEdgeLengthKm
943  
944 Exact edge length of specific unidirectional edge in kilometers.
945  
946 =cut
947  
948 #double exactEdgeLengthKm(H3Index edge);
949 $ffi->attach(exactEdgeLengthKm=> ['uint64_t'] => 'double' => \&_oowrapper);
950  
951 =head2 exactEdgeLengthM
952  
953 Exact edge length of specific unidirectional edge in meters.
954  
955 =cut
956  
957 #double exactEdgeLengthM(H3Index edge);
958 $ffi->attach(exactEdgeLengthM => ['uint64_t'] => 'double' => \&_oowrapper);
959  
960 =head2 exactEdgeLengthRads
961  
962 Exact edge length of specific unidirectional edge in radians.
963  
964 =cut
965  
966 #double exactEdgeLengthRads(H3Index edge);
967 $ffi->attach(exactEdgeLengthRads => ['uint64_t'] => 'double' => \&_oowrapper);
968  
969 =head2 numHexagons
970  
971 Number of unique H3 indexes at the given resolution.
972  
973 =cut
974  
975 #int64_t numHexagons(int res);
976 $ffi->attach(numHexagons => ['int'] => 'int64_t' => \&_oowrapper);
977  
978 =head2 getRes0Indexes
979  
980 All the resolution 0 H3 indexes. out must be an array of at least size res0IndexCount().
981  
982 =cut
983  
984 #void getRes0Indexes(H3Index *out);
985  
986 =head2 res0IndexCount
987  
988 Number of resolution 0 H3 indexes.
989  
990 =cut
991  
992 #int res0IndexCount();
993 $ffi->attach(res0IndexCount => [] => 'int' => \&_oowrapper);
994  
995 =head2 getPentagonIndexes
996  
997 All the pentagon H3 indexes at the specified resolution. out must be an array of at least size pentagonIndexCount().
998  
999 =cut
1000  
1001 #void getPentagonIndexes(int res, H3Index *out);
1002  
1003 =head2 pentagonIndexCount
1004  
1005 Number of pentagon H3 indexes per resolution. This is always 12, but provided as a convenience.
1006  
1007 =cut
1008  
1009 #int pentagonIndexCount();
1010 $ffi->attach(pentagonIndexCount => [] => 'int' => \&_oowrapper);
1011  
1012 =head2 pointDistKm
1013  
1014 Gives the "great circle" or "haversine" distance between pairs of GeoCoord points (lat/lon pairs) in kilometers.
1015  
1016 =cut
1017  
1018 #double pointDistKm(const GeoCoord *a, const GeoCoord *b);
1019 $ffi->attach(pointDistKm => ['geo_coord_t', 'geo_coord_t'] => 'double' => \&_oowrapper);
1020  
1021 =head2 pointDistM
1022  
1023 Gives the "great circle" or "haversine" distance between pairs of GeoCoord points (lat/lon pairs) in meters.
1024  
1025 =cut
1026  
1027 #double pointDistM(const GeoCoord *a, const GeoCoord *b);
1028 $ffi->attach(pointDistM => ['geo_coord_t', 'geo_coord_t'] => 'double' => \&_oowrapper);
1029  
1030  
1031 =head2 pointDistRads
1032  
1033 Gives the "great circle" or "haversine" distance between pairs of GeoCoord points (lat/lon pairs) in radians.
1034  
1035 =cut
1036  
1037 #double pointDistRads(const GeoCoord *a, const GeoCoord *b);
1038 $ffi->attach(pointDistRads => ['geo_coord_t', 'geo_coord_t'] => 'double' => \&_oowrapper);
1039  
1040  
1041 =head1 SEE ALSO
1042  
1043 L<https://h3geo.org/docs/api/indexing>, L<https://h3geo.org/docs/community/bindings>, L<FFI::CheckLib>, L<FFI::Platypus>, L<FFI::C>
1044  
1045 =head1 AUTHOR
1046  
1047 Michael R. Davis
1048  
1049 =head1 COPYRIGHT AND LICENSE
1050  
1051 MIT License
1052  
1053 Copyright (c) 2020 Michael R. Davis
1054  
1055 Permission is hereby granted, free of charge, to any person obtaining a copy
1056 of this software and associated documentation files (the "Software"), to deal
1057 in the Software without restriction, including without limitation the rights
1058 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1059 copies of the Software, and to permit persons to whom the Software is
1060 furnished to do so, subject to the following conditions:
1061  
1062 The above copyright notice and this permission notice shall be included in all
1063 copies or substantial portions of the Software.
1064  
1065 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1066 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1067 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1068 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1069 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1070 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1071 SOFTWARE.
1072  
1073 =cut
1074  
1075 1;
↑ Summary