diff options
-rw-r--r-- | perllib/Utils.pm | 70 | ||||
-rw-r--r-- | t/utils.t | 41 |
2 files changed, 105 insertions, 6 deletions
diff --git a/perllib/Utils.pm b/perllib/Utils.pm index 24f4a6f94..d54e081c8 100644 --- a/perllib/Utils.pm +++ b/perllib/Utils.pm @@ -13,18 +13,76 @@ package Utils; use strict; use mySociety::DBHandle qw(dbh); +use mySociety::GeoUtil; sub workaround_pg_bytea { - my ($st, $img_idx, @elements) = @_; + my ( $st, $img_idx, @elements ) = @_; my $s = dbh()->prepare($st); - for (my $i=1; $i<=@elements; $i++) { - if ($i == $img_idx) { - $s->bind_param($i, $elements[$i-1], { pg_type => DBD::Pg::PG_BYTEA }); - } else { - $s->bind_param($i, $elements[$i-1]); + for ( my $i = 1 ; $i <= @elements ; $i++ ) { + if ( $i == $img_idx ) { + $s->bind_param( + $i, + $elements[ $i - 1 ], + { pg_type => DBD::Pg::PG_BYTEA } + ); + } + else { + $s->bind_param( $i, $elements[ $i - 1 ] ); } } $s->execute(); } +=head2 convert_en_to_latlon + + ( $latitude, $longitude ) = Utils::convert_en_to_latlon( $easting, $northing ); + +Takes the easting and northing and returns latitude and longitude. + +=cut + +sub convert_en_to_latlon { + my ( $easting, $northing ) = @_; + + my ( $latitude, $longitude ) = + + # map { truncate_coordinate($_) } + mySociety::GeoUtil::national_grid_to_wgs84( $easting, $northing, 'G' ); + + return ( $latitude, $longitude ); +} + +=head2 convert_en_to_latlon_truncated + + ( $lat, $lon ) = Utils::convert_en_to_latlon( $easting, $northing ); + +Takes the easting and northing and returns latitude and longitude (truncated +using C<Utils::truncate_coordinate>). + +=cut + +sub convert_en_to_latlon_truncated { + my ( $easting, $northing ) = @_; + + return + map { truncate_coordinate($_) } + convert_en_to_latlon( $easting, $northing ); +} + +=head2 truncate_coordinate + + $short = Utils::truncate_coordinate( $long ); + +Given a long coordinate returns a shorter one - rounded to 6 decimal places - +which is < 1m at the equator. + +=cut + +sub truncate_coordinate { + my $in = shift; + my $out = sprintf( '%0.6f', $in ); + $out =~ s{\.?0+\z}{} if $out =~ m{\.}; + return $out; +} + 1; diff --git a/t/utils.t b/t/utils.t new file mode 100644 index 000000000..385c482ed --- /dev/null +++ b/t/utils.t @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use Test::More tests => 9; + +use FindBin; +use lib "$FindBin::Bin/../perllib"; +use lib "$FindBin::Bin/../commonlib/perllib"; + +use Utils; + +my @truncate_tests = ( + [ '1.1234567890123', '1.123457', "truncate down" ], + [ '1.123456', '1.123456', "leave untouched" ], + [ '1.12', '1.12', "don't extend" ], + [ '1.100000001', '1.1', "knock off trailing zeros" ], + [ '1.000000001', '1', "knock off trailing zeros" ], + [ '0.0', '0', "knock off trailing zeros" ], + [ '+123', '123', "drop plus sign" ], + [ '-123', '-123', "keep minus sign" ], +); + +foreach my $test (@truncate_tests) { + my ( $in, $out, $msg ) = @$test; + is Utils::truncate_coordinate($in), $out, $msg; +} + +my @convert_en_to_latlon_tests = ( + + # e n lat lon + [ 1234, 4567, 49.808509, -7.544784 ], +); + +foreach my $test (@convert_en_to_latlon_tests) { + my ( $e, $n, $lat, $lon ) = @$test; + is_deeply # + [ Utils::convert_en_to_latlon_truncated( $e, $n ) ], # + [ $lat, $lon ], # + "convert ($e,$n) to ($lat,$lon)"; +} |