summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2020-01-05 11:09:08 +0100
committerDaniel Friesel <derf@finalrewind.org>2020-01-05 11:09:08 +0100
commit06e65334b39b210c367256d128df5de9e1b29425 (patch)
tree1c77e0c3e416e4b62c6c4baaa776c399c54fe0ef
parent7e825ba8a3480773e42823756355ff5e2367586c (diff)
map: show arr/dep times2.5.3
-rw-r--r--lib/DBInfoscreen/Controller/Map.pm67
-rw-r--r--templates/route_map.html.ep11
2 files changed, 64 insertions, 14 deletions
diff --git a/lib/DBInfoscreen/Controller/Map.pm b/lib/DBInfoscreen/Controller/Map.pm
index 01520b7..e85dfcb 100644
--- a/lib/DBInfoscreen/Controller/Map.pm
+++ b/lib/DBInfoscreen/Controller/Map.pm
@@ -4,7 +4,7 @@ use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw(decode_json);
use Mojo::Promise;
-use Encode qw(decode);
+use DateTime::Format::Strptime;
my $dbf_version = qx{git describe --dirty} || 'experimental';
@@ -31,8 +31,7 @@ sub get_hafas_polyline_p {
->then(
sub {
my ($tx) = @_;
- my $body = decode( 'utf-8', $tx->res->body );
- my $json = decode_json($body);
+ my $json = decode_json( $tx->res->body );
my @coordinate_list;
for my $feature ( @{ $json->{polyline}{features} } ) {
@@ -46,9 +45,9 @@ sub get_hafas_polyline_p {
}
my $ret = {
- name => $json->{line}{name} // '?',
- polyline => [@coordinate_list],
- stopovers => $json->{stopovers},
+ name => $json->{line}{name} // '?',
+ polyline => [@coordinate_list],
+ raw => $json,
};
$cache->freeze( $url, $ret );
@@ -79,6 +78,11 @@ sub route {
my @line_pairs;
my @station_coordinates;
+ my $strp = DateTime::Format::Strptime->new(
+ pattern => '%Y-%m-%dT%H:%M:%S.000%z',
+ time_zone => 'Europe/Berlin',
+ );
+
for my $i ( 1 .. $#polyline ) {
push(
@line_pairs,
@@ -89,7 +93,40 @@ sub route {
);
}
- for my $stop ( @{ $pl->{stopovers} // [] } ) {
+ for my $stop ( @{ $pl->{raw}{stopovers} // [] } ) {
+ my @stop_lines = ( $stop->{stop}{name} );
+ my $platform;
+
+ if ( $stop->{arrival}
+ and my $arrival
+ = $strp->parse_datetime( $stop->{arrival} ) )
+ {
+ my $delay = $stop->{arrivalDelay} // 0;
+ $platform //= $stop->{arrivalPlatform};
+ my $arr_line = $arrival->strftime('Ankunft: %H:%M');
+ if ($delay) {
+ $arr_line .= sprintf( ' (%+d)', $delay / 60 );
+ }
+ push( @stop_lines, $arr_line );
+ }
+
+ if ( $stop->{departure}
+ and my $departure
+ = $strp->parse_datetime( $stop->{departure} ) )
+ {
+ my $delay = $stop->{departureDelay} // 0;
+ $platform //= $stop->{departurePlatform};
+ my $dep_line = $departure->strftime('Abfahrt: %H:%M');
+ if ($delay) {
+ $dep_line .= sprintf( ' (%+d)', $delay / 60 );
+ }
+ push( @stop_lines, $dep_line );
+ }
+
+ if ($platform) {
+ splice( @stop_lines, 1, 0, "Gleis $platform" );
+ }
+
push(
@station_coordinates,
[
@@ -97,16 +134,24 @@ sub route {
$stop->{stop}{location}{latitude},
$stop->{stop}{location}{longitude}
],
- $stop->{stop}{name}
+ [@stop_lines],
]
);
}
$self->render(
'route_map',
- title => $pl->{name},
- hide_opts => 1,
- with_map => 1,
+ title => $pl->{name},
+ hide_opts => 1,
+ with_map => 1,
+ origin => {
+ name => $pl->{raw}{origin}{name},
+ ts => $strp->parse_datetime( $pl->{raw}{departure} ),
+ },
+ destination => {
+ name => $pl->{raw}{destination}{name},
+ ts => $strp->parse_datetime( $pl->{raw}{arrival} ),
+ },
polyline_groups => [
{
polylines => \@line_pairs,
diff --git a/templates/route_map.html.ep b/templates/route_map.html.ep
index 215444f..4ba5917 100644
--- a/templates/route_map.html.ep
+++ b/templates/route_map.html.ep
@@ -1,4 +1,9 @@
- <div class="container">
+<div class="container" style="margin-top: 1ex; margin-bottom: 1ex;">
+Fahrt von <strong><%= $origin->{name} %></strong>
+nach <strong><%= $destination->{name} %></strong>
+</div>
+
+<div class="container">
<div id="map" style="height: 500px;">
</div>
</div>
@@ -11,7 +16,7 @@ L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
}).addTo(map);
var stations = [
% for my $station ( @{stash('station_coordinates') // [] } ) {
-[[<%= $station->[0][0] %>,<%= $station->[0][1] %>],'<%= $station->[1] %>'],
+[[<%= $station->[0][0] %>,<%= $station->[0][1] %>],['<%== join("','", map { Mojo::Util::xml_escape($_) } @{$station->[1]}) %>']],
% }
];
@@ -38,7 +43,7 @@ for (var station_id in stations) {
fillColor: '#f03',
fillOpacity: 0.5,
radius: 250
- }).bindPopup(stations[station_id][1]).addTo(map);
+ }).bindPopup(stations[station_id][1].join('<br/>')).addTo(map);
}
</script>