diff options
author | Edmund von der Burg <evdb@mysociety.org> | 2011-02-10 12:59:40 +0000 |
---|---|---|
committer | Edmund von der Burg <evdb@mysociety.org> | 2011-02-10 12:59:40 +0000 |
commit | 83b5f08e8441b5b46dc857bfecef7b9102e82f6d (patch) | |
tree | 28bb21eb7d227760c1417805e0fa35b4c21d168c /perllib/Utils.pm | |
parent | 4dd9c56287f1f8c2349ae8aa51c1441749ce31c1 (diff) |
Created function to convert e,n to lat,lon and optionally truncate results
Diffstat (limited to 'perllib/Utils.pm')
-rw-r--r-- | perllib/Utils.pm | 70 |
1 files changed, 64 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; |