| 1 | package Geo::H3; | ||
| 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 | use Geo::H3::Index; | ||
| 6 | use Geo::H3::Geo; | ||
| 7 | |||
| 8 | our $VERSION = '0.04'; | ||
| 9 | our $PACKAGE = __PACKAGE__; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Geo::H3 - H3 Geospatial Hexagon Indexing System | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use Geo::H3; | ||
| 18 | my $gh3 = Geo::H3->new; | ||
| 19 | |||
| 20 | my $h3 = $gh3->h3(index => $int); #isa Geo::H3::Index | ||
| 21 | my $h3 = $gh3->h3(string => $string); #isa Geo::H3::Index | ||
| 22 | |||
| 23 | my $geo = $gh3->geo(lat=>$lat, lon=>$lon); #isa Geo::H3::Geo | ||
| 24 | my $h3 = $geo->h3($resolution); #isa Geo::H3::Index | ||
| 25 | |||
| 26 | my $center = $h3->center; #isa Geo::H3::GeoCoord | ||
| 27 | my $lat = $center->lat; #isa Double WGS-84 Decimal Degrees | ||
| 28 | my $lon = $center->lon; #isa Double WGS-84 Decimal Degrees | ||
| 29 | my $distance = $center->pointDistM($geo); #isa Double meters | ||
| 30 | |||
| 31 | =head1 DESCRIPTION | ||
| 32 | |||
| 33 | This Perl distribution provides a Perl Object Oriented interface to the H3 Core Library. It accesses the H3 C library using L<libffi|https://github.com/libffi/libffi> and L<FFI::Platypus>. | ||
| 34 | |||
| 35 | H3 is a geospatial indexing system that partitions the world into hexagonal cells. | ||
| 36 | |||
| 37 | The H3 Core Library implements the H3 grid system. It includes functions for converting from latitude and longitude coordinates to the containing H3 cell, finding the center of H3 cells, finding the boundary geometry of H3 cells, finding neighbors of H3 cells, and more. | ||
| 38 | |||
| 39 | The H3 Core Library can be installed from Uber's H3 repository on GitHub L<https://github.com/uber/h3> which is well documented at L<https://h3geo.org/docs/>. | ||
| 40 | |||
| 41 | =head2 CONVENTIONS | ||
| 42 | |||
| 43 | The Geo::H3 lib is an Object Oriented wrapper on top of the L<Geo::H3::FFI> library. Geo::H3 was written as a wrapper so that in the future we are able to re-write against different backends such as the yet to be developed Geo::H3::XS backend. | ||
| 44 | |||
| 45 | =head3 libh3 | ||
| 46 | |||
| 47 | - Latitude and longitue cordinates are in radians WGS-84 | ||
| 48 | - H3 Index values are handled as uint64 integers | ||
| 49 | - GeoCoord values are handled as C structures with lat and lon | ||
| 50 | - GeoBoundary values are handled as C structures with num_verts and verts | ||
| 51 | |||
| 52 | =head3 Geo::H3::FFI | ||
| 53 | |||
| 54 | - Latitude and Longitue cordinates are in radians WGS-84 | ||
| 55 | - H3 Index values are handled as uint64 integers | ||
| 56 | - GeoCoord values are handled as Geo::H3::FFI::Struct::GeoCoord objects | ||
| 57 | - GeoBoundary values are handled as Geo::H3::FFI::Struct::GeoBoundary objects | ||
| 58 | |||
| 59 | =head3 Geo::H3 | ||
| 60 | |||
| 61 | - Latitude and longitue cordinates are in decimal degrees WGS-84 | ||
| 62 | - H3 Index values are handled as Geo::H3::Index objects | ||
| 63 | - GeoCoord values are handled as Geo::H3::GeoCoord objects | ||
| 64 | - GeoBoundary values are handled as Geo::H3::GeoBoundary objects | ||
| 65 | |||
| 66 | =head1 CONSTRUCTORS | ||
| 67 | |||
| 68 | =head2 h3 | ||
| 69 | |||
| 70 | Returns a L<Geo::H3::Index> object | ||
| 71 | |||
| 72 | my $h3 = $gh3->h3(index => $int); #isa Geo::H3::Index | ||
| 73 | my $h3 = $gh3->h3(string => $string); #isa Geo::H3::Index | ||
| 74 | my $h3 = Geo::H3::Index->new(index => $index); #isa Geo::H3::Index | ||
| 75 | |||
| 76 | =cut | ||
| 77 | |||
| 78 | sub h3 { | ||
| 79 | my $self = shift; | ||
| 80 | my %data = @_; | ||
| 81 | my $index = $data{'index'}; | ||
| 82 | my $string = $data{'string'}; | ||
| 83 | if ($index) { | ||
| 84 | return Geo::H3::Index->new(index => $index, ffi => $self->ffi); | ||
| 85 | } elsif ($string) { | ||
| 86 | my $index = $self->ffi->stringToH3Wrapper($string) or die("Error: package $PACKAGE constructor h3 string invalid"); | ||
| 87 | return Geo::H3::Index->new(index => $index, ffi => $self->ffi); | ||
| 88 | } else { | ||
| 89 | die("Error: package $PACKAGE constructor h3 requires either index or string"); | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 geo | ||
| 94 | |||
| 95 | Returns a L<Geo::H3::Geo> object | ||
| 96 | |||
| 97 | my $geo = $gh3->geo(lat=>$lat_deg, lon=>$lon_deg); #isa Geo::H3::Geo | ||
| 98 | my $geo = Geo::H3::Geo->new(lat=>$lat_deg, lon=>$lon_deg); #isa Geo::H3::Geo | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub geo { | ||
| 103 | my $self = shift; | ||
| 104 | my %data = @_; | ||
| 105 | $data{'ffi'} = $self->ffi unless exists $data{'ffi'}; | ||
| 106 | return Geo::H3::Geo->new(%data); | ||
| 107 | }; | ||
| 108 | |||
| 109 | =head1 SEE ALSO | ||
| 110 | |||
| 111 | L<https://h3geo.org/>, L<https://github.com/uber/h3/>, L<Geo::H3::FFI> | ||
| 112 | |||
| 113 | =head1 INSTALLATION | ||
| 114 | |||
| 115 | L<Geo::H3> has some pretty deep requirements that are not available in many OS repositories. For RedHat and CentOS 7 users, I have have built RPMs and placed them on my L<Linux Yum Repository|http://linux.davisnetworks.com/el7/> | ||
| 116 | |||
| 117 | To install the distribution with all dependencies - CentOS 7 | ||
| 118 | |||
| 119 | $ sudo yum install http://linux.davisnetworks.com/el7/updates/mrdvt92-release-8-2.el7.mrdvt92.noarch.rpm | ||
| 120 | $ sudo yum install 'perl(Geo::H3)' | ||
| 121 | |||
| 122 | To install the additional dependencies for the example script perl-Geo-H3-geo-to-googleearth.pl | ||
| 123 | |||
| 124 | $ sudo yum install 'perl(Geo::GoogleEarth::Pluggable)' 'perl(Geo::GoogleEarth::Pluggable::Plugin::Styles)' 'perl(Path::Class)' | ||
| 125 | |||
| 126 | =head1 AUTHOR | ||
| 127 | |||
| 128 | Michael R. Davis | ||
| 129 | |||
| 130 | =head1 COPYRIGHT AND LICENSE | ||
| 131 | |||
| 132 | MIT License | ||
| 133 | |||
| 134 | Copyright (c) 2021 Michael R. Davis | ||
| 135 | |||
| 136 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 137 | of this software and associated documentation files (the "Software"), to deal | ||
| 138 | in the Software without restriction, including without limitation the rights | ||
| 139 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 140 | copies of the Software, and to permit persons to whom the Software is | ||
| 141 | furnished to do so, subject to the following conditions: | ||
| 142 | |||
| 143 | The above copyright notice and this permission notice shall be included in all | ||
| 144 | copies or substantial portions of the Software. | ||
| 145 | |||
| 146 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 147 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 148 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 149 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 150 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 151 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 152 | SOFTWARE. | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | 1; | ||