Changeset 757 for trunk


Ignore:
Timestamp:
Oct 6, 2015, 10:48:18 PM (4 years ago)
Author:
Dominic Hargreaves
Message:

Patch from David to hopefully remove use of Geography::NationalGrid? (with a few tweaks from me)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/npemap.org.uk/perllib/NPEMap/GeoConverter.pm

    r635 r757  
    3030
    3131# Include stuff, so we can offer lat+long
    32 use Geo::HelmertTransform;
    33 use Geography::NationalGrid;
    34 use Geography::NationalGrid::GB;
    35 use Geography::NationalGrid::IE;
     32use Geo::Coordinates::OSGB;
     33use Geo::Coordinates::ITM;
     34
    3635
    3736require Exporter;
     
    4140sub eastingNorthingToLatLong {
    4241        my ($e,$n,$grid) = @_;
    43 
    44         # Turn e+n into OSGB/OSIE lat+long
    45         my $point = getNGPoint($e,$n,$grid);
    46         my $oslat = $point->latitude;
    47         my $oslong = $point->longitude;
    48 
    49         # Turn OSGB/OSIE lat+long into WGS84 lat+long
    50         my $datum = getDatum($grid);
    51         my $osgb_helper = Geo::HelmertTransform::datum($datum);
    52         my $wgs84_helper = Geo::HelmertTransform::datum('WGS84');
    53 
    54         my ($lat,$long,$h) =
    55                 Geo::HelmertTransform::convert_datum($osgb_helper, $wgs84_helper, $oslat, $oslong, 0);
    56 
    57         return ($lat,$long);
     42       
     43    if ($grid eq 'osie') {
     44      return Geo::Coordinates::ITM::grid_to_ll($e, $n);
     45    }
     46    else {
     47      my ($oslat, $oslong) = Geo::Coordinates::OSGB::grid_to_ll($e, $n);
     48      my ($lat, $long, $h) = Geo::Coordinates::OSGB::shift_ll_into_WGS84($oslat, $oslong, 0);
     49      return ($lat, $long);
     50    }
    5851}
    5952
     
    6154        my ($e,$n,$grid) = @_;
    6255
    63         # Turn e+n into 2+6 NGR
    64         my $point = getNGPoint($e,$n,$grid);
    65         return $point->gridReference(100);
    66 }
     56    if ($grid eq "osie") {
     57      my $index_east  = int $e/100_000;
     58      my $index_north = int $n/100_000;
    6759
    68 ###########################################################################
     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               );
    6966
    70 sub getNGPoint {
    71         my ($e,$n,$grid) = @_;
    72         unless($grid) { $grid = "osgb"; }
    73 
    74         if($grid eq "osgb") {
    75                 return Geography::NationalGrid::GB->new( Easting=>$e, Northing=>$n, NoOSAreaWarn=>1 );
    76         }
    77         if($grid eq "osie") {
    78                 return Geography::NationalGrid::IE->new( Easting=>$e, Northing=>$n );
    79         }
    80 
    81         warn("Unknown grid '$grid' requested, treating as osgb");
    82         return getNGPoint($e,$n,'osgb');
    83 }
    84 
    85 sub getDatum {
    86         my $grid = shift;
    87         unless($grid) { $grid = "osgb"; }
    88 
    89         if($grid eq "osgb") {
    90                 return "Airy1830";
    91         }
    92         if($grid eq "osie") {
    93                 return "Airy1830Modified";
    94         }
    95 
    96         warn("Unknown grid '$grid' requested, treating as osgb");
    97         return getDatum('osgb');
     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::format_grid_trad($e, $n);
     72      my $gridref = "$sheet $east$north";
     73      return $gridref;
     74    }
    9875}
    9976
Note: See TracChangeset for help on using the changeset viewer.