Unverified Commit b8a15268 authored by Birte Kristina Friesel's avatar Birte Kristina Friesel
Browse files

Add coord request mode

parent 98296b5a
Loading
Loading
Loading
Loading
+31 −1
Original line number Diff line number Diff line
@@ -95,7 +95,7 @@ if ($use_cache) {
@grep_mots      = split( qr{,}, join( q{,}, @grep_mots ) );
@grep_platforms = split( qr{,}, join( q{,}, @grep_platforms ) );

my ( $place, $input, $stopseq );
my ( $place, $input, $coord, $stopseq, $stopfinder );

if ( @ARGV == 1 ) {
	if ( $ARGV[0] =~ m{ ^ ([^@]*) @ ([^@]*) [(] ([^)]*) [)] (.*)  $ }x ) {
@@ -106,6 +106,17 @@ if ( @ARGV == 1 ) {
			key       => $4
		};
	}
	elsif ( $ARGV[0] =~ m{ ^ [?] (?<name> .*) $ }x ) {
		$stopfinder = {
			name => $+{name},
		};
	}
	elsif ( $ARGV[0] =~ m{ ^ (?<lat> [0-9.]+ ) : (?<lon> [0-9].+ ) $ }x ) {
		$coord = {
			lat => $+{lat},
			lon => $+{lon},
		};
	}
	else {
		$input = $ARGV[0];
	}
@@ -160,6 +171,8 @@ sub new_efa {
		full_routes    => $full_routes,
		place          => $place,
		name           => $input,
		coord          => $coord,
		stopfinder     => $stopfinder,
		stopseq        => $stopseq,
		time           => $time,
		type           => $input_type,
@@ -301,6 +314,17 @@ sub display_result {
	return;
}

sub show_coord {
	my $max_len = max map { length( $_->full_name ) } $efa->results;
	for my $stop ( $efa->results ) {
		printf(
			"%5.1f km  %-${max_len}s  %s\n",
			$stop->distance_m * 1e-3,
			$stop->full_name, $stop->id
		);
	}
}

sub show_stopseq {
	my $trip = $efa->result;

@@ -540,6 +564,12 @@ if ($json_output) {
		say JSON->new->convert_blessed->encode( [ $efa->results ] );
	}
}
elsif ($coord) {
	show_coord();
}
elsif ($stopfinder) {
	show_stopfinder();
}
elsif ($stopseq) {
	show_stopseq();
}
+76 −4
Original line number Diff line number Diff line
@@ -71,7 +71,14 @@ sub new {
		delete $opt{timeout};
	}

	if ( not( $opt{name} or $opt{stopseq} or $opt{from_json} ) ) {
	if (
		not(   $opt{coord}
			or $opt{name}
			or $opt{stopfinder}
			or $opt{stopseq}
			or $opt{from_json} )
	  )
	{
		confess('You must specify a name');
	}
	if ( $opt{type}
@@ -85,7 +92,13 @@ sub new {
			= Travel::Status::DE::EFA::Services::get_service( $opt{service} ) )
		{
			$opt{efa_url} = $service->{url};
			if ( $opt{stopseq} ) {
			if ( $opt{coord} ) {
				$opt{efa_url} .= '/XML_COORD_REQUEST';
			}
			elsif ( $opt{stopfinder} ) {
				$opt{efa_url} .= '/XML_STOPFINDER_REQUEST';
			}
			elsif ( $opt{stopseq} ) {
				$opt{efa_url} .= '/XML_STOPSEQCOORD_REQUEST';
			}
			else {
@@ -159,7 +172,33 @@ sub new {
		json => JSON->new->utf8,
	};

	if ( $opt{stopseq} ) {
	if ( $opt{coord} ) {

		# outputFormat => 'JSON' returns invalid JSON
		$self->{post} = {
			coord => sprintf( '%.7f:%.7f:%s',
				$opt{coord}{lon}, $opt{coord}{lat}, 'WGS84[DD.ddddd]' ),
			radius_1              => 1320,
			type_1                => 'STOP',
			coordListOutputFormat => 'list',
			max                   => 30,
			inclFilter            => 1,
			outputFormat          => 'rapidJson',
		};
	}
	elsif ( $opt{stopfinder} ) {

		# filter: 2 (stop) | 4 (street) | 8 (address) | 16 (crossing) | 32 (poi) | 64 (postcod)
		$self->{post} = {
			locationServerActive => 1,
			type_sf              => 'any',
			name                 => $opt{stopfinder}{name},
			anyObjFilter_sf      => 2,
			coordOutputFormat    => 'WGS84[DD.DDDDD]',
			outputFormat         => 'JSON',
		};
	}
	elsif ( $opt{stopseq} ) {

		# outputFormat => 'JSON' also works; leads to different output
		$self->{post} = {
@@ -489,14 +528,47 @@ sub parse_line {

sub results {
	my ($self) = @_;
	my @results;

	if ( $self->{results} ) {
		return @{ $self->{results} };
	}

	if ( $self->{post}{coord} ) {
		return $self->results_coord;
	}
	else {
		return $self->results_dm;
	}
}

sub results_coord {
	my ($self) = @_;
	my $json = $self->{response};

	my @results;
	for my $stop ( @{ $json->{locations} // [] } ) {
		push(
			@results,
			Travel::Status::DE::EFA::Stop->new(
				place      => $stop->{parent}{name},
				full_name  => $stop->{properties}{STOP_NAME_WITH_PLACE},
				distance_m => $stop->{properties}{distance},
				name       => $stop->{name},
				id         => $stop->{id},
			)
		);
	}

	$self->{results} = \@results;

	return @results;
}

sub results_dm {
	my ($self) = @_;
	my $json = $self->{response};

	my @results;
	for my $departure ( @{ $json->{departureList} // [] } ) {
		push(
			@results,
+6 −1
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ our $VERSION = '3.01';
Travel::Status::DE::EFA::Stop->mk_ro_accessors(
	qw(sched_arr rt_arr arr arr_delay
	  sched_dep rt_dep dep dep_delay
	  occupancy delay
	  occupancy delay distance_m
	  place name full_name id stop_id latlon
	  platform niveau)
);
@@ -117,6 +117,11 @@ first scheduled stop.
DateTime(3pm) object holding departure date and time. undef if this is the
final scheduled stop.

=item $stop->distance_m

Distance from request coordinates in meters. undef if the object has not
been obtained by means of a coord request.

=item $stop->id

Stop ID.