diff options
author | Daniel Friesel <daniel.friesel@uos.de> | 2020-01-29 18:48:58 +0100 |
---|---|---|
committer | Daniel Friesel <daniel.friesel@uos.de> | 2020-01-29 18:48:58 +0100 |
commit | 993f6be6c9524031d878f2c83dcba1fdde11d060 (patch) | |
tree | f0d76b331a5a0e8092c68dab4e9de3d67cabbdf3 /lib | |
parent | 6b1aa9cd3905ac18ef0c21bea37d638fc48ef17d (diff) |
map: set bounds on stations; move polyline feature to staging1.14.1
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/Travelynx.pm | 14 | ||||
-rwxr-xr-x | lib/Travelynx/Controller/Traveling.pm | 75 |
2 files changed, 80 insertions, 9 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm index 8e21f16..f44bb8a 100755 --- a/lib/Travelynx.pm +++ b/lib/Travelynx.pm @@ -2785,6 +2785,10 @@ sub startup { # Otherwise, we grab a fresh one. my $db = $opt{db} // $self->pg->db; + my @select + = ( + qw(journey_id train_type train_line train_no checkin_ts sched_dep_ts real_dep_ts dep_eva checkout_ts sched_arr_ts real_arr_ts arr_eva edited route messages user_data) + ); my %where = ( user_id => $uid, cancelled => 0 @@ -2812,9 +2816,13 @@ sub startup { -between => [ $opt{after}->epoch, $opt{before}->epoch, ] }; } + if ( $opt{with_polyline} ) { + push( @select, 'polyline' ); + } + my @travels; - my $res = $db->select( 'journeys_str', '*', \%where, \%order ); + my $res = $db->select( 'journeys_str', \@select, \%where, \%order ); for my $entry ( $res->expand->hashes->each ) { @@ -2837,6 +2845,10 @@ sub startup { user_data => $entry->{user_data}, }; + if ( $opt{with_polyline} ) { + $ref->{polyline} = $entry->{polyline}; + } + if ( my $station = $self->app->station_by_eva->{ $ref->{from_eva} } ) { diff --git a/lib/Travelynx/Controller/Traveling.pm b/lib/Travelynx/Controller/Traveling.pm index 8d1a901..29af8f2 100755 --- a/lib/Travelynx/Controller/Traveling.pm +++ b/lib/Travelynx/Controller/Traveling.pm @@ -3,7 +3,7 @@ use Mojo::Base 'Mojolicious::Controller'; use DateTime; use DateTime::Format::Strptime; -use List::Util qw(uniq); +use List::Util qw(uniq min max); use List::UtilsBy qw(uniq_by); use List::MoreUtils qw(first_index); use Travel::Status::DE::IRIS::Stations; @@ -439,7 +439,9 @@ sub map_history { my $location = $self->app->coordinates_by_station; - my @journeys = $self->get_user_travels; + my $with_polyline = $self->param('poly') ? 1 : 0; + + my @journeys = $self->get_user_travels( with_polyline => $with_polyline ); if ( not @journeys ) { $self->render( @@ -464,11 +466,56 @@ sub map_history { grep { exists $location->{$_} } @stations; my @station_pairs; + my @coord_pairs; my %seen; my @skipped_journeys; - for my $journey (@journeys) { + for my $journey ( grep { $_->{polyline} } @journeys ) { + my @polyline = @{ $journey->{polyline} }; + my $from_eva = $journey->{from_eva}; + my $to_eva = $journey->{to_eva}; + + my $from_index + = first_index { $_->[2] and $_->[2] == $from_eva } @polyline; + my $to_index = first_index { $_->[2] and $_->[2] == $to_eva } @polyline; + + if ( $from_index == -1 + or $to_index == -1 ) + { + # Fall back to route + delete $journey->{polyline}; + next; + } + + my $key + = $from_eva . '!' . $to_eva . '!' . $from_index . '!' . $to_index; + + if ( $seen{$key} ) { + next; + } + + $seen{$key} = 1; + + # direction does not matter at the moment + $key = $to_eva . '!' . $from_eva . '!' . $to_index . '!' . $from_index; + $seen{$key} = 1; + + @polyline = @polyline[ $from_index .. $to_index ]; + my $prev_coord = shift @polyline; + for my $coord (@polyline) { + push( + @coord_pairs, + [ + [ $prev_coord->[1], $prev_coord->[0] ], + [ $coord->[1], $coord->[0] ] + ] + ); + $prev_coord = $coord; + } + } + + for my $journey ( grep { not $_->{polyline} } @journeys ) { my @route = map { $_->[0] } @{ $journey->{route} }; @@ -545,6 +592,13 @@ sub map_history { my @routes; + my @lats = map { $_->[0][0] } @station_coordinates; + my @lons = map { $_->[0][1] } @station_coordinates; + my $min_lat = min @lats; + my $max_lat = max @lats; + my $min_lon = min @lons; + my $max_lon = max @lons; + $self->render( template => 'history_map', with_map => 1, @@ -552,12 +606,17 @@ sub map_history { station_coordinates => \@station_coordinates, polyline_groups => [ { - polylines => \@station_pairs, - color => '#673ab7', - opacity => 0.6, - fit_bounds => 1, + polylines => \@station_pairs, + color => '#673ab7', + opacity => $with_polyline ? 0.4 : 0.6, + }, + { + polylines => \@coord_pairs, + color => '#673ab7', + opacity => 0.9, } - ] + ], + bounds => [ [ $min_lat, $min_lon ], [ $max_lat, $max_lon ] ], ); } |