Routing/Driver/Gosmore.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 93.7% 30 / 32 CC 1 SCAR 0.3
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Routing/Driver/OSRM.pm ↑ Summary Routing/Role/Query.pm »
1 package Geo::Routing::Driver::Gosmore;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Geo::Routing::Driver::Gosmore::AUTHORITY = 'cpan:AVAR';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Geo::Routing::Driver::Gosmore::VERSION = '0.11';
7 }
8 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Any::Moose '::Util::TypeConstraints';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings FATAL => "all";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶use Geo::Routing::Driver::Gosmore::Route;
Subroutine BEGIN: called CC=1 SCAR=2.6
15  
16 with qw(Geo::Routing::Role::Driver);
17  
18 =head1 NAME
19  
20 Geo::Routing::Driver::Gosmore - Gosmore driver for L<Geo::Routing>
21  
22 =head1 SYNOPSIS
23  
24 First install L<gosmore(1)>, e.g. on Debian:
25  
26 sudo aptitude install gosmore
27  
28 Then build a F<gosmore.pak> file:
29  
30 wget http://download.geofabrik.de/osm/europe/british_isles.osm.bz2
31 # pv(1) is not needed, it just shows you the import progress
32 bzcat british_isles.osm.bz2 | pv | gosmore rebuild
33  
34 Then use this library, with C<gosmore_path> being the full path to
35 your new F<gosmore.pak>.
36  
37 my $gosmore = Geo::Routing->new(
38 gosmore_path => $gosmore_path,
39 );
40  
41 my $query = Geo::Gosmore::Query->new(
42 flat => '51.5425',
43 flon => '-0.111',
44 tlat => '51.5614',
45 tlon => '-0.0466',
46 fast => 1,
47 v => 'motorcar',
48 );
49  
50 # Returns false if we can't find a route
51 my $route = $gosmore->route($query);
52 my $distance = $route->distance;
53  
54 =head1 DESCRIPTION
55  
56 Provides an interface to the headless version of the
57 L<gosmore|http://wiki.openstreetmap.org/wiki/Gosmore> routing
58 library. When compiled with headless support it provides a simple
59 interface to do routing. This library just parses its simple output
60 and provides accessors for it.
61  
62 We also support accessing the headless L<gosmore(1)> program through a
63 remote CGI interface.
64  
65 =head2 ATTRIBUTES
66  
67 =head2 gosmore_method
68  
69 Either C<binary> or C<http>. If binary L</gosmore_path> is a path to a
70 F<gosmore.pak> and we'll invoke L<gosmore(1)> from your C<$PATH>.
71  
72 If it's C<http> L</gosmore_path> is a URL to an online gosmore router.
73  
74 =cut
75  
76 enum GosmoreMethod => qw(
77 binary
78 http
79 );
80  
81 has gosmore_method => (
82 is => 'ro',
83 isa => 'GosmoreMethod',
84 required => 1,
85 default => "binary",
86 documentation => "The gosmore method to use. Either 'binary' or 'http'",
87 );
88  
89 =head2 gosmore_path
90  
91 Either a path to a F<gosmore.pak> (see L</gosmore_method>) or a HTTP
92 URL to a gosmore CGI routing script without query parameters.
93  
94 =cut
95  
96 has gosmore_path => (
97 is => 'ro',
98 isa => 'Str',
99 required => 1,
100 documentation => "The full path to the gosmore.pak file to use, or a HTTP URL with a gosmore instance we can send queries to",
101 );
102  
103 has _gosmore_dirname => (
104 is => 'ro',
105 isa => 'Str',
106 documentation => "The full path to the directory the gosmore.pak file is in",
107 lazy_build => 1,
108 );
109  
110 sub _build__gosmore_dirname {
111 my ($self) = @_;
112  
113 my $gosmore_path = $self->gosmore_path;
114 my $gosmore_dirname = dirname($gosmore_path);
115  
116 return $gosmore_dirname;
117 }
118  
119 sub route {
120 my ($self, $query) = @_;
121  
122 my $lines = $self->_get_normalized_routing_lines($query);
123  
124 my @points;
125 for my $line (@$lines) {
126 # We couldn't find a route
127 return if $line eq 'No route found';
128  
129 print STDERR "$line\n" if $ENV{DEBUG};
130  
131 # We're getting a stream of lat/lon values
132 next unless $line =~ /^[0-9]/;
133  
134 my ($lat, $lon, $junction_type, $style, $remaining_time, $name) = split /,/, $line;
135 push @points => [ $lat, $lon, $junction_type, $style, $remaining_time, $name ];
136 }
137  
138 my $route = Geo::Routing::Driver::Gosmore::Route->new(
139 points => \@points,
140 );
141  
142 return $route;
143 }
144  
145 sub _get_normalized_routing_lines {
146 my ($self, $query) = @_;
147  
148 my $method = $self->gosmore_method;
149 my @lines;
150  
151 my $query_string = $query->query_string;
152 if ($method eq 'binary') {
153 my $gosmore_dirname = $self->_gosmore_dirname;
154  
155 local $ENV{QUERY_STRING} = $query_string;
156 local $ENV{LC_NUMERIC} = "en_US";
157 my $current_dirname = getcwd();
158 chdir $gosmore_dirname;
159 open my $gosmore, "gosmore |";
160 chdir $current_dirname;
161  
162 while (my $line = <$gosmore>) {
163 # Skip the HTTP header
164 next if $. == 1 || $. == 2;
165  
166 $line =~ s/[[:cntrl:]]//g;
167  
168 push @lines => $line;
169 }
170  
171 } elsif ($method eq 'http') {
172 my $mech = $self->_mech;
173 my $url = sprintf "%s?%s", $self->gosmore_path, $query_string;
174 $mech->get($url);
175 my $content = $mech->content;
176 use Data::Dumper;
177 @lines = map {
178 s/[[:cntrl:]]//g;
179 $_;
180 } split /\n/, $content;
181 }
182  
183 return \@lines;
184 }
185  
186  
187 1;
« Routing/Driver/OSRM.pm ↑ Summary Routing/Role/Query.pm »