Author: kake Date: 2012-07-04 10:58:34 +0100 (Wed, 04 Jul 2012) New Revision: 1419
Added: trunk/t/209_max_min_wgs84_coords.t Modified: trunk/MANIFEST trunk/lib/OpenGuides.pm trunk/lib/OpenGuides/Utils.pm Log: Factor out thing that works out min/max/centre lat/long to OpenGuides::Utils so other bits of the code can use it.
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2012-07-01 19:06:50 UTC (rev 1418) +++ trunk/MANIFEST 2012-07-04 09:58:34 UTC (rev 1419) @@ -107,6 +107,7 @@ t/206_geo_data_search_form.t t/207_geo_data_edit_form.t t/208_wgs84_coords.t +t/209_max_min_wgs84_coords.t t/300_search_raw.t t/301_search.t t/302_search_simple_metadata.t
Modified: trunk/lib/OpenGuides/Utils.pm =================================================================== --- trunk/lib/OpenGuides/Utils.pm 2012-07-01 19:06:50 UTC (rev 1418) +++ trunk/lib/OpenGuides/Utils.pm 2012-07-04 09:58:34 UTC (rev 1419) @@ -341,6 +341,44 @@ } }
+=item B<get_wgs84_min_max> + +Given a set of WGS84 coordinate data, returns the minimum, maximum, +and centre latitude and longitude. + + %data = OpenGuides::Utils->get_wgs84_min_max( + nodes => [ + { wgs84_lat => 51.1, wgs84_long => 1.1 }, + { wgs84_lat => 51.2, wgs84_long => 1.2 }, + ] + ); + print "Top right-hand corner is $data{max_lat}, $data{max_long}"; + print "Centre point is $data{centre_lat}, $data{centre_long}"; + +The hashes in the C<nodes> argument can include other key/value pairs; +these will just be ignored. + +=cut + +sub get_wgs84_min_max { + my ( $self, %args ) = @_; + my @nodes = @{$args{nodes}}; + + my @lats = sort + grep { defined $_ && /^[-.\d]+$/ } + map { $_->{wgs84_lat} } + @nodes; + my @longs = sort + grep { defined $_ && /^[-.\d]+$/ } + map { $_->{wgs84_long} } + @nodes; + my %data = ( min_lat => $lats[0], max_lat => $lats[$#lats], + min_long => $longs[0], max_long => $longs[$#longs] ); + $data{centre_lat} = ( $data{min_lat} + $data{max_lat} ) / 2; + $data{centre_long} = ( $data{min_long} + $data{max_long} ) / 2; + return %data; +} + =item B<detect_redirect>
$redir = OpenGuides::Utils->detect_redirect( content => "foo" );
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2012-07-01 19:06:50 UTC (rev 1418) +++ trunk/lib/OpenGuides.pm 2012-07-04 09:58:34 UTC (rev 1419) @@ -1083,9 +1083,9 @@ param => $formatter->node_name_to_node_param($_) } } sort @selnodes;
- # Convert the lat+long to WGS84 as required. If displaying a map - # using Leaflet, also grab the min and max lat and long. - my ( $min_lat, $max_lat, $min_long, $max_long ); + # Convert the lat+long to WGS84 as required, and count how many nodes + # we have for the map (if using Leaflet). + my $nodes_on_map; for(my $i=0; $i<scalar @nodes;$i++) { my $node = $nodes[$i]; if($node) { @@ -1107,20 +1107,7 @@ $node->{has_geodata} = 1; $node->{wgs84_lat} = $wgs84_lat; $node->{wgs84_long} = $wgs84_long; - if ( !defined $min_lat ) { - $min_lat = $max_lat = $wgs84_lat; - } elsif ( $wgs84_lat < $min_lat ) { - $min_lat = $wgs84_lat; - } elsif ( $wgs84_lat > $max_lat ) { - $max_lat = $wgs84_lat; - } - if ( !defined $min_long ) { - $min_long = $max_long = $wgs84_long; - } elsif ( $wgs84_long < $min_long ) { - $min_long = $wgs84_long; - } elsif ( $wgs84_long > $max_long ) { - $max_long = $wgs84_long; - } + $nodes_on_map++; } } } @@ -1143,15 +1130,17 @@ } elsif ( $args{format} eq "map" ) { $tt_vars{display_google_maps} = 1; # override for this page if ( $use_leaflet ) { - if ( defined $min_lat ) { - %tt_vars = ( %tt_vars, - min_lat => $min_lat, - max_lat => $max_lat, - min_long => $min_long, - max_long => $max_long, - centre_lat => ( ( $max_lat + $min_lat ) / 2 ), - centre_long => ( ( $max_long + $min_long ) / 2 ), - ); + if ( $nodes_on_map ) { + my @points = map { + { wgs84_lat => + $_->{node_data}->{metadata}->{wgs84_lat}[0], + wgs84_long => + $_->{node_data}->{metadata}->{wgs84_long}[0] + } + } @nodes; + my %minmaxdata = OpenGuides::Utils->get_wgs84_min_max( + nodes => @points ); + %tt_vars = ( %tt_vars, %minmaxdata ); } else { $tt_vars{no_nodes_on_map} = 1; }
Added: trunk/t/209_max_min_wgs84_coords.t =================================================================== --- trunk/t/209_max_min_wgs84_coords.t (rev 0) +++ trunk/t/209_max_min_wgs84_coords.t 2012-07-04 09:58:34 UTC (rev 1419) @@ -0,0 +1,22 @@ +use strict; +use OpenGuides::Utils; +use Test::More; + +plan tests => 2; + +# Simple tests of the max/min/centre lat/long utility. + +# One node. +my @nodes = ( { wgs84_lat => 10, wgs84_long => 20 } ); +my %data = OpenGuides::Utils->get_wgs84_min_max( nodes => @nodes ); +is_deeply( %data, { min_lat => 10, max_lat => 10, min_long => 20, + max_long => 20, centre_lat => 10, centre_long => 20 }, + "get_wgs84_min_max gives correct answers for one node" ); + +# Two nodes. +@nodes = ( { wgs84_lat => 10, wgs84_long => 20 }, + { wgs84_lat => 18, wgs84_long => 28 } ); +%data = OpenGuides::Utils->get_wgs84_min_max( nodes => @nodes ); +is_deeply( %data, { min_lat => 10, max_lat => 18, min_long => 20, + max_long => 28, centre_lat => 14, centre_long => 24 }, + "get_wgs84_min_max gives correct answers for two nodes" );