| 1 | package Geo::H3::Geo; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw{Geo::H3::Base}; #provides new, ffi, and Geo::H3::FFI::Struct::GeoCoord | ||
| 5 | require Geo::H3::Index; | ||
| 6 | |||
| 7 | our $VERSION = '0.10'; | ||
| 8 | our $PACKAGE = __PACKAGE__; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Geo::H3::Geo - H3 Geospatial Hexagon Indexing System Geo Object | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | use Geo::H3::Geo; | ||
| 17 | my $geo = Geo::H3::Geo->new(lat=>$lat, lon=>$lon); #isa Geo::H3::Geo | ||
| 18 | my $h3 = $geo->h3($resolution); #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 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | H3 Geospatial Hexagon Indexing System Geo Object exposes the lat and lon properties as WGS-84 Decimal Degrees and converts coordinates to radians in the struct method for passing into theL<Geo::H3::FFI> API as a L<Geo::H3::FFI::Struct::GeoCoord> object. | ||
| 26 | |||
| 27 | The methods h3 and distance are wrappers around select L<Geo::H3::FFI> methods. | ||
| 28 | |||
| 29 | =head1 CONSTRUCTORS | ||
| 30 | |||
| 31 | =head2 new | ||
| 32 | |||
| 33 | my $geo = Geo::H3::Geo->new(lat => $lat, lon => $lon); | ||
| 34 | |||
| 35 | =head1 PROPERTIES | ||
| 36 | |||
| 37 | =head2 lat | ||
| 38 | |||
| 39 | Returns the latitude in decimal degrees WGS-84 | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub lat {shift->{'lat'}}; | ||
| 44 | |||
| 45 | =head2 lon | ||
| 46 | |||
| 47 | Returns the longitude in decimal degrees WGS-84 | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub lon {shift->{'lon'}}; | ||
| 52 | |||
| 53 | =head1 METHODS | ||
| 54 | |||
| 55 | =head2 struct | ||
| 56 | |||
| 57 | Returns the Geo object as an L<FFI::C> struct in the L<Geo::H3::FFI::Struct::GeoCoord> namespace for use in the L<Geo::H3::FFI> API. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub struct { | ||
| 62 | my $self = shift; | ||
| 63 | my $lat = $self->ffi->degsToRads($self->lat); | ||
| 64 | my $lon = $self->ffi->degsToRads($self->lon); | ||
| 65 | return Geo::H3::FFI::Struct::GeoCoord->new({lat=>$lat, lon=>$lon}); | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 h3 | ||
| 69 | |||
| 70 | Indexes the location at the specified resolution, returning the hex object L<Geo::H3::Index> of the cell containing the location. | ||
| 71 | |||
| 72 | my $h3 = $geo->h3; #default resolution is 0 | ||
| 73 | my $h3 = $geo->h3(7); #isa Geo::H3::H3Index | ||
| 74 | |||
| 75 | Returns undef on error. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub h3 { | ||
| 80 | my $self = shift; | ||
| 81 | my $res = shift || 0; | ||
| 82 | my $uint64 = $self->ffi->geoToH3Wrapper(lat=>$self->lat, lon=>$self->lon, resolution=>$res, uom=>"deg") | ||
| 83 | or die("Error: API geoToH3Wrapper returned an invalid value"); | ||
| 84 | return Geo::H3::Index->new(uint64 => $uint64, ffi=>$self->ffi); | ||
| 85 | } | ||
| 86 | |||
| 87 | =head2 distance | ||
| 88 | |||
| 89 | Returns in meters the "great circle" or "haversine" distance between pairs of points (lat/lon pairs). | ||
| 90 | |||
| 91 | my $distance = $geoA->distance($geoB); #isa Double | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub distance { | ||
| 96 | my $a = shift; | ||
| 97 | my $b = shift or die("Error: package $PACKAGE method distance requires a destination point"); | ||
| 98 | my $distance = $a->ffi->pointDistM($a->struct, $b->struct); | ||
| 99 | return $distance; | ||
| 100 | } | ||
| 101 | |||
| 102 | =head1 SEE ALSO | ||
| 103 | |||
| 104 | L<Geo::H3>, L<Geo::H3::FFI>, L<Geo::H3::Index>, L<Geo::H3::FFI::Struct::GeoCoord> | ||
| 105 | |||
| 106 | =head1 AUTHOR | ||
| 107 | |||
| 108 | Michael R. Davis | ||
| 109 | |||
| 110 | =head1 COPYRIGHT AND LICENSE | ||
| 111 | |||
| 112 | MIT License | ||
| 113 | |||
| 114 | Copyright (c) 2021 Michael R. Davis | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | 1; |