diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2024-04-07 11:18:04 +0200 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2024-04-07 11:18:04 +0200 |
commit | 875ec1c3e551b9d31c64fd67a83738dbfa90b86d (patch) | |
tree | 470a7dfb321788daf3c3405b86e0edad2d6ebf84 | |
parent | c9e5bfae620c3df002c286b1463a2838395797fd (diff) |
Request polylines for HAFAS journeys as well; only store sensible ones2.6.5
Decreases severity of #99
Fixes #108
-rwxr-xr-x | lib/Travelynx.pm | 92 | ||||
-rw-r--r-- | lib/Travelynx/Helper/HAFAS.pm | 2 |
2 files changed, 92 insertions, 2 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 6d371b5..6bf9f02 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -536,7 +536,10 @@ sub startup { my $promise = Mojo::Promise->new; - $self->hafas->get_journey_p( trip_id => $train_id )->then( + $self->hafas->get_journey_p( + trip_id => $train_id, + with_polyline => 1 + )->then( sub { my ($journey) = @_; my $found; @@ -579,6 +582,93 @@ sub startup { data => { trip_id => $journey->id } ); + my $polyline; + if ( $journey->polyline ) { + my @station_list; + my @coordinate_list; + for my $coord ( $journey->polyline ) { + if ( $coord->{name} ) { + push( + @coordinate_list, + [ + $coord->{lon}, $coord->{lat}, + $coord->{eva} + ] + ); + push( @station_list, $coord->{name} ); + } + else { + push( @coordinate_list, + [ $coord->{lon}, $coord->{lat} ] ); + } + } + + # equal length → polyline only consists of straight + # lines between stops. that's not helpful. + if ( @station_list == @coordinate_list ) { + $self->log->debug( 'Ignoring polyline for ' + . $journey->line + . ' as it only consists of straight lines between stops.' + ); + } + else { + $polyline = { + from_eva => ( $journey->route )[0]->loc->eva, + to_eva => ( $journey->route )[-1]->loc->eva, + coords => \@coordinate_list, + }; + } + } + + if ($polyline) { + my $coords = $polyline->{coords}; + my $from_eva = $polyline->{from_eva}; + my $to_eva = $polyline->{to_eva}; + + my $polyline_str = JSON->new->encode($coords); + + my $pl_res = $db->select( + 'polylines', + ['id'], + { + origin_eva => $from_eva, + destination_eva => $to_eva, + polyline => $polyline_str + }, + { limit => 1 } + ); + + my $polyline_id; + if ( my $h = $pl_res->hash ) { + $polyline_id = $h->{id}; + } + else { + eval { + $polyline_id = $db->insert( + 'polylines', + { + origin_eva => $from_eva, + destination_eva => $to_eva, + polyline => $polyline_str + }, + { returning => 'id' } + )->hash->{id}; + }; + if ($@) { + $self->log->warn( + "add_route_timestamps: insert polyline: $@" + ); + } + } + if ($polyline_id) { + $self->in_transit->set_polyline_id( + uid => $uid, + db => $db, + polyline_id => $polyline_id + ); + } + } + # mustn't be called during a transaction if ( not $opt{in_transaction} ) { $self->run_hook( $uid, 'checkin' ); diff --git a/lib/Travelynx/Helper/HAFAS.pm b/lib/Travelynx/Helper/HAFAS.pm index 2bab8dc..7671d78 100644 --- a/lib/Travelynx/Helper/HAFAS.pm +++ b/lib/Travelynx/Helper/HAFAS.pm @@ -172,7 +172,7 @@ sub get_journey_p { journey => { id => $opt{trip_id}, }, - with_polyline => 0, + with_polyline => $opt{with_polyline}, cache => $self->{realtime_cache}, promise => 'Mojo::Promise', user_agent => $self->{user_agent}->request_timeout(10), |