blib/lib/CGI/Wiki/Plugin/Locator/UTM.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package CGI::Wiki::Plugin::Locator::UTM;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use vars qw( $VERSION @ISA );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 $VERSION = '0.04';
7  
8 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use CGI::Wiki::Plugin;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 @ISA = qw( CGI::Wiki::Plugin );
12  
13 use Geo::Coordinates::UTM;
14  
15 =head1 NAME
16  
17 CGI::Wiki::Plugin::Locator::UTM - A CGI::Wiki plugin to manage international location data.
18  
19 =head1 SYNOPSIS
20  
21 use CGI::Wiki;
22 use CGI::Wiki::Plugin::Locator::UTM;
23  
24 my $wiki = CGI::Wiki->new( ... );
25 my $locator = CGI::Wiki::Plugin::Locator::UTM->new('International');
26 $wiki->register_plugin( $locator );
27  
28 $wiki->write_node( "King's Head",
29 "Tip top pride",
30 $checksum,
31 { lat => 51.507497,
32 long => -0.271797
33 }
34 );
35  
36 # Just retrieve the location
37 my ( $lat, $long ) = $locator->location( node => "King's Head" );
38  
39 # or as coordinates
40 my ( $zone, $east, $north ) = $locator->coordinates( node => "King's Head" );
41  
42 # Find the straight-line distance between two nodes, in kilometres.
43 my $distance = $locator->distance( from_node => "King's Head",
44 to_node => "Calthorpe Arms" );
45  
46 # Find all the things within 200 metres of a given place.
47 my @others = $locator->find_within_distance( node => "Albion",
48 metres => 200 );
49  
50 =head1 DESCRIPTION
51  
52 This module provides access to the Universal Transverse Mercator
53 international coordinates system. It is a thin wrapper for the module
54 L<Geo::Coordinates::UTM>. To use this module, you must specify an
55 ellipsoid, on which the mercator projection is based.
56  
57 =head1 METHODS
58  
59 =over 4
60  
61 =item B<new>
62  
63 my $locator = CGI::Wiki::Plugin::Locator::UK->new('Clarke 1880');
64  
65 The parameter is the name of the ellipsoid to be used for coordinate
66 conversion. The default value of this is 'International'. Please see
67 L<Geo::Coordinates::UTM> for explanation and for the full list of
68 valid values.
69  
70 Note that you can only do coordinate conversion within one ellipsoid.
71  
72 =cut
73  
74 sub new {
75 my ( $class, $ellipsoid ) = @_;
76 $ellipsoid ||= 'International';
77 my $self = { ellipsoid => $ellipsoid };
78 bless $self, $class;
79 return $self;
80 }
81  
82 =item B<location>
83  
84 my ($lat, $long) = $locator->coordinates( node => "King's Head" );
85  
86 Returns the latitude and longitude co-ordinates stored as metadata
87 last time the node was written.
88  
89 =cut
90  
91 sub location {
92 my ( $self, %args ) = @_;
93 my $store = $self->datastore;
94 my %node_data = $store->retrieve_node( $args{node} );
95 my %metadata = %{ $node_data{metadata} };
96 return ( $metadata{lat}[0], $metadata{long}[0] );
97 }
98  
99 =item B<coordinates>
100  
101 my ($zone, $x, $y) = $locator->coordinates( node => "King's Head" );
102
103 my ($zone, $x, $y) = $locator->coordinates( lat => 51.507497,
104 long => -0.271797);
105
106 Returns the UTM easting and northing co-ordinates.
107 See L<Geo::Coordinates::UTM> for the meaning of the zone value.
108  
109 =cut
110  
111 sub coordinates {
112 my ( $self, %args ) = @_;
113 my ( $lat, $long, $zone, $east, $north );
114  
115 return @args{qw(zone easting northing)}
116 if exists( $args{zone} )
117 && exists( $args{easting} )
118 && exists( $args{northing} );
119  
120 if ( exists $args{lat} ) {
121 ( $lat, $long ) = @args{qw/lat long/};
122 }
123 else {
124 my $store = $self->datastore;
125 my %node_data = $store->retrieve_node( $args{node} );
126 $lat = $node_data{metadata}{lat}[0];
127 $long = $node_data{metadata}{long}[0];
128  
129 # Use UTM metadata if it is available
130 $zone = $node_data{metadata}{zone}[0];
131 $east = $node_data{metadata}{easting}[0];
132 $north = $node_data{metadata}{northing}[0];
133 }
134 if ( !defined($zone) && !defined($east) && !defined($north) ) {
135 return undef unless defined($lat) && defined($long);
136 ( $zone, $east, $north ) =
137 latlon_to_utm( $self->{ellipsoid}, $lat, $long );
138 }
139 ( $zone, $east, $north );
140 }
141  
142 =item B<distance>
143  
144 # Find the straight-line distance between two nodes, in kilometres.
145 my $distance = $locator->distance( from_node => "King's Head",
146 to_node => "Duke Of York" );
147  
148 # Or in metres between a node and a point.
149 my $distance = $locator->distance(from_lat => 51.507497,
150 from_long => -0.271797,
151 to_node => "Duke of Cambridge",
152 unit => "metres" );
153  
154 Defaults to kilometres if C<unit> is not supplied or is not recognised.
155 Recognised units at the moment: C<metres>, C<kilometres>.
156  
157 Returns C<undef> if one of the endpoints does not exist, or does not
158 have both co-ordinates defined.
159  
160 B<Note:> Works to the nearest metre. Well, actually, calls C<int> and
161 rounds down, but if anyone cares about that they can send a patch.
162  
163 =cut
164  
165 sub distance {
166 my ( $self, %args ) = @_;
167  
168 $args{unit} ||= "kilometres";
169  
170 # Split off the prefix from_ or to_
171 my ( %from, %to );
172 while ( my ( $arg, $val ) = each %args ) {
173 $from{$1} = $val if $arg =~ /from_(.*)/;
174 $to{$1} = $val if $arg =~ /to_(.*)/;
175 }
176  
177 my @from = $self->coordinates(%from);
178 my @to = $self->coordinates(%to);
179  
180 return undef unless ( $from[0] and $from[1] and $to[0] and $to[1] );
181  
182 croak("Locations are in different zones") unless $from[0] eq $to[0];
183  
184 my $metres =
185 int( sqrt( ( $from[1] - $to[1] )**2 + ( $from[2] - $to[2] )**2 ) + 0.5 );
186  
187 if ( $args{unit} eq "metres" ) {
188 return $metres;
189 }
190 else {
191 return $metres / 1000;
192 }
193 }
194  
195 =item B<find_within_distance>
196  
197 # Find all the things within 200 metres of a given place.
198 my @others = $locator->find_within_distance( node => "Albion",
199 metres => 200 );
200  
201 # Or within 200 metres of a given location.
202 my @things = $locator->find_within_distance( lat => 51.507497,
203 long => -0.271797,
204 metres => 200 );
205  
206 Requires the node metadata to be stored in the database, in particular
207 zone, easting and northing.
208  
209 Units currently understood: C<metres>, C<kilometres>. If both C<node>
210 and C<os_x>/C<os_y> are supplied then C<node> takes precedence. Croaks
211 if insufficient start point data supplied.
212  
213 =cut
214  
215 sub find_within_distance {
216 my ( $self, %args ) = @_;
217  
218 my $store = $self->datastore;
219 my $dbh = eval { $store->dbh; }
220 or croak "find_within_distance is only implemented for database stores";
221 my $metres = $args{metres}
222 || ( $args{kilometres} * 1000 )
223 || croak "Please supply a distance";
224 my ( $zone, $sx, $sy ) = $self->coordinates(%args);
225 croak "Insufficient start location data supplied"
226 unless defined($sx) && defined($sy);
227  
228 # Only consider nodes within the square containing the circle of
229 # radius $distance. The SELECT DISTINCT is needed because we might
230 # have multiple versions in the table.
231 my $sql = "SELECT DISTINCT x.node
232 FROM metadata AS x, metadata AS y, metadata AS z
233 WHERE x.node = y.node
234 AND x.node = z.node
235 AND x.metadata_type = 'easting'
236 AND x.metadata_value >= ?
237 AND x.metadata_value <= ?
238 AND y.metadata_type = 'northing'
239 AND y.metadata_value >= ?
240 AND y.metadata_value <= ?
241 AND z.metadata_type = 'zone'
242 AND z.metadata_value = ?
243 AND x.node != ?";
244  
245 # Postgres is a fussy bugger.
246 if ( ref $store eq "CGI::Wiki::Store::Pg" ) {
247 $sql =~ s/([xy]\.metadata_value)/$1::float/gs;
248 }
249 my $node = $args{node} || '';
250  
251 my $sth = $dbh->prepare($sql);
252 $sth->execute(
253 $sx - $metres,
254 $sx + $metres,
255 $sy - $metres,
256 $sy + $metres,
257 $zone, $node
258 );
259 my @results;
260 while ( my ($result) = $sth->fetchrow_array ) {
261 my $dist = $self->distance(
262 from_easting => $sx,
263 from_northing => $sy,
264 from_zone => $zone,
265 to_node => $result,
266 unit => "metres"
267 );
268 if ( $dist && $dist <= $metres ) {
269 push @results, $result;
270 }
271 }
272 return @results;
273 }
274  
275 =item B<og_config>
276  
277 my $config = OpenGuides::Config->new(file => 'wiki.conf');
278 $locator->og_config($config);
279 ...
280 $ellipsoid = $locator->og_config->ellipsoid;
281  
282 This is a get-and-set method, used in conjunction with the L<OpenGuides>
283 package to make all the information from the configuration available to the
284 plugin.
285  
286 =cut
287  
288 sub og_config {
289 my $self = shift;
290  
291 if (@_) {
292 my $config = shift;
293 if ($config->geo_handler eq 3) {
294 $self->{ellipsoid} = $config->ellipsoid;
295 }
296 $self->{og_config} = $config;
297 }
298 $self->{og_config};
299 }
300  
301 =item B<pre_write>
302  
303 $ap->despatch( 'pre_write', $node, $content, \%metadata );
304  
305 This is a L<CGI::Wiki::AutoPlugin> compatible method, which should be called
306 before committing data to the wiki store. This performs the necessary geodata
307 convertions needed between coordinate systems.
308  
309 This is a refactoring of code in L<OpenGuides::Template> which deals with
310 converting input data into the coordinate system used on a particular guide.
311 This pre-write method updates the metadata hash to populate the remaining
312 fields of latitude, longitude, x, y, which are not already populated.
313  
314 =cut
315  
316 our @geo_mechanism = (
317 {},
318 {
319 x => 'os_x',
320 y => 'os_y',
321 module => 'Geography::NationalGrid::GB',
322 },
323 {
324 x => 'osie_x',
325 y => 'osie_y',
326 module => 'Geography::NationalGrid::IE',
327 },
328 {
329 x => 'easting',
330 y => 'northing',
331 module => 'Geo::Coordinates::UTM',
332 },
333 ) ;
334
335  
336 sub pre_write {
337 my ($self, $node, $content, $metadata) = @_;
338  
339 my $config = $self->og_config or croak "No config set";
340 my $geo_handler = $config->geo_handler;
341 my %geo_mech = %{$geo_mechanism[$geo_handler]};
342
343 # Case 1 : all geodata missing
344  
345 return 1 if !exists ($metadata->{latitude}) &&
346 !exists ($metadata->{longitude}) &&
347 !exists ($metadata->{$geo_mech{x}}) &&
348 !exists ($metadata->{$geo_mech{y}});
349
350 # Case 2 : all geodata present - do no convertions
351  
352 return 1 if exists ($metadata->{latitude}) &&
353 exists ($metadata->{longitude}) &&
354 exists ($metadata->{$geo_mech{x}}) &&
355 exists ($metadata->{$geo_mech{y}});
356  
357 eval "require $geo_mech{module}";
358  
359 if ($geo_handler == 3) {
360 my $lat = $metadata->{latitude}[0];
361 my $long = $metadata->{longitude}[0];
362 if ( $lat && $long ) {
363 my ($zone, $easting, $northing) =
364 Geo::Coordinates::UTM::latlon_to_utm (
365 $self->{ellipsoid}, $lat, $long );
366 $easting =~ s/\..*//; # chop off decimal places
367 $northing =~ s/\..*//; # - metre accuracy enough
368 $metadata->{easting}[0] = $easting;
369 $metadata->{northing}[0] = $northing;
370 }
371 }
372 else {
373 my $x = $metadata->{$geo_mech{x}}[0];
374 my $y = $metadata->{$geo_mech{y}}[0];
375 if ($x && $y) {
376 my $point = $geo_mech{module}->new ( Easting => $x,
377 Northing => $y );
378 $metadata->{latitude}[0] = sprintf "%.6f", $point->latitude;
379 $metadata->{longitude}[0] = sprintf "%.6f", $point->longitude;
380 }
381 else {
382 my $lat = $metadata->{latitude}[0];
383 my $long = $metadata->{longitude}[0];
384 my $point = $geo_mech{module}->new ( Latitude => $lat,
385 Longitude => $long );
386 $metadata->{$geo_mech{x}}[0] = $point->easting;
387 $metadata->{$geo_mech{y}}[0] = $point->northing;
388 }
389 }
390 }
391  
392 =back
393
394 =head1 SEE ALSO
395  
396 =over 4
397  
398 =item * L<CGI::Wiki>
399  
400 =item * L<CGI::Wiki::Plugin::Locator::UK>
401  
402 =item * L<Geo::Coordinates::UTM>
403  
404 =back
405  
406 =head1 AUTHOR
407  
408 Ivor Williams (ivorw at CPAN dot org)
409  
410 =head1 ACKNOWLEDGEMENT
411  
412 This module is derived from CGI::Wiki::Plugin::Locator::UK, written by
413 Kate Pugh.
414  
415 =head1 COPYRIGHT
416  
417 Copyright (C) 2004 Ivor Williams. All Rights Reserved.
418  
419 This module is free software; you can redistribute it and/or modify it
420 under the same terms as Perl itself.
421  
422 =cut
423  
424 1;
↑ Summary