AzimuthAero/RouteMap.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« AzimuthAero/Mock.pm ↑ Summary AzimuthAero/Utils.pm »
1 package WWW::AzimuthAero::RouteMap;
2 $WWW::AzimuthAero::RouteMap::VERSION = '0.3';
3  
4 # ABSTRACT: Route map representation
5  
6  
7 1 ▶use utf8; # important cause of L<WWW::AzimuthAero::RouteMap/neighbor_airports>
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Util qw/uniq first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use WWW::AzimuthAero::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Graph;
12 use Data::Dumper;
13  
14  
15 sub new {
16 my ( $self, $rm_raw ) = @_;
17  
18 confess "Wrong raw route map structure, not a hash"
19 unless ( ref($rm_raw) eq 'HASH' );
20  
21 bless { raw => $rm_raw }, $self;
22 }
23  
24  
25 sub raw {
26 return shift->{raw};
27 }
28  
29  
30 sub all_cities {
31 my ($self) = @_;
32 my @res;
33  
34 for my $v ( values %{ $self->raw } ) {
35 push @res, { NAME => $v->{NAME}, IATA => $v->{IATA} };
36 }
37  
38 return sort { lc( $a->{NAME} ) cmp lc( $b->{NAME} ) } @res;
39 }
40  
41  
42 sub get {
43 my ( $self, $what, $by, $val ) = @_;
44 my $city = first { $_->{$by} eq $val } $self->all_cities;
45 return $city->{$what};
46 }
47  
48 sub get_iata_by_azo {
49 my ( $self, $azo_code ) = @_;
50 return $self->raw->{$azo_code}{IATA};
51 }
52  
53  
54 sub route_map_iata {
55 my ( $self, @cities ) = @_;
56 my $res = {};
57 while ( my ( $azo_code, $data ) = each( %{ $self->raw } ) ) {
58 $res->{ $self->get_iata_by_azo($azo_code) } = [
59 sort { lc($a) cmp lc($b) }
60 map { $self->get_iata_by_azo($_) }
61 keys %{ $self->raw->{$azo_code}{ROUTES} }
62 ];
63 }
64  
65 if (@cities) {
66 use experimental 'smartmatch';
67 while ( my ( $k, $v ) = each(%$res) ) {
68 delete $res->{$k} unless ( $k ~~ @cities );
69 @$v = grep { $_ ~~ @cities } @$v;
70 }
71 }
72  
73 return $res;
74 }
75  
76  
77 sub neighbor_airports {
78 return {
79 'Ростов-на-Дону' => [qw/Краснодар/], # KRR
80 'Москва' => [qw/Калуга/], # KLG
81 'Санкт-Петербург' => [qw/Псков/] # PKV
82 };
83 }
84  
85  
86 sub get_neighbor_airports_iata {
87 my ( $self, $city_iata ) = @_;
88  
89 my $city = first { $_->{IATA} eq $city_iata } $self->all_cities;
90 return
91 map { $self->get( 'IATA', 'NAME', $_ ) }
92 @{ $self->neighbor_airports->{ $city->{NAME} } };
93  
94 }
95  
96  
97 sub transfer_routes {
98 my ( $self, %params ) = @_;
99  
100 my $raw = $self->route_map_iata();
101  
102 # warn "IATA map : ".Dumper $raw;
103  
104 my $g = Graph::Undirected->new;
105  
106 while ( my ( $from, $destinations ) = each(%$raw) ) {
107 for my $to (@$destinations) {
108 $g->add_edge( $from, $to ); # add_weighted_edge if price
109 }
110 }
111  
112 # print $g; # OK
113  
114 # внутри могут быть уже маршруты с пересадками, внимательнее!
115 return $self->_all_paths_btw_vertexes_w_l2(
116 graph => $g,
117 v1 => $params{from},
118 v2 => $params{to}
119 );
120  
121 }
122  
123 sub _all_paths_btw_vertexes_w_l2 {
124 my ( $sef, %params ) = @_;
125  
126 my $g = $params{graph};
127 my @res;
128  
129 #my ( $v1, $v2, $max_path_length ) = @_;
130 for my $neighbour_of_v1 ( $g->neighbours( $params{v1} ) ) {
131 next if $params{v2} eq $neighbour_of_v1; # direct route
132 for
133 my $neighbour_of_neighbour_of_v1 ( $g->neighbours($neighbour_of_v1) )
134 {
135 next
136 if $params{v1} eq
137 $neighbour_of_neighbour_of_v1; # ignore backlink to start
138 push @res, [ $params{v1}, $neighbour_of_v1, $params{v2} ]
139 if $params{v2} eq $neighbour_of_neighbour_of_v1;
140 }
141 }
142  
143 return [ uniq @res ];
144 }
145  
146 1;
147  
148 __END__
149  
150 =pod
151  
152 =encoding UTF-8
153  
154 =head1 NAME
155  
156 WWW::AzimuthAero::RouteMap - Route map representation
157  
158 =head1 VERSION
159  
160 version 0.3
161  
162 =head1 SYNOPSIS
163  
164 my $rm = WWW::AzimuthAero::RouteMap->new($route_map_raw_hash);
165 $rm->route_map_iata()
166  
167 =head1 DESCRIPTION
168  
169 https://azimuth.aero/ru/about/flight-map
170  
171 =head1 METHODS
172  
173 =head2 new
174  
175 my $rm = WWW::AzimuthAero::RouteMap->new($route_map_raw_hash);
176  
177 =head2 new
178  
179 Return hash with original route map parsed from site
180  
181 =head2 all_cities
182  
183 Return sorted list of city names in route map
184  
185 print $rm->all_cities()
186 print join(',' map { "\'". $_ ."\'" } $rm->all_cities() ); # for json array
187  
188 =head2 get
189  
190 Universal accessor function for route map, wrapper under L<WWW::AzimuthAero::RouteMap/all_cities>
191  
192 $rm->get( $which_property, $by_what_property, $what_property_val )
193  
194 Examples:
195  
196 $rm->get('IATA', 'NAME', 'Ростов-на-Дону')
197 $rm->get('IATA', 'AZO', 'РОВ')
198  
199 =head2 route_map_iata
200  
201 Return hash with IATA route map
202  
203 perl -Ilib -MWWW::AzimuthAero -MData::Dumper -e 'my $x = WWW::AzimuthAero->new->route_map->route_map_iata; warn Dumper $x;'
204  
205 Amount of cities
206  
207 my $x = WWW::AzimuthAero->new->route_map->route_map_iata; print scalar values %$x;
208  
209 Amount of all routes
210  
211 perl -Ilib -MWWW::AzimuthAero -e 'my $x = WWW::AzimuthAero->new->route_map->route_map_iata; my $i = 0; $i+= scalar @$_ for values %$x; print $i;'
212  
213 Params:
214  
215 cities
216  
217 perl -Ilib -MWWW::AzimuthAero -MData::Dumper -e 'my $x = WWW::AzimuthAero->new->route_map->route_map_iata('ROV', 'LED', 'KRR'); warn Dumper $x;'
218  
219 =head2 neighbor_airports
220  
221 Return hash of airports that are no more than 4 hours by train from each other
222  
223 For now it's manually hardcoded
224  
225 {
226 'Ростов-на-Дону' => [qw/Краснодар/],
227 'Москва' => [qw/Калуга/],
228 'Санкт-Петербург' => [qw/Псков/]
229 };
230  
231 Cities are set by name, not IATA code, for convenience
232  
233 When you set new city please check it's availability at L<WWW::AzimuthAero::RouteMap/all_cities>
234  
235 TO-DO: check correctness with Yandex Maps API and RZD API
236  
237 =head2 get_neighbor_airports_iata
238  
239 Return list of IATA codes of neighbor airports based on L<WWW::AzimuthAero::RouteMap/neighbor_airports>
240  
241 $rm->get_neighbor_airports_iata('LED') # ( 'PKV' )
242  
243 =head2 transfer_routes
244  
245 Convert route map to L<Graph> object and return ARRAYref of routes with one transfer maximum
246  
247 perl -Ilib -MData::Dumper -MWWW::AzimuthAero -e 'my $x = WWW::AzimuthAero->new->route_map->transfer_routes; warn Dumper $x;'
248  
249 Params:
250  
251 # graph processing options :
252 # $params{max_edges} - 2 by default, hardcoded for now
253 # $params{check_neighbors}
254  
255 # source and destination cities :
256 # $params{from} +
257 # $params{to} +
258  
259 # schedule processing :
260 # $params{min}
261 # $params{max}
262 # $params{max_delay_days}
263  
264 =head1 AUTHOR
265  
266 Pavel Serikov <pavelsr@cpan.org>
267  
268 =head1 COPYRIGHT AND LICENSE
269  
270 This software is copyright (c) 2019 by Pavel Serikov.
271  
272 This is free software; you can redistribute it and/or modify it under
273 the same terms as the Perl 5 programming language system itself.
274  
275 =cut
« AzimuthAero/Mock.pm ↑ Summary AzimuthAero/Utils.pm »