summaryrefslogtreecommitdiff
path: root/lib/DBInfoscreen/Controller/Map.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/DBInfoscreen/Controller/Map.pm')
-rw-r--r--lib/DBInfoscreen/Controller/Map.pm217
1 files changed, 90 insertions, 127 deletions
diff --git a/lib/DBInfoscreen/Controller/Map.pm b/lib/DBInfoscreen/Controller/Map.pm
index 63b8b40..bced612 100644
--- a/lib/DBInfoscreen/Controller/Map.pm
+++ b/lib/DBInfoscreen/Controller/Map.pm
@@ -1,6 +1,6 @@
package DBInfoscreen::Controller::Map;
-# Copyright (C) 2011-2020 Daniel Friesel
+# Copyright (C) 2011-2020 Birte Kristina Friesel
#
# SPDX-License-Identifier: AGPL-3.0-or-later
@@ -61,14 +61,14 @@ sub get_route_indexes {
# - position 4 seconds from now
# - ...
sub estimate_train_positions {
- my (%opt) = @_;
+ my ( $self, %opt ) = @_;
my $now = $opt{now};
- my $from_dt = $opt{from}{dep} // $opt{from}{arr};
- my $to_dt = $opt{to}{arr} // $opt{to}{dep};
- my $from_name = $opt{from}{name};
- my $to_name = $opt{to}{name};
+ my $from_dt = $opt{from}->dep // $opt{from}->arr;
+ my $to_dt = $opt{to}->arr // $opt{to}->dep;
+ my $from_name = $opt{from}->loc->name;
+ my $to_name = $opt{to}->loc->name;
my $route = $opt{route};
my $polyline = $opt{polyline};
@@ -135,16 +135,23 @@ sub estimate_train_positions {
}
}
else {
+ $self->log->debug(
+ "Did not find route indexes for $from_name → $to_name");
+ $self->log->debug(
+"Falling back to $opt{from}{lat} $opt{from}{lon} → $opt{to}{lat} $opt{to}{lon}"
+ );
for my $ratio (@completion_ratios) {
my $lat
- = $opt{from}{lat} + ( $opt{to}{lat} - $opt{from}{lat} ) * $ratio;
+ = $opt{from}->loc->lat
+ + ( $opt{to}->loc->lat - $opt{from}->loc->lat ) * $ratio;
my $lon
- = $opt{from}{lon} + ( $opt{to}{lon} - $opt{from}{lon} ) * $ratio;
+ = $opt{from}->loc->lon
+ + ( $opt{to}->loc->lon - $opt{from}->loc->lon ) * $ratio;
push( @train_positions, [ $lat, $lon ] );
}
return @train_positions;
}
- return [ $opt{to}{lat}, $opt{to}{lon} ];
+ return [ $opt{to}->loc->lat, $opt{to}->loc->lon ];
}
# Input:
@@ -172,17 +179,17 @@ sub estimate_train_positions2 {
for my $i ( 1 .. $#route ) {
if ( not $next_stop
- and ( $route[$i]{arr} // $route[$i]{dep} )
- and ( $route[ $i - 1 ]{dep} // $route[ $i - 1 ]{arr} )
- and $now > ( $route[ $i - 1 ]{dep} // $route[ $i - 1 ]{arr} )
- and $now < ( $route[$i]{arr} // $route[$i]{dep} ) )
+ and ( $route[$i]->arr // $route[$i]->dep )
+ and ( $route[ $i - 1 ]->dep // $route[ $i - 1 ]->arr )
+ and $now > ( $route[ $i - 1 ]->dep // $route[ $i - 1 ]->arr )
+ and $now < ( $route[$i]->arr // $route[$i]->dep ) )
{
# HAFAS does not provide delays for past stops
$self->backpropagate_delay( $route[ $i - 1 ], $route[$i] );
# (current position, future positons...) in 2 second steps
- @train_positions = estimate_train_positions(
+ @train_positions = $self->estimate_train_positions(
from => $route[ $i - 1 ],
to => $route[$i],
now => $now,
@@ -200,15 +207,15 @@ sub estimate_train_positions2 {
and $now <= ( $route[ $i - 1 ]{dep} // $route[ $i - 1 ]{arr} ) )
{
@train_positions
- = ( [ $route[ $i - 1 ]{lat}, $route[ $i - 1 ]{lon} ] );
+ = ( [ $route[ $i - 1 ]->loc->lat, $route[ $i - 1 ]->loc->lon ] );
$next_stop = {
type => 'present',
station => $route[ $i - 1 ],
};
}
$stop_distance_sum += $distance->distance_metal(
- $route[ $i - 1 ]{lat}, $route[ $i - 1 ]{lon},
- $route[$i]{lat}, $route[$i]{lon}
+ $route[ $i - 1 ]->loc->lat, $route[ $i - 1 ]->loc->lon,
+ $route[$i]->loc->lat, $route[$i]->loc->lon
) / 1000;
}
@@ -217,7 +224,7 @@ sub estimate_train_positions2 {
}
if ( @route and not $next_stop ) {
- @train_positions = ( [ $route[-1]{lat}, $route[-1]{lon} ] );
+ @train_positions = ( [ $route[-1]->loc->lat, $route[-1]->loc->lon ] );
$next_stop = {
type => 'present',
station => $route[-1]
@@ -240,12 +247,12 @@ sub route_to_ajax {
my @route_entries;
for my $stop (@stopovers) {
- my @stop_entries = ( $stop->{name} );
+ my @stop_entries = ( $stop->loc->name );
my $platform;
- if ( my $arr = $stop->{arr} and not $stop->{arr_cancelled} ) {
- my $delay = $stop->{arr_delay} // 0;
- $platform = $stop->{arr_platform};
+ if ( my $arr = $stop->arr and not $stop->arr_cancelled ) {
+ my $delay = $stop->arr_delay // 0;
+ $platform = $stop->platform;
push( @stop_entries, $arr->epoch, $delay );
}
@@ -253,9 +260,9 @@ sub route_to_ajax {
push( @stop_entries, q{}, q{} );
}
- if ( my $dep = $stop->{dep} and not $stop->{dep_cancelled} ) {
- my $delay = $stop->{dep_delay} // 0;
- $platform //= $stop->{dep_platform} // q{};
+ if ( my $dep = $stop->dep and not $stop->dep_cancelled ) {
+ my $delay = $stop->dep_delay // 0;
+ $platform //= $stop->platform // q{};
push( @stop_entries, $dep->epoch, $delay, $platform );
}
@@ -307,13 +314,26 @@ sub route {
my ($self) = @_;
my $trip_id = $self->stash('tripid');
my $line_no = $self->stash('lineno');
+ my $hafas = $self->param('hafas');
my $from_name = $self->param('from');
my $to_name = $self->param('to');
$self->render_later;
- $self->hafas->get_polyline_p( $trip_id, $line_no )->then(
+ my $service = 'DB';
+ if ( $hafas
+ and $hafas ne '1'
+ and Travel::Status::DE::HAFAS::get_service($hafas) )
+ {
+ $service = $hafas;
+ }
+
+ $self->hafas->get_polyline_p(
+ id => $trip_id,
+ line => $line_no,
+ service => $service
+ )->then(
sub {
my ($journey) = @_;
@@ -338,51 +358,51 @@ sub route {
# Prepare from/to markers and name/time/delay overlays for stations
for my $stop (@route) {
- my @stop_lines = ( $stop->{name} );
+ my @stop_lines = ( $stop->loc->name );
- if ( $from_name and $stop->{name} eq $from_name ) {
+ if ( $from_name and $stop->loc->name eq $from_name ) {
push(
@markers,
{
- lon => $stop->{lon},
- lat => $stop->{lat},
- title => $stop->{name},
+ lon => $stop->loc->lon,
+ lat => $stop->loc->lat,
+ title => $stop->loc->name,
icon => 'goldIcon',
}
);
}
- if ( $to_name and $stop->{name} eq $to_name ) {
+ if ( $to_name and $stop->loc->name eq $to_name ) {
push(
@markers,
{
- lon => $stop->{lon},
- lat => $stop->{lat},
- title => $stop->{name},
+ lon => $stop->loc->lon,
+ lat => $stop->loc->lat,
+ title => $stop->loc->name,
icon => 'greenIcon',
}
);
}
- if ( $stop->{platform} ) {
- push( @stop_lines, 'Gleis ' . $stop->{platform} );
+ if ( $stop->platform ) {
+ push( @stop_lines, 'Gleis ' . $stop->platform );
}
- if ( $stop->{arr} ) {
- my $arr_line = $stop->{arr}->strftime('Ankunft: %H:%M');
- if ( $stop->{arr_delay} ) {
- $arr_line .= sprintf( ' (%+d)', $stop->{arr_delay} );
+ if ( $stop->arr ) {
+ my $arr_line = $stop->arr->strftime('Ankunft: %H:%M');
+ if ( $stop->arr_delay ) {
+ $arr_line .= sprintf( ' (%+d)', $stop->arr_delay );
}
push( @stop_lines, $arr_line );
}
- if ( $stop->{dep} ) {
- my $dep_line = $stop->{dep}->strftime('Abfahrt: %H:%M');
- if ( $stop->{dep_delay} ) {
- $dep_line .= sprintf( ' (%+d)', $stop->{dep_delay} );
+ if ( $stop->dep ) {
+ my $dep_line = $stop->dep->strftime('Abfahrt: %H:%M');
+ if ( $stop->dep_delay ) {
+ $dep_line .= sprintf( ' (%+d)', $stop->dep_delay );
}
push( @stop_lines, $dep_line );
}
push( @station_coordinates,
- [ [ $stop->{lat}, $stop->{lon} ], [@stop_lines], ] );
+ [ [ $stop->loc->lat, $stop->loc->lon ], [@stop_lines], ] );
}
push(
@@ -397,6 +417,7 @@ sub route {
$self->render(
'route_map',
+ description => "Karte für " . $journey->name,
title => $journey->name,
hide_opts => 1,
with_map => 1,
@@ -405,12 +426,12 @@ sub route {
ajax_polyline => join( '|',
map { join( ';', @{$_} ) } @{ $train_pos->{positions} } ),
origin => {
- name => $journey->route_start,
- ts => ( $journey->route )[0]->{dep},
+ name => ( $journey->route )[0]->loc->name,
+ ts => ( $journey->route )[0]->dep,
},
destination => {
name => $journey->route_end,
- ts => ( $journey->route )[-1]->{arr},
+ ts => ( $journey->route )[-1]->arr,
},
train_no => $journey->number
? ( $journey->type . ' ' . $journey->number )
@@ -450,12 +471,25 @@ sub ajax_route {
my ($self) = @_;
my $trip_id = $self->stash('tripid');
my $line_no = $self->stash('lineno');
+ my $hafas = $self->param('hafas');
delete $self->stash->{layout};
$self->render_later;
- $self->hafas->get_polyline_p( $trip_id, $line_no )->then(
+ my $service = 'DB';
+ if ( $hafas
+ and $hafas ne '1'
+ and Travel::Status::DE::HAFAS::get_service($hafas) )
+ {
+ $service = $hafas;
+ }
+
+ $self->hafas->get_polyline_p(
+ id => $trip_id,
+ line => $line_no,
+ service => $service
+ )->then(
sub {
my ($journey) = @_;
@@ -477,13 +511,16 @@ sub ajax_route {
ajax_polyline => join( '|',
map { join( ';', @{$_} ) } @{ $train_pos->{positions} } ),
origin => {
- name => $journey->route_start,
- ts => ( $journey->route )[0]->{dep},
+ name => ( $journey->route )[0]->loc->name,
+ ts => ( $journey->route )[0]->dep,
},
destination => {
name => $journey->route_end,
- ts => ( $journey->route )[-1]->{arr},
+ ts => ( $journey->route )[-1]->arr,
},
+ train_no => $journey->number
+ ? ( $journey->type . ' ' . $journey->number )
+ : undef,
next_stop => $train_pos->{next_stop},
);
}
@@ -498,78 +535,4 @@ sub ajax_route {
)->wait;
}
-sub search {
- my ($self) = @_;
-
- my $t1 = $self->param('train1');
- my $t2 = $self->param('train2');
-
- my $t1_data;
- my $t2_data;
-
- my @requests;
-
- if ( not( $t1 and $t1 =~ m{^\S+\s+\d+$} )
- or ( $t2 and not $t2 =~ m{^\S+\s+\d+$} ) )
- {
- $self->render(
- 'trainsearch',
- title => 'Fahrtverlauf',
- hide_opts => 1,
- error => $t1
- ? "Züge müssen im Format 'Zugtyp Nummer' angegeben werden, z.B. 'RE 1234'"
- : undef,
- );
- return;
- }
-
- $self->render_later;
-
- push( @requests, $self->hafas->trainsearch_p( train_no => $t1 ) );
-
- if ($t2) {
- push( @requests, $self->hafas->trainsearch_p( train_no => $t2 ) );
- }
-
- Mojo::Promise->all(@requests)->then(
- sub {
- my ( $t1_data, $t2_data ) = @_;
-
- if ($t2_data) {
- $self->redirect_to(
- sprintf(
- "/intersection/%s,0;%s,0",
- $t1_data->[0]{trip_id},
- $t2_data->[0]{trip_id},
- )
- );
- }
- else {
- $self->redirect_to(
- sprintf( "/map/%s/0", $t1_data->[0]{trip_id}, ) );
- }
- }
- )->catch(
- sub {
- my ($err) = @_;
- $self->render(
- 'trainsearch',
- title => 'Fahrtverlauf',
- hide_opts => 1,
- error => $err
- );
- }
- )->wait;
-}
-
-sub search_form {
- my ($self) = @_;
-
- $self->render(
- 'trainsearch',
- title => 'Fahrtverlauf',
- hide_opts => 1,
- );
-}
-
1;