blib/lib/Geo/H3/FFI.pm untested

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