blib/lib/Wiki/Toolkit/Plugin/Locator/Grid.pm

filter: Statementstmt 21.1% 15 / 71 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 36.3% 4 / 11 Podpod 100% 6 / 6 totaltotal 14.7% 25 / 170 CC 28 SCAR 49.5
CC
28
Cov
11%
find_within_distance
210.0
distance
182.0
_init
12.0
CRAP 140.9
↑ Summary
1 package Wiki::Toolkit::Plugin::Locator::Grid;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use vars qw( $VERSION @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $VERSION = '0.05';
7  
8 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Wiki::Toolkit::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 @ISA = qw( Wiki::Toolkit::Plugin );
12  
13 =head1 NAME
14  
15 Wiki::Toolkit::Plugin::Locator::Grid - A Wiki::Toolkit plugin to manage co-ordinate data.
16  
17 =head1 DESCRIPTION
18  
19 Access to and calculations using co-ordinate metadata supplied to a
20 Wiki::Toolkit wiki when writing a node.
21  
22 B<Note:> This is I<read-only> access. If you want to write to a node's
23 metadata, you need to do it using the C<write_node> method of
24 L<Wiki::Toolkit>.
25  
26 We assume that the points are located on a flat, square grid with unit
27 squares of side 1 metre.
28  
29 =head1 SYNOPSIS
30  
31 use Wiki::Toolkit;
32 use Wiki::Toolkit::Plugin::Locator::Grid;
33  
34 my $wiki = Wiki::Toolkit->new( ... );
35 my $locator = Wiki::Toolkit::Plugin::Locator::Grid->new;
36 $wiki->register_plugin( plugin => $locator );
37  
38 $wiki->write_node( "Jerusalem Tavern", "A good pub", $checksum,
39 { x => 531674, y => 181950 } ) or die "argh";
40  
41 # Just retrieve the co-ordinates.
42 my ( $x, $y ) = $locator->coordinates( node => "Jerusalem Tavern" );
43  
44 # Find the straight-line distance between two nodes, in metres.
45 my $distance = $locator->distance( from_node => "Jerusalem Tavern",
46 to_node => "Calthorpe Arms" );
47  
48 # Find all the things within 200 metres of a given place.
49 my @others = $locator->find_within_distance( node => "Albion",
50 metres => 200 );
51  
52 # Maybe our wiki calls the x and y co-ordinates something else.
53 my $locator = Wiki::Toolkit::Plugin::Locator::Grid->new(
54 x => "os_x",
55 y => "os_y",
56 );
57  
58 =head1 METHODS
59  
60 =over 4
61  
62 =item B<new>
63  
64 # By default we assume that x and y co-ordinates are stored in
65 # metadata called "x" and "y".
66 my $locator = Wiki::Toolkit::Plugin::Locator::Grid->new;
67  
68 # But maybe our wiki calls the x and y co-ordinates something else.
69 my $locator = Wiki::Toolkit::Plugin::Locator::Grid->new(
70 x => "os_x",
71 y => "os_y",
72 );
73  
74 =cut
75  
76 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
77 0 my $class = shift;
78 0 my $self = {};
79 0 bless $self, $class;
80 0 return $self->_init( @_ );
81 }
82  
83 0 ▶sub _init {
Subroutine _init: not called CC=3 SCAR=24.8
84 0 my ($self, %args) = @_;
85 0 ▶ $self->{x} = $args{x} || "x";
Condition0%0/2
$args{'x'} || 'x'
l!l
00
MC/DC0%0/1
$args{'x'} || 'x'
$args{'x'}
Truth table0%0/2
A
A= $args{'x'}
Aresult
11
00
86 0 ▶ $self->{y} = $args{y} || "y";
Condition0%0/2
$args{'y'} || 'y'
l!l
00
MC/DC0%0/1
$args{'y'} || 'y'
$args{'y'}
Truth table0%0/2
A
A= $args{'y'}
Aresult
11
00
87 0 return $self;
88 }
89  
90 =item B<x_field>
91  
92 my $x_field = $locator->x_field;
93  
94 An accessor, returns the name of the metadata field used to store the
95 x-coordinate.
96  
97 =cut
98  
99 0 ▶sub x_field {
Subroutine x_field: not called CC=1 SCAR=6.9
Pod documented
100 0 my $self = shift;
101 0 return $self->{x};
102 }
103  
104 =item B<y_field>
105  
106 my $y_field = $locator->y_field;
107  
108 An accessor, returns the name of the metadata field used to store the
109 y-coordinate.
110  
111 =cut
112  
113 0 ▶sub y_field {
Subroutine y_field: not called CC=1 SCAR=6.9
Pod documented
114 0 my $self = shift;
115 0 return $self->{y};
116 }
117  
118 =item B<coordinates>
119  
120 my ($x, $y) = $locator->coordinates( node => "Jerusalem Tavern" );
121  
122 Returns the x and y co-ordinates stored as metadata last time the node
123 was written.
124  
125 =cut
126  
127 0 ▶sub coordinates {
Subroutine coordinates: not called CC=1 SCAR=6.9
Pod documented
128 0 my ($self, %args) = @_;
129 0 my $store = $self->datastore;
130 # This is the slightly inefficient but neat and tidy way to do it -
131 # calling on as much existing stuff as possible.
132 0 my %node_data = $store->retrieve_node( $args{node} );
133 0 my %metadata = %{$node_data{metadata}};
134 0 return ($metadata{$self->{x}}[0], $metadata{$self->{y}}[0]);
135 }
136  
137 =item B<distance>
138  
139 # Find the straight-line distance between two nodes, in metres.
140 my $distance = $locator->distance( from_node => "Jerusalem Tavern",
141 to_node => "Calthorpe Arms" );
142  
143 # Or in kilometres, and between a node and a point.
144 my $distance = $locator->distance( from_x => 531467,
145 from_y => 183246,
146 to_node => "Duke of Cambridge",
147 unit => "kilometres" );
148  
149 Defaults to metres if C<unit> is not supplied or is not recognised.
150 Recognised units at the moment: C<metres>, C<kilometres>.
151  
152 Returns C<undef> if one of the endpoints does not exist, or does not
153 have both co-ordinates defined. The C<node> specification of an
154 endpoint overrides the x/y co-ords if both specified (but don't do
155 that).
156  
157 B<Note:> Works to the nearest metre. Well, actually, calls C<int> and
158 rounds down, but if anyone cares about that they can send a patch.
159  
160 =cut
161  
162 0 ▶sub distance {
Subroutine distance: not called CC=13 SCAR=52.0
Pod documented
163 0 my ($self, %args) = @_;
164  
165 0 ▶ $args{unit} ||= "metres";
Condition0%0/2
$args{"unit"} ||= "metres"
l!l
00
MC/DC0%0/1
$args{"unit"} ||= "metres"
$args{"unit"}
Truth table0%0/2
A
A= $args{"unit"}
Aresult
11
00
166 0 my (@from, @to);
167  
168 0 ▶ if ( $args{from_node} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'from_node'}) { } 0 0 0
elsif ($args{'from_x'} and $args{'from_y'}) { } 0 0 0
169 0 @from = $self->coordinates( node => $args{from_node} );
170 ▶ } elsif ( $args{from_x} and $args{from_y} ) {
Condition0%0/3
$args{"from_x"} and $args{"from_y"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"from_x"} and $args{"from_y"}
$args{"from_x"} $args{"from_y"}
Truth table0%0/3
A and B
A= $args{"from_x"}B= $args{"from_y"}
ABresult
0X0
100
111
171 0 @from = @args{ qw( from_x from_y ) };
172 }
173  
174 0 ▶ if ( $args{to_node} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'to_node'}) { } 0 0 0
elsif ($args{'to_x'} and $args{'to_y'}) { } 0 0 0
175 0 @to = $self->coordinates( node => $args{to_node} );
176 ▶ } elsif ( $args{to_x} and $args{to_y} ) {
Condition0%0/3
$args{"to_x"} and $args{"to_y"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"to_x"} and $args{"to_y"}
$args{"to_x"} $args{"to_y"}
Truth table0%0/3
A and B
A= $args{"to_x"}B= $args{"to_y"}
ABresult
0X0
100
111
177 0 @to = @args{ qw( to_x to_y ) };
178 }
179  
180 0 ▶ return undef unless ( $from[0] and $from[1] and $to[0] and $to[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless $from[0] and $from[1] and $to[0] and $to[1] 0 0 0
Condition0%0/9
$from[0] and $from[1] and $to[0] and $to[1]
!ll&&!rl&&r
000
$from[0] and $from[1] and $to[0]
!ll&&!rl&&r
000
$from[0] and $from[1]
!ll&&!rl&&r
000
MC/DC0%0/4
$from[0] and $from[1] and $to[0] and $to[1]
$from[0] $from[1] $to[0] $to[1]
Truth table0%0/5
A and B and C and D
A= $from[0]B= $from[1]C= $to[0]D= $to[1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
181  
182 0 my $metres = int( sqrt( ($from[0] - $to[0])**2
183 + ($from[1] - $to[1])**2 ) + 0.5 );
184  
185 0 ▶ if ( $args{unit} eq "metres" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'unit'} eq 'metres') { } 0 0 0
186 0 return $metres;
187 } else {
188 0 return $metres/1000;
189 }
190 }
191  
192 =item B<find_within_distance>
193  
194 # Find all the things within 200 metres of a given place.
195 my @others = $locator->find_within_distance( node => "Albion",
196 metres => 200 );
197  
198 # Or within 200 metres of a given location.
199 my @things = $locator->find_within_distance( x => 530774,
200 y => 182260,
201 metres => 200 );
202  
203 Units currently understood: C<metres>, C<kilometres>. If both C<node>
204 and C<x>/C<y> are supplied then C<node> takes precedence. Croaks if
205 insufficient start point data supplied.
206  
207 =cut
208  
209 0 ▶sub find_within_distance {
Subroutine find_within_distance: not called CC=14 SCAR=53.5
Pod documented
210 0 my ($self, %args) = @_;
211 0 my $store = $self->datastore;
212 0 ▶ my $dbh = eval { $store->dbh; }
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = eval { do { $store->dbh } } or croak("find_within_distance is only implemented for database stores") 0 0 0
213 or croak "find_within_distance is only implemented for database stores";
214 my $metres = $args{metres}
215 0 ▶ || ($args{kilometres} * 1000)
Condition0%0/5
$args{'metres'} || $args{'kilometres'} * 1000 || croak('Please supply a distance')
l!l
00
$args{"metres"} or $args{"kilometres"} * 1000
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'metres'} || $args{'kilometres'} * 1000 || croak('Please supply a distance')
$args{"metres"} $args{"kilometres"} * 1000
Truth table0%0/3
A or B
A= $args{"metres"}B= $args{"kilometres"} * 1000
ABresult
1X1
011
000
216 || croak "Please supply a distance";
217 0 my ($sx, $sy);
218 0 ▶ if ( $args{node} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'node'}) { } 0 0 0
elsif ($args{'x'} and $args{'y'}) { } 0 0 0
219 0 ($sx, $sy) = $self->coordinates( node => $args{node} );
220 ▶ } elsif ( $args{x} and $args{y} ) {
Condition0%0/3
$args{"x"} and $args{"y"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"x"} and $args{"y"}
$args{"x"} $args{"y"}
Truth table0%0/3
A and B
A= $args{"x"}B= $args{"y"}
ABresult
0X0
100
111
221 0 ($sx, $sy) = @args{ qw( x y ) };
222 } else {
223 0 croak "Insufficient start location data supplied";
224 }
225  
226 # Only consider nodes within the square containing the circle of
227 # radius $distance. The SELECT DISTINCT is needed because we might
228 # have multiple versions in the table.
229 0 my $sql = "SELECT DISTINCT x.name ".
230 "FROM node AS x ".
231 "INNER JOIN metadata AS mx ".
232 " ON (mx.node_id = x.id AND mx.version = x.version) ".
233 "INNER JOIN node AS y ".
234 " ON (x.id = y.id) ".
235 "INNER JOIN metadata my ".
236 " ON (my.node_id = y.id AND my.version = y.version) ".
237 " WHERE mx.metadata_type = '$self->{x}' ".
238 " AND my.metadata_type = '$self->{y}' ".
239 " AND mx.metadata_value >= " . ($sx - $metres) .
240 " AND mx.metadata_value <= " . ($sx + $metres) .
241 " AND my.metadata_value >= " . ($sy - $metres) .
242 " AND my.metadata_value <= " . ($sy + $metres);
243 $sql .= " AND x.name != " . $dbh->quote($args{node})
244 0 ▶ if $args{node};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"node"} 0 0 0
245 # Postgres is a fussy bugger.
246 0 ▶ if ( ref $store eq "Wiki::Toolkit::Store::Pg" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $store eq "Wiki::Toolkit::Store::Pg") 0 0 0
247 0 $sql =~ s/metadata_value/metadata_value::integer/gs;
248 }
249 # SQLite 3 is even fussier.
250 0 ▶ if ( ref $store eq "Wiki::Toolkit::Store::SQLite"
Branch: 1 branch
BranchTrueFalseTotal
if (ref $store eq "Wiki::Toolkit::Store::SQLite" and $DBD::SQLite::VERSION >= "1.00") 0 0 0
Condition0%0/3
ref $store eq "Wiki::Toolkit::Store::SQLite" and $DBD::SQLite::VERSION >= "1.00"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $store eq "Wiki::Toolkit::Store::SQLite" and $DBD::SQLite::VERSION >= "1.00"
ref $store eq "Wiki::Toolkit::Store::SQLite" $DBD::SQLite::VERSION >= "1.00"
Truth table0%0/3
A and B
A= ref $store eq "Wiki::Toolkit::Store::SQLite"B= $DBD::SQLite::VERSION >= "1.00"
ABresult
0X0
100
111
251 && $DBD::SQLite::VERSION >= "1.00" ) {
252 0 $sql =~ s/metadata_value/metadata_value+0/gs; # yuck
253 }
254 0 my $sth = $dbh->prepare($sql);
255 0 $sth->execute;
256 0 my @results;
257 0 ▶ while ( my ($result) = $sth->fetchrow_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($result) = $sth->fetchrow_array) 0 0 0
258 0 my $dist = $self->distance( from_x => $sx,
259 from_y => $sy,
260 to_node => $result,
261 unit => "metres" );
262 0 ▶ if ( defined $dist && $dist <= $metres ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dist and $dist <= $metres) 0 0 0
Condition0%0/3
defined $dist and $dist <= $metres
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dist and $dist <= $metres
defined $dist $dist <= $metres
Truth table0%0/3
A and B
A= defined $distB= $dist <= $metres
ABresult
0X0
100
111
263 0 push @results, $result;
264 }
265 }
266 0 return @results;
267 }
268  
269 =head1 SEE ALSO
270  
271 =over 4
272  
273 =item * L<Wiki::Toolkit>
274  
275 =item * L<OpenGuides> - an application that uses this plugin.
276  
277 =back
278  
279 =head1 AUTHOR
280  
281 Kake Pugh (kake@earth.li).
282 The Wiki::Toolkit team (http://www.wiki-toolkit.org/)
283  
284 =head1 COPYRIGHT
285  
286 Copyright (C) 2004 Kake L Pugh. All Rights Reserved.
287 Copyright (C) 2006 the Wiki::Toolkit Team. All Rights Reserved.
288  
289 This module is free software; you can redistribute it and/or modify it
290 under the same terms as Perl itself.
291  
292 =head1 CREDITS
293  
294 This module is based heavily on (and is the replacement for)
295 L<Wiki::Toolkit::Plugin::Locator::UK>.
296  
297 The following thanks are due to people who helped with
298 L<Wiki::Toolkit::Plugin::Locator::UK>: Nicholas Clark found a very silly
299 bug in a pre-release version, oops :) Stephen White got me thinking in
300 the right way to implement C<find_within_distance>. Marcel Gruenauer
301 helped me make C<find_within_distance> work properly with postgres.
302  
303 =cut
304  
305  
306 3 1;
↑ Summary