| 1 | package Geo::H3::Index; | ||
| 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{Geo::H3::Base}; #provides new and ffi |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | require Geo::H3::Geo; | ||
| 6 | require Geo::H3::GeoBoundary; | ||
| 7 | |||
| 8 | our $VERSION = '0.04'; | ||
| 9 | our $PACKAGE = __PACKAGE__; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Geo::H3::Index - H3 Geospatial Hexagon Indexing System | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Geo::H3::Index; | ||
| 18 | my $h3 = Geo::H3::Index->new(index=$index); #isa Geo::H3::Index | ||
| 19 | my $centroid = $h3->geo; #isa Geo::H3::GeoCoord | ||
| 20 | my $lat = $center->lat; #isa double WGS-84 Decimal Degrees | ||
| 21 | my $lon = $center->lon; #isa double WGS-84 Decimal Degrees | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Perl API to the H3 Geospatial Hexagon Indexing System. | ||
| 26 | |||
| 27 | =head1 CONSTRUCTORS | ||
| 28 | |||
| 29 | =head2 new | ||
| 30 | |||
| 31 | my $geo = Geo::H3::Index->new(index=>$index); | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub _bless_aref { | ||
| 36 | my $self = shift; | ||
| 37 | my $aref = shift; | ||
| 38 | return [map {Geo::H3::Index->new(index=>$_)} @$aref]; | ||
| 39 | } | ||
| 40 | |||
| 41 | =head1 PROPERTIES | ||
| 42 | |||
| 43 | =head2 index | ||
| 44 | |||
| 45 | Returns the H3 index uint64 representation | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | sub index { | ||
| 50 | my $self = shift; | ||
| 51 | return $self->{'index'}; | ||
| 52 | } | ||
| 53 | |||
| 54 | =head2 string | ||
| 55 | |||
| 56 | Returns the H3 string representation. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub string { | ||
| 61 | my $self = shift; | ||
| 62 | return sprintf("%x", $self->index); | ||
| 63 | } | ||
| 64 | |||
| 65 | =head2 resolution | ||
| 66 | |||
| 67 | Returns the resolution of the index. | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | sub resolution { | ||
| 72 | my $self = shift; | ||
| 73 | return $self->ffi->h3GetResolution($self->index); | ||
| 74 | } | ||
| 75 | |||
| 76 | =head2 baseCell | ||
| 77 | |||
| 78 | Returns the base cell number of the index. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub baseCell { | ||
| 83 | my $self = shift; | ||
| 84 | return $self->ffi->h3GetBaseCell($self->index); | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 isValid | ||
| 88 | |||
| 89 | Returns non-zero if this is a valid H3 index. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub isValid { | ||
| 94 | my $self = shift; | ||
| 95 | return $self->ffi->h3IsValid($self->index); | ||
| 96 | } | ||
| 97 | |||
| 98 | =head2 isResClassIII | ||
| 99 | |||
| 100 | Returns non-zero if this index has a resolution with Class III orientation. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | sub isResClassIII { | ||
| 105 | my $self = shift; | ||
| 106 | return $self->ffi->h3IsResClassIII($self->index); | ||
| 107 | } | ||
| 108 | |||
| 109 | =head2 isPentagon | ||
| 110 | |||
| 111 | Returns non-zero if this index represents a pentagonal cell. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | sub isPentagon { | ||
| 116 | my $self = shift; | ||
| 117 | return $self->ffi->h3IsPentagon($self->index); | ||
| 118 | } | ||
| 119 | |||
| 120 | =head2 maxFaceCount | ||
| 121 | |||
| 122 | Returns the maximum number of icosahedron faces the given H3 index may intersect. | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub maxFaceCount { | ||
| 127 | my $self = shift; | ||
| 128 | return $self->ffi->maxFaceCount($self->index); | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 area | ||
| 132 | |||
| 133 | Returns the area in square meters of this index. | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | sub area { | ||
| 138 | my $self = shift; | ||
| 139 | return $self->ffi->cellAreaM2($self->index); | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 areaApprox | ||
| 143 | |||
| 144 | Returns the average area in square meters of indexes at this resolution. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub areaApprox { | ||
| 149 | my $self = shift; | ||
| 150 | return $self->ffi->hexAreaM2($self->resolution); | ||
| 151 | } | ||
| 152 | |||
| 153 | =head2 edgeLength | ||
| 154 | |||
| 155 | Returns the exact edge length in meters of this index. | ||
| 156 | |||
| 157 | =cut | ||
| 158 | |||
| 159 | sub edgeLength { | ||
| 160 | my $self = shift; | ||
| 161 | return $self->ffi->exactEdgeLengthM($self->index); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 edgeLengthApprox | ||
| 165 | |||
| 166 | Returns the average edge length in meters of indexes at this resolution. | ||
| 167 | |||
| 168 | =cut | ||
| 169 | |||
| 170 | sub edgeLengthApprox { | ||
| 171 | my $self = shift; | ||
| 172 | return $self->ffi->edgeLengthM($self->resolution); | ||
| 173 | } | ||
| 174 | |||
| 175 | =head1 METHODS | ||
| 176 | |||
| 177 | =head2 geo | ||
| 178 | |||
| 179 | Returns the centroid of the index as a L<Geo::H3::Geo> object. | ||
| 180 | |||
| 181 | =cut | ||
| 182 | |||
| 183 | sub geo { | ||
| 184 | my $self = shift; | ||
| 185 | my $geo = $self->ffi->h3ToGeoWrapper($self->index); | ||
| 186 | my $lat = $self->ffi->radsToDegs($geo->lat); | ||
| 187 | my $lon = $self->ffi->radsToDegs($geo->lon); | ||
| 188 | return Geo::H3::Geo->new(lat=>$lat, lon=>$lon, ffi=>$self->ffi); | ||
| 189 | } | ||
| 190 | |||
| 191 | =head2 geoBoundary | ||
| 192 | |||
| 193 | Returns the boundary of the index as a L<Geo::H3::GeoBoundary> object | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub geoBoundary { | ||
| 198 | my $self = shift; | ||
| 199 | return Geo::H3::GeoBoundary->new(gb=>$self->ffi->h3ToGeoBoundaryWrapper($self->index), ffi=>$self->ffi); | ||
| 200 | } | ||
| 201 | |||
| 202 | =head2 parent | ||
| 203 | |||
| 204 | Returns a parent index of this index as a L<Geo::H3::Index> object. | ||
| 205 | |||
| 206 | my $parent = $h3->parent; #next larger resolution | ||
| 207 | my $parent = $h3->parent(1); #isa Geo::H3::Index | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub parent { | ||
| 212 | my $self = shift; | ||
| 213 | my $resolution = shift || $self->resolution - 1; | ||
| 214 | return Geo::H3::Index->new(index=>$self->ffi->h3ToParent($self->index, $resolution)); | ||
| 215 | } | ||
| 216 | |||
| 217 | =head2 children | ||
| 218 | |||
| 219 | Returns the children of the index as an array reference of L<Geo::H3::Index> objects. | ||
| 220 | |||
| 221 | my $children = $h3->children(12); #isa ARRAY | ||
| 222 | my $children = $h3->children; #next smaller resolution | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | sub children { | ||
| 227 | my $self = shift; | ||
| 228 | my $resolution = shift || $self->resolution + 1; | ||
| 229 | return $self->_bless_aref($self->ffi->h3ToChildrenWrapper($self->index, $resolution)); | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 centerChild | ||
| 233 | |||
| 234 | Returns the center child (finer) index contained by this index at given resolution. | ||
| 235 | |||
| 236 | my $centerChild = $index->centerChild; #isa Geo::H3::Index | ||
| 237 | my $centerChild = $index->centerChild(12); #isa Geo::H3::Index | ||
| 238 | |||
| 239 | =cut | ||
| 240 | |||
| 241 | sub centerChild { | ||
| 242 | my $self = shift; | ||
| 243 | my $resolution = shift || $self->resolution + 1; | ||
| 244 | return Geo::H3::Index->new(index=>$self->ffi->h3ToCenterChild($self->index, $resolution)); | ||
| 245 | } | ||
| 246 | |||
| 247 | =head2 kRing | ||
| 248 | |||
| 249 | Returns k-rings indexes within k distance of the origin index. | ||
| 250 | |||
| 251 | my $list $index->kRing($k); #isa ARRAY of L<Geo::H3::Index> objects | ||
| 252 | |||
| 253 | =cut | ||
| 254 | |||
| 255 | sub kRing { | ||
| 256 | my $self = shift; | ||
| 257 | my $k = shift || 1; | ||
| 258 | return $self->_bless_aref($self->ffi->kRingWrapper($self->index, $k)); | ||
| 259 | } | ||
| 260 | |||
| 261 | =head2 kRingDistances | ||
| 262 | |||
| 263 | Returns a hash reference where the keys are the H3 index and values are the k distance for the given index and k value. | ||
| 264 | |||
| 265 | my $hash = $index->kRingDistances($k); | ||
| 266 | |||
| 267 | =cut | ||
| 268 | |||
| 269 | sub kRingDistances { | ||
| 270 | my $self = shift; | ||
| 271 | my $k = shift || 1; | ||
| 272 | return $self->ffi->kRingDistancesWrapper($self->index, $k); | ||
| 273 | } | ||
| 274 | |||
| 275 | =head2 hexRange | ||
| 276 | |||
| 277 | my $indexes = $index->hexRange($k); | ||
| 278 | |||
| 279 | =cut | ||
| 280 | |||
| 281 | sub hexRange { | ||
| 282 | my $self = shift; | ||
| 283 | my $k = shift || 1; | ||
| 284 | return $self->_bless_aref($self->ffi->hexRangeWrapper($self->index, $k)); | ||
| 285 | } | ||
| 286 | |||
| 287 | =head2 hexRangeDistances | ||
| 288 | |||
| 289 | Returns a hash reference where the keys are the H3 index and values are the k distance for the given index and k value. | ||
| 290 | |||
| 291 | my $hash = $index->hexRangeDistances($k); | ||
| 292 | |||
| 293 | =cut | ||
| 294 | |||
| 295 | sub hexRangeDistances { | ||
| 296 | my $self = shift; | ||
| 297 | my $k = shift || 1; | ||
| 298 | return $self->ffi->hexRangeDistancesWrapper($self->index, $k); | ||
| 299 | } | ||
| 300 | |||
| 301 | =head2 hexRing | ||
| 302 | |||
| 303 | Returns the hex ring of this index as an array reference of L<Geo::H3::Index> objects | ||
| 304 | |||
| 305 | my $hexes = $h3->hexRing; #default k = 1 | ||
| 306 | my $hexes = $h3->hexRing(5); #isa ARRAY | ||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | sub hexRing { | ||
| 311 | my $self = shift; | ||
| 312 | my $k = shift || 1; | ||
| 313 | return $self->_bless_aref($self->ffi->hexRingWrapper($self->index, $k)); | ||
| 314 | } | ||
| 315 | |||
| 316 | =head2 areNeighbors | ||
| 317 | |||
| 318 | Returns whether or not the provided H3Indexes are neighbors. | ||
| 319 | |||
| 320 | my $areNeighbors = $start_index->areNeighbors($end_index); | ||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | sub areNeighbors { | ||
| 325 | my $self = shift; | ||
| 326 | my $end = shift; | ||
| 327 | return $self->_bless_aref($self->ffi->h3IndexesAreNeighbors($self->index, $end->index)); | ||
| 328 | } | ||
| 329 | |||
| 330 | =head2 line | ||
| 331 | |||
| 332 | Returns the indexes starting at this index to the given end index as array reference of L<Geo::H3::Index> objects. | ||
| 333 | |||
| 334 | my $list_aref = $start_index->line($end_index); | ||
| 335 | |||
| 336 | =cut | ||
| 337 | |||
| 338 | sub line { | ||
| 339 | my $self = shift; | ||
| 340 | my $end = shift; | ||
| 341 | return $self->_bless_aref($self->ffi->h3LineWrapper($self->index, $end->index)); | ||
| 342 | } | ||
| 343 | |||
| 344 | =head2 distance | ||
| 345 | |||
| 346 | Returns the distance in grid cells between this index to the given end index. | ||
| 347 | |||
| 348 | my $distance = $start_index->distance($end_index); | ||
| 349 | |||
| 350 | =cut | ||
| 351 | |||
| 352 | sub distance { | ||
| 353 | my $self = shift; | ||
| 354 | my $end = shift; | ||
| 355 | return $self->h3Distance($self->index, $end->index); | ||
| 356 | } | ||
| 357 | |||
| 358 | =head2 struct | ||
| 359 | |||
| 360 | Returns the H3 index as an L<FFI::C> struct in the L<Geo::H3::FFI::Struct::Index> namespace | ||
| 361 | |||
| 362 | =cut | ||
| 363 | |||
| 364 | sub struct { | ||
| 365 | my $self = shift; | ||
| 366 | return Geo::H3::FFI::Struct::Index->new({index=>$self->index}); | ||
| 367 | } | ||
| 368 | |||
| 369 | =head1 SEE ALSO | ||
| 370 | |||
| 371 | L<Geo::H3>, L<Geo::H3::FFI> | ||
| 372 | |||
| 373 | =head1 AUTHOR | ||
| 374 | |||
| 375 | Michael R. Davis | ||
| 376 | |||
| 377 | =head1 COPYRIGHT AND LICENSE | ||
| 378 | |||
| 379 | MIT License | ||
| 380 | |||
| 381 | Copyright (c) 2021 Michael R. Davis | ||
| 382 | |||
| 383 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 384 | of this software and associated documentation files (the "Software"), to deal | ||
| 385 | in the Software without restriction, including without limitation the rights | ||
| 386 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 387 | copies of the Software, and to permit persons to whom the Software is | ||
| 388 | furnished to do so, subject to the following conditions: | ||
| 389 | |||
| 390 | The above copyright notice and this permission notice shall be included in all | ||
| 391 | copies or substantial portions of the Software. | ||
| 392 | |||
| 393 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 394 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 395 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 396 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 397 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 398 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 399 | SOFTWARE. | ||
| 400 | |||
| 401 | =cut | ||
| 402 | |||
| 403 | 1; | ||