GMap/Files.pm

filter: Statementstmt 26.4% 14 / 53 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 4 / 8 Podpod 0.0% 0 / 4 totaltotal 23.3% 18 / 77 CC 8 SCAR 26.1
CC
8
Cov
22%
create_files
30.0
new
12.0
temp_dir
6.0
CRAP 13.6
↑ Summary GMap.pm »
1 package HTML::GMap::Files;
2  
3 1 our $VERSION = '0.06';
4  
5 # $Id: Files.pm,v 1.13 2007/09/19 01:49:12 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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
14 0 my ($class, %params) = @_;
15  
16 0 my $self = bless {}, $class;
17  
18 0 eval {
19 0 ▶ exists $params{temp_dir} or croak("A temp_dir param is required!");
Branch: 1 branch
BranchTrueFalseTotal
exists $params{"temp_dir"} or croak("A temp_dir param is required!") 0 0 0
20 0 $self->temp_dir($params{temp_dir});
21 };
22  
23 0 ▶ $self->croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
24  
25 0 $self->create_files;
26  
27 0 return $self;
28 }
29  
30 0 ▶sub create_files {
Subroutine create_files: not called CC=5 SCAR=34.0
Pod undocumented
31 0 my ($self) = @_;
32  
33 0 my $files_ref = $self->files;
34 0 my $temp_dir = $self->temp_dir;
35  
36 0 foreach my $file_name (keys %$files_ref) {
37 0 my $file_content = $files_ref->{$file_name}->{content};
38 0 my $file_type = $files_ref->{$file_name}->{type};
39
40 0 ▶ if ($file_type eq 'binary') {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_type eq 'binary') { } 0 0 0
41 0 $file_content =~ s/\n//g;
42
43 0 my $binary_content = pack('H*', $file_content);
44
45 0 ▶ open(OUT, ">$temp_dir/$file_name")
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$temp_dir/$file_name" or croak("Cannot write file ($temp_dir/$file_name): $!") 0 0 0
46 or croak("Cannot write file ($temp_dir/$file_name): $!");
47 0 binmode OUT;
48
49 0 print OUT $binary_content;
50  
51 0 close OUT;
52 }
53
54 else {
55 0 ▶ open(OUT, ">$temp_dir/$file_name")
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$temp_dir/$file_name" or croak("Cannot write file ($temp_dir/$file_name): $!") 0 0 0
56 or croak("Cannot write file ($temp_dir/$file_name): $!");
57  
58 0 $file_content =~ s/^\s+//;
59 0 $file_content =~ s/\s+$/\n/;
60  
61 0 print OUT $file_content;
62  
63 0 close OUT;
64 }
65  
66  
67 }
68  
69 0 return 1;
70 }
71  
72 0 ▶sub temp_dir {
Subroutine temp_dir: not called CC=2 SCAR=17.9
Pod undocumented
73 0 my ($self, $value) = @_;
74  
75 0 ▶ $self->{temp_dir} = $value if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 0 0
76  
77 0 return $self->{temp_dir};
78 }
79  
80 0 ▶sub files {
Subroutine files: not called CC=1 SCAR=6.9
Pod undocumented
81 0 my ($self) = @_;
82  
83 0 my %files;
84  
85
86 0 $files{'gmap-main.css'}{'type'} = 'ascii';
87 0 $files{'gmap-main.css'}{'content'} = <<'CONTENT';
88 /*
89 Author: Payan Canaran (pcanaran@cpan.org)
90 Copyright 2006-2007 Cold Spring Harbor Laboratory
91 $Id: Files.pm,v 1.13 2007/09/19 01:49:12 canaran Exp $
92 */
93  
94 body {
95 text-align: center;
96 font-family: arial;
97 }
98  
99 th {
100 font-size: 12px;
101 }
102  
103 td {
104 font-size: 12px;
105 vertical-align: top;
106 }
107  
108 table.container {
109 border: 1px dashed #333;
110 background-color: #F5F5DC;
111 text-align: left;
112 font-size: 13px;
113 }
114  
115 div.sub_header {
116 padding: 4px;
117 font-size: 14px;
118 font-weight: bold;
119 }
120  
121 div.status {
122 padding: 4px;
123 width: 250px;
124 }
125  
126 div.filter {
127 padding: 4px;
128 width: 240px;
129 }
130  
131 div.messages {
132 padding: 4px;
133 width: 250px;
134 overflow: auto;
135 }
136  
137 div.details_info {
138 padding: 4px;
139 height: 110px;
140 width: 240px;
141 overflow: auto;
142 }
143  
144 div.legend_info {
145 padding: 4px;
146 height: 490px;
147 width: 180px;
148 font-weight: bold;
149 overflow: auto;
150 }
151  
152 div.float_right {
153 clear: right;
154 display: inline;
155 }
156  
157 div.hidden {
158 visibility: hidden;
159 display: inline;
160 }
161  
162 div.visible {
163 visibility: visible;
164 display: inline;
165 }
166  
167 font.bold {
168 font-weight: bold;
169 }
170  
171 CONTENT
172
173 0 $files{'gmap-main.html'}{'type'} = 'ascii';
174 0 $files{'gmap-main.html'}{'content'} = <<'CONTENT';
175 <!--
176 Author: Payan Canaran (pcanaran@cpan.org)
177 Copyright 2006-2007 Cold Spring Harbor Laboratory
178 $Id: Files.pm,v 1.13 2007/09/19 01:49:12 canaran Exp $
179 -->
180  
181 [% cgi_header %]
182 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
183 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
184 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
185  
186 <head>
187 <link rel="stylesheet" type="text/css" href="[% gmap_main_css_file_eq %]" />
188 <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
189 <title>[% page_title %]</title>
190  
191 <style type="text/css">
192 v\:* {
193 behavior:url(#default#VML);
194 }
195 </style>
196 </head>
197  
198 <body>
199 [% header %]
200  
201 <h1>[% page_title %]</h1>
202  
203 <table align="center" class="container">
204 <tr>
205 <td>
206 <div class="sub_header">Legend: </div>
207 <div id="legend" class="legend_info"></div>
208 <p/>
209 <div id="legend_message"></div>
210 </td>
211
212 <td>
213 <div id="map" style="height: [% image_height_pix %]px; width: [% image_width_pix %]px">
214 Loading map ...
215 </div>
216 </td>
217  
218 <td>
219 <div class="sub_header">Status: </div>
220 <div id="status" class="status">Initializing ...</div>
221  
222 <div>
223 <form id="filter">
224 <div class="sub_header">Filter: </div>
225  
226 <div class="filter">
227 <table width="100%">
228 [% FOREACH field = param_fields_with_values %]
229 <tr>
230 <!-- Param [% field.name %] -->
231 <td align="left">
232 <font class="bold">[% field.display %]:</font>
233 </td>
234 <td align="right">
235 <select id="[% field.name %]" name="[% field.name %]">
236 [% FOREACH value = field.values %]
237 <option value="[% value.param %]">[% value.display %]</option>
238 [% END %]
239 </select>
240 </td>
241 [% END %]
242 </tr>
243 </table>
244
245 <div id="cluster_slices_group" class="visible">
246 [% IF display_cluster_slices %]
247 <table width="100%">
248 <tr>
249 <td align="left">
250 <input id="cluster_slices" type="checkbox" name="cluster_slices"></input>
251 Cluster rare values:
252 </td>
253 </tr>
254 <tr>
255 <td align="right">
256 (less than
257 <input id="cluster_slices_value" type="text" name="cluster_slices_value" size="2" value="2">
258 <select id="cluster_slices_by" name="cluster_slices_by">
259 <option value="percent" selected="1">%</option>
260 <option value="count">pt</option>
261 </select>
262 per tile)
263 </td>
264 </tr>
265 </table>
266 [% END %]
267 </div>
268
269 <div>
270 <table width="100%">
271 <tr>
272 <td align="right">
273 <input type="button" name="Filter" value="Filter" onClick="doRefresh()"/>
274 </td>
275 </tr>
276 </table>
277 </div>
278 </div>
279 </form>
280 </div>
281  
282 <div>
283 <div class="sub_header">Details: </div>
284 <div id="details" class="details_info"></div>
285 </div>
286  
287 <div>
288 <div class="sub_header">Messages: </div>
289 <div id="messages" class="messages">[% messages %]</div>
290 </div>
291 </td>
292 </tr>
293 </table>
294
295 <div id="debug">
296 </div>
297  
298 <!-- Execute scripts here (1/2) -->
299 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=[% gmap_key %]"
300 type="text/javascript">
301 </script>
302  
303 <script src="[% prototype_js_file_eq %]"
304 type="text/javascript">
305 </script>
306  
307 <script type="text/javascript">
308 var varStore = {
309 centerLat :'[% center_latitude %]',
310 centerLng :'[% center_longitude %]',
311 centerZoom :[% center_zoom %],
312 imageHeightPix :'[% image_height_pix %]',
313 tileHeightPix :'[% tile_height_pix %]',
314 imageWidthPix :'[% image_width_pix %]',
315 tileWidthPix :'[% tile_width_pix %]',
316 queryParams :[[% param_fields %]], // This must be an array
317 urlTemplate :'[% url_template %]',
318 clusterField :'[% cluster_field %]',
319 drawGrid :'[% draw_grid %]'
320 };
321 </script>
322  
323 <!-- Execute scripts here (2/2) -->
324 <script src="[% gmap_main_js_file_eq %]"
325 type="text/javascript">
326 </script>
327  
328 [% footer %]
329 </body>
330 </html>
331  
332 CONTENT
333
334 0 $files{'gmap-main.js'}{'type'} = 'ascii';
335 0 $files{'gmap-main.js'}{'content'} = <<'CONTENT';
336 // Author: Payan Canaran (pcanaran@cpan.org)
337 // Copyright 2006-2007 Cold Spring Harbor Laboratory
338 // $Id: Files.pm,v 1.13 2007/09/19 01:49:12 canaran Exp $
339  
340 // Create and initialize map
341 var map = new GMap2(document.getElementById('map'));
342  
343 var centerLat = varStore.centerLat;
344 var centerLng = varStore.centerLng;
345 var centerZoom = varStore.centerZoom;
346  
347 // map.addControl(new GSmallZoomControl());
348 map.addControl(new GLargeMapControl());
349 map.addControl(new GMapTypeControl());
350 map.addControl(new GScaleControl());
351 map.setCenter(new GLatLng(centerLat, centerLng), centerZoom);
352  
353 // Register map events
354 GEvent.addListener(map, "movestart", startRefresh);
355 GEvent.addListener(map, "zoomend", limitZoom);
356 GEvent.addListener(map, "moveend", limitVerticalMove);
357 GEvent.addListener(map, "moveend", doRefresh);
358  
359 // Register non-map events
360 var clusterField = varStore.clusterField;
361 if (clusterField && clusterField != '_default') {
362 Event.observe(document.getElementById(clusterField), 'change', toggleClusterSlicesGroup);
363 }
364 Event.observe(window, 'load', doRefresh);
365  
366 // [End of main script]
367  
368 // Create a URL from form variables
369 function constructUrl() {
370 var queryParams = varStore.queryParams;
371 var urlTemplate = varStore.urlTemplate;
372  
373 // Determine geographic boundaries of the map in view
374 var mapBounds = map.getBounds();
375  
376 var boundSouthWest = mapBounds.getSouthWest();
377 var boundNorthEast = mapBounds.getNorthEast();
378  
379 var latNorth = boundNorthEast.lat();
380 var lngEast = boundNorthEast.lng();
381 var latSouth = boundSouthWest.lat();
382 var lngWest = boundSouthWest.lng();
383  
384 // Make a copy of urlTemplate, will be overwritten
385 var url = urlTemplate;
386  
387 // Retrieve and substitute query param values
388 for (var i = 0; i < queryParams.length; i++) {
389 var paramName = queryParams[i];
390 var paramValue = document.getElementById(paramName).value;
391 url += ';' + escape(paramName) + "=" + escape(paramValue);
392 }
393  
394 // Add coordinates
395 url += "&latitude_north=" + escape(latNorth);
396 url += "&longitude_east=" + escape(lngEast);
397 url += "&latitude_south=" + escape(latSouth);
398 url += "&longitude_west=" + escape(lngWest);
399  
400 // Add clustering (slices) params
401 if (document.getElementById('cluster_slices_group')
402 && Element.hasClassName(document.getElementById('cluster_slices_group'), 'visible')
403 && document.getElementById('cluster_slices')
404 ) {
405 url += "&cluster_slices=" + escape(document.getElementById('cluster_slices').checked);
406 url += "&cluster_slices_value=" + escape(document.getElementById('cluster_slices_value').value);
407 url += "&cluster_slices_by=" + escape(document.getElementById('cluster_slices_by').value);
408 };
409  
410 //Add zoom level
411 url += "&zoom_level=" + map.getZoom()
412
413 // document.getElementById("debug").innerHTML = url;
414  
415 return url;
416 }
417  
418 // Add latitude-longitude grid to map
419 function addLatLngLines() {
420 var imageHeightPix = varStore.imageHeightPix;
421 var tileHeightPix = varStore.tileHeightPix;
422  
423 var imageWidthPix = varStore.imageWidthPix;
424 var tileWidthPix = varStore.tileWidthPix;
425  
426 var numberOfVerticalTiles = imageHeightPix / tileHeightPix;
427 var numberOfHorizontalTiles = imageWidthPix / tileWidthPix;
428  
429 // Determine geographic boundaries of the map in view
430 var mapBounds = map.getBounds();
431  
432 var boundSouthWest = mapBounds.getSouthWest();
433 var boundNorthEast = mapBounds.getNorthEast();
434  
435 var latNorth = boundNorthEast.lat();
436 var lngEast = boundNorthEast.lng();
437 var latSouth = boundSouthWest.lat();
438 var lngWest = boundSouthWest.lng();
439  
440 var lngMid = (lngWest < lngEast) ? lngWest + (lngEast - lngWest) / 2
441 : lngWest + ((lngEast - (-180)) + (180 - lngWest)) / 2;
442 if (lngMid > 180) { // Longitude +180 continues with -180
443 lngMid = -180 + (lngMid - 180); // (lngMid - 180) is overflow over 180'
444 }
445  
446 // Longitude +180 continues with -180
447 tileSideLng = (lngWest < lngEast) ? (lngEast - lngWest) / numberOfHorizontalTiles
448 : ((lngEast - (-180)) + (180 - lngWest)) / numberOfHorizontalTiles;
449  
450 tileSideLat = (latNorth - latSouth) / numberOfVerticalTiles;
451  
452 // document.getElementById("debug").innerHTML = '> latNorth:' + latNorth
453 // + ' latSouth:' + latSouth
454 // + '<br>'
455 // + ' lngWest:' + lngWest
456 // + ' lngEast:' + lngEast
457 // + ' tileSideLng:' + tileSideLng
458 // + '<br/>';
459  
460 for (var n = 1; n < numberOfHorizontalTiles; n++) {
461 // Draw longitudes
462 var lngNth = lngWest + n * tileSideLng;
463  
464 if (lngNth > 180) { // Longitude +180 continues with -180
465 lngNth = -180 + (lngNth - 180); // (lngNth - 180) is overflow over 180'
466 }
467  
468 var polyline = new GPolyline([
469 new GLatLng(latNorth, lngNth),
470 new GLatLng(latSouth, lngNth)
471 ], "#ff8b04", 2);
472 map.addOverlay(polyline);
473 map.addOverlay(polyline); // Every other polyline is skipped, a potential bug; observe 8/10/07
474 }
475  
476 for (var n = 1; n < numberOfVerticalTiles; n++) {
477 // Draw latitudes
478 var latNth = latSouth + n * tileSideLat;
479  
480 var polyline = new GPolyline([
481 new GLatLng(latNth, lngWest),
482 new GLatLng(latNth, lngMid), // A mid point is needed to ensure direction of the line is correct
483 new GLatLng(latNth, lngEast)
484 ], "#ff8b04", 2);
485 map.addOverlay(polyline);
486 map.addOverlay(polyline); // Every other polyline is skipped, a potential bug; observe 8/10/07
487 }
488  
489 // document.getElementById("debug").innerHTML = 'zoom:' + map.getZoom();
490  
491 return 1;
492 }
493  
494 // Start refresh (clear overlays and messages)
495 function startRefresh() {
496 // Clear existing overlays
497 map.clearOverlays();
498  
499 document.getElementById("status").innerHTML = 'Moving ...';
500 document.getElementById("legend").innerHTML = '';
501 document.getElementById("legend_message").innerHTML = '';
502 document.getElementById("details").innerHTML = '';
503 }
504  
505 // Refresh display (clear overlays, send an AJAX request, process request)
506 function doRefresh(event) {
507 var drawGrid = varStore.drawGrid;
508  
509 // Clear existing overlays
510 map.clearOverlays();
511  
512 document.getElementById("status").innerHTML = "Refreshing ...";
513  
514 // Construct URL based on coordinates and form values
515 var requestUrl = constructUrl();
516  
517 // Query database, pass results on for parsing
518 GDownloadUrl(requestUrl, processRequest); // No parentheses
519  
520 // Draw grid
521 if (drawGrid > 0) {
522 addLatLngLines();
523 }
524 }
525  
526 // Place markers on the map
527 function processRequest(data, responseCode) {
528 var xml = GXml.parse(data);
529  
530 var markers = xml.documentElement.getElementsByTagName("marker");
531  
532 for (var i = 0; i < markers.length; i++) {
533 var lat = parseFloat(markers[i].getAttribute("latitude"));
534 var lng = parseFloat(markers[i].getAttribute("longitude"));
535  
536 var iconUrl = markers[i].getAttribute("icon_url");
537 var iconSize = markers[i].getAttribute("icon_size");
538 var messagesOnClick = markers[i].getAttribute("messages_on_click");
539 var detailsOnClick = markers[i].getAttribute("details_on_click");
540 var legendOnClick = markers[i].getAttribute("legend_on_click");
541  
542 var point = new GLatLng(lat, lng);
543  
544 addMarker(point, iconUrl, iconSize, legendOnClick, detailsOnClick, messagesOnClick);
545  
546 document.getElementById("details").innerHTML = '[Click on an icon on the map for details ...]';
547 }
548  
549 var metaData = xml.documentElement.getElementsByTagName("meta_data"); // Only one present
550  
551 var legendByDefault = metaData[0].getAttribute("legend_by_default");
552 var detailsByDefault = metaData[0].getAttribute("details_by_default")
553 var messagesByDefault = metaData[0].getAttribute("messages_by_default")
554  
555 if (legendByDefault) {
556 document.getElementById("legend").innerHTML = legendByDefault;
557 }
558  
559 if (detailsByDefault) {
560 document.getElementById("details").innerHTML = detailsByDefault;
561 }
562  
563 if (messagesByDefault) {
564 document.getElementById("messages").innerHTML = messagesByDefault;
565 }
566  
567 document.getElementById("status").innerHTML = "Ready";
568 }
569  
570 // Utility function to add a marker
571 function addMarker(point, iconUrl, iconSize, legendOnClick, detailsOnClick, messagesOnClick) {
572 var icon = createIcon(iconUrl, iconSize);
573 var marker = new GMarker(point, icon);
574  
575 if (legendOnClick) {
576 GEvent.addListener(marker, "click", function() {
577 document.getElementById("legend").innerHTML = legendOnClick;
578 })
579 };
580  
581 if (detailsOnClick) {
582 GEvent.addListener(marker, "click", function() {
583 document.getElementById("details").innerHTML = detailsOnClick;
584 })
585 };
586  
587 if (messagesOnClick) {
588 GEvent.addListener(marker, "click", function() {
589 document.getElementById("message").innerHTML = messagesOnClick;
590 })
591 };
592  
593 map.addOverlay(marker);
594 }
595  
596 // Utility function to create an icon object to be used as a marker
597 function createIcon(imageUrl, size) {
598 var icon = new GIcon();
599 icon.image = imageUrl;
600  
601 icon.shadow = "";
602 icon.iconSize = new GSize(size, size);
603 icon.shadowSize = "";
604 icon.iconAnchor = new GPoint(Math.floor(size/2)+1, Math.floor(size/2)+1);
605 icon.infoWindowAnchor = new GPoint(Math.floor(size/2)+1, Math.floor(size/2)+1);;
606 return icon;
607 }
608  
609 // Utility function to toggle cluster slices group
610 function toggleClusterSlicesGroup(event) {
611 var element = Event.element(event);
612 var elementValue = element.value;
613  
614 var clusterSlicesGroup = document.getElementById("cluster_slices_group");
615  
616 if (elementValue == 'all') {
617 clusterSlicesGroup.className = 'visible';
618 }
619  
620 else {
621 clusterSlicesGroup.className = 'hidden';
622 }
623 }
624  
625 // Utility function to limit zoom
626 function limitZoom() {
627 var zoom = map.getZoom();
628  
629 if (zoom < 2) {
630 map.setZoom(2);
631 }
632 }
633  
634 // Utility function to limit vertical move
635 function limitVerticalMove() {
636 var mapBounds = map.getBounds();
637  
638 var boundSouthWest = mapBounds.getSouthWest();
639 var boundNorthEast = mapBounds.getNorthEast();
640  
641 var latNorth = boundNorthEast.lat();
642 var lngEast = boundNorthEast.lng();
643 var latSouth = boundSouthWest.lat();
644 var lngWest = boundSouthWest.lng();
645  
646 var latDelta = latNorth - latSouth;
647  
648 var mapCenter = map.getCenter();
649 var mapCenterLng = mapCenter.lng();
650  
651 if (latNorth > 84.5) {
652 var newMapCenterLat = 84.5 - latDelta / 2;
653 map.panTo(new GLatLng(newMapCenterLat, mapCenterLng));
654 }
655  
656 if (latSouth < -84.5) {
657 var newMapCenterLat = -84.5 + latDelta / 2;
658 map.panTo(new GLatLng(newMapCenterLat, mapCenterLng));
659 }
660 }
661  
662 // End of Functions
663  
664 CONTENT
665
666  
667 0 return \%files;
668 }
669  
670 1 1;
671  
672 __END__
↑ Summary GMap.pm »