| 1 | package Geo::Routing::Driver::OSRM; | ||
| 2 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | $Geo::Routing::Driver::OSRM::AUTHORITY = 'cpan:AVAR'; | |
| 4 | } | ||
| 5 | 1 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | $Geo::Routing::Driver::OSRM::VERSION = '0.11'; | |
| 7 | } | ||
| 8 | 1 | ▶ | use Any::Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings FATAL => "all"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Any::Moose '::Util::TypeConstraints'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Text::Trim; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use Geo::Routing::Driver::OSRM::Route; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use JSON::XS qw(decode_json); | ||
| 14 | |||
| 15 | with qw(Geo::Routing::Role::Driver); | ||
| 16 | |||
| 17 | has osrm_path => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'Str', | ||
| 20 | required => 1, | ||
| 21 | documentation => "The base URL of a HTTP with OSRM instance we can send queries to", | ||
| 22 | ); | ||
| 23 | |||
| 24 | has use_curl => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'Bool', | ||
| 27 | default => 1, | ||
| 28 | documentation => "Should we shell out to curl(1) to get http content?", | ||
| 29 | ); | ||
| 30 | |||
| 31 | enum OSRMQueryMethod => qw( | ||
| 32 | json | ||
| 33 | xml | ||
| 34 | ); | ||
| 35 | |||
| 36 | has query_method => ( | ||
| 37 | is => 'ro', | ||
| 38 | isa => 'OSRMQueryMethod', | ||
| 39 | default => 'json', | ||
| 40 | documentation => "Should we make XML or JSON requests?", | ||
| 41 | ); | ||
| 42 | |||
| 43 | sub route { | ||
| 44 | my ($self, $query) = @_; | ||
| 45 | |||
| 46 | my $method = $self->query_method; | ||
| 47 | |||
| 48 | # Get the XML content | ||
| 49 | my $query_string = $query->query_string($method); | ||
| 50 | my $mech = $self->_mech; | ||
| 51 | my $url = sprintf "%s%s", $self->osrm_path, $query_string; | ||
| 52 | my $content; | ||
| 53 | if ($self->use_curl) { | ||
| 54 | chomp($content = qx[curl -s '$url']); | ||
| 55 | } else { | ||
| 56 | $mech->get($url); | ||
| 57 | $content = $mech->content; | ||
| 58 | } | ||
| 59 | |||
| 60 | return unless $content; | ||
| 61 | |||
| 62 | my $parsed; | ||
| 63 | if ($method eq 'xml') { | ||
| 64 | if ($content =~ m[<Document>\s*</Document>]s) { | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | my ($distance, $duration) = $content =~ / | ||
| 69 | Distance: \s+ ([0-9]+) .*? m | ||
| 70 | .*? | ||
| 71 | ([0-9]+) \s+ minutes | ||
| 72 | /x; | ||
| 73 | |||
| 74 | $parsed = { | ||
| 75 | distance => ($distance / 1000), | ||
| 76 | travel_time => ($duration * 60), | ||
| 77 | points => [], | ||
| 78 | }; | ||
| 79 | } elsif ($method eq 'json') { | ||
| 80 | my $json = decode_json($content); | ||
| 81 | |||
| 82 | return if $json->{status} eq '207'; | ||
| 83 | |||
| 84 | my $route_summary = $json->{route_summary}; | ||
| 85 | my ($distance, $duration) = @$route_summary{qw(total_distance total_time)}; | ||
| 86 | |||
| 87 | $parsed = { | ||
| 88 | distance => ($distance / 1000), | ||
| 89 | travel_time => ($duration), | ||
| 90 | points => $json->{route_geometry}, | ||
| 91 | }; | ||
| 92 | } | ||
| 93 | |||
| 94 | my $route = Geo::Routing::Driver::OSRM::Route->new(%$parsed); | ||
| 95 | |||
| 96 | return $route; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||