File Coverage

blib/lib/Geo/Horizon.pm
Criterion Covered Total %
statement 38 38 100.0
branch 4 6 66.6
condition 5 7 71.4
subroutine 9 9 100.0
pod 4 5 80.0
total 60 65 92.3


line stmt bran cond sub pod time code
1             package Geo::Horizon;
2 1     1   134792 use Math::Trig qw{acos};
  1         22990  
  1         80  
3              
4             =head1 NAME
5              
6             Geo::Horizon - Calculate distance to the visual horizon
7              
8             =head1 SYNOPSIS
9              
10             use Geo::Horizon;
11             my $gh = Geo::Horizon->new("WGS84");
12             my $lat=39;
13             my $alt=1.7;
14             my $distance_to_horizon=$gh->distance($alt,$lat);
15             print "Input Lat: $lat1\n";
16             print "Output Distance: $dist\n";
17              
18             =head1 DESCRIPTION
19              
20             A perl object for calculating the distance to the visual horizon on an ellipsoid.
21              
22             =cut
23              
24 1     1   9 use strict;
  1         2  
  1         33  
25 1     1   4 use vars qw($VERSION);
  1         6  
  1         193  
26             $VERSION = sprintf("%d.%02d", q{Revision: 0.02} =~ /(\d+)\.(\d+)/);
27              
28             =head1 CONSTRUCTOR
29              
30             =head2 new
31              
32             my $gh = Geo::Horizon->new(); #default WGS84
33              
34             =cut
35              
36             sub new {
37 3     3 1 883 my $this = shift();
38 3   33     22 my $class = ref($this) || $this;
39 3         6 my $self = {};
40 3         9 bless $self, $class;
41 3         12 $self->initialize(@_);
42 3         8 return $self;
43             }
44              
45             =head1 METHODS
46              
47             =cut
48              
49             sub initialize {
50 3     3 0 6 my $self = shift();
51 3         6 $self->ellipsoid(shift);
52             }
53              
54             =head2 ellipsoid
55              
56             Method to set or retrieve the current ellipsoid object. The ellipsoid is a L object.
57              
58             my $ellipsoid=$gh->ellipsoid; #Default is WGS84
59              
60             $gh->ellipsoid('Clarke 1866'); #Built in ellipsoids from Geo::Ellipsoids
61             $gh->ellipsoid({a=>1}); #Custom Sphere 1 unit radius
62              
63             =cut
64              
65             sub ellipsoid {
66 719     719 1 68882 my $self = shift();
67 719 100       1463 if (@_) {
68 3         4 my $param=shift();
69 1     1   1310 use Geo::Ellipsoids;
  1         7624  
  1         196  
70 3         15 my $obj=Geo::Ellipsoids->new($param);
71 3         464 $self->{'ellipsoid'}=$obj;
72             }
73 719         2510 return $self->{'ellipsoid'};
74             }
75              
76             =head2 distance
77              
78             The straight-line of sight distance to the horizon: This formula does not take in account radio or optical refraction which will be further the longer the wavelength.
79              
80             my $dist=$obj->distance($alt, $lat); #alt in meters (ellipsoid units)
81             #lat in signed decimal degrees
82             my $dist=$obj->distance($alt); #default lat => 0 (equator)
83             my $dist=$obj->distance; #default alt => 1.7
84              
85             Formula from http://newton.ex.ac.uk/research/qsystems/people/sque/physics/horizon/
86              
87             Ds = sqrt(h(2R + h))
88              
89             =cut
90              
91             sub distance {
92 202     202 1 41167 my $self=shift();
93 202         258 my $alt=shift(); #usually meters but actaully ellipsoid units
94 202 50       497 $alt=1.7 unless defined $alt;
95 202   100     571 my $lat=shift() || 0; #degrees
96             #Geometric Mean (http://mentorsoftwareinc.com/CC/gistips/TIPS0899.HTM)
97 202         397 my $R=sqrt($self->ellipsoid->n($lat) * $self->ellipsoid->rho($lat));
98 202         8114 return sqrt($alt * (2 * $R + $alt));
99             }
100              
101             =head2 distance_great_circle
102              
103             The curved distance along the ellipsoid to the horizon: This is the great circle distance from the track point snapped to the ellipsoid to the visual horizon of the observer.
104              
105             my $dist=$obj->distance_great_circle($alt, $lat);
106             my $dist=$obj->distance_great_circle($alt); #default lat => 0
107             my $dist=$obj->distance_great_circle(); #default alt => 1.7
108              
109             Formula from http://newton.ex.ac.uk/research/qsystems/people/sque/physics/horizon/
110              
111             Dc = R acos(R / (R + h))
112              
113             =cut
114              
115             sub distance_great_circle {
116 102     102 1 3850 my $self=shift();
117 102         112 my $alt=shift(); #usually meters but actaully ellipsoid units
118 102 50       304 $alt=1.7 unless defined $alt;
119 102   100     234 my $lat=shift() || 0; #degrees
120 102         206 my $R=sqrt($self->ellipsoid->n($lat) * $self->ellipsoid->rho($lat));
121 102         2802 return $R * acos($R / ($R + $alt));
122             }
123              
124             1;
125              
126             __END__