MakeGraphViz/Map.pm

filter: Statementstmt 93.7% 30 / 32 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.3% 41 / 43 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« MakeGraphViz.pm ↑ Summary
1 1 ▶use 5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Dist::Zilla::Plugin::MapMetro::MakeGraphViz::Map;
5  
6 our $VERSION = '0.1104';
7  
8 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use MooseX::AttributeShortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Types::Standard qw/HashRef ArrayRef Str Maybe/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Types::Path::Tiny qw/Path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Map::Metro::Shim;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use String::CamelCase qw/decamelize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 0 ▶use GraphViz2;
Subroutine BEGIN: called CC=1 SCAR=2.6
17  
18 has map => (
19 is => 'rw',
20 isa => Str,
21 required => 1,
22 );
23  
24 has settings => (
25 is => 'rw',
26 isa => HashRef,
27 traits => ['Hash'],
28 init_arg => undef,
29 default => sub { { } },
30 handles => {
31 set_setting => 'set',
32 get_setting => 'get',
33 },
34 );
35 has hidden_positions => (
36 is => 'rw',
37 isa => ArrayRef,
38 traits => ['Array'],
39 init_arg => undef,
40 default => sub { [] },
41 handles => {
42 add_hidden => 'push',
43 all_hiddens => 'elements',
44 },
45 );
46 has generated_file => (
47 is => 'rw',
48 isa => Path,
49 predicate => 1,
50 init_arg => undef,
51 );
52  
53 sub BUILD {
54 my $self = shift;
55  
56 my $decamelized_map = decamelize($self->map);
57  
58 my @possible_graphvizfiles = (
59 path('share', 'graphviz.conf'),
60 path('share', $decamelized_map, "graphviz-$decamelized_map.conf")
61 );
62 my $graphvizfile = (grep { $_->exists } @possible_graphvizfiles)[0];
63  
64 my @possible_mapfiles = (
65 path('share', "map-$decamelized_map.metro"),
66 path('share', $decamelized_map, "map-$decamelized_map.metro")
67 );
68 my $mapfile = (grep { $_->exists } @possible_mapfiles)[0];
69 return if !defined $mapfile;
70  
71 my $graph = Map::Metro::Shim->new(filepath => $mapfile)->parse;
72  
73 my $customconnections = {};
74 if($graphvizfile) {
75 my $settings = $graphvizfile->slurp;
76 $settings =~ s{^#.*$}{}g;
77 $settings =~ s{\n}{ }g;
78  
79 for my $custom (split m/ +/ => $settings) {
80 if($custom =~ m{^(\d+)-(\d+):([\d\.]+)$}) {
81 my $origin_station_id = $1;
82 my $destination_station_id = $2;
83 my $len = $3;
84  
85 $self->set_setting(sprintf ('len-%s-%s', $origin_station_id, $destination_station_id), $len);
86 $self->set_setting(sprintf ('len-%s-%s', $destination_station_id, $origin_station_id), $len);
87 }
88 elsif($custom =~ m{^\*(\d+):(-?[\d\.]+,-?[\d\.]+)}) {
89 my $station_id = $1;
90 my $hidden_station_pos = $2;
91  
92 $self->add_hidden({ station_id => $station_id, pos => $hidden_station_pos });
93 }
94 elsif($custom =~ m{^(\d+):(-?\d+,-?\d+!?)$}) {
95 my $station_id = $1;
96 my $pos = $2;
97  
98 $self->set_setting(sprintf ('pos-%s', $station_id) => $pos);
99 }
100 elsif($custom =~ m{^!(\d+)-(\d+):(\d+)\^([\d\.]+)$}) {
101 my $origin_station_id = $1;
102 my $destination_station_id = $2;
103 my $connections = $3;
104 my $len = $4;
105  
106 $customconnections->{ $origin_station_id }{ $destination_station_id } = { connections => $connections, len => $len };
107 }
108 }
109 }
110  
111 my $viz = GraphViz2->new(
112 global => { directed => 0 },
113 graph => { epsilon => 0.00001, fontname => 'sans-serif', fontsize => 100, label => $self->map, labelloc => 'top' },
114 node => { shape => 'circle', fixedsize => 'true', width => 0.8, height => 0.8, penwidth => 3, fontname => 'sans-serif', fontsize => 20 },
115 edge => { penwidth => 5, len => 1.2 },
116 );
117 for my $station ($graph->all_stations) {
118 my %pos = $self->get_pos_for($station->id);
119 my %node = (name => $station->id, label => $station->id, %pos);
120 $viz->add_node(%node);
121 }
122  
123 for my $transfer ($graph->all_transfers) {
124 my %len = $self->get_len_for($transfer->origin_station->id, $transfer->destination_station->id);
125 $viz->add_edge(from => $transfer->origin_station->id, to => $transfer->destination_station->id, color => '#888888', style => 'dashed', %len);
126 }
127 for my $segment ($graph->all_segments) {
128 for my $line_id ($segment->all_line_ids) {
129 my $color = $graph->get_line_by_id($line_id)->color;
130 my $width = $graph->get_line_by_id($line_id)->width;
131 my %len = $self->get_len_for($segment->origin_station->id, $segment->destination_station->id);
132  
133 $viz->add_edge(from => $segment->origin_station->id,
134 to => $segment->destination_station->id,
135 color => $color,
136 penwidth => $width,
137 %len,
138 );
139 }
140 }
141 #* Custom connections (for better visuals)
142 my $invisible_station_id = 99000000;
143 for my $hidden ($self->all_hiddens) {
144 $viz->add_node(name => ++$invisible_station_id,
145 label => '',
146 ($ENV{'MMVIZDEBUG'} ? () : (style => 'invis')),
147 width => 0.1,
148 height => 0.1,
149 penwidth => 5,
150 color => '#ff0000',
151 pos => "$hidden->{'pos'}!",
152 );
153 $viz->add_edge(from => $invisible_station_id,
154 to => $hidden->{'station_id'},
155 color => $ENV{'MMVIZDEBUG'} ? '#ff0000' : '#ffffff',
156 penwidth => 5,
157 len => 1,
158 weight => 100,
159 );
160 }
161  
162  
163 for my $origin_station_id (keys %{ $customconnections }) {
164 for my $destination_station_id (keys %{ $customconnections->{ $origin_station_id }}) {
165 my $len = $customconnections->{ $origin_station_id }{ $destination_station_id }{'len'};
166 my $connection_count = $customconnections->{ $origin_station_id }{ $destination_station_id }{'connections'};
167  
168 my $previous_station_id = $origin_station_id;
169  
170 for my $extra_connection (1 .. $connection_count - 1) {
171 $viz->add_node(name => ++$invisible_station_id, label => '', style => 'invis', width => 0.1, height => 0.1, penwidth => 5, color => '#ff0000');
172  
173 $viz->add_edge(from => $previous_station_id,
174 to => $invisible_station_id,
175 color => '#ff0000',
176 penwidth => $ENV{'MMVIZDEBUG'} ? 1 : 0,
177 len => $len,
178 );
179  
180 $previous_station_id = $invisible_station_id;
181 }
182  
183 $viz->add_edge(from => $previous_station_id,
184 to => $destination_station_id,
185 color => '#ff0000',
186 penwidth => $ENV{'MMVIZDEBUG'} ? 1 : 0,
187 len => $len,
188 );
189 }
190 }
191  
192 my $image_dir = path(qw/static images/);
193 $image_dir->mkpath;
194 my $file = $image_dir->child(decamelize($self->map) . '.png');
195 $viz->run(format => 'png', output_file => $file->stringify, driver => 'neato');
196 $self->generated_file($file);
197 }
198  
199  
200 sub get_len_for {
201 my $self = shift;
202 my ($origin_station_id, $destination_station_id) = @_;
203 return (len => $self->get_setting("len-$origin_station_id-$destination_station_id")) if $self->get_setting("len-$origin_station_id-$destination_station_id");
204 return (len => $self->get_setting("len-$origin_station_id-0")) if $self->get_setting("len-$origin_station_id-0");
205 return (len => $self->get_setting("len-0-$destination_station_id")) if $self->get_setting("len-0-$destination_station_id");
206 return ();
207 }
208  
209 sub get_pos_for {
210 my $self = shift;
211 my $station_id = shift;
212 return (pos => $self->get_setting("pos-$station_id")) if $self->get_setting("pos-$station_id");
213 return ();
214 }
215  
216 __PACKAGE__->meta->make_immutable;
217  
218 1;
219  
220 __END__
« MakeGraphViz.pm ↑ Summary