| 1 | package Map::Tube::Plugin::Graph; | ||
| 2 | |||
| 3 | 1 | ▶ | use version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = qv('v1.3.0'); | ||
| 6 | our $AUTHORITY = 'cpan:MANWAR'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Map::Tube::Plugin::Graph - Graph plugin for Map::Tube. | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version v1.3.0 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 0 | ▶ | use Map::Tube::Plugin::Graph::Utils qw(graph_line_image graph_map_image get_graphviz_supported); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | use MIME::Base64; | ||
| 21 | use Graph; | ||
| 22 | |||
| 23 | use Moo::Role; | ||
| 24 | use namespace::autoclean; | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This is a graph plugin for L<Map::Tube> to create maps of individual tube lines | ||
| 29 | or of a complete tube network. This module is defined as a Moo Role. Once | ||
| 30 | installed, it gets plugged into the Map::Tube::* family. | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | use strict; use warnings; | ||
| 35 | use MIME::Base64; | ||
| 36 | use Map::Tube::London; | ||
| 37 | |||
| 38 | my $tube = Map::Tube::London->new; | ||
| 39 | |||
| 40 | # Entire map image to STDOUT | ||
| 41 | binmode(STDOUT); | ||
| 42 | print STDOUT $tube->as_png(); | ||
| 43 | |||
| 44 | # Entire binary map image | ||
| 45 | my (undef, $name_bin) = $tube->render( output_file => undef ); | ||
| 46 | # print "Image written to $name_bin"; | ||
| 47 | |||
| 48 | # Entire base64-encoded map image | ||
| 49 | my ($base64_string, $name_txt) = $tube->render( output_file => undef, base64 => 1 ); | ||
| 50 | |||
| 51 | # Just a particular line map image in binary format | ||
| 52 | my (undef, $name_lin) = $tube->render( 'Bakerloo', output_file => undef ); | ||
| 53 | # print "Image written to $name_lin"; | ||
| 54 | |||
| 55 | =head1 INSTALLATION | ||
| 56 | |||
| 57 | The plugin primarily depends on the GraphViz2 library, which provides an | ||
| 58 | interface to the external GraphViz tool. GraphViz2 as of 2.61 can only be | ||
| 59 | installed on Perl v5.008008 or above. | ||
| 60 | |||
| 61 | For example, on my Windows 11 box running WSL2 (Ubuntu 24.04 LTS), try this: | ||
| 62 | |||
| 63 | $ sudo apt install libgraphviz2-perl | ||
| 64 | |||
| 65 | =head1 METHODS | ||
| 66 | |||
| 67 | =head2 as_graph | ||
| 68 | |||
| 69 | Returns a C<multiedged>, directed L<Graph> object with the entire map. | ||
| 70 | You can do graph-theory stuff with it yourself, or even control a | ||
| 71 | visualisation using the L<GraphViz2/from_graph> API: | ||
| 72 | |||
| 73 | use Map::Tube::London; | ||
| 74 | # a convention with GraphViz2 is to have a separate decorating function: | ||
| 75 | sub graphvizify { | ||
| 76 | my ($g) = @_; | ||
| 77 | my $l2c = $g->get_graph_attribute('line2colour'); | ||
| 78 | for my $ft ($g->edges) { | ||
| 79 | $g->set_edge_attribute_by_id(@$ft, $_, graphviz=>{color=>$l2c->{$_}}) | ||
| 80 | for $g->get_multiedge_ids(@$ft); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | $tube = Map::Tube::London->new; | ||
| 84 | $g = $tube->as_graph->undirected_copy_attributes; # make undirected version | ||
| 85 | graphvizify($g); | ||
| 86 | binmode STDOUT; | ||
| 87 | print +GraphViz2->from_graph($g) | ||
| 88 | ->run(format=>"png",driver=>"neato") # neato, because dot is bad undirected | ||
| 89 | ->dot_output; | ||
| 90 | |||
| 91 | Or you can show only two lines using L<Graph/filter_edges>: | ||
| 92 | |||
| 93 | # uses same graphvizify as above | ||
| 94 | $g = $tube->as_graph->undirected_copy_attributes; # make undirected version | ||
| 95 | my %keep = map +($_=>1), qw(Central Northern); | ||
| 96 | $g->filter_edges(sub { $keep{$_[3]} }); | ||
| 97 | $g->filter_vertices(sub { !$_[0]->is_isolated_vertex($_[1]) }); | ||
| 98 | graphvizify($g); | ||
| 99 | # binmode and print as above | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub as_graph { | ||
| 104 | my ($self) = @_; | ||
| 105 | my $g = Graph->new(multiedged=>1); | ||
| 106 | my (%station2line, %station2station, %line_name2id); | ||
| 107 | for my $line (@{ $self->lines }) { | ||
| 108 | $line_name2id{$line->name} = $line->id; | ||
| 109 | } | ||
| 110 | for my $station (@{ $self->get_stations }) { | ||
| 111 | my $from = $station->id; | ||
| 112 | @{ $station2line{$from} }{ map $_->name, @{$station->line} } = (); | ||
| 113 | @{ $station2station{$from} }{ | ||
| 114 | map $self->get_node_by_id($_)->id, split /\,/,$station->link | ||
| 115 | } = (); | ||
| 116 | $g->add_vertex($from); | ||
| 117 | $g->set_vertex_attribute( $from, graphviz => { label => $station->name } ); | ||
| 118 | } | ||
| 119 | for my $from (keys %station2station) { | ||
| 120 | my @tos = keys %{$station2station{$from}}; | ||
| 121 | for my $line (keys %{ $station2line{$from} }) { | ||
| 122 | my $line_id = $line_name2id{$line}; | ||
| 123 | for my $to_on_line ( | ||
| 124 | grep { | ||
| 125 | exists $station2line{$_}{$line} | ||
| 126 | && !_line_restricted_out($self, $from, $_, $line_id) | ||
| 127 | } @tos | ||
| 128 | ) { | ||
| 129 | # gives false positive for Northern between Waterloo and Bank | ||
| 130 | $g->add_edge_by_id($from, $to_on_line, $line); | ||
| 131 | delete $station2station{$from}{$to_on_line}; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | } | ||
| 135 | delete $station2station{$_} | ||
| 136 | for grep !keys %{$station2station{$_}}, keys %station2station; | ||
| 137 | for my $f (sort keys %station2station) { | ||
| 138 | warn qq{Map has link from "$f" to "$_" but no lines in common\n} | ||
| 139 | for sort keys %{$station2station{$f}}; | ||
| 140 | } | ||
| 141 | $g->set_graph_attribute(line2colour=>+{ | ||
| 142 | map +($_->name=>$_->color), @{$self->lines} | ||
| 143 | }); | ||
| 144 | $g; | ||
| 145 | } | ||
| 146 | |||
| 147 | # Returns true if the map's data says the link from $from to $to is | ||
| 148 | # restricted to a subset of lines that does NOT include $line_id -- i.e. | ||
| 149 | # the physical link exists, but this particular line doesn't run on it in | ||
| 150 | # this direction, even though both stations otherwise share the line. | ||
| 151 | # See Map::Tube's "A:x" style link syntax. | ||
| 152 | sub _line_restricted_out { | ||
| 153 | my ($self, $from, $to, $line_id) = @_; | ||
| 154 | return 0 unless defined $line_id; | ||
| 155 | my $restrictions = $self->{_link_lines}; | ||
| 156 | return 0 unless ref $restrictions eq 'HASH'; | ||
| 157 | my $from_restrictions = $restrictions->{uc($from)}; | ||
| 158 | return 0 unless defined $from_restrictions; | ||
| 159 | my $allowed = $from_restrictions->{uc($to)}; | ||
| 160 | return 0 unless defined $allowed; | ||
| 161 | return !(grep { uc($_) eq uc($line_id) } @$allowed); | ||
| 162 | } | ||
| 163 | |||
| 164 | =head2 render([ $line_name] [, more_parameters...] ) | ||
| 165 | |||
| 166 | All parameters are optional. If the C<$line_name> is passed in, the method returns | ||
| 167 | a map for the given line. Otherwise you get the entire tube map. | ||
| 168 | |||
| 169 | There are a number of parameters that influence the rendering process and the result: | ||
| 170 | |||
| 171 | =over 4 | ||
| 172 | |||
| 173 | =item * C<format =E<gt> ...> | ||
| 174 | |||
| 175 | By default, output is in the PNG bitmap format. This can be changed by passing | ||
| 176 | the C<format=> parameter. GraphViz supports many output formats, among them SVG | ||
| 177 | and PDF and the native GraphViz DOT (or GV) format. While DOT and GV are | ||
| 178 | formally identical, this module makes an important distinction: GV yields the | ||
| 179 | raw, un-layouted graph description, whereas DOT yields a graph description where | ||
| 180 | each element is already precisely placed. -- For a list of supported formats, | ||
| 181 | see the L<list_formats()> method. | ||
| 182 | |||
| 183 | =item * C<driver =E<gt> ...> | ||
| 184 | |||
| 185 | GraphViz also supports a number of layout engines, or drivers, which can be | ||
| 186 | customised through the C<driver=> parameter. Usually, the C<dot> driver (which | ||
| 187 | is the default) or the C<neato> driver will produce good results. -- For a | ||
| 188 | list of supported drivers, see the L<list_drivers()> method. | ||
| 189 | |||
| 190 | =item * C<output_file =E<gt> ...> | ||
| 191 | |||
| 192 | If the C<output_file> parameter passes in the name of a file, output will be | ||
| 193 | written to a file of this name. If its value is C<undef>, a name is | ||
| 194 | automatically chosen, based on the name of the map or the chosen line. If the | ||
| 195 | parameter is missing, no output will be written. In this case, the calling | ||
| 196 | software is responsible for further processing of the output. In particular, | ||
| 197 | when writing binary output, care must be taken to open the output file in | ||
| 198 | binary mode on systems (like Windows) that differentiate between text and binary | ||
| 199 | files. | ||
| 200 | |||
| 201 | =item * C<base64 =E<gt> 0|1> | ||
| 202 | |||
| 203 | Most of the output formats will produce a binary stream. If the output is meant | ||
| 204 | to be embedded in a text file (like a web page source), it is useful to | ||
| 205 | transform it into a purely textual representation. This can be achieved by | ||
| 206 | passing C<base64 =E<gt> 1>. The default is 0. | ||
| 207 | |||
| 208 | =item * C<overlap =E<gt> 0|1> | ||
| 209 | |||
| 210 | Some drivers (notably, C<neato>, C<fdp>, and C<sfdp>) may produce diagrams | ||
| 211 | where station nodes overlap, unless told otherwise. The default for L<Map::Tube> | ||
| 212 | graphs is indeed to avoid such overlaps, equivalent to C<overlap =E<gt> 0>. You | ||
| 213 | can explicitly allow overlaps by passing C<overlap =E<gt> 1>. (This may be | ||
| 214 | necessary if your implementation of the external GraphViz binary (C<dot>) does | ||
| 215 | not fully support the non-overlapping feature.) | ||
| 216 | |||
| 217 | =item * C<line_name =E<gt> ...> | ||
| 218 | |||
| 219 | The name of the tube line (if any) may be given as the first parameter, as shown | ||
| 220 | above, but also, alternatively, as a named parameter C<line_name=>. If both are | ||
| 221 | given, the value of the first parameter will take precedence. | ||
| 222 | |||
| 223 | =back | ||
| 224 | |||
| 225 | In scalar context, the method will return the representation of the graph | ||
| 226 | in the desired format (which may be binary). In array context, the method will | ||
| 227 | return an array of two elements: first the repesentation of the graph, then the | ||
| 228 | name of the output file (or undef if no file output was requested). | ||
| 229 | |||
| 230 | See L</SYNOPSIS> for more details on how this method can be used. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub render { | ||
| 235 | my ($self, @args) = @_; | ||
| 236 | my $line_name = ( scalar(@args) & 1 ) ? shift(@args) : undef; | ||
| 237 | my %args = @args; | ||
| 238 | $line_name //= $args{line_name} if exists $args{line_name}; | ||
| 239 | delete $args{line_name}; | ||
| 240 | my $base64 = $args{base64}; | ||
| 241 | delete $args{base64}; | ||
| 242 | my @result = defined $line_name ? graph_line_image($self, $line_name, %args) | ||
| 243 | : graph_map_image($self, %args); | ||
| 244 | $result[0] = encode_base64($result[0]) if $base64 && @result; | ||
| 245 | return wantarray ? @result : $result[0]; | ||
| 246 | } | ||
| 247 | |||
| 248 | =head2 as_png($line_name [, ...]) | ||
| 249 | |||
| 250 | The C<$line_name> parameter is optional. If it is passed, the method returns the | ||
| 251 | given line map as a PNG bitmap. Otherwise you get the entire map. | ||
| 252 | |||
| 253 | (In fact, this method is almost an alias for the L<render()> method. However, it always | ||
| 254 | returns only the graph representation, never an output file name.) | ||
| 255 | |||
| 256 | See "SYNOPSIS" for more details on how it can be used. | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub as_png { | ||
| 261 | my ($self, @args) = @_; | ||
| 262 | return scalar($self->render(@args)); | ||
| 263 | } | ||
| 264 | |||
| 265 | =head2 as_image($line_name, [, ...]) | ||
| 266 | |||
| 267 | The C<$line_name> parameter is optional. If it is passed, the method returns the | ||
| 268 | base64 encoded string of a PNG bitmap showing the given line map. Otherwise you | ||
| 269 | get the entire map as a base64 encoded string. | ||
| 270 | |||
| 271 | (In fact, this method is almost an alias for the L<render()> method. One | ||
| 272 | difference is that the C<base64=> parameter is set to TRUE. And it always | ||
| 273 | returns only the graph representation, never an output file name.) | ||
| 274 | |||
| 275 | See L</SYNOPSIS> for more details on how it can be used. | ||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | sub as_image { | ||
| 280 | my ($self, @args) = @_; | ||
| 281 | push( @args, base64 => 1 ); | ||
| 282 | return scalar($self->render(@args)); | ||
| 283 | } | ||
| 284 | |||
| 285 | =head2 list_drivers() | ||
| 286 | |||
| 287 | Returns a list of drivers (formatters) that GraphViz supports on your machine. | ||
| 288 | The basic C<dot> driver must be available via the search path for executables. | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | sub list_drivers { | ||
| 293 | return get_graphviz_supported('-K?'); | ||
| 294 | } | ||
| 295 | |||
| 296 | =head2 list_formats() | ||
| 297 | |||
| 298 | Returns a list of output formats that GraphViz supports on your machine. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub list_formats { | ||
| 303 | return get_graphviz_supported('-T?'); | ||
| 304 | } | ||
| 305 | |||
| 306 | =head1 AUTHOR | ||
| 307 | |||
| 308 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||
| 309 | |||
| 310 | =head1 REPOSITORY | ||
| 311 | |||
| 312 | L<https://github.com/manwar/Map-Tube-Plugin-Graph> | ||
| 313 | |||
| 314 | =head1 SEE ALSO | ||
| 315 | |||
| 316 | =over 4 | ||
| 317 | |||
| 318 | =item * L<Map::Tube::GraphViz> | ||
| 319 | |||
| 320 | =item * L<Map::Metro::Graph> | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | =head1 CONTRIBUTORS | ||
| 325 | |||
| 326 | =over 2 | ||
| 327 | |||
| 328 | =item * Gisbert W. Selke | ||
| 329 | |||
| 330 | =item * Ed J | ||
| 331 | |||
| 332 | =back | ||
| 333 | |||
| 334 | =head1 BUGS | ||
| 335 | |||
| 336 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Map-Tube-Plugin-Graph/issues>. | ||
| 337 | I will be notified and then you'll automatically be notified of progress on your | ||
| 338 | bug as I make changes. | ||
| 339 | |||
| 340 | =head1 SUPPORT | ||
| 341 | |||
| 342 | You can find documentation for this module with the perldoc command. | ||
| 343 | |||
| 344 | perldoc Map::Tube::Plugin::Graph | ||
| 345 | |||
| 346 | You can also look for information at: | ||
| 347 | |||
| 348 | =over 4 | ||
| 349 | |||
| 350 | =item * BUG Report | ||
| 351 | |||
| 352 | L<https://github.com/manwar/Map-Tube-Plugin-Graph/issues> | ||
| 353 | |||
| 354 | =item * Search MetaCPAN | ||
| 355 | |||
| 356 | L<https://metacpan.org/dist/Map-Tube-Plugin-Graph> | ||
| 357 | |||
| 358 | =back | ||
| 359 | |||
| 360 | =head1 LICENSE AND COPYRIGHT | ||
| 361 | |||
| 362 | Copyright (C) 2015 - 2026 Mohammad Sajid Anwar. | ||
| 363 | |||
| 364 | This program is free software; you can redistribute it and / or modify it under | ||
| 365 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||
| 366 | license at: | ||
| 367 | |||
| 368 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 369 | |||
| 370 | Any use, modification, and distribution of the Standard or Modified Versions is | ||
| 371 | governed by this Artistic License.By using, modifying or distributing the Package, | ||
| 372 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||
| 373 | not accept this license. | ||
| 374 | |||
| 375 | If your Modified Version has been derived from a Modified Version made by someone | ||
| 376 | other than you,you are nevertheless required to ensure that your Modified Version | ||
| 377 | complies with the requirements of this license. | ||
| 378 | |||
| 379 | This license does not grant you the right to use any trademark, service mark, | ||
| 380 | tradename, or logo of the Copyright Holder. | ||
| 381 | |||
| 382 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||
| 383 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||
| 384 | Package with respect to any patent claims licensable by the Copyright Holder that | ||
| 385 | are necessarily infringed by the Package. If you institute patent litigation | ||
| 386 | (including a cross-claim or counterclaim) against any party alleging that the | ||
| 387 | Package constitutes direct or contributory patent infringement,then this Artistic | ||
| 388 | License to you shall terminate on the date that such litigation is filed. | ||
| 389 | |||
| 390 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||
| 391 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||
| 392 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||
| 393 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||
| 394 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||
| 395 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||
| 396 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 397 | |||
| 398 | =cut | ||
| 399 | |||
| 400 | 1; # End of Map::Tube::Plugin::Graph | ||