summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2025-03-22 21:23:16 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2025-03-22 21:23:16 +0100
commite896cdbf310ae63f2a03ed9e6ec58e0fbb4bacd9 (patch)
tree28e100c9590f91ce1b438b867a6f0d6c289a7888
parentc2f01f4c922344b975eb484ff0576134cd6c90d1 (diff)
Trip->polyline: augment (likely) stop coordinates with name and ID
Needs GIS::Distance; augmentation is skipped if it is not available
-rw-r--r--lib/Travel/Status/DE/EFA/Trip.pm50
1 files changed, 47 insertions, 3 deletions
diff --git a/lib/Travel/Status/DE/EFA/Trip.pm b/lib/Travel/Status/DE/EFA/Trip.pm
index f3a58bf..31938ec 100644
--- a/lib/Travel/Status/DE/EFA/Trip.pm
+++ b/lib/Travel/Status/DE/EFA/Trip.pm
@@ -57,13 +57,57 @@ sub polyline {
if ( $opt{fallback} and not @{ $self->{polyline_raw} // [] } ) {
# TODO add $_->{id} as well?
- return
- map { { lat => $_->{latlon}[0], lon => $_->{latlon}[1] } }
- $self->route;
+ return map {
+ {
+ lat => $_->{latlon}[0],
+ lon => $_->{latlon}[1],
+ name => $_->name,
+ id_num => $_->id_num,
+ id_code => $_->id_code
+ }
+ } $self->route;
}
$self->{polyline} = [ map { { lat => $_->[0], lon => $_->[1] } }
@{ $self->{polyline_raw} } ];
+ my $distance;
+
+ eval {
+ require GIS::Distance;
+ $distance = GIS::Distance->new;
+ };
+
+ if ($distance) {
+ my %min_dist;
+ for my $stop ( $self->route ) {
+ for my $polyline_index ( 0 .. $#{ $self->{polyline} } ) {
+ my $pl = $self->{polyline}[$polyline_index];
+ my $dist = $distance->distance_metal(
+ $stop->{latlon}[0],
+ $stop->{latlon}[1],
+ $pl->{lat}, $pl->{lon}
+ );
+ if ( not $min_dist{ $stop->{id_code} }
+ or $min_dist{ $stop->{id_code} }{dist} > $dist )
+ {
+ $min_dist{ $stop->{id_code} } = {
+ dist => $dist,
+ index => $polyline_index,
+ };
+ }
+ }
+ }
+ for my $stop ( $self->route ) {
+ if ( $min_dist{ $stop->{id_code} } ) {
+ $self->{polyline}[ $min_dist{ $stop->{id_code} }{index} ]{name}
+ = $stop->{name};
+ $self->{polyline}[ $min_dist{ $stop->{id_code} }{index} ]
+ {id_num} = $stop->{id_num};
+ $self->{polyline}[ $min_dist{ $stop->{id_code} }{index} ]
+ {id_code} = $stop->{id_code};
+ }
+ }
+ }
return @{ $self->{polyline} };
}