summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2024-08-15 17:33:28 +0200
committerBirte Kristina Friesel <derf@finalrewind.org>2024-08-15 17:38:13 +0200
commit9b7013907b8e7aeb9703c841c33ef337bdfbcece (patch)
tree2f5f7f80984ead00eca52201d2617e1bc9c4b123
parentd156e3389ee4786b0f8dc3b6a4cc9da1245109b8 (diff)
stats, map: deal with multiple EVA IDs mapping to the same station name
-rwxr-xr-xlib/Travelynx.pm25
-rwxr-xr-xlib/Travelynx/Model/Journeys.pm74
2 files changed, 79 insertions, 20 deletions
diff --git a/lib/Travelynx.pm b/lib/Travelynx.pm
index 36824c3..b4a9e06 100755
--- a/lib/Travelynx.pm
+++ b/lib/Travelynx.pm
@@ -2136,6 +2136,31 @@ sub startup {
my $to_index
= first_index { $_->[2] and $_->[2] == $to_eva } @polyline;
+ # Work around inconsistencies caused by a multiple EVA IDs mapping to the same station name
+ if ( $from_index == -1 ) {
+ for my $entry ( @{ $journey->{route} // [] } ) {
+ if ( $entry->[0] eq $journey->{from_name} ) {
+ $from_eva = $entry->[1];
+ $from_index
+ = first_index { $_->[2] and $_->[2] == $from_eva }
+ @polyline;
+ last;
+ }
+ }
+ }
+
+ if ( $to_index == -1 ) {
+ for my $entry ( @{ $journey->{route} // [] } ) {
+ if ( $entry->[0] eq $journey->{to_name} ) {
+ $to_eva = $entry->[1];
+ $to_index
+ = first_index { $_->[2] and $_->[2] == $to_eva }
+ @polyline;
+ last;
+ }
+ }
+ }
+
if ( $from_index == -1
or $to_index == -1 )
{
diff --git a/lib/Travelynx/Model/Journeys.pm b/lib/Travelynx/Model/Journeys.pm
index 2a0a9ec..1d1761b 100755
--- a/lib/Travelynx/Model/Journeys.pm
+++ b/lib/Travelynx/Model/Journeys.pm
@@ -1126,6 +1126,39 @@ sub get_travel_distance {
->warn("Journey $journey->{id} has no from_name for EVA $from_eva");
}
+ # Work around inconsistencies caused by a multiple EVA IDs mapping to the same station name
+ if (
+ not List::MoreUtils::any { $_->[2] and $_->[2] == $from_eva }
+ @{ $polyline_ref // [] }
+ )
+ {
+ $self->{log}->debug(
+"Journey $journey->{id} from_eva ($from_eva) is not part of polyline"
+ );
+ for my $entry ( @{$route_ref} ) {
+ if ( $entry->[0] eq $from ) {
+ $from_eva = $entry->[1];
+ $self->{log}->debug("... setting to $from_eva");
+ last;
+ }
+ }
+ }
+ if (
+ not List::MoreUtils::any { $_->[2] and $_->[2] == $to_eva }
+ @{ $polyline_ref // [] }
+ )
+ {
+ $self->{log}->debug(
+ "Journey $journey->{id} to_eva ($to_eva) is not part of polyline");
+ for my $entry ( @{$route_ref} ) {
+ if ( $entry->[0] eq $to ) {
+ $to_eva = $entry->[1];
+ $self->{log}->debug("... setting to $to_eva");
+ last;
+ }
+ }
+ }
+
my $distance_polyline = 0;
my $distance_intermediate = 0;
my $geo = GIS::Distance->new();
@@ -1134,7 +1167,7 @@ sub get_travel_distance {
my @route = after_incl { $_->[0] eq $from } @{$route_ref};
@route = before_incl { $_->[0] eq $to } @route;
- if ( @route < 2 ) {
+ if ( @route < 2 or $route[-1][0] ne $to ) {
# I AM ERROR
return ( 0, 0, $distance_beeline );
@@ -1145,30 +1178,31 @@ sub get_travel_distance {
@polyline
= before_incl { $_->[2] and $_->[2] == $to_eva } @polyline;
- my $prev_station = shift @polyline;
- for my $station (@polyline) {
- $distance_polyline += $geo->distance_metal(
- $prev_station->[1], $prev_station->[0],
- $station->[1], $station->[0]
- );
- $prev_station = $station;
- }
-
- if ( not( defined $route[0][2]{lat} and defined $route[0][2]{lon} ) ) {
- return ( $distance_polyline, 0, $distance_beeline );
- }
-
- $prev_station = shift @route;
- for my $station (@route) {
- if ( defined $station->[2]{lat} and defined $station->[2]{lon} ) {
- $distance_intermediate += $geo->distance_metal(
- $prev_station->[2]{lat}, $prev_station->[2]{lon},
- $station->[2]{lat}, $station->[2]{lon}
+ # ensure that before_incl matched -- otherwise, @polyline is too long
+ if ( @polyline and $polyline[-1][2] == $to_eva ) {
+ my $prev_station = shift @polyline;
+ for my $station (@polyline) {
+ $distance_polyline += $geo->distance_metal(
+ $prev_station->[1], $prev_station->[0],
+ $station->[1], $station->[0]
);
$prev_station = $station;
}
}
+ if ( defined $route[0][2]{lat} and defined $route[0][2]{lon} ) {
+ my $prev_station = shift @route;
+ for my $station (@route) {
+ if ( defined $station->[2]{lat} and defined $station->[2]{lon} ) {
+ $distance_intermediate += $geo->distance_metal(
+ $prev_station->[2]{lat}, $prev_station->[2]{lon},
+ $station->[2]{lat}, $station->[2]{lon}
+ );
+ $prev_station = $station;
+ }
+ }
+ }
+
return ( $distance_polyline, $distance_intermediate, $distance_beeline );
}