Changeset 870


Ignore:
Timestamp:
Sep 17, 2006, 5:26:28 PM (15 years ago)
Author:
nick
Message:

Support doing Helmert Transforms as required, using the MySociety? Helmert transform library, if installed. References #89

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/OpenGuides/Utils.pm

    r867 r870  
    236236       or croak "No longitude supplied to get_wgs84_coords";
    237237    croak "geo_handler not defined!" unless $config->geo_handler;
     238
    238239    if ($config->force_wgs84) {
    239240        # Only as a rough approximation, good enough for large scale guides
    240241        return ($longitude, $latitude);
    241     } elsif ($config->geo_handler == 1) {
     242    }
     243
     244    # If we don't have a lat and long, return undef right away
     245    unless($args{longitude} || $args{latitude}) {
     246        return undef;
     247    }
     248
     249    # Try to load a provider of Helmert Transforms
     250    my $helmert;
     251    # First up, try the MySociety Geo::HelmertTransform
     252    unless($helmert) {
     253        eval {
     254            require Geo::HelmertTransform;
     255            $helmert = sub($$$) {
     256                my ($datum,$oldlat,$oldlong) = @_;
     257                my $datum_helper = Geo::HelmertTransform::datum($datum);
     258                my $wgs84_helper = Geo::HelmertTransform::datum('WGS84');
     259                unless($datum_helper) {
     260                    croak("No convertion helper for datum '$datum'");
     261                    return undef;
     262                }
     263
     264                my ($lat,$long,$h) =
     265                    Geo::HelmertTransform::convert_datum($datum_helper,$wgs84_helper,$oldlat,$oldlong,0);
     266                return ($long,$lat);
     267            };
     268        };
     269    }
     270    # Next, try .....
     271    unless($helmert) {
     272        eval {
     273        };
     274    }
     275    # Give up, return undef
     276    unless($helmert) {
     277       return undef;
     278    }
     279   
     280
     281    if ($config->geo_handler == 1) {
    242282        # Do conversion here
    243         return undef;
     283        return &$helmert('Airy1830',$latitude,$longitude);
    244284    } elsif ($config->geo_handler == 2) {
    245285        # Do conversion here
    246         return undef;
     286        return &$helmert('Airy1830Modified',$latitude,$longitude);
    247287    } elsif ($config->geo_handler == 3) {
    248288        if ($config->ellipsoid eq "WGS-84") {
     
    250290        } else {
    251291            # Do conversion here
    252             return undef;
     292            return &$helmert($config->ellipsoid,$latitude,$longitude);
    253293        }
    254294    } else {
  • trunk/t/28_wgs84_coords.t

    r792 r870  
    55use Test::More;
    66
    7 plan tests => 2;
     7plan tests => 4;
    88
    99# Clear out the database from any previous runs.
     
    1818my $guide = OpenGuides->new( config => $config );
    1919
    20 my ($longitude, $latitude) = (0, 0);
     20my ($longitude, $latitude) = (10, 12);
    2121
    2222my ($wgs_long, $wgs_lat) = OpenGuides::Utils->get_wgs84_coords(
     
    2929is( $wgs_lat, $latitude,
    3030    "get_wgs84_coords returns the original latitude when force_wgs84 is on");
     31
     32
     33# Now claim to be in the UK
     34eval{ require Geo::HelmertTransform; };
     35my $have_helmert = $@ ? 0 : 1;
     36SKIP : {
     37    skip "Geo::HelmertTransform not installed - can't do transforms", 2
     38        unless $have_helmert;
     39
     40    $config->force_wgs84(0);
     41    $config->geo_handler(1);
     42
     43    # Set our location to be somewhere known
     44       ($longitude,$latitude)  = (-1.258200,51.754349);
     45    my ($wgs84_lon,$wgs84_lat) = (-1.259687,51.754813);
     46
     47    ($wgs_long, $wgs_lat) = OpenGuides::Utils->get_wgs84_coords(
     48                                                     longitude => $longitude,
     49                                                     latitude => $latitude,
     50                                                     config => $config);
     51
     52    # Round to 5 dp
     53    my $fivedp = 1 * 1000 * 100;
     54    $wgs_long = int($wgs_long * $fivedp)/$fivedp;
     55    $wgs_lat  = int($wgs_lat  * $fivedp)/$fivedp;
     56    $wgs84_lon = int($wgs84_lon * $fivedp)/$fivedp;
     57    $wgs84_lat = int($wgs84_lat * $fivedp)/$fivedp;
     58
     59    is( $wgs_long, $wgs84_lon,
     60        "get_wgs84_coords does Airy1830 -> WGS84 convertion properly");
     61    is( $wgs_lat, $wgs84_lat,
     62        "get_wgs84_coords does Airy1830 -> WGS84 convertion properly");
     63}
Note: See TracChangeset for help on using the changeset viewer.