Leaflet.pm

filter: Statementstmt 90.4% 132 / 146 Branchbran 67.5% 27 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 33 / 36 Podpod 100% 24 / 24 totaltotal 87.8% 216 / 246 CC 23 SCAR 8.6
CC
23
Cov
85%
center
5.2
title
3.1
setView
3.1
CRAP 2.4
« Leaflet/marker.pm ↑ Summary Leaflet/circle.pm »
1 package Geo::Leaflet;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Geo::Leaflet::tileLayer;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Geo::Leaflet::marker;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Geo::Leaflet::circle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Geo::Leaflet::polygon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Geo::Leaflet::polyline;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Geo::Leaflet::rectangle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Geo::Leaflet::icon;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use JSON::XS qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTML::Tiny qw{};;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 our $VERSION = '0.03';
16 2 our $PACKAGE = __PACKAGE__;
17 2 our @OBJECTS = ();
18 2 our @ICONS = ();
19  
20 =head1 NAME
21  
22 Geo::Leaflet - Generates a Leaflet JavaScript map web page
23  
24 =head1 SYNOPSIS
25  
26 use Geo::Leaflet;
27 my $map = Geo::Leaflet->new;
28 print $map->html;
29  
30 =head1 DESCRIPTION
31  
32 This package generates a L<Leaflet JavaScript|https://leafletjs.com/> map web page.
33  
34 =head1 CONSTRUCTORS
35  
36 =head2 new
37  
38 Returns a map object
39  
40 my $map = Geo::Leaflet->new(
41 id => "map",
42 center => [$lat, $lon],
43 zoom => 13,
44 );
45  
46 =head1 MAP PROPERTIES
47  
48 =head2 id
49  
50 Sets and returns the html id of the map.
51  
52 Default: "map"
53  
54 =cut
55  
56 sub id {
57 2 ▶ my $self = shift;
Subroutine id: called CC=3 SCAR=11.0
Pod documented
58 2 ▶ $self->{'id'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
59 2 ▶ $self->{'id'} = 'map' unless defined $self->{'id'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"id"} 1 1 2
60 2 return $self->{'id'};
61 }
62  
63 =head2 center
64  
65 Sets and returns the center of the map.
66  
67 $map->center([$lat, $lon]);
68 my $center = $map->center;
69  
70 Default: [38.2, -97.2]
71  
72 =cut
73  
74 sub center {
75 3 ▶ my $self = shift;
Subroutine center: called CC=5 SCAR=16.4
Pod documented
76 3 ▶ $self->{'center'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
77 3 ▶ $self->{'center'} = [38.2, -97.2] unless defined $self->{'center'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"center"} 1 2 3
78 3 my $error_template = "Error: $PACKAGE center expected %s (e.g., [\$lat, \$lon])";
79 3 ▶ die(sprintf($error_template, 'array reference')) unless ref($self->{'center'}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"center"} eq "ARRAY" 0 3 3
80 3 ▶ die(sprintf($error_template, 'two elements' )) unless @{$self->{'center'}} == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"center"};} == 2 0 3 3
81 3 return $self->{'center'};
82 }
83  
84 =head2 zoom
85  
86 Sets and returns the zoom of the map.
87  
88 $map->zoom(4.5);
89 my $zoom = $map->zoom;
90  
91 Default: 4.5
92  
93 =cut
94  
95 sub zoom {
96 2 ▶ my $self = shift;
Subroutine zoom: called CC=3 SCAR=11.0
Pod documented
97 2 ▶ $self->{'zoom'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
98 2 ▶ $self->{'zoom'} = 4.5 unless defined $self->{'zoom'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"zoom"} 1 1 2
99 2 return $self->{'zoom'};
100 }
101  
102 =head2 setView
103  
104 Sets the center and zoom of the map and returns the map object (i.e., matches leaflet.js interface).
105  
106 $map->setView([51.505, -0.09], 13);
107  
108 =cut
109  
110 sub setView {
111 1 ▶ my $self = shift;
Subroutine setView: called CC=3 SCAR=11.2
Pod documented
112 1 my $center = shift;
113 1 my $zoom = shift;
114 1 ▶ $self->center($center) if defined $center;
Branch: 1 branch
BranchTrueFalseTotal
if defined $center 0 1 1
115 1 ▶ $self->zoom($zoom) if defined $zoom;
Branch: 1 branch
BranchTrueFalseTotal
if defined $zoom 0 1 1
116 1 return $self;
117 }
118  
119 =head2 width
120  
121 Sets and returns the percent or pixel width of the map.
122  
123 $map->width('600px');
124 $map->width('100%');
125 my $width = $map->width;
126  
127 Default: 100%
128  
129 =cut
130  
131 sub width {
132 2 ▶ my $self = shift;
Subroutine width: called CC=3 SCAR=11.0
Pod documented
133 2 ▶ $self->{'width'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
134 2 ▶ $self->{'width'} = '100%' unless defined $self->{'width'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"width"} 1 1 2
135 2 return $self->{'width'};
136 }
137  
138 =head2 height
139  
140 Sets and returns the percent or pixel height of the map.
141  
142 $map->height('400px');
143 $map->height('100%');
144 my $height = $map->height;
145  
146 Default: 100%
147  
148 =cut
149  
150 sub height {
151 2 ▶ my $self = shift;
Subroutine height: called CC=3 SCAR=11.0
Pod documented
152 2 ▶ $self->{'height'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 2 2
153 2 ▶ $self->{'height'} = '100%' unless defined $self->{'height'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"height"} 1 1 2
154 2 return $self->{'height'};
155 }
156  
157 =head1 HTML PROPERTIES
158  
159 =head2 title
160  
161 Sets and returns the HTML title.
162  
163 Default: "Leaflet Map"
164  
165 =cut
166  
167 sub title {
168 1 ▶ my $self = shift;
Subroutine title: called CC=3 SCAR=11.4
Pod documented
169 1 ▶ $self->{'title'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
170 1 ▶ $self->{'title'} = 'Leaflet Map' unless defined $self->{'title'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"title"} 1 0 1
171 1 return $self->{'title'};
172 }
173  
174 =head1 TILE LAYER CONSTRUCTOR
175  
176 =head2 tileLayer
177  
178 Creates and returns a tileLayer object which is added to the map.
179  
180 $map->tileLayer(
181 url => 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
182 options => {
183 maxZoom => 19,
184 attribution => '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
185 },
186 );
187  
188 Default: OpenStreetMaps
189  
190 See: L<https://leafletjs.com/reference.html#tilelayer>
191  
192 =cut
193  
194 sub tileLayer {
195 3 ▶ my $self = shift;
Subroutine tileLayer: called CC=3 SCAR=11.0
Pod documented
196 3 ▶ $self->{'tileLayer'} = Geo::Leaflet::tileLayer->new(@_, JSON=>$self->JSON) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
197 3 ▶ $self->{'tileLayer'} = Geo::Leaflet::tileLayer->osm((), JSON=>$self->JSON) unless defined $self->{'tileLayer'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"tileLayer"} 2 1 3
198 3 return $self->{'tileLayer'};
199 }
200  
201 =head1 ICON CONSTRUCTOR
202  
203 =head2 icon
204  
205 my $icon = $map->icon(
206 name => "my_icon", #must be a valid JavaScript variable name
207 options => {
208 iconUrl => "my-icon.png",
209 iconSize => [38, 95],
210 iconAnchor => [22, 94],
211 popupAnchor => [-3, -76],
212 shadowUrl => "my-icon-shadow.png",
213 shadowSize => [68, 95],
214 shadowAnchor => [22, 94],
215 }
216 );
217  
218 See: L<https://leafletjs.com/reference.html#icon>
219  
220 =cut
221  
222 sub icon {
223 0 ▶ my $self = shift;
Subroutine icon: not called CC=1 SCAR=6.9
Pod documented
224 0 my $icon = Geo::Leaflet::icon->new(@_, JSON=>$self->JSON);
225 0 push @ICONS, $icon;
226 0 return $icon;
227 }
228  
229 =head1 MAP OBJECT CONSTRUCTORS
230  
231 =head2 marker
232  
233 Adds a marker object to the map and returns a reference to the marker object.
234  
235 $map->marker(lat=>$lat, lon=>$lon);
236  
237 See: L<https://leafletjs.com/reference.html#marker>
238  
239 =cut
240  
241 sub marker {
242 1 ▶ my $self = shift;
Subroutine marker: called CC=1 SCAR=0.0
Pod documented
243 1 my $marker = Geo::Leaflet::marker->new(@_, JSON=>$self->JSON);
244 1 push @OBJECTS, $marker;
245 1 return $marker;
246 }
247  
248 =head2 polyline
249  
250 Adds a polyline object to the map and returns a reference to the polyline object.
251  
252 my $latlngs = [[$lat, $lon], ...]
253 $map->polyline(coordinates=>$latlngs, options=>{});
254  
255 See: L<https://leafletjs.com/reference.html#polyline>
256  
257 =cut
258  
259 sub polyline {
260 0 ▶ my $self = shift;
Subroutine polyline: not called CC=1 SCAR=6.9
Pod documented
261 0 my $polyline = Geo::Leaflet::polyline->new(@_, JSON=>$self->JSON);
262 0 push @OBJECTS, $polyline;
263 0 return $polyline;
264 }
265  
266 =head2 polygon
267  
268 Adds a polygon object to the map and returns a reference to the polygon object.
269  
270 my $latlngs = [[$lat, $lon], ...]
271 $map->polygon(coordinates=>$latlngs, options=>{});
272  
273 See: L<https://leafletjs.com/reference.html#polygon>
274  
275 =cut
276  
277 sub polygon {
278 1 ▶ my $self = shift;
Subroutine polygon: called CC=1 SCAR=0.0
Pod documented
279 1 my $polygon = Geo::Leaflet::polygon->new(@_, JSON=>$self->JSON);
280 1 push @OBJECTS, $polygon;
281 1 return $polygon;
282 }
283  
284 =head2 rectangle
285  
286 Adds a rectangle object to the map and returns a reference to the rectangle object.
287  
288 $map->rectangle(llat => $llat,
289 llon => $llon,
290 ulat => $ulat,
291 ulon => $ulon,
292 options => {});
293  
294 See: L<https://leafletjs.com/reference.html#rectangle>
295  
296 =cut
297  
298 sub rectangle {
299 0 ▶ my $self = shift;
Subroutine rectangle: not called CC=1 SCAR=6.9
Pod documented
300 0 my $rectangle = Geo::Leaflet::rectangle->new(@_, JSON=>$self->JSON);
301 0 push @OBJECTS, $rectangle;
302 0 return $rectangle;
303 }
304  
305 =head2 circle
306  
307 Adds a circle object to the map and returns a reference to the circle object.
308  
309 $map->circle(lat=>$lat, lon=>$lon, radius=>$radius, options=>{});
310  
311 See: L<https://leafletjs.com/reference.html#circle>
312  
313 =cut
314  
315 sub circle {
316 1 ▶ my $self = shift;
Subroutine circle: called CC=1 SCAR=0.0
Pod documented
317 1 my $circle = Geo::Leaflet::circle->new(@_, JSON=>$self->JSON);
318 1 push @OBJECTS, $circle;
319 1 return $circle;
320 }
321  
322 =head1 METHODS
323  
324 =head2 html
325  
326 =cut
327  
328 sub html {
329 1 ▶ my $self = shift;
Subroutine html: called CC=1 SCAR=0.0
Pod documented
330 1 my $html = $self->HTML;
331 1 return $html->html([
332 $html->head([
333 $html->title($self->title),
334 $self->html_head_link,
335 $self->html_head_script,
336 $self->html_head_style,
337 ]),
338 $html->body([
339 $self->html_body_div,
340 $self->html_body_script
341 ]),
342 ]);
343 }
344  
345 =head2 html_head_link
346  
347 =cut
348  
349 sub html_head_link {
350 1 ▶ my $self = shift;
Subroutine html_head_link: called CC=1 SCAR=0.0
Pod documented
351 1 my $html = $self->HTML;
352 1 return $html->link({
353 rel => 'stylesheet',
354 href => 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css',
355 integrity => 'sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=',
356 crossorigin => '',
357 });
358 }
359  
360 =head2 html_head_script
361  
362 =cut
363  
364 sub html_head_script {
365 1 ▶ my $self = shift;
Subroutine html_head_script: called CC=1 SCAR=0.0
Pod documented
366 1 my $html = $self->HTML;
367 1 return $html->script({
368 src => 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js',
369 integrity => 'sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=',
370 crossorigin => '',
371 }, ''); #empty script required
372 }
373  
374 =head2 html_head_style
375  
376 =cut
377  
378 sub html_head_style {
379 1 ▶ my $self = shift;
Subroutine html_head_style: called CC=1 SCAR=0.0
Pod documented
380 1 my $html = $self->HTML;
381 1 my $style_size = sprintf('width: %s; height: %s;', $self->width, $self->height);
382 1 my $style_full = 'html, body { height: 100%; margin: 0; } '.
383 ".leaflet-container { $style_size max-width: 100%; max-height: 100%; }";
384 1 return $html->style({}, $style_full);
385 }
386  
387 =head2 html_body_div
388  
389 =cut
390  
391 sub html_body_div {
392 1 ▶ my $self = shift;
Subroutine html_body_div: called CC=1 SCAR=0.0
Pod documented
393 1 my $html = $self->HTML;
394 1 my $style_size = sprintf('width: %s; height: %s;', $self->width, $self->height);
395 1 return $html->div({id => $self->id, style => $style_size});
396 }
397  
398 =head2 html_body_script
399  
400 =cut
401  
402 sub html_body_script {
403 1 ▶ my $self = shift;
Subroutine html_body_script: called CC=1 SCAR=0.0
Pod documented
404 1 my $html = $self->HTML;
405 1 return $html->script({}, $self->html_body_script_contents);
406 }
407  
408 =head2 html_body_script_map
409  
410 =cut
411  
412 sub html_body_script_map {
413 1 ▶ my $self = shift;
Subroutine html_body_script_map: called CC=1 SCAR=0.0
Pod documented
414 1 return sprintf(q{const map = L.map(%s).setView(%s, %s);},
415 $self->JSON->encode($self->id),
416 $self->JSON->encode($self->center),
417 $self->JSON->encode($self->zoom),
418 );
419 }
420  
421 =head2 html_body_script_contents
422  
423 =cut
424  
425 sub html_body_script_contents {
426 1 ▶ my $self = shift;
Subroutine html_body_script_contents: called CC=3 SCAR=11.1
Pod documented
427 1 my $empty = '';
428 1 my @commands = (
429 $empty,
430 $empty,
431 $self->html_body_script_map,
432 $self->tileLayer->stringify,
433 $empty,
434 );
435 1 foreach my $icon (@ICONS) {
436 0 my $name = $icon->name;
437 0 push @commands, "const $name = " . $icon->stringify;
438 }
439 1 my $loop = 0;
440 1 foreach my $object (@OBJECTS) {
441 3 $loop++;
442 3 push @commands, "const object$loop = " . $object->stringify;
443 }
444 1 push @commands, $empty;
445  
446 1 return join $empty, map {" $_\n"} @commands;
447 }
448  
449 =head1 OBJECT ACCESSORS
450  
451 =head2 HTML
452  
453 Returns an L<HTML:Tiny> object to generate HTML.
454  
455 =cut
456  
457 sub HTML {
458 6 ▶ my $self = shift;
Subroutine HTML: called CC=3 SCAR=11.0
Pod documented
459 6 ▶ $self->{'HTML'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
460 6 ▶ $self->{'HTML'} = HTML::Tiny->new unless defined $self->{'HTML'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"HTML"} 1 5 6
461 6 return $self->{'HTML'};
462 }
463  
464 =head2 JSON
465  
466 Returns a L<JSON::XS> object to generate JSON.
467  
468 =cut
469  
470 sub JSON {
471 8 ▶ my $self = shift;
Subroutine JSON: called CC=1 SCAR=0.0
Pod documented
472 8 $self->{'JSON'} = JSON::XS->new->allow_nonref;
473 8 return $self->{'JSON'};
474 }
475  
476 =head1 SEE ALSO
477  
478 L<Geo::Google::StaticMaps::V2>
479 https://leafletjs.com/
480  
481 =head1 AUTHOR
482  
483 Michael R. Davis
484  
485 =head1 COPYRIGHT AND LICENSE
486  
487 Copyright (C) 2024 by Michael R. Davis
488  
489 MIT LICENSE
490  
491 =cut
492  
493 2 1;
« Leaflet/marker.pm ↑ Summary Leaflet/circle.pm »