Ham-WorldMap

 view release on metacpan or  search on metacpan

lib/Ham/WorldMap.pm  view on Meta::CPAN


=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Ham::WorldMap


You can also look for information at:

=over 4

=item * RT: CPAN's request tracker (report bugs here)

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Ham-WorldMap>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Ham-WorldMap>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Ham-WorldMap>

=item * Search CPAN

L<http://search.cpan.org/dist/Ham-WorldMap/>

=back


=head1 LICENSE AND COPYRIGHT

Copyright 2016 Matt Gumbley.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    L<http://www.apache.org/licenses/LICENSE-2.0>

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


=cut


# Hate that Perl doesn't define these....
use constant TRUE => 1;
use constant FALSE => 0;


sub new {
    my $class = shift;
    my %init = @_;

    my $mapPngFile = dist_file('Ham-WorldMap', 'grey-map.png');
    die "Cannot locate shared data file $mapPngFile" unless -f $mapPngFile;

    my $mapImage = Imager->new();
    $mapImage->read(file => $mapPngFile) or die "Could not read map $mapPngFile: " . $mapImage->errstr;
    $mapImage = $mapImage->convert(preset => 'addalpha');

    my $locator = Ham::Locator->new();

    my $grey = Imager::Color->new(64, 64, 64);

    my $fontFile = $init{'fontFile'} || "/Library/Fonts/Microsoft/Lucida Console.ttf";

    my $font = Imager::Font->new(file => $fontFile);

    my $obj = {
        'height' => $mapImage->getheight(),
        'width' => $mapImage->getwidth(),
        'image' => $mapImage,
        'gridx' => $mapImage->getwidth() / 18,
        'gridy' => $mapImage->getheight() / 18,
        'locator' => $locator,
        'grey' => $grey,
        'font' => $font,
    };

    bless $obj, $class;
    return $obj;
}

sub dotAtLocator {
    my ($self, $gridLocation, $radius, $colour) = @_;

    my ($x, $y) = $self->locatorToXY($gridLocation);

    my ($r, $g, $b, $a) = $colour->rgba();
    my $grey = Imager::Color->new(192, 192, 192, $a);
    $self->{image}->circle(color => $grey, r => $radius, x => $x, y => $y, aa => 1);

    $self->{image}->circle(color => $colour, r => $radius - 1, x => $x, y => $y, aa => 1);
}

sub locatorToXY {
    my ($self, $gridLocation) = @_;

    $self->{locator}->set_loc($gridLocation);
    my ($latitude, $longitude) = $self->{locator}->loc2latlng;

    my $x = $longitude; # -180 .. 180
    $x += 180; # 0 .. 360
    $x *= ($self->{width} / 360); # 0 .. width

    my $y = - $latitude; # -90 .. 90
    $y += 90; # 0 .. 180
    $y *= ($self->{height} / 180); # 0 .. height

    return ($x, $y);
}

sub drawLocatorGrid {
    my $self = shift;
    my $map = $self->{image};
    my $grey = $self->{grey};
    my $xinc = $self->{gridx};
    my $yinc = $self->{gridy};



( run in 1.763 second using v1.01-cache-2.11-cpan-39bf76dae61 )