lib/Geo/Heatmap.pm

filter: Statementstmt 96.0% 96 / 100 Branchbran 73.3% 22 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 90.5% 125 / 138 CC 17 SCAR 22.2
CC
17
Cov
91%
calc_hm_tile
13.3
tile
5.0
BEGIN
1.0
CRAP 9.2
« lib/Geo/Heatmap/USNaviguide_Google_Tiles.pm ↑ Summary script/max_points_per_tile_zoom.pl »
1 package Geo::Heatmap;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Geo::Heatmap::USNaviguide_Google_Tiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 has 'debug' => (isa => 'Str', is => 'rw', default => 0);
8 2 has 'cache' => (isa => 'Object', is => 'rw');
9 2 has 'logfile' => (isa => 'Str', is => 'rw');
10 2 has 'return_points' => (isa => 'CodeRef', is => 'rw');
11 2 has 'zoom_scale' => (isa => 'HashRef', is => 'rw');
12 2 has 'palette' => (isa => 'Str', is => 'rw');
13 2 has 'scale' => (isa => 'Int', is => 'rw', default => 1);
14 2 has 'blur' => (isa => 'Int', is => 'rw', default => 4);
15 2 has 'bin' => (isa => 'Int', is => 'rw', default => 8); # should be a divisor of 256
16 2 has 'density' => (isa => 'HashRef', is => 'rw');
17  
18 2 our $VERSION = '0.18';
19  
20 2 __PACKAGE__->meta->make_immutable;
21  
22 1 ▶sub tile {
Subroutine tile: called CC=5 SCAR=16.1
Pod documented
23 1 my ($self, $tile, $debug) = @_;
24 1 $debug |= 0;
25  
26 1 my ($x, $y, $z) = split(/\s+/, $tile);
27  
28 1 my $e;
29 my %ubblob;
30 1 my $k = 0;
31 1 my $stitch;
32 my $wi;
33 1 my $line;
34 1 my $ts = 256;
35  
36 1 my $mca = sprintf("blur_%s_%s_%s", $x, $y, $z);
37 1 my $ubblob = $self->cache->get($mca);
38 1 ▶ return $ubblob if defined $ubblob;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ubblob 0 1 1
39  
40 1 my $image = Imager->new(xsize=>768, ysize=>768);
41 1 ▶ for (my $i = -1; $i <= 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= 1) 3 1 4
42 3 my $li = Imager->new();
43 3 ▶ for (my $j = -1; $j <= 1; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= 1) 9 3 12
44 9 $ubblob{$i}{$j} = $self->calc_hm_tile([$x+$j, $y+$i, $z]);
45 9 $li->read(data => $ubblob{$i}{$j} );
46 # printf "%s %s\n", ($i+1)*$ts, ($j+1)*$ts;
47 9 $image->paste(left=> ($j+1)*$ts, top=> ($i+1)*$ts, img=>$li);
48 }
49 }
50  
51 1 $image->filter(type=>"gaussian", stddev=>$self->blur);
52 1 my $cropped = $image->crop(left=>255, top=>255, width=>256, height=>256);
53
54 1 ▶ if ($debug > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 1) 0 1 1
55 0 print "Debugging stitch\n";
56 }
57  
58 1 $cropped->write(data => \$ubblob, type => 'png');
59 1 $self->cache->set($mca, $ubblob);
60 1 return $ubblob;
61 }
62  
63  
64 9 ▶sub calc_hm_tile {
Subroutine calc_hm_tile: called CC=13 SCAR=25.9
Pod undocumented
65 9 my ($self, $coord) = @_;
66  
67 9 my ($x, $y, $z) = @$coord;
68  
69 9 my $mca = sprintf("raw_%s_%s_%s", $x, $y, $z);
70 9 my $ubblob = $self->cache->get($mca);
71 9 ▶ return $ubblob if defined $ubblob;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ubblob 0 9 9
72 9 my $zoom_scale = $self->zoom_scale;
73  
74 9 my $value = &Google_Tile_Factors($z, 0) ;
75 9 my %r = Google_Tile_Calc($value, $y, $x);
76  
77 9 my $image = Imager->new(xsize=>256, ysize=>256);
78 9 $image->box(filled => 1, color => 'white');
79 9 my $rp = $self->return_points();
80 9 my $ps = &$rp(\%r);;
81 9 my $palette = Storable::retrieve($self->palette);
82 9 $palette->[-1] = [100, 100, 100];
83 9 my @density;
84 9 my $bin = $self->bin;
85 9 foreach my $p (@$ps) {
86 47668 my @d = Google_Coord_to_Pix($value, $p->[0], $p->[1]);
87 47668 my $ix = $d[1] - $r{PXW};
88 47668 my $iy = $d[0] - $r{PYN};
89 47668 $density[int($ix/$bin)][int($iy/$bin)] ++;
90 47668 ▶ printf "%s %s %s %s\n", $ix, $iy, $ix % $bin, $iy % $bin if $self->debug >5;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 5 0 47668 47668
91 }
92
93 9 my $maxval = ($bin)**2;
94 # $zoom_scale->{$z} |= 0;
95 9 ▶ my $defscale = $zoom_scale->{$z} > 0 ? $zoom_scale->{$z} : $maxval;
Branch: 1 branch
BranchTrueFalseTotal
$zoom_scale->{$z} > 0 ? : 9 0 9
96 9 $defscale *= 1.1 ;
97 9 my $scale = 500/log($defscale);
98 9 my $max = 256/$bin - 1;
99 9 my $dmax = 0;
100
101 9 ▶ for (my $i = 0; $i <= $max; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max) 288 9 297
102 288 ▶ for (my $j = 0; $j <= $max; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $max) 9216 288 9504
103 9216 my $xc = $i*$bin;
104 9216 my $yc = $j*$bin;
105 9216 my $xlc = $xc+$bin;
106 9216 my $ylc = $yc+$bin;
107 9216 ▶ my $d = $density[$i][$j] ? $density[$i][$j] : 1;
Branch: 1 branch
BranchTrueFalseTotal
$density[$i][$j] ? : 4431 4785 9216
108 9216 ▶ $dmax = $d > $dmax ? $d : $dmax;
Branch: 1 branch
BranchTrueFalseTotal
$d > $dmax ? : 74 9142 9216
109 9216 my $color_index = int(500-log($d)*$scale);
110 9216 ▶ $color_index = 5 if $color_index < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $color_index < 1 0 9216 9216
111 9216 ▶ $color_index = -1 if $d < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $d < 3 6624 2592 9216
112 9216 my $color = $palette->[$color_index];
113 # from percent to RGB - gna
114 9216 my $rgb = Imager::Color->new( $color->[0] * 2.55, $color->[1]*2.55, $color->[2]*2.55 );
115 9216 $image->box(color=> $rgb, xmin=>$xc, ymin=>$yc,
116 xmax=>$xlc, ymax=>$ylc, filled=>1);
117  
118 # $image->Draw(fill=>"rgb($rgb)" , primitive=>'rectangle', points=>"$xc,$yc $xlc,$ylc");
119 9216 ▶ printf "[%s][%s] %s %s\n", $i, $j, $d, $color_index if $self->debug > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 0 0 9216 9216
120 }
121 }
122
123 9 ▶ if ($self->logfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->logfile) 0 9 9
124 0 open (LOG, ">>". $self->logfile);
125 0 printf LOG "densitylog: x y z pointcount: %s %s %s %s\n", $x, $y, $z, $dmax;
126 0 close LOG;
127 }
128
129 9 $image->write(data => \$ubblob, type => 'png');
130 9 $self->cache->set($mca, $ubblob);
131 9 return $ubblob;
132 }
133  
134 2 1;
135  
136  
137 __END__
« lib/Geo/Heatmap/USNaviguide_Google_Tiles.pm ↑ Summary script/max_points_per_tile_zoom.pl »