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