| 1 | package Geo::H3::Index; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw{Geo::H3::Base}; #provides new and ffi | ||
| 5 | require Geo::H3::Geo; | ||
| 6 | require Geo::H3::GeoBoundary; | ||
| 7 | use List::Util qw{sum}; | ||
| 8 | |||
| 9 | our $VERSION = '0.10'; | ||
| 10 | our $PACKAGE = __PACKAGE__; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Geo::H3::Index - H3 Geospatial Hexagon Indexing System Index Object | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use Geo::H3::Index; | ||
| 19 | my $h3 = Geo::H3::Index->new(string => $string); #isa Geo::H3::Index | ||
| 20 | my $center = $h3->geo; #isa Geo::H3::Geo | ||
| 21 | my $lat = $center->lat; #isa double WGS-84 Decimal Degrees | ||
| 22 | my $lon = $center->lon; #isa double WGS-84 Decimal Degrees | ||
| 23 | my $boundary = $h3->geoBoundary; #isa Geo::H3::GeoBoundary | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | H3 Geospatial Hexagon Indexing System Index Object provides the primary interface for working with H3 Indexes. | ||
| 28 | |||
| 29 | =head1 CONSTRUCTORS | ||
| 30 | |||
| 31 | =head2 new | ||
| 32 | |||
| 33 | my $h3 = Geo::H3::Index->new(string=>$string); | ||
| 34 | my $h3 = Geo::H3::Index->new(uint64=>$uint64); | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub _bless_aref { | ||
| 39 | my $self = shift; | ||
| 40 | my $aref = shift; | ||
| 41 | return [map {Geo::H3::Index->new(uint64=>$_, ffi=>$self->ffi)} @$aref]; | ||
| 42 | } | ||
| 43 | |||
| 44 | =head1 PROPERTIES | ||
| 45 | |||
| 46 | =head2 string | ||
| 47 | |||
| 48 | Returns the H3 string representation. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub string { | ||
| 53 | my $self = shift; | ||
| 54 | unless ($self->{'string'}) { | ||
| 55 | if (defined($self->{'uint64'})) { | ||
| 56 | $self->{'string'} = $self->ffi->h3ToStringWrapper($self->{'uint64'}); | ||
| 57 | } elsif (defined($self->{'index'})) { | ||
| 58 | if ($self->{'index'} =~ m/\A[0-9a-f]{15}\Z/) { #821c07fffffffff | ||
| 59 | $self->{'string'} = $self->{'index'}; | ||
| 60 | } else { | ||
| 61 | $self->{'string'} = $self->ffi->h3ToStringWrapper($self->{'index'}); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return $self->{'string'}; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 uint64 | ||
| 69 | |||
| 70 | Returns the H3 uint64 representation. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub uint64 { | ||
| 75 | my $self = shift; | ||
| 76 | unless ($self->{'uint64'}) { | ||
| 77 | $self->{'uint64'} = $self->ffi->stringToH3Wrapper($self->string); | ||
| 78 | } | ||
| 79 | return $self->{'uint64'}; | ||
| 80 | } | ||
| 81 | |||
| 82 | =head2 index (DEPRECATED) | ||
| 83 | |||
| 84 | Returns the H3 uint64 representation. | ||
| 85 | |||
| 86 | Please note that `index()` was difficult to remember consistently. Therefore, I plan to move to the `uint64()` property moving forward while maintaining backwards compatibility. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | sub index { | ||
| 91 | my $self = shift; | ||
| 92 | $self->{'index'} = $self->{'uint64'} unless $self->{'index'}; | ||
| 93 | return $self->{'index'}; | ||
| 94 | } | ||
| 95 | |||
| 96 | =head2 resolution | ||
| 97 | |||
| 98 | Returns the resolution of the hex. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub resolution { | ||
| 103 | my $self = shift; | ||
| 104 | return $self->ffi->h3GetResolution($self->uint64); | ||
| 105 | } | ||
| 106 | |||
| 107 | =head2 baseCell | ||
| 108 | |||
| 109 | Returns the base cell number of the hex. | ||
| 110 | |||
| 111 | =cut | ||
| 112 | |||
| 113 | sub baseCell { | ||
| 114 | my $self = shift; | ||
| 115 | return $self->ffi->h3GetBaseCell($self->uint64); | ||
| 116 | } | ||
| 117 | |||
| 118 | =head2 isValid | ||
| 119 | |||
| 120 | Returns non-zero if this is a valid H3 hex. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | sub isValid { | ||
| 125 | my $self = shift; | ||
| 126 | return $self->ffi->h3IsValid($self->uint64); | ||
| 127 | } | ||
| 128 | |||
| 129 | =head2 isResClassIII | ||
| 130 | |||
| 131 | Returns non-zero if this hex has a resolution with Class III orientation. | ||
| 132 | |||
| 133 | =cut | ||
| 134 | |||
| 135 | sub isResClassIII { | ||
| 136 | my $self = shift; | ||
| 137 | return $self->ffi->h3IsResClassIII($self->uint64); | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 isPentagon | ||
| 141 | |||
| 142 | Returns non-zero if this hex represents a pentagonal cell. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub isPentagon { | ||
| 147 | my $self = shift; | ||
| 148 | return $self->ffi->h3IsPentagon($self->uint64); | ||
| 149 | } | ||
| 150 | |||
| 151 | =head2 maxFaceCount | ||
| 152 | |||
| 153 | Returns the maximum number of icosahedron faces the given H3 hex may intersect. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub maxFaceCount { | ||
| 158 | my $self = shift; | ||
| 159 | return $self->ffi->maxFaceCount($self->uint64); | ||
| 160 | } | ||
| 161 | |||
| 162 | =head2 area | ||
| 163 | |||
| 164 | Returns the area in square meters of this hex. | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | sub area { | ||
| 169 | my $self = shift; | ||
| 170 | return $self->ffi->cellAreaM2($self->uint64); | ||
| 171 | } | ||
| 172 | |||
| 173 | =head2 areaApprox | ||
| 174 | |||
| 175 | Returns the average area in square meters of hexes at this resolution. | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | sub areaApprox { | ||
| 180 | my $self = shift; | ||
| 181 | return $self->ffi->hexAreaM2($self->resolution); | ||
| 182 | } | ||
| 183 | |||
| 184 | =head2 edgeLength | ||
| 185 | |||
| 186 | Returns the exact edge length in meters that this hex shares with the passed in neighbor. | ||
| 187 | |||
| 188 | my $edgeLength_meters = $hex->edgeLength(); #default hex is first kring neighbor | ||
| 189 | my $edgeLength_meters = $hex->edgeLength($neighbor_hex_obj); | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | sub edgeLength { | ||
| 194 | my $self = shift; | ||
| 195 | my $neighbor = shift || [grep {$_->[1] == 1} @{$self->kRingDistances(1)}]->[0]->[0]; #default first kring | ||
| 196 | my $edge = $self->ffi->getH3UnidirectionalEdge($self->uint64, $neighbor->uint64); #edges not exposed currently | ||
| 197 | return $self->ffi->exactEdgeLengthM($edge); | ||
| 198 | } | ||
| 199 | |||
| 200 | =head2 edgeLengthAverage | ||
| 201 | |||
| 202 | Returns the average of the exact edge lengths in meters of this hex. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | sub edgeLengthAverage { | ||
| 207 | my $self = shift; | ||
| 208 | my @neighbors = map {$_->[0]} grep {$_->[1] == 1} @{$self->kRingDistances(1)}; | ||
| 209 | my @edgeLengths = (); | ||
| 210 | foreach my $neighbor (@neighbors) { | ||
| 211 | my $edge = $self->ffi->getH3UnidirectionalEdge($self->uint64, $neighbor->uint64); | ||
| 212 | my $edgeLength = $self->ffi->exactEdgeLengthM($edge); | ||
| 213 | push @edgeLengths, $edgeLength; | ||
| 214 | } | ||
| 215 | return @edgeLengths > 0 ? sum(@edgeLengths)/@edgeLengths : undef; | ||
| 216 | } | ||
| 217 | |||
| 218 | =head2 edgeLengthApprox | ||
| 219 | |||
| 220 | Returns the average edge length in meters of hexes at this resolution. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | sub edgeLengthApprox { | ||
| 225 | my $self = shift; | ||
| 226 | return $self->ffi->edgeLengthM($self->resolution); | ||
| 227 | } | ||
| 228 | |||
| 229 | =head1 METHODS | ||
| 230 | |||
| 231 | =head2 geo | ||
| 232 | |||
| 233 | Returns the centroid of the hex as a L<Geo::H3::Geo> object. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub geo { | ||
| 238 | my $self = shift; | ||
| 239 | my $geo = $self->ffi->h3ToGeoWrapper($self->uint64); | ||
| 240 | my $lat = $self->ffi->radsToDegs($geo->lat); | ||
| 241 | my $lon = $self->ffi->radsToDegs($geo->lon); | ||
| 242 | return Geo::H3::Geo->new(lat=>$lat, lon=>$lon, ffi=>$self->ffi); | ||
| 243 | } | ||
| 244 | |||
| 245 | =head2 geoBoundary | ||
| 246 | |||
| 247 | Returns the boundary of the hex as a L<Geo::H3::GeoBoundary> object | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub geoBoundary { | ||
| 252 | my $self = shift; | ||
| 253 | return Geo::H3::GeoBoundary->new(gb=>$self->ffi->h3ToGeoBoundaryWrapper($self->uint64), ffi=>$self->ffi); | ||
| 254 | } | ||
| 255 | |||
| 256 | =head2 parent | ||
| 257 | |||
| 258 | Returns a parent hex of this hex as a L<Geo::H3::Index> object. | ||
| 259 | |||
| 260 | my $parent = $h3->parent; #next larger resolution | ||
| 261 | my $parent = $h3->parent(1); #isa Geo::H3::Index | ||
| 262 | |||
| 263 | =cut | ||
| 264 | |||
| 265 | sub parent { | ||
| 266 | my $self = shift; | ||
| 267 | my $resolution = shift || $self->resolution - 1; | ||
| 268 | return Geo::H3::Index->new(uint64=>$self->ffi->h3ToParent($self->uint64, $resolution), ffi=>$self->ffi); | ||
| 269 | } | ||
| 270 | |||
| 271 | =head2 children | ||
| 272 | |||
| 273 | Returns the children of the hex as an array reference of L<Geo::H3::Index> objects. | ||
| 274 | |||
| 275 | my $children = $h3->children(12); #isa ARRAY | ||
| 276 | my $children = $h3->children; #next smaller resolution | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub children { | ||
| 281 | my $self = shift; | ||
| 282 | my $resolution = shift || $self->resolution + 1; | ||
| 283 | return $self->_bless_aref($self->ffi->h3ToChildrenWrapper($self->uint64, $resolution)); | ||
| 284 | } | ||
| 285 | |||
| 286 | =head2 centerChild | ||
| 287 | |||
| 288 | Returns the center child (finer) hex contained by this hex at given resolution. | ||
| 289 | |||
| 290 | my $centerChild = $hex->centerChild; #isa Geo::H3::Index | ||
| 291 | my $centerChild = $hex->centerChild(12); #isa Geo::H3::Index | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | sub centerChild { | ||
| 296 | my $self = shift; | ||
| 297 | my $resolution = shift || $self->resolution + 1; | ||
| 298 | return Geo::H3::Index->new(uint64=>$self->ffi->h3ToCenterChild($self->uint64, $resolution), ffi=>$self->ffi); | ||
| 299 | } | ||
| 300 | |||
| 301 | =head2 kRing | ||
| 302 | |||
| 303 | Returns k-rings hexes within k distance of the origin hex. | ||
| 304 | |||
| 305 | my $hexes_aref = $hex->kRing($k); #isa ARRAY of L<Geo::H3::Index> objects | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | sub kRing { | ||
| 310 | my $self = shift; | ||
| 311 | my $k = shift || 1; | ||
| 312 | return $self->_bless_aref($self->ffi->kRingWrapper($self->uint64, $k)); | ||
| 313 | } | ||
| 314 | |||
| 315 | =head2 kRingDistances | ||
| 316 | |||
| 317 | Returns a hash reference where the keys are the H3 hex and values are the k distance for the given hex and k value. | ||
| 318 | |||
| 319 | my $distances_aref = $hex->kRingDistances($k); #isa ARRAY-ARRAY [ [$hex1, $dist1], [$hex2, $dist2], ... [$hexN, $distN] ] | ||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | sub kRingDistances { | ||
| 324 | my $self = shift; | ||
| 325 | my $k = shift || 1; | ||
| 326 | my $aref = $self->ffi->kRingDistancesWrapperArray($self->uint64, $k); #isa ARRAY-ARRAY [[$uint64, $k_distance], ..] | ||
| 327 | $_->[0] = $self->new(uint64=>$_->[0]) foreach @$aref; #bless uint64 column | ||
| 328 | return $aref; | ||
| 329 | } | ||
| 330 | |||
| 331 | =head2 hexRange | ||
| 332 | |||
| 333 | Returns an array reference of hexes within k distance of the hex object. 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. | ||
| 334 | |||
| 335 | my $hexes_aref = $hex->hexRange($k); | ||
| 336 | |||
| 337 | =cut | ||
| 338 | |||
| 339 | sub hexRange { | ||
| 340 | my $self = shift; | ||
| 341 | my $k = shift || 1; | ||
| 342 | return $self->_bless_aref($self->ffi->hexRangeWrapper($self->uint64, $k)); | ||
| 343 | } | ||
| 344 | |||
| 345 | =head2 hexRangeDistances | ||
| 346 | |||
| 347 | Returns a hash reference where the keys are the H3 uint64 and values are the k distance for the given hex and k value. | ||
| 348 | |||
| 349 | my $hash = $hex->hexRangeDistances($k); #isa ARRAY-ARRAY [ [$hex1, $dist1], [$hex2, $dist2], ... [$hexN, $distN] ] | ||
| 350 | |||
| 351 | =cut | ||
| 352 | |||
| 353 | sub hexRangeDistances { | ||
| 354 | my $self = shift; | ||
| 355 | my $k = shift || 1; | ||
| 356 | my $aref = $self->ffi->hexRangeDistancesWrapperArray($self->uint64, $k); #isa ARRAY-ARRAY [[$uint64, $k_distance], ..] | ||
| 357 | $_->[0] = $self->new(uint64=>$_->[0]) foreach @$aref; #bless uint64 column | ||
| 358 | return $aref; | ||
| 359 | } | ||
| 360 | |||
| 361 | =head2 hexRing | ||
| 362 | |||
| 363 | Returns the hex ring of this hex as an array reference of L<Geo::H3::Index> objects | ||
| 364 | |||
| 365 | my $hexes = $h3->hexRing; #default k = 1 | ||
| 366 | my $hexes = $h3->hexRing(5); #isa ARRAY | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub hexRing { | ||
| 371 | my $self = shift; | ||
| 372 | my $k = shift || 1; | ||
| 373 | return $self->_bless_aref($self->ffi->hexRingWrapper($self->uint64, $k)); | ||
| 374 | } | ||
| 375 | |||
| 376 | =head2 areNeighbors | ||
| 377 | |||
| 378 | Returns a 1 or 0 based on whether or not the provided hex object is a neighbor. | ||
| 379 | |||
| 380 | my $areNeighbors = $start_hex->areNeighbors($end_hex); | ||
| 381 | |||
| 382 | =cut | ||
| 383 | |||
| 384 | sub areNeighbors { | ||
| 385 | my $self = shift; | ||
| 386 | my $end = shift; | ||
| 387 | return $self->ffi->h3IndexesAreNeighbors($self->uint64, $end->uint64); | ||
| 388 | } | ||
| 389 | |||
| 390 | =head2 line | ||
| 391 | |||
| 392 | Returns the hexes starting at this hex to the given end hex as array reference of L<Geo::H3::Index> objects. | ||
| 393 | |||
| 394 | my $list_aref = $start_hex_obj->line($end_hex_obj); | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | sub line { | ||
| 399 | my $self = shift; | ||
| 400 | my $end = shift; | ||
| 401 | return $self->_bless_aref($self->ffi->h3LineWrapper($self->uint64, $end->uint64)); | ||
| 402 | } | ||
| 403 | |||
| 404 | =head2 distance | ||
| 405 | |||
| 406 | Returns the distance in grid cells between this hex to the given end hex. | ||
| 407 | |||
| 408 | my $distance = $start_hex_obj->distance($end_hex_obj); | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | sub distance { | ||
| 413 | my $self = shift; | ||
| 414 | my $end = shift; | ||
| 415 | return $self->ffi->h3Distance($self->uint64, $end->uint64); | ||
| 416 | } | ||
| 417 | |||
| 418 | =head1 SEE ALSO | ||
| 419 | |||
| 420 | L<Geo::H3>, L<Geo::H3::FFI> | ||
| 421 | |||
| 422 | =head1 AUTHOR | ||
| 423 | |||
| 424 | Michael R. Davis | ||
| 425 | |||
| 426 | =head1 COPYRIGHT AND LICENSE | ||
| 427 | |||
| 428 | MIT License | ||
| 429 | |||
| 430 | Copyright (c) 2021 Michael R. Davis | ||
| 431 | |||
| 432 | =cut | ||
| 433 | |||
| 434 | 1; |