source: trunk/npemap.org.uk/perllib/NPEMap/GeoConverter.pm @ 777

Last change on this file since 777 was 777, checked in by Dominic Hargreaves, 2 years ago

use

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1# Copyright (c) 2007 Nick Burch
2# Permission is hereby granted, free of charge, to any person obtaining a
3# copy of this software and associated documentation files (the "Software"),
4# to deal in the Software without restriction, including without limitation
5# the rights to use, copy, modify, merge, publish, distribute, sublicense,
6# and/or sell copies of the Software, and to permit persons to whom the
7# Software is furnished to do so, subject to the following conditions:
8#
9# The above copyright notice and this permission notice shall be included in
10# all copies or substantial portions of the Software.
11#
12# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18# IN THE SOFTWARE.
19#
20package NPEMap::GeoConverter;
21
22use vars qw(@ISA @EXPORT);
23use warnings;
24use strict;
25
26use vars qw($tt_config);
27
28use FindBin;
29use lib "$FindBin::Bin/../";
30
31# Include stuff, so we can offer lat+long
32use Geo::Coordinates::OSGB;
33use Geo::Coordinates::ITM;
34use Geo::Coordinates::Grid;
35
36
37require Exporter;
38@ISA    = qw(Exporter);
39@EXPORT = qw(eastingNorthingToLatLong eastingNorthingToNGR);
40
41sub eastingNorthingToLatLong {
42        my ($e,$n,$grid) = @_;
43       
44    if ($grid eq 'osie') {
45      return Geo::Coordinates::ITM::grid_to_ll($e, $n);
46    }
47    else {
48      my ($lat, $long, $h) = Geo::Coordinates::OSGB::grid_to_ll($e, $n);
49      return ($lat, $long);
50    }
51}
52
53sub eastingNorthingToNGR {
54        my ($e,$n,$grid) = @_;
55
56    if ($grid eq "osie") {
57      my $index_east  = int $e/100_000;
58      my $index_north = int $n/100_000;
59
60      my @grid = ( [ qw( V W X Y Z ) ],
61                 [ qw( Q R S T U ) ],
62                 [ qw( L M N O P ) ],
63                 [ qw( F G H J K ) ],
64                 [ qw( A B C D E ) ],
65               );
66
67      my $sq = $grid[$index_north][$index_east];
68      return sprintf("%s  %03d %03d", $sq, $e % 100_000, $n % 100_000);
69    }
70    else {
71      my ($sheet, $east, $north) = Geo::Coordinates::OSGB::Grid::format_grid($e, $n, { form => 'trad' });
72      my $gridref = "$sheet $east$north";
73      return $gridref;
74    }
75}
76
77############################################################################
78
Note: See TracBrowser for help on using the repository browser.