GMap.pm

filter: Statementstmt 92.5% 25 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 94.4% 34 / 36 CC 1 SCAR 0.3
CC
1
Cov
93%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« GMap/Files.pm ↑ Summary GMap/Tutorial.pm »
1 package HTML::GMap;
2  
3 our $VERSION = '0.06';
4  
5 # $Id: GMap.pm,v 1.24 2007/09/19 01:48:58 canaran Exp $
6  
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use HTML::GMap::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use CGI::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 0 ▶use GD::Graph::pie;
Subroutine BEGIN: called CC=1 SCAR=2.6
18 use GD::Icons;
19 use List::MoreUtils qw(any);
20 use List::Util qw(first);
21 use LWP::Simple;
22 use Template;
23 use Time::Format qw(%time);
24 use XML::Simple;
25  
26 ###############
27 # CONSTRUCTOR #
28 ###############
29  
30 sub new {
31 my ($class, %params) = @_;
32  
33 my $self = bless {}, $class;
34  
35 eval {
36 my $cgi = CGI->new();
37 $self->cgi($cgi);
38  
39 my $cgi_params = $self->{cgi}->Vars;
40 $self->cgi_params($cgi_params);
41  
42 my $page_title =
43 exists $params{page_title}
44 ? $params{page_title}
45 : "Geographical Display";
46 $self->page_title($page_title);
47  
48 exists $params{base_sql_table}
49 or croak("A base_sql_table param is required!");
50 $self->base_sql_table($params{base_sql_table});
51  
52 exists $params{base_sql_fields}
53 or croak("A base_sql_fields param is required!");
54 $self->base_sql_fields($params{base_sql_fields});
55  
56 exists $params{base_output_headers}
57 or croak("A base_output_headers param is required!");
58 $self->base_output_headers($params{base_output_headers});
59  
60 my $param_fields =
61 exists $params{param_fields} ? $params{param_fields} : {};
62 $self->param_fields($param_fields);
63  
64 exists $params{gmap_key} or croak("A gmap_key param is required!");
65 $self->gmap_key($params{gmap_key});
66  
67 exists $params{gmap_key} or croak("A gmap_key param is required!");
68 $self->gmap_key($params{gmap_key});
69  
70 exists $params{temp_dir} or croak("A temp_dir param is required!");
71 $self->temp_dir($params{temp_dir});
72  
73 exists $params{temp_dir_eq}
74 or croak("A temp_dir_eq param is required!");
75 $self->temp_dir_eq($params{temp_dir_eq});
76  
77 my $install_dir =
78 exists $params{install_dir}
79 ? $params{install_dir}
80 : $self->temp_dir;
81 $self->install_dir($install_dir);
82  
83 my $install_dir_eq =
84 exists $params{install_dir_eq}
85 ? $params{install_dir_eq}
86 : $self->temp_dir_eq;
87 $self->install_dir_eq($install_dir_eq);
88  
89 # Create HTML/js files
90 HTML::GMap::Files->new(temp_dir => $self->install_dir);
91  
92 my $session_id = $self->cgi->param('session_id');
93  
94 my $session_dir = $self->temp_dir . '/sessions';
95 my $session =
96 CGI::Session->new('file', $session_id, {Directory => $session_dir});
97 if ($session_id && $session_id ne $session->id) {
98 croak("Cannot create session!");
99 }
100 $self->session_id($session->id);
101 $self->session($session);
102  
103 $self->legend_field1($params{legend_field1});
104  
105 $self->legend_field2($params{legend_field2});
106  
107 my $max_hires_display =
108 exists $params{max_hires_display}
109 ? $params{max_hires_display}
110 : 100;
111 $self->max_hires_display($max_hires_display);
112  
113 my $center_latitude =
114 exists $params{center_latitude}
115 ? $params{center_latitude}
116 : 40.863233;
117 $self->center_latitude($center_latitude);
118  
119 my $center_longitude =
120 exists $params{center_longitude}
121 ? $params{center_longitude}
122 : -73.466566;
123 $self->center_longitude($center_longitude);
124  
125 my $center_zoom =
126 exists $params{center_zoom}
127 ? $params{center_zoom}
128 : 4;
129 $self->center_zoom($center_zoom);
130  
131 $self->messages($params{messages});
132  
133 $self->header($params{header});
134  
135 $self->footer($params{footer});
136  
137 $self->hires_shape_keys($params{hires_shape_keys});
138 $self->hires_shape_values($params{hires_shape_values});
139  
140 $self->hires_color_keys($params{hires_color_keys});
141 $self->hires_color_values($params{hires_color_values});
142  
143 my $image_height_pix =
144 exists $params{image_height_pix} ? $params{image_height_pix} : 600;
145 $self->image_height_pix($image_height_pix);
146  
147 my $image_width_pix =
148 exists $params{image_width_pix} ? $params{image_width_pix} : 600;
149 $self->image_width_pix($image_width_pix);
150  
151 my $tile_width_pix =
152 exists $params{tile_width_pix} ? $params{tile_width_pix} : 60;
153 $self->tile_width_pix($tile_width_pix);
154  
155 my $tile_height_pix =
156 exists $params{tile_height_pix} ? $params{tile_height_pix} : 60;
157 $self->tile_height_pix($tile_height_pix);
158  
159 my $cluster_field =
160 exists $params{cluster_field} ? $params{cluster_field} : '_default';
161 $self->cluster_field($cluster_field);
162  
163 my $gmap_main_css_file =
164 exists $params{gmap_main_css_file}
165 ? $params{gmap_main_css_file}
166 : 'gmap-main.css';
167 $self->gmap_main_css_file($gmap_main_css_file);
168  
169 my $gmap_main_html_file =
170 exists $params{gmap_main_html_file}
171 ? $params{gmap_main_html_file}
172 : 'gmap-main.html';
173 $self->gmap_main_html_file($gmap_main_html_file);
174  
175 my $gmap_main_js_file =
176 exists $params{gmap_main_js_file}
177 ? $params{gmap_main_js_file}
178 : 'gmap-main.js';
179 $self->gmap_main_js_file($gmap_main_js_file);
180  
181 my $prototype_js_file =
182 exists $params{prototype_js_file}
183 ? $params{prototype_js_file}
184 : 'prototype.js';
185 $self->prototype_js_file($prototype_js_file);
186  
187 # If db_access_params are provided, generate a db handle and store it
188 my $db_access_params = $params{db_access_params};
189 if ($db_access_params) {
190 # Re-format if a single db is enteredd
191 if ( ref($db_access_params)
192 and ref($db_access_params) eq 'ARRAY') {
193 my ($datasource, $username, $password) = @$db_access_params;
194  
195 $db_access_params = {
196 database => [{ alias => 'default',
197 datasource => $datasource,
198 username => $username,
199 password => $password,
200 }
201 ]
202 }
203 }
204  
205 $self->db_access_params($db_access_params);
206
207 my $database = $self->{cgi_params}->{database};
208  
209 my @available_databases =
210 ( ref($db_access_params->{database})
211 and ref($db_access_params->{database}) eq 'ARRAY')
212 ? @{$db_access_params->{database}}
213 : ($db_access_params->{database});
214  
215 unless (@available_databases) {
216 croak("No database specified!");
217 }
218  
219 if (!$database) {
220 $database = $available_databases[0]->{alias};
221 }
222  
223 my $selected_db =
224 first { $_->{alias} eq $database } @available_databases;
225  
226 if (!defined($selected_db)) {
227 croak("Cannot determine database ($database)!");
228 }
229  
230 my $dbh = DBI->connect(
231 $selected_db->{datasource},
232 $selected_db->{username}, $selected_db->{password},
233 {PrintError => 1, RaiseError => 1}
234 ) || croak("Cannot connect to database!");
235  
236 $self->dbh($dbh);
237 $self->db_selected($database);
238 $self->db_display($selected_db->{display});
239 }
240  
241 my $request_url_template;
242 if (exists $params{request_url_template}) {
243 if ($params{request_url_template} =~ /session_id=/) {
244 croak( "request_url_template cannot contain a session_id, "
245 . "session_id must be incorporated by this module!");
246 }
247  
248 $request_url_template = $params{request_url_template};
249  
250 my $session_id_param = 'session-id=' . $self->session_id;
251 $request_url_template =~
252 s/session_id=[^\;\&]+/session_id=$session_id/;
253 }
254 else {
255 my ($default_request_url_template) =
256 $self->cgi->self_url =~ /^([^\?]+)/;
257  
258 my @additional_params;
259  
260 if ($self->db_selected) {
261 push @additional_params, 'database=' . $self->db_selected;
262 }
263  
264 exists $params{initial_format}
265 or croak("A initial_format param is required if "
266 . "request_url_template is not specified!");
267 my $initial_format = $params{initial_format};
268  
269 if ( $initial_format ne 'xml-piechart'
270 && $initial_format ne 'xml-hires') {
271 croak( "initial_format parameter can only be "
272 . "xml-piechart or xml-hires!");
273 }
274  
275 push @additional_params, "format=$initial_format";
276  
277 $self->initial_format($initial_format);
278  
279 push @additional_params, 'session_id=' . $self->session_id;
280  
281 $default_request_url_template .=
282 '?' . join(';', @additional_params);
283  
284 $request_url_template = $default_request_url_template;
285 }
286 $self->request_url_template($request_url_template);
287 };
288  
289 $self->error($@) if $@;
290  
291 return $self;
292 }
293  
294 ##################
295 # PUBLIC METHODS #
296 ##################
297  
298 # Function :
299 # Arguments : none
300 # Returns : 1
301 # Notes : None specified.
302  
303 sub display {
304 my ($self) = @_;
305  
306 $self->_process_params;
307  
308 my $cgi = $self->cgi;
309 my $format = $cgi->param("format");
310  
311 if (!$format) {
312 $self->_display_js_page;
313 }
314  
315 elsif ($format eq "js") {
316 $self->_display_js_page;
317 }
318  
319 elsif ($format eq "xml-hires") {
320 $self->_serve_xml_data;
321 }
322  
323 elsif ($format eq "xml-piechart") {
324 $self->_serve_xml_data;
325 }
326  
327 else {
328 $self->error("Invalid format parameter ($format)!");
329 }
330  
331 return 1;
332 }
333  
334 # Function :
335 # Arguments : $message
336 # Returns : exits
337 # Notes : None specified.
338  
339 sub error {
340 my ($self, $message) = @_;
341 croak($message);
342 }
343  
344 ###########################################################
345 # HOOKS (Methods intended to be overridden by subclasses) #
346 ###########################################################
347  
348 # Function :
349 # Arguments : \@data
350 # Returns : 1
351 # Notes : None specified
352  
353 sub process_data_post_retrieve {
354 my ($self, $data_ref) = @_;
355  
356 return 1;
357 }
358  
359 # Function :
360 # Arguments : \%markers
361 # Returns : 1
362 # Notes : None specified
363  
364 sub process_markers_pre_filter {
365 my ($self, $markers_ref) = @_;
366  
367 return 1;
368 }
369  
370 # Function :
371 # Arguments : \%markers
372 # Returns : 1
373 # Notes : None specified
374  
375 sub process_markers_pre_cluster {
376 my ($self, $markers_ref) = @_;
377  
378 return 1;
379 }
380  
381 # Function :
382 # Arguments : \%markers
383 # Returns : 1
384 # Notes : None specified
385  
386 sub process_markers_post_cluster {
387 my ($self, $markers_ref) = @_;
388  
389 return 1;
390 }
391  
392 # Function :
393 # Arguments : $data_count, $max_data_count, $min_chart_size, $max_chart_size
394 # Returns : $piechart_icon_size
395 # Notes : None specified
396  
397 sub piechart_icon_size {
398 my ($self, $data_count, $max_data_count, $min_chart_size, $max_chart_size) = @_;
399
400 my $piechart_icon_size = $self->_round(
401 $min_chart_size + (
402 ($data_count / $max_data_count) *
403 ($max_chart_size - $min_chart_size)
404 )
405 );
406
407 return $piechart_icon_size;
408 }
409  
410 # Function :
411 # Arguments : \@info ([$icon_url, $label, $count], ...)
412 # Returns : $html
413 # Notes :
414  
415 sub generate_piechart_legend_html {
416 my ($self, $info_ref) = @_;
417  
418 my @sorted_info = sort {
419 if ( ($a->[1] eq 'Clustered' || $a->[1] eq 'Other')
420 && ($b->[1] eq 'Clustered' || $b->[1] eq 'Other')) {
421 $a cmp $b;
422 }
423 elsif (($a->[1] eq 'Clustered' || $a->[1] eq 'Other')
424 && ($b->[1] ne 'Clustered' && $b->[1] ne 'Other')) {
425 1;
426 }
427 elsif (($a->[1] ne 'Clustered' && $a->[1] ne 'Other')
428 && ($b->[1] eq 'Clustered' || $b->[1] eq 'Other')) {
429 -1;
430 }
431 else { $b->[2] <=> $a->[2] }
432 } @$info_ref;
433  
434 $info_ref = \@sorted_info;
435  
436 my $html;
437  
438 $html .= qq[<table>\n];
439  
440 $html .= qq[<tr>\n];
441 $html .= qq[<td colspan="2">
442 This section displays data points in current view and
443 is updated as the map is moved and/or filtering is applied.<br/>
444 </td>\n];
445 $html .= qq[</tr>\n];
446  
447 foreach my $info (@{$info_ref}) {
448 my ($icon_url, $label, $count) = @$info;
449 $html .= qq[<tr>\n];
450 $html .= qq[<td align="left">
451 <img src="$icon_url"/> $label ($count points)
452 </td>\n];
453 $html .= qq[</tr>\n];
454 }
455 $html .= qq[</table>\n];
456  
457 return $html;
458 }
459  
460 # Function :
461 # Arguments : \%markers
462 # Returns : $html
463 # Notes :
464  
465 sub generate_hires_legend_html {
466 my ($self, $rows_ref, $type) = @_;
467  
468 my $legend_field1 = $self->legend_field1;
469 my $legend_field2 = $self->legend_field2;
470  
471 my $temp_dir_eq = $self->temp_dir_eq;
472 my $session_id = $self->session_id;
473  
474 my $multiples_icon_url =
475 "$temp_dir_eq/Multiple-icon-$session_id-0-0-0.png";
476  
477 my $legend_info;
478 my @legend_markers;
479  
480 if ($type eq 'hires') {
481 $legend_info = qq[(The coordinates with overlapping data points
482 are displayed as <img src="$multiples_icon_url">.)];
483  
484 my %legend_markers;
485 foreach my $key (keys %$rows_ref) {
486 foreach my $row_ref (@{$rows_ref->{$key}->{rows}}) {
487 my $icon_url = $row_ref->{icon_url};
488 my $legend_field1_value = $row_ref->{$legend_field1};
489 my $legend_field2_value = $row_ref->{$legend_field2};
490  
491 $legend_markers{$icon_url}{count}++;
492 $legend_markers{$icon_url}{text} = join(
493 '; ',
494 map { s/^(.{5}).+/$1 .../; $_; } $legend_field1_value,
495 $legend_field2_value
496 );
497 }
498 }
499  
500 foreach my $icon_url (
501 sort { $legend_markers{$b}{count} <=> $legend_markers{$a}{count} }
502 keys %legend_markers
503 ) {
504 my $text = $legend_markers{$icon_url}{text};
505  
506 push @legend_markers,
507 { icon_url => $icon_url,
508 icon_size => 11,
509 text => $text,
510 };
511 }
512 }
513  
514 else {
515 $legend_info = qq[];
516  
517 @legend_markers = @$rows_ref;
518 }
519  
520 my $html;
521  
522 $html .= qq[<table>\n];
523  
524 $html .= qq[<tr>\n];
525 $html .= qq[<td colspan="2">
526 $legend_info<br/>
527 </td>\n];
528 $html .= qq[</tr>\n];
529  
530 foreach my $legend_marker (@legend_markers) {
531 my $icon_url = $legend_marker->{icon_url};
532 my $icon_size = $legend_marker->{icon_size};
533 my $text = $legend_marker->{text};
534 $html .= qq[<tr>\n];
535 $html .= qq[<td align="left">
536 <img height="$icon_size" src="$icon_url"/> $text
537 </td>\n];
538 $html .= qq[</tr>\n];
539 }
540 $html .= qq[</table>\n];
541  
542 return $html;
543 }
544  
545 # Function :
546 # Arguments : $data_ref
547 # Returns : $html
548 # Notes :
549  
550 sub generate_piechart_details_html {
551 my ($self, $key_ref) = @_;
552  
553 my $data_ref = $key_ref->{cluster_set};
554  
555 my $session = $self->session;
556 my $color_table_ref = $session->param('color_table');
557 my $temp_dir_eq = $self->temp_dir_eq;
558  
559 my $total_count = $key_ref->{cluster_data_count};
560  
561 my $html;
562  
563 $html .= qq[<table>\n];
564 $html .= qq[<tr>\n];
565 $html .= qq[<th align="left" width="50%">
566 Total Count</th><th align="left">: $total_count
567 </th>\n];
568 $html .= qq[</tr>\n];
569 $html .= qq[</table>\n];
570  
571 $html .= qq[<table>\n];
572  
573 foreach my $label (
574 sort {
575 if ($b eq 'Clustered' || $b eq 'Other') { -1 }
576 else { $data_ref->{$b} <=> $data_ref->{$a} }
577 } keys %{$data_ref}
578 ) {
579 my $count = $data_ref->{$label};
580 my $color = $color_table_ref->{$label};
581 my $icon_url = "$temp_dir_eq/Legend-icon-$color.png";
582  
583 my $rounded_percent = $self->_round($count / $total_count * 100);
584  
585 $html .= qq[<tr>\n];
586 $html .= qq[<td align="left">
587 <img src="$icon_url"/> $label ($count points)
588 </td>\n];
589 $html .= qq[<td align="right"> $rounded_percent %</td>\n];
590 $html .= qq[</tr>\n];
591 }
592 $html .= qq[</table>\n];
593  
594 return $html;
595 }
596  
597 # Function :
598 # Arguments : $data_ref
599 # Returns : $html
600 # Notes :
601  
602 sub generate_hires_details_html {
603 my ($self, $key_ref) = @_;
604  
605 my $data_ref = $key_ref->{rows};
606  
607 my $legend_field1 = $self->legend_field1;
608 my $legend_field2 = $self->legend_field2;
609 my $session = $self->session;
610 my $temp_dir_eq = $self->temp_dir_eq;
611  
612 my %icon_urls;
613  
614 my $total_count = 0;
615  
616 foreach my $row_ref (@$data_ref) {
617 my $icon_url = $row_ref->{icon_url};
618 my $legend_field1_value = $row_ref->{$legend_field1};
619 my $legend_field2_value = $row_ref->{$legend_field2};
620  
621 $icon_urls{$icon_url}{count}++;
622 $icon_urls{$icon_url}{text} = join(
623 '; ', map { s/^(.{5}).+/$1 .../; $_; } $legend_field1_value,
624 $legend_field2_value
625 );
626  
627 $total_count++;
628 }
629  
630 my $html;
631  
632 $html .= qq[<table>\n];
633 $html .= qq[<tr>\n];
634 $html .= qq[<th align="left" width="50%">Total Count</th>
635 <th align="left">: $total_count</th>\n];
636 $html .= qq[</tr>\n];
637 $html .= qq[</table>\n];
638  
639 $html .= qq[<table>\n];
640  
641 foreach my $icon_url (
642 sort { $icon_urls{$b}{count} <=> $icon_urls{$a}{count} }
643 keys %icon_urls
644 ) {
645 my $count = $icon_urls{$icon_url}{count};
646 my $text = $icon_urls{$icon_url}{text};
647  
648 my $rounded_percent = $self->_round($count / $total_count * 100);
649  
650 $html .= qq[<tr>\n];
651 $html .= qq[<td align="left">
652 <img src="$icon_url"/> $text ($count points)
653 </td>\n];
654 $html .= qq[<td align="right"> $rounded_percent%</td>\n];
655 $html .= qq[</tr>\n];
656 }
657 $html .= qq[</table>\n];
658  
659 return $html;
660 }
661  
662 ###################
663 # GET/SET METHODS #
664 ###################
665  
666 sub base_output_headers {
667 my ($self, $value) = @_;
668 $self->{base_output_headers} = $value if @_ > 1;
669 return $self->{base_output_headers};
670 }
671  
672 sub base_sql_fields {
673 my ($self, $value) = @_;
674 $self->{base_sql_fields} = $value if @_ > 1;
675 return $self->{base_sql_fields};
676 }
677  
678 sub base_sql_table {
679 my ($self, $value) = @_;
680 $self->{base_sql_table} = $value if @_ > 1;
681 return $self->{base_sql_table};
682 }
683  
684 sub center_latitude {
685 my ($self, $value) = @_;
686 $self->{center_latitude} = $value if @_ > 1;
687 return $self->{center_latitude};
688 }
689  
690 sub center_longitude {
691 my ($self, $value) = @_;
692 $self->{center_longitude} = $value if @_ > 1;
693 return $self->{center_longitude};
694 }
695  
696 sub center_zoom {
697 my ($self, $value) = @_;
698 $self->{center_zoom} = $value if @_ > 1;
699 return $self->{center_zoom};
700 }
701  
702 sub cgi {
703 my ($self, $value) = @_;
704 $self->{cgi} = $value if @_ > 1;
705 return $self->{cgi};
706 }
707  
708 sub cgi_params {
709 my ($self, $value) = @_;
710 $self->{cgi_params} = $value if @_ > 1;
711 return $self->{cgi_params};
712 }
713  
714 sub cluster_field {
715 my ($self, $value) = @_;
716 $self->{cluster_field} = $value if @_ > 1;
717 return $self->{cluster_field};
718 }
719  
720 sub db_access_params {
721 my ($self, $value) = @_;
722 $self->{db_access_params} = $value if @_ > 1;
723 return $self->{db_access_params};
724 }
725  
726 sub db_display {
727 my ($self, $value) = @_;
728 $self->{db_display} = $value if @_ > 1;
729 return $self->{db_display};
730 }
731  
732 sub db_selected {
733 my ($self, $value) = @_;
734 $self->{db_selected} = $value if @_ > 1;
735 return $self->{db_selected};
736 }
737  
738 sub dbh {
739 my ($self, $value) = @_;
740 $self->{dbh} = $value if @_ > 1;
741 return $self->{dbh};
742 }
743  
744 sub fields {
745 my ($self, $value) = @_;
746 $self->{fields} = $value if @_ > 1;
747 return $self->{fields};
748 }
749  
750 sub footer {
751 my ($self, $value) = @_;
752 $self->{footer} = $value if @_ > 1;
753 return $self->{footer};
754 }
755  
756 sub gmap_key {
757 my ($self, $value) = @_;
758 $self->{gmap_key} = $value if @_ > 1;
759 return $self->{gmap_key};
760 }
761  
762 sub gmap_main_css_file {
763 my ($self, $value) = @_;
764 $self->{gmap_main_css_file} = $value if @_ > 1;
765 return $self->{gmap_main_css_file};
766 }
767  
768 sub gmap_main_html_file {
769 my ($self, $value) = @_;
770 $self->{gmap_main_html_file} = $value if @_ > 1;
771 return $self->{gmap_main_html_file};
772 }
773  
774 sub gmap_main_js_file {
775 my ($self, $value) = @_;
776 $self->{gmap_main_js_file} = $value if @_ > 1;
777 return $self->{gmap_main_js_file};
778 }
779  
780 sub hires_shape_keys {
781 my ($self, $value) = @_;
782 $self->{hires_shape_keys} = $value if @_ > 1;
783 return $self->{hires_shape_keys};
784 }
785  
786 sub hires_shape_values {
787 my ($self, $value) = @_;
788 $self->{hires_shape_values} = $value if @_ > 1;
789 return $self->{hires_shape_values};
790 }
791  
792 sub hires_color_keys {
793 my ($self, $value) = @_;
794 $self->{hires_color_keys} = $value if @_ > 1;
795 return $self->{hires_color_keys};
796 }
797  
798 sub hires_color_values {
799 my ($self, $value) = @_;
800 $self->{hires_color_values} = $value if @_ > 1;
801 return $self->{hires_color_values};
802 }
803  
804 sub header {
805 my ($self, $value) = @_;
806 $self->{header} = $value if @_ > 1;
807 return $self->{header};
808 }
809  
810 sub image_height_pix {
811 my ($self, $value) = @_;
812 $self->{image_height_pix} = $value if @_ > 1;
813 return $self->{image_height_pix};
814 }
815  
816 sub image_width_pix {
817 my ($self, $value) = @_;
818 $self->{image_width_pix} = $value if @_ > 1;
819 return $self->{image_width_pix};
820 }
821  
822 sub initial_format {
823 my ($self, $value) = @_;
824 $self->{initial_format} = $value if @_ > 1;
825 return $self->{initial_format};
826 }
827  
828 sub install_dir {
829 my ($self, $value) = @_;
830 $self->{install_dir} = $value if @_ > 1;
831 return $self->{install_dir};
832 }
833  
834 sub install_dir_eq {
835 my ($self, $value) = @_;
836 $self->{install_dir_eq} = $value if @_ > 1;
837 return $self->{install_dir_eq};
838 }
839  
840 sub legend_field1 {
841 my ($self, $value) = @_;
842 $self->{legend_field1} = $value if @_ > 1;
843 return $self->{legend_field1};
844 }
845  
846 sub legend_field2 {
847 my ($self, $value) = @_;
848 $self->{legend_field2} = $value if @_ > 1;
849 return $self->{legend_field2};
850 }
851  
852 sub max_hires_display {
853 my ($self, $value) = @_;
854 $self->{max_hires_display} = $value if @_ > 1;
855 return $self->{max_hires_display};
856 }
857  
858 sub messages {
859 my ($self, $value) = @_;
860 $self->{messages} = $value if @_ > 1;
861 return $self->{messages};
862 }
863  
864 sub page_title {
865 my ($self, $value) = @_;
866 $self->{page_title} = $value if @_ > 1;
867 return $self->{page_title};
868 }
869  
870 sub param_fields {
871 my ($self, $value) = @_;
872 $self->{param_fields} = $value if @_ > 1;
873 return $self->{param_fields};
874 }
875  
876 sub prototype_js_file {
877 my ($self, $value) = @_;
878 $self->{prototype_js_file} = $value if @_ > 1;
879 return $self->{prototype_js_file};
880 }
881  
882 sub request_url_template {
883 my ($self, $value) = @_;
884 $self->{request_url_template} = $value if @_ > 1;
885 return $self->{request_url_template};
886 }
887  
888 sub session {
889 my ($self, $value) = @_;
890 $self->{session} = $value if @_ > 1;
891 return $self->{session};
892 }
893  
894 sub session_id {
895 my ($self, $value) = @_;
896 $self->{session_id} = $value if @_ > 1;
897 return $self->{session_id};
898 }
899  
900 sub temp_dir {
901 my ($self, $value) = @_;
902 $self->{temp_dir} = $value if @_ > 1;
903 return $self->{temp_dir};
904 }
905  
906 sub temp_dir_eq {
907 my ($self, $value) = @_;
908 $self->{temp_dir_eq} = $value if @_ > 1;
909 return $self->{temp_dir_eq};
910 }
911  
912 sub tile_height_pix {
913 my ($self, $value) = @_;
914 $self->{tile_height_pix} = $value if @_ > 1;
915 return $self->{tile_height_pix};
916 }
917  
918 sub tile_width_pix {
919 my ($self, $value) = @_;
920 $self->{tile_width_pix} = $value if @_ > 1;
921 return $self->{tile_width_pix};
922 }
923  
924 ###########################
925 # PRIVATE/UTILITY METHODS #
926 ###########################
927  
928 # Function : Display Javascript page, use provided URL template.
929 # Arguments : None
930 # Returns : 1
931 # Notes : This is a private method.
932  
933 sub _display_js_page {
934 my ($self) = @_;
935  
936 my $initial_format = $self->initial_format;
937  
938 my @fields = @{$self->fields};
939 my @param_fields_with_values;
940  
941 foreach my $field (@fields) {
942 if ( $field->{param}
943 && exists $field->{values}
944 && @{$field->{values}} > 0) {
945 push @param_fields_with_values, $field;
946 }
947 }
948  
949 my $cgi_header = CGI::header();
950  
951 my $center_latitude =
952 defined $self->center_latitude
953 ? $self->center_latitude
954 : 40.863233;
955 my $center_longitude =
956 defined $self->center_longitude
957 ? $self->center_longitude
958 : -73.466566;
959 my $center_zoom =
960 defined $self->center_zoom
961 ? $self->center_zoom
962 : 4;
963 my $param_fields = join(
964 ", ",
965 map { qq["] . $_->{name} . qq["] } @param_fields_with_values
966 );
967  
968 my $gmap_main_css_file_eq =
969 $self->install_dir_eq . '/' . $self->gmap_main_css_file;
970  
971 my $gmap_main_js_file_eq =
972 $self->install_dir_eq . '/' . $self->gmap_main_js_file;
973  
974 my $prototype_js_file_eq =
975 $self->install_dir_eq . '/' . $self->prototype_js_file;
976  
977 my %vars = (
978  
979 # HTML variables
980 cgi_header => $cgi_header,
981 header => $self->_content($self->header),
982 footer => $self->_content($self->footer),
983 page_title => $self->page_title,
984 legend => undef,
985 param_fields_with_values => \@param_fields_with_values,
986 messages => $self->messages,
987 gmap_key => $self->gmap_key,
988 gmap_main_css_file_eq => $gmap_main_css_file_eq,
989 gmap_main_js_file_eq => $gmap_main_js_file_eq,
990 prototype_js_file_eq => $prototype_js_file_eq,
991 container_height_pix => $self->image_height_pix + 20,
992 container_width_pix => $self->image_width_pix + 450,
993 center_width_pix => $self->image_width_pix + 0,
994 display_cluster_slices => $initial_format eq 'xml-piechart' ? 1 : 0,
995  
996 # var_store variables
997 center_latitude => $center_latitude,
998 center_longitude => $center_longitude,
999 center_zoom => $center_zoom,
1000 image_height_pix => $self->image_height_pix,
1001 tile_height_pix => $self->tile_height_pix,
1002 image_width_pix => $self->image_width_pix,
1003 tile_width_pix => $self->tile_width_pix,
1004 param_fields => $param_fields,
1005 url_template => $self->request_url_template,
1006 cluster_field => $self->cluster_field,
1007 draw_grid => $self->initial_format eq 'xml-piechart' ? 1 : 0,
1008 );
1009  
1010 my $template = Template->new(INCLUDE_PATH => $self->install_dir);
1011  
1012 $template->process($self->gmap_main_html_file, \%vars)
1013 or $self->error("Template process failed: " . $template->error);
1014  
1015 return 1;
1016 }
1017  
1018 # Function : Display XML data.
1019 # Arguments : None
1020 # Returns : 1
1021 # Notes : This is a private method.
1022  
1023 sub _serve_xml_data {
1024 my ($self) = @_;
1025  
1026 $self->_clean_temp_dir;
1027  
1028 my $dbh = $self->dbh;
1029 my $cgi = $self->cgi;
1030 my $base_sql_table = $self->base_sql_table;
1031 my @base_sql_fields = @{$self->base_sql_fields};
1032 my @base_output_headers = @{$self->base_output_headers};
1033  
1034 my @fields = @{$self->fields};
1035  
1036 my $format = $cgi->param("format");
1037 if ($format ne 'xml-piechart' && $format ne 'xml-hires') {
1038 $self->error("Invalid format param($format)!");
1039 }
1040  
1041 my $cluster_field = $self->cluster_field;
1042  
1043 # Generate WHERE clauses (Two statements are needed,
1044 my @where_clauses;
1045  
1046 # - filter params
1047 foreach my $field (@fields) {
1048 my $name = $field->{name};
1049 my $display = $field->{display};
1050 my $values = $field->{values};
1051  
1052 # For pie charts, handling of this field is done by script
1053 next
1054 if ($name eq $cluster_field and $format eq 'xml-piechart');
1055  
1056 my $cgi_value = $cgi->param($name);
1057  
1058 if ($cgi_value and $cgi_value ne 'all') {
1059 push @where_clauses, qq[$name = ] . $dbh->quote($cgi_value);
1060 }
1061 }
1062  
1063 # - coordinates
1064 push @where_clauses,
1065 qq[latitude >= ] . $dbh->quote($cgi->param("latitude_south"));
1066 push @where_clauses,
1067 qq[latitude <= ] . $dbh->quote($cgi->param("latitude_north"));
1068  
1069 if ($cgi->param("longitude_west") <= $cgi->param("longitude_east")) {
1070 push @where_clauses,
1071 qq[longitude >= ] . $dbh->quote($cgi->param("longitude_west"));
1072 push @where_clauses,
1073 qq[longitude <= ] . $dbh->quote($cgi->param("longitude_east"));
1074 }
1075  
1076 else {
1077 push @where_clauses,
1078 qq[((longitude >= ]
1079 . $dbh->quote($cgi->param("longitude_west"))
1080 . qq[AND longitude <= 180)]
1081 . qq[ OR ]
1082 . qq[(longitude <= ]
1083 . $dbh->quote($cgi->param("longitude_east"))
1084 . qq[AND longitude >= -180))];
1085 }
1086  
1087 # Generate query SQL statement
1088 my $statement = "SELECT " . join(", ", @base_sql_fields);
1089 $statement .= " FROM " . $base_sql_table;
1090 $statement .= " WHERE " . join(" AND ", @where_clauses) if @where_clauses;
1091  
1092 # Retrieve data
1093 my $data_ref;
1094 my $sth = $dbh->prepare($statement);
1095 $sth->execute;
1096 while (my @row = $sth->fetchrow_array) { push @{$data_ref}, \@row; }
1097 $sth->finish;
1098  
1099 # Process data array (this is a hook intended to be used in subclasses)
1100 $self->process_data_post_retrieve($data_ref);
1101  
1102 # Remove any undef rows
1103 my $clean_data_ref;
1104 foreach (@{$data_ref}) {
1105 push @{$clean_data_ref}, $_ if $_;
1106 }
1107 $data_ref = $clean_data_ref;
1108  
1109 # Generate XML output
1110 my $xml_ref;
1111 if ($format eq "xml-hires") {
1112 $xml_ref = $self->_generate_hires_xml_data($data_ref);
1113 }
1114  
1115 elsif ($format eq "xml-piechart") {
1116 $xml_ref = $self->_generate_piechart_xml_data($data_ref);
1117 }
1118  
1119 else {
1120 $self->error("Invalid XML data format ($format)!");
1121 }
1122  
1123 # # Generate XML headers
1124 # my @xml_boh = map { my ($h) = $_ =~ /^([^:]+)/;
1125 # $h =~ s/[^a-zA-Z0-9]/_/g;
1126 # $h =~ s/^[^a-zA-Z]//g;
1127 # $h =~ s/^xml//gi;
1128 # lc($h);
1129 # } @base_output_headers;
1130  
1131 my $formatted_data = XMLout($xml_ref, keyattr => []);
1132  
1133 # Print XML data out
1134 print CGI::header(-type => 'text/plain');
1135 print $formatted_data;
1136  
1137 return 1;
1138 }
1139  
1140 # Function :
1141 # Arguments : $\@data
1142 # Returns : \%xml_ref
1143 # Notes : This is a private method.
1144  
1145 sub _generate_hires_xml_data {
1146 my ($self, $data_ref) = @_;
1147  
1148 my @base_sql_fields = @{$self->base_sql_fields};
1149  
1150 my $legend_field1 = $self->legend_field1;
1151 my $legend_field2 = $self->legend_field2;
1152 my $session = $self->session;
1153  
1154 my $temp_dir = $self->temp_dir;
1155 my $temp_dir_eq = $self->temp_dir_eq;
1156 my $session_id = $self->session_id;
1157  
1158 my $max_hires_display = $self->max_hires_display;
1159  
1160 my $markers_ref = {};
1161 my $max_data_count = 0;
1162  
1163 # Cluster data points by geo coords (how many distinct geo coords?)
1164 foreach my $data (@{$data_ref}) {
1165 my $row_ref;
1166  
1167 foreach my $i (0 .. $#base_sql_fields) {
1168 $row_ref->{$base_sql_fields[$i]} = $data->[$i];
1169 }
1170  
1171 my $latitude = $row_ref->{latitude};
1172 my $longitude = $row_ref->{longitude};
1173  
1174 my $key = join(':', $latitude, $longitude);
1175  
1176 push @{$markers_ref->{$key}->{rows}}, $row_ref;
1177 $markers_ref->{$key}->{cluster_data_count}++;
1178  
1179 if ( $markers_ref->{$key}->{cluster_data_count}
1180 and $markers_ref->{$key}->{cluster_data_count} > $max_data_count)
1181 {
1182 $max_data_count = $markers_ref->{$key}->{cluster_data_count};
1183 }
1184 }
1185  
1186 # Process marker hash to generate cumulative information
1187 my $xml_ref = {};
1188  
1189 # If there are more than max_hires_display markers, cluster data and display low res view
1190 # *** Override $markers_ref and $max_data_count ***
1191 if (scalar(keys %$markers_ref) > $max_hires_display) {
1192 ($markers_ref, $max_data_count) = $self->_cluster_data($data_ref);
1193  
1194 $self->_add_hires_icon_urls($markers_ref);
1195  
1196 my $lowres_legend_marker_count = 5;
1197  
1198 my $density_icon_prefix = "Density-icon-$session_id";
1199 my $icon = GD::Icons->new(
1200 shape_keys => [":default"],
1201 shape_values => ["_large_square"],
1202 color_keys => [":default"],
1203 color_values => ["#0009ff"],
1204 sval_keys => [0 .. $lowres_legend_marker_count - 1],
1205 icon_dir => $temp_dir,
1206 icon_prefix => $density_icon_prefix,
1207 );
1208 $icon->generate_icons;
1209  
1210 my @lowres_legend_markers;
1211 foreach my $i (0 .. $lowres_legend_marker_count - 1) {
1212 my $icon_url = "$temp_dir_eq/$density_icon_prefix-0-0-$i.png";
1213 my $text =
1214 int($i * $max_data_count / $lowres_legend_marker_count) + 1
1215 . ' to '
1216 . int(($i + 1) * $max_data_count / $lowres_legend_marker_count)
1217 . ' points';
1218 my $icon_size = 22;
1219 push @lowres_legend_markers,
1220 { icon_url => $icon_url,
1221 icon_size => $icon_size,
1222 text => $text,
1223 };
1224 }
1225  
1226 foreach my $key (keys %{$markers_ref}) {
1227 my ($latitude, $longitude) = split(':', $key);
1228  
1229 my $data_ref = $markers_ref->{$key}->{rows};
1230  
1231 my $data_count = scalar(@$data_ref);
1232  
1233 my $density_icon_index =
1234 int(($data_count / $max_data_count) *
1235 ($lowres_legend_marker_count - 1));
1236 my $icon_url =
1237 "$temp_dir_eq/$density_icon_prefix-0-0-$density_icon_index.png";
1238 my $icon_size = 22;
1239  
1240 my $details_on_click =
1241 $self->generate_hires_details_html($markers_ref->{$key});
1242  
1243 my $row_ref = {
1244 latitude => $latitude,
1245 longitude => $longitude,
1246 icon_url => $icon_url,
1247 icon_size => $icon_size,
1248 details_on_click => $details_on_click,
1249 messages_on_click => '',
1250 legend_on_click => '',
1251 };
1252  
1253 push(@{$xml_ref->{marker}}, $row_ref);
1254 }
1255  
1256 my $legend = $self->generate_hires_legend_html(
1257 \@lowres_legend_markers,
1258 'lowres'
1259 );
1260  
1261 my $meta_data_ref = {
1262 messages_by_default => $self->messages,
1263 details_by_default => '[Click an icon for details ...]',
1264 legend_by_default => $legend,
1265 };
1266 push(@{$xml_ref->{meta_data}}, $meta_data_ref);
1267 }
1268  
1269 # Else
1270 else {
1271 $self->_add_hires_icon_urls($markers_ref);
1272  
1273 # my $multiples_icon_prefix = "Multiple-icon-$session_id";
1274 # my $icon = GD::Icons->new(
1275 # shape_keys => [":default"],
1276 # shape_values => ["_letter-m"],
1277 # color_keys => [":default"],
1278 # color_values => ["Blue"],
1279 # sval_keys => [":default"],
1280 # sval_values => [":default"],
1281 # icon_dir => $temp_dir,
1282 # icon_prefix => $multiples_icon_prefix,
1283 # );
1284 # $icon->generate_icons;
1285 #
1286 # my $multiples_icon_url =
1287 # "$temp_dir_eq/" . $icon->icon(':default', ':default', ':default');
1288  
1289 foreach my $key (keys %{$markers_ref}) {
1290 my ($latitude, $longitude) = split(':', $key);
1291  
1292 my $data_ref = $markers_ref->{$key}->{rows};
1293  
1294 my $data_count = scalar(@$data_ref);
1295  
1296 my $icon_size = $data_count > 1 ? 14 : 11;
1297  
1298 my $multiples_icon_url;
1299
1300 if ($data_count > 1) {
1301 my $multiples_icon_prefix = "Multiple-icon-$data_count-$session_id";
1302 my $icon = GD::Icons->new(
1303 alpha => 30,
1304 shape_keys => ["Multiple:$data_count"],
1305 shape_values => ["_number-flag"],
1306 color_keys => [":default"],
1307 color_values => ["#c1caff"],
1308 sval_keys => [":default"],
1309 sval_values => [":default"],
1310 icon_dir => $temp_dir,
1311 icon_prefix => $multiples_icon_prefix,
1312 );
1313 $icon->generate_icons;
1314 $multiples_icon_url =
1315 "$temp_dir_eq/" . $icon->icon("Multiple:$data_count", ':default', ':default');
1316 }
1317
1318 my $icon_url =
1319 $data_count > 1
1320 ? $multiples_icon_url
1321 : $data_ref->[0]->{icon_url};
1322  
1323 my $details_on_click =
1324 $self->generate_hires_details_html($markers_ref->{$key});
1325  
1326 my $row_ref = {
1327 latitude => $latitude,
1328 longitude => $longitude,
1329 icon_url => $icon_url,
1330 icon_size => $icon_size,
1331 details_on_click => $details_on_click,
1332 messages_on_click => '',
1333 legend_on_click => '',
1334 };
1335  
1336 push(@{$xml_ref->{marker}}, $row_ref);
1337 }
1338  
1339 my $legend = $self->generate_hires_legend_html($markers_ref, 'hires');
1340  
1341 my $meta_data_ref = {
1342 messages_by_default => $self->messages,
1343 details_by_default => '[Click icons for details ...]',
1344 legend_by_default => $legend
1345 };
1346 push(@{$xml_ref->{meta_data}}, $meta_data_ref);
1347 }
1348  
1349 return $xml_ref;
1350 }
1351  
1352 # Function :
1353 # Arguments : \%markers_ref
1354 # Returns : 1
1355 # Notes : This is a private method.
1356  
1357 sub _add_hires_icon_urls {
1358 my ($self, $markers_ref) = @_;
1359  
1360 my $legend_field1 = $self->legend_field1;
1361 my $legend_field2 = $self->legend_field2;
1362 my $session = $self->session;
1363  
1364 my $hires_shape_keys = $self->hires_shape_keys;
1365 my $hires_shape_values = $self->hires_shape_values;
1366
1367 my $hires_color_keys = $self->hires_color_keys;
1368 my $hires_color_values = $self->hires_color_values;
1369  
1370 my $temp_dir = $self->temp_dir;
1371 my $temp_dir_eq = $self->temp_dir_eq;
1372 my $session_id = $self->session_id;
1373  
1374 # Create icon set and store in row_refs
1375 my %legend_field1_values;
1376 my %legend_field2_values;
1377 foreach my $key (keys %{$markers_ref}) {
1378 my $data_ref = $markers_ref->{$key}->{rows};
1379 foreach my $row_ref (@$data_ref) {
1380 $legend_field1_values{$row_ref->{$legend_field1}} = 1
1381 if exists $row_ref->{$legend_field1};
1382 $legend_field2_values{$row_ref->{$legend_field2}} = 1
1383 if exists $row_ref->{$legend_field2};
1384 }
1385 }
1386 my @legend_field1_values = sort keys %legend_field1_values;
1387 my @legend_field2_values = sort keys %legend_field2_values;
1388  
1389 my $small_icon_prefix = "Small-icon-$session_id";
1390 my $icon = GD::Icons->new(
1391 color_keys => $hires_color_keys ? $hires_color_keys : \@legend_field2_values,
1392 color_values => $hires_color_values,
1393 shape_keys => $hires_shape_keys ? $hires_shape_keys : \@legend_field1_values,
1394 shape_values => $hires_shape_values,
1395 sval_keys => [":default"],
1396 icon_dir => $temp_dir,
1397 icon_prefix => $small_icon_prefix,
1398 );
1399 $icon->generate_icons;
1400  
1401 foreach my $key (keys %{$markers_ref}) {
1402 my $data_ref = $markers_ref->{$key}->{rows};
1403 foreach my $row_ref (@$data_ref) {
1404 $row_ref->{icon_url} = "$temp_dir_eq/"
1405 . $icon->icon(
1406 $row_ref->{$legend_field1},
1407 $row_ref->{$legend_field2}, ':default' # GD::Icons uses first color, then shape
1408 );
1409 }
1410 }
1411  
1412 return 1;
1413 }
1414  
1415 # Function :
1416 # Arguments : $\@data
1417 # Returns : \%xml_ref
1418 # Notes : This is a private method.
1419  
1420 sub _generate_piechart_xml_data {
1421 my ($self, $data_ref) = @_;
1422  
1423 my $cgi = $self->cgi;
1424 my $cluster_field = $self->cluster_field;
1425 my $session = $self->session;
1426  
1427 # Whether filter by value is valid
1428 my $cluster_filter_value;
1429 if ($cgi->param($cluster_field) && $cgi->param($cluster_field) ne 'all') {
1430 $cluster_filter_value = $cgi->param($cluster_field);
1431 }
1432  
1433 # Cluster data points and cluster them in a hash (key being the lat-lng pair)
1434 my ($markers_ref, $max_data_count) = $self->_cluster_data($data_ref);
1435  
1436 # Process markers hash (this is a hook intended to be used in subclasses)
1437 $self->process_markers_pre_filter($markers_ref);
1438  
1439 # Apply single cluster field filter if applicable
1440 if ($cluster_filter_value) {
1441 foreach my $key (keys %{$markers_ref}) {
1442 my $data = $markers_ref->{$key}->{cluster_set};
1443 my $cluster_data_count =
1444 $markers_ref->{$key}->{cluster_data_count};
1445  
1446 my $blank_value = 0;
1447  
1448 foreach my $cluster_value (keys %$data) {
1449 if ($cluster_value eq $cluster_filter_value) {
1450 next;
1451 }
1452 else {
1453 $blank_value += $data->{$cluster_value};
1454 delete $data->{$cluster_value};
1455 }
1456 }
1457  
1458 $data->{Other} = $blank_value;
1459 }
1460 }
1461  
1462 # Process markers hash (this is a hook intended to be used in subclasses)
1463 $self->process_markers_pre_cluster($markers_ref);
1464  
1465 # Cluster small slices
1466 my $cluster_slices = $cgi->param('cluster_slices');
1467 my $cluster_slices_by = $cgi->param('cluster_slices_by');
1468 my $cluster_slices_value = $cgi->param('cluster_slices_value');
1469  
1470 if ( $cluster_slices
1471 && $cluster_slices ne 'off'
1472 && $cluster_slices ne 'false'
1473 && $cluster_slices_value > 0) {
1474 foreach my $key (keys %{$markers_ref}) {
1475 my $data = $markers_ref->{$key}->{cluster_set};
1476  
1477 my $other_value = 0;
1478 foreach my $cluster_value (keys %$data) {
1479 my $cluster_count = $data->{$cluster_value};
1480 my $cluster_percent =
1481 $data->{$cluster_value} /
1482 $markers_ref->{$key}->{cluster_data_count} * 100;
1483  
1484 if ($cluster_slices_by eq 'count') {
1485 if ($cluster_count < $cluster_slices_value) {
1486 $other_value += $cluster_count;
1487 delete $data->{$cluster_value};
1488 }
1489 }
1490  
1491 elsif ($cluster_slices_by eq 'percent') {
1492 if ($cluster_percent < $cluster_slices_value) {
1493 $other_value += $cluster_count;
1494 delete $data->{$cluster_value};
1495 }
1496 }
1497  
1498 else {
1499 $self->error(
1500 "Invalid cluster_slices_type value ($cluster_slices_by)!"
1501 );
1502 }
1503 }
1504 $data->{Clustered} = $other_value;
1505 }
1506 }
1507  
1508 # Process markers hash (this is a hook intended to be used in subclasses)
1509 $self->process_markers_post_cluster($markers_ref);
1510  
1511 # Generate list of all cluster values
1512 my %all_cluster_values;
1513 foreach my $key (keys %{$markers_ref}) {
1514 my $data = $markers_ref->{$key}->{cluster_set};
1515 foreach my $cluster_value (keys %$data) {
1516 $all_cluster_values{$cluster_value} += $data->{$cluster_value};
1517 }
1518 }
1519  
1520 # Generate/store color table
1521 my $color_table_ref = $session->param('color_table') || {};
1522 my $last_color_index = $session->param('last_color_index') || 0;
1523  
1524 my @colors = @{$self->_colors};
1525 my @all_cluster_values =
1526 sort { $all_cluster_values{$b} <=> $all_cluster_values{$a} }
1527 keys %all_cluster_values;
1528  
1529 my $color_index;
1530 foreach my $i (0 .. $#all_cluster_values) {
1531 my $cluster_value = $all_cluster_values[$i];
1532  
1533 next if $color_table_ref->{$cluster_value};
1534  
1535 $color_index = ($i + $last_color_index + 1) % @colors;
1536 $color_table_ref->{$cluster_value} = $colors[$color_index];
1537 }
1538 $color_table_ref->{Other} = 'white';
1539 $color_table_ref->{Clustered} = 'purple';
1540  
1541 $session->param('color_table', $color_table_ref);
1542 $session->param('last_color_index', $color_index);
1543  
1544 # Process marker hash to generate cumulative information
1545 my $xml_ref = {};
1546  
1547 foreach my $key (keys %{$markers_ref}) {
1548 my ($latitude, $longitude) = split(':', $key);
1549  
1550 my $data_ref = $markers_ref->{$key}->{cluster_set};
1551  
1552 my @piechart_labels;
1553 my @piechart_values;
1554 my @piechart_colors;
1555  
1556 foreach my $label (
1557 sort { $data_ref->{$b} <=> $data_ref->{$a} }
1558 keys %{$data_ref}
1559 ) { # sort by frequent to rare
1560 push @piechart_labels, $label;
1561 push @piechart_values, $data_ref->{$label};
1562 push @piechart_colors, $color_table_ref->{$label};
1563 }
1564  
1565 my ($icon_url, $icon_size) = $self->_make_piechart_icon(
1566 [\@piechart_labels, \@piechart_values],
1567 \@piechart_colors, $max_data_count
1568 );
1569  
1570 my $details_on_click =
1571 $self->generate_piechart_details_html($markers_ref->{$key});
1572  
1573 my $row_ref = {
1574 latitude => $latitude,
1575 longitude => $longitude,
1576 icon_url => $icon_url,
1577 icon_size => $icon_size,
1578 details_on_click => $details_on_click,
1579 messages_on_click => '',
1580 legend_on_click => '',
1581 };
1582  
1583 push(@{$xml_ref->{marker}}, $row_ref);
1584 }
1585  
1586 my $legend_info =
1587 $self->_generate_piechart_legend_info(\%all_cluster_values);
1588 my $legend = $self->generate_piechart_legend_html($legend_info);
1589  
1590 my $meta_data_ref = {
1591 messages_by_default => $self->messages,
1592 details_by_default => '[Click a pie chart for details ...]',
1593 legend_by_default => $legend
1594 };
1595 push(@{$xml_ref->{meta_data}}, $meta_data_ref);
1596  
1597 return $xml_ref;
1598 }
1599  
1600 # Function :
1601 # Arguments : \@data
1602 # Returns : (\%markers, $max_data_count)
1603 # Notes :
1604  
1605 sub _cluster_data {
1606 my ($self, $data_ref) = @_;
1607  
1608 my $cgi = $self->cgi;
1609 my @base_sql_fields = @{$self->base_sql_fields};
1610  
1611 my $cluster_field = $self->cluster_field;
1612  
1613 my $image_height_pix = $self->image_height_pix;
1614 my $tile_height_pix = $self->tile_height_pix;
1615  
1616 my $image_width_pix = $self->image_width_pix;
1617 my $tile_width_pix = $self->tile_width_pix;
1618  
1619 # Determine map geographical boundaries
1620 my $latitude_south = $cgi->param("latitude_south");
1621 my $latitude_north = $cgi->param("latitude_north");
1622 my $longitude_east = $cgi->param("longitude_east");
1623 my $longitude_west = $cgi->param("longitude_west");
1624  
1625 # Calculate size of map in degrees
1626 my $latitude_delta = $latitude_north - $latitude_south;
1627 my $longitude_delta =
1628 ($longitude_west < $longitude_east)
1629 ? ($longitude_east - $longitude_west)
1630 : (($longitude_east - (-180)) + (180 - $longitude_west));
1631  
1632 # Number of tiles
1633 my $number_of_vertical_tiles = $image_height_pix / $tile_height_pix;
1634 my $number_of_horizontal_tiles = $image_width_pix / $tile_width_pix;
1635  
1636 my %markers, my $max_data_count = 0;
1637  
1638 foreach my $data (@{$data_ref}) {
1639 my $row_ref;
1640  
1641 foreach my $i (0 .. $#base_sql_fields) {
1642 $row_ref->{$base_sql_fields[$i]} = $data->[$i];
1643 }
1644  
1645 my $latitude = $row_ref->{latitude};
1646 my $latitude_from_origin = $latitude - $latitude_south;
1647  
1648 my $longitude = $row_ref->{longitude};
1649 my $longitude_from_origin =
1650 ($longitude_west < $longitude)
1651 ? ($longitude - $longitude_west)
1652 : (($longitude - (-180)) + (180 - $longitude_west));
1653  
1654 my $rounded_latitude =
1655 $number_of_vertical_tiles * $latitude_from_origin / $latitude_delta;
1656 my $lowres_latitude =
1657 $latitude_south + (int($rounded_latitude) + 0.5) *
1658 ($latitude_delta / $number_of_vertical_tiles);
1659  
1660 my $rounded_longitude =
1661 $number_of_horizontal_tiles * $longitude_from_origin /
1662 $longitude_delta;
1663 my $lowres_longitude =
1664 $longitude_west + (int($rounded_longitude) + 0.5) *
1665 ($longitude_delta / $number_of_horizontal_tiles);
1666 if ($lowres_longitude > 180) {
1667 $lowres_longitude = -180 + ($lowres_longitude - 180);
1668 }
1669  
1670 my $key = join(':', $lowres_latitude, $lowres_longitude);
1671  
1672 my $cluster_value = $row_ref->{$cluster_field} || '_default';
1673  
1674 push @{$markers{$key}{rows}}, $row_ref;
1675 $markers{$key}{cluster_set}{$cluster_value}++;
1676 $markers{$key}{cluster_data_count}++;
1677  
1678 if ( $markers{$key}{cluster_data_count}
1679 and $markers{$key}{cluster_data_count} > $max_data_count) {
1680 $max_data_count = $markers{$key}{cluster_data_count};
1681 }
1682 }
1683  
1684 return (\%markers, $max_data_count);
1685 }
1686  
1687 # Function :
1688 # Arguments : \%all_cluster_values (key: $label, value: count), \%color_table (key: $label, value: color)
1689 # Returns : $html
1690 # Notes :
1691  
1692 sub _generate_piechart_legend_info {
1693 my ($self, $data_ref) = @_;
1694  
1695 my $session = $self->session;
1696 my $color_table_ref = $session->param('color_table');
1697 my $temp_dir = $self->temp_dir;
1698 my $temp_dir_eq = $self->temp_dir_eq;
1699  
1700 my @legend_data;
1701  
1702 foreach my $label (
1703 sort { $data_ref->{$b} <=> $data_ref->{$a} }
1704 keys %{$data_ref}
1705 ) {
1706 my $count = $data_ref->{$label};
1707 my $color = $color_table_ref->{$label};
1708  
1709 my $icon_file = "$temp_dir/Legend-icon-$color.png";
1710 my $icon_url = "$temp_dir_eq/Legend-icon-$color.png";
1711  
1712 if (!-e $icon_file) {
1713 my @icon_data = (
1714 [$label, 'empty'],
1715 [75, 25],
1716 );
1717  
1718 my $graph = GD::Graph::pie->new(15, 15)
1719 or croak("Cannot create an GD::Graph object!");
1720  
1721 $graph->set(
1722 '3d' => 0,
1723 'labelclr' => 0,
1724 'axislabelclr' => 0,
1725 'legendclr' => 0,
1726 'valuesclr' => 0,
1727 'textclr' => 0,
1728 'start_angle' => 180,
1729 'accentclr' => 'dgray',
1730 'dclrs' => [$color, 'white'],
1731 ) or croak($graph->error);
1732  
1733 my $icon = $graph->plot(\@icon_data)
1734 or croak($graph->error); # Convert to GD object
1735  
1736 open(IMG, ">$icon_file")
1737 or croak("Cannot write file ($icon_file): $!");
1738 binmode IMG;
1739 print IMG $icon->png;
1740 close IMG;
1741 }
1742  
1743 push @legend_data, [$icon_url, $label, $count];
1744 }
1745  
1746 return \@legend_data;
1747 }
1748  
1749 # Function :
1750 # Arguments : $data_ref (an array ref of two equal-length arrays is needed)
1751 # Returns : 1
1752 # Notes : This is a private method.
1753  
1754 sub _make_piechart_icon {
1755 my ($self, $data_ref, $color_ref, $max_data_count) = @_;
1756  
1757 my $temp_dir = $self->temp_dir;
1758 my $temp_dir_eq = $self->temp_dir_eq;
1759 my $session_id = $self->session_id;
1760  
1761 # Check data (must be an array of two arrays
1762 unless ($data_ref
1763 && ref $data_ref
1764 && ref $data_ref eq 'ARRAY'
1765 && $data_ref->[0]
1766 && ref $data_ref->[0]
1767 && ref $data_ref->[0] eq 'ARRAY'
1768 && $data_ref->[1]
1769 && ref $data_ref->[1]
1770 && ref $data_ref->[1] eq 'ARRAY'
1771 && scalar(@{$data_ref->[0]}) == scalar(@{$data_ref->[1]})) {
1772 $self->error("Invalid data param (an array ref of two "
1773 . "equal-length arrays is needed)!");
1774 }
1775  
1776 # Get data count
1777 my $data_count = $self->_total(@{$data_ref->[1]});
1778  
1779 my $max_chart_size = 50; # This can go into constructor
1780 my $min_chart_size = 20; # This can go into constructor
1781  
1782 my $piechart_icon_size = $self->piechart_icon_size( # This method can be overridden
1783 $data_count, $max_data_count, $min_chart_size, $max_chart_size
1784 );
1785
1786 # Generate pie chart and render it as a GD object
1787 my $graph = GD::Graph::pie->new($piechart_icon_size, $piechart_icon_size)
1788 or $self->error("Cannot create an GD::Graph object!");
1789  
1790 $graph->set(
1791 '3d' => 0,
1792 'labelclr' => 0,
1793 'axislabelclr' => 0,
1794 'legendclr' => 0,
1795 'valuesclr' => 0,
1796 'textclr' => 0,
1797 'start_angle' => 180,
1798 'accentclr' => 'dgray',
1799 'dclrs' => $color_ref,
1800 ) or $self->error($graph->error);
1801  
1802 my $graph_as_gd = $graph->plot($data_ref) or $self->error($graph->error);
1803  
1804 # Generate a temp file and print it out
1805 my $file_temp = File::Temp->new(
1806 TEMPLATE => "PieChart-icon-$session_id-XXXXX",
1807 DIR => $temp_dir,
1808 SUFFIX => '.png',
1809 UNLINK => 0,
1810 );
1811 my $icon_file = $file_temp->filename;
1812  
1813 open(IMG, ">$icon_file")
1814 or $self->error("Cannot write file ($icon_file): $!");
1815 binmode IMG;
1816 print IMG $graph_as_gd->png;
1817 close IMG;
1818  
1819 my ($icon_file_name) = $icon_file =~ /([^\/]+)$/;
1820 my $icon_url = "$temp_dir_eq/$icon_file_name";
1821  
1822 return ($icon_url, $piechart_icon_size);
1823 }
1824  
1825 # Function :
1826 # Arguments :
1827 # Returns : 1
1828 # Notes : This is a private method.
1829  
1830 sub _process_params {
1831 my ($self) = @_;
1832  
1833 my $base_sql_fields = $self->base_sql_fields;
1834 my $base_output_headers = $self->base_output_headers;
1835 my $param_fields = $self->param_fields;
1836  
1837 if (@{$base_sql_fields} != @{$base_output_headers}) {
1838 croak(
1839 "Count of base_sql_fields and base_output_headers do not match!");
1840 }
1841  
1842 my @fields;
1843 foreach my $i (0 .. @{$base_sql_fields} - 1) {
1844 my $name = $base_sql_fields->[$i];
1845 my $display = $base_output_headers->[$i];
1846 my $values = $param_fields->{$name} || [];
1847 my $param = (any { $_ eq $name } (keys %$param_fields)) ? 1 : 0;
1848  
1849 foreach (@$values) {
1850 my ($param, $display) = split(':', $_);
1851 if (!defined $display) { $display = $param }
1852  
1853 $_ = {param => $param, display => $display};
1854 }
1855  
1856 push @fields,
1857 { name => $name,
1858 display => $display,
1859 values => $values,
1860 param => $param,
1861 };
1862 }
1863  
1864 $self->fields(\@fields);
1865  
1866 return 1;
1867 }
1868  
1869 # Function : URL-encodes a given string.
1870 # Arguments : $string
1871 # Returns : $url_encoded_string
1872 # Notes : This is a private method.
1873  
1874 sub _url_encode {
1875 my ($self, $string) = @_;
1876  
1877 $string =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
1878  
1879 return $string;
1880 }
1881  
1882 # Function : URL-decodes a given string.
1883 # Arguments : $string
1884 # Returns : $url_decoded_string
1885 # Notes : This is a private method.
1886  
1887 sub _url_decode {
1888 my ($self, $string) = @_;
1889  
1890 $string =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
1891  
1892 return $string;
1893 }
1894  
1895 # Function : Retrieves the content for the directive specified;
1896 # supports GET (retrieval by LWP), EXEC (executes a command-line
1897 # and captures output), FILE (retrieves a file content).
1898 # Arguments : $directive
1899 # Returns : $content
1900 # Notes : This is a private method.
1901  
1902 sub _content {
1903 my ($self, $container) = @_;
1904  
1905 return '&nbsp;' unless $container;
1906  
1907 my $content = $container;
1908  
1909 if ($container =~ s/^(FILE|EXEC|GET)://) {
1910 my $type = $1;
1911  
1912 if ($type eq 'GET') {
1913 $content = get($container)
1914 or croak("Cannor get container ($container)!");
1915 }
1916  
1917 elsif ($type eq 'EXEC') {
1918 open(EXEC, "$container|")
1919 or croak("Cannot exec container ($container)! - $!");
1920 { local $/; $content = <EXEC>; }
1921 close EXEC;
1922 }
1923  
1924 elsif ($type eq 'FILE') {
1925 open(FILE, "<$container")
1926 or croak("Cannot open container ($container)! - $!");
1927 { local $/; $content = <FILE>; }
1928 close FILE;
1929 }
1930 }
1931  
1932 return $content;
1933 }
1934  
1935 # Function : Rounds a number.
1936 # Arguments : $number
1937 # Returns : $number
1938 # Notes : This is a private method.
1939  
1940 sub _round {
1941 my ($self, $number) = @_;
1942  
1943 return int($number + 0.5);
1944 }
1945  
1946 # Function : Totals values in an array.
1947 # Arguments : @array
1948 # Returns : $number
1949 # Notes : This is a private method.
1950  
1951 sub _total {
1952 my ($self, @values) = @_;
1953  
1954 my $total;
1955  
1956 foreach my $value (@values) {
1957 $total += $value;
1958 }
1959  
1960 return $total;
1961 }
1962  
1963 # Function :
1964 # Arguments : None
1965 # Returns : \@colors
1966 # Notes : This is a private method.
1967  
1968 sub _colors {
1969 my ($self) = @_;
1970  
1971 my @colors = qw(
1972 lyellow
1973 lblue
1974 lorange
1975 lgreen
1976 cyan
1977 red
1978 gold
1979 lred
1980 pink
1981 dpurple
1982 lgray
1983 yellow
1984 lbrown
1985 orange
1986 dpink
1987 marine
1988 gray
1989 dyellow
1990 dgreen
1991 dbrown
1992 dred
1993 blue
1994 dblue
1995 green
1996 );
1997  
1998 return \@colors;
1999 }
2000  
2001 # Function :
2002 # Arguments : None
2003 # Returns : 1
2004 # Notes : This is a private method.
2005  
2006 sub _clean_temp_dir {
2007 my ($self) = @_;
2008  
2009 my $temp_dir = $self->temp_dir;
2010 # my $session_id = $self->session_id;
2011  
2012 my @cmds = (
2013 "find $temp_dir -name \'Legend-icon-*\' -cmin +20 -exec rm -f {} \\;",
2014 # "find $temp_dir -name \'PieChart-icon-$session_id-*\' -exec rm -f {} \\;",
2015 # "find $temp_dir -name \'Density-icon-$session_id-*\' -exec rm -f {} \\;",
2016 # "find $temp_dir -name \'Small-icon-$session_id-*\' -exec rm -f {} \\;",
2017 "find $temp_dir -name \'PieChart-icon-*\' -cmin +2 -exec rm -f {} \\;",
2018 "find $temp_dir -name \'Density-icon-*\' -cmin +2 -exec rm -f {} \\;",
2019 "find $temp_dir -name \'Small-icon-*\' -cmin +2 -exec rm -f {} \\;",
2020 "find $temp_dir/sessions -name \'cgisess_*\' -cmin +20 -exec rm -f {} \\;",
2021 );
2022  
2023 foreach my $cmd (@cmds) {
2024 system($cmd);
2025 }
2026  
2027 return 1;
2028 }
2029  
2030 1;
2031  
2032 __END__
« GMap/Files.pm ↑ Summary GMap/Tutorial.pm »