package Geo::H3::GeoBoundary;
use strict;
use warnings;
use base qw{Geo::H3::Base}; #provides new and ffi

our $VERSION = '0.09';
our $PACKAGE = __PACKAGE__;

=head1 NAME

Geo::H3::GeoBoundary - H3 Geospatial Hexagon Indexing System GeoBoundary Object

=head1 SYNOPSIS

  use Geo::H3::GeoBoundary;
  my $GeoBoundary = Geo::H3::GeoBoundary->new(gb=>$gb);            #isa Geo::H3::GeoBoundary
  my $GeoBoundary = Geo::H3::GeoBoundary->new(gb=>$gb, ffi=>$ffi); #isa Geo::H3::GeoBoundary

=head1 DESCRIPTION

H3 Geospatial Hexagon Indexing System GeoBoundary Object provides coordinates method to extract data from the FFI GeoBoundary object

=head1 CONSTRUCTORS

=head2 new

  my $GeoBoundary = Geo::H3::GeoBoundary->new(gb=>$gb);

=head1 PROPERTIES

=head2 gb

Returns the H3 GeoBoundary Object from the API as a L<Geo::H3::FFI::Struct::GeoBoundary> object

  my $struct = $GeoBoundary->gb; #isa Geo::H3::FFI::Struct::GeoBoundary

=cut

sub gb {shift->{'gb'}};

=head1 METHODS

=head2 coordinates

Returns an OGC compatible closed polygon as an array reference of hashes i.e. [{lat=>$lat, lon=>$lon}, ...].

This coordinates format plugs directly into the format required for many L<Geo::GoogleEarth::Pluggable> objects.

=cut

sub coordinates {
  my $self        = shift;
  my @coordinates = ();
  my $gb          = $self->gb or die;
  my $max         = $gb->num_verts - 1;
  foreach my $j (0 .. $max, 0) {                      # Open Geospatial Consortium (OGC) Simple Feature Access standard
    my $vert = $gb->verts->[$j];                      # for a closed ring requires the first and last coordinate points
    my $lat  = $self->ffi->radsToDegs($vert->lat);    # in the ring sequence to be identical to create a topologically
    my $lon  = $self->ffi->radsToDegs($vert->lon);    # closed object.
    push @coordinates, {lat=>$lat, lon=>$lon};
  }
  return \@coordinates;
}

=head1 SEE ALSO

L<Geo::H3>, L<Geo::H3::FFI::Struct::GeoBoundary>, L<Geo::GoogleEarth::Pluggable>

=head1 AUTHOR

Michael R. Davis

=head1 COPYRIGHT AND LICENSE

MIT License

Copyright (c) 2021 Michael R. Davis

=cut

1;
