geoutm 0.0.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,11 @@
1
+ == 1.0.0
2
+
3
+ * RDoc comments
4
+ * Changed the way options are specified
5
+ * Use of throw was replaced by raise and GeoUtm::GeoUtmException
6
+ * Refactoring
7
+ * Better support for zone 32V
8
+
1
9
  == 0.0.4
2
10
 
3
11
  Publish to gemcutter
data/README.rdoc CHANGED
@@ -6,51 +6,22 @@ GeoUtm
6
6
 
7
7
  require 'geoutm'
8
8
 
9
- coordinate = LatLon.new 100.0 100.0
10
- utm_coord = coordinate.to_utm
11
- reverse = utm_coord.to_lat_lon
9
+ coordinate = GeoUtm::LatLon.new 100.0, 100.0
10
+ to_utm = coordinate.to_utm
11
+ and_back = utm_coord.to_lat_lon
12
12
 
13
13
  =INSTALL
14
14
 
15
- sudo gem sources -a http://gems.github.com
16
- sudo gem install tallakt-geoutm
15
+ gem install geoutm
17
16
 
18
17
 
19
18
  =DESCRIPTION
20
19
 
21
- This module will translate latitude longitude coordinates to Universal Transverse Mercator(UTM) coordinates and vice versa.
20
+ This gem translates between latitude and longitude coordinates to Universal Transverse Mercator(UTM) coordinates.
21
+ The first kind is generally reported by GPS units, while the second is useful as they are represented in meters,
22
+ thus making distance calculations easier.
22
23
 
23
- This is a shameless port of the Perl library Geo::Coordinates::UTM written by Graham Crookham. Most text is copied directly from this library, adapted to ruby syntax.
24
-
25
-
26
- == Mercator Projection
27
-
28
- The Mercator projection was first invented to help mariners. They needed to be able to take a course and know the distance traveled, and draw a line on the map which showed the day's journey. In order to do this, Mercator invented a projection which preserved length, by projecting the earth's surface onto a cylinder, sharing the same axis as the earth itself.
29
- This caused all Latitude and Longitude lines to intersect at a 90 degree angle, thereby negating the problem that longitude lines get closer together at the poles.
30
-
31
- == Transverse Mercator Projection
32
-
33
- A Transverse Mercator projection takes the cylinder and turns it on its side. Now the cylinder's axis passes through the equator, and it can be rotated to line up with the area of interest. Many countries use Transverse Mercator for their grid systems.
34
-
35
- == Universal Transverse Mercator
36
-
37
- The Universal Transverse Mercator(UTM) system sets up a universal world wide system for mapping. The Transverse Mercator projection is used, with the cylinder in 60 positions. This creates 60 zones around the world.
38
- Positions are measured using Eastings and Northings, measured in meters, instead of Latitude and Longitude. Eastings start at 500,000 on the centre line of each zone.
39
- In the Northern Hemisphere, Northings are zero at the equator and increase northward. In the Southern Hemisphere, Northings start at 10 million at the equator, and decrease southward. You must know which hemisphere and zone you are in to interpret your location globally.
40
- Distortion of scale, distance, direction and area increase away from the central meridian.
41
-
42
- UTM projection is used to define horizontal positions world-wide by dividing the surface of the Earth into 6 degree zones, each mapped by the Transverse Mercator projection with a central meridian in the center of the zone.
43
- UTM zone numbers designate 6 degree longitudinal strips extending from 80 degrees South latitude to 84 degrees North latitude. UTM zone characters designate 8 degree zones extending north and south from the equator. Eastings are measured from the central meridian (with a 500 km false easting to insure positive coordinates). Northings are measured from the equator (with a 10,000 km false northing for positions south of the equator).
44
-
45
- UTM is applied separately to the Northern and Southern Hemisphere, thus within a single UTM zone, a single X / Y pair of values will occur in both the Northern and Southern Hemisphere.
46
- To eliminate this confusion, and to speed location of points, a UTM zone is sometimes subdivided into 20 zones of Latitude. These grids can be further subdivided into 100,000 meter grid squares with double-letter designations. This subdivision by Latitude and further division into grid squares is generally referred to as the Military Grid Reference System (MGRS).
47
- The unit of measurement of UTM is always meters and the zones are numbered from 1 to 60 eastward, beginning at the 180th meridian.
48
- The scale distortion in a north-south direction parallel to the central meridian (CM) is constant However, the scale distortion increases either direction away from the CM. To equalize the distortion of the map across the UTM zone, a scale factor of 0.9996 is applied to all distance measurements within the zone. The distortion at the zone boundary, 3 degrees away from the CM is approximately 1%.
49
-
50
- == Datums and Ellipsoids
51
-
52
- Unlike local surveys, which treat the Earth as a plane, the precise determination of the latitude and longitude of points over a broad area must take into account the actual shape of the Earth. To achieve the precision necessary for accurate location, the Earth cannot be assumed to be a sphere. Rather, the Earth's shape more closely approximates an ellipsoid (oblate spheroid): flattened at the poles and bulging at the Equator. Thus the Earth's shape, when cut through its polar axis, approximates an ellipse.
53
- A "Datum" is a standard representation of shape and offset for coordinates, which includes an ellipsoid and an origin. You must consider the Datum when working with geospatial data, since data with two different Datum will not line up. The difference can be as much as a kilometer!
24
+ For more information please refer to http://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system
54
25
 
55
26
  = EXAMPLES
56
27
 
@@ -58,8 +29,7 @@ A description of the available ellipsoids and sample usage of the conversion rou
58
29
 
59
30
  == Ellipsoids
60
31
 
61
- The Ellipsoids available are as follows:
62
-
32
+ Ellipsoids describe the shape of the earth used to calculate the UTM grid. As a default, WGS-84 is selected, but any of the following ellipses may be used:
63
33
 
64
34
  1. Airy
65
35
  2. Australian National
@@ -91,23 +61,8 @@ The Ellipsoids available are as follows:
91
61
  28. WGS-72
92
62
  29. WGS-84
93
63
 
94
-
95
- == ellipsoid_names
96
-
97
- The ellipsoids can be accessed using
98
-
99
- GeoUtm::Ellipsoid::list_names
100
-
101
- Alternatively
102
-
103
- GeouUtm::Ellipsoid::each {|ellipsoid| ... }
104
-
105
- When converting to and from UTM coordinates, the ellipsoid
106
- is specified as a parameter which defaults to WGS-84. To use another
107
- ellipsoid, use
108
-
109
- coord.to_utm Ellipsoid::lookup :wgs60
110
-
64
+ When specifying the ellipsoid as a parameter, you may use an instance of the GeoUtm::Ellipsoid class,
65
+ a string containing the name above, or alternatively just a symbol (eg. :wgs84).
111
66
  The name is not case sensitive and symbols are allowed. Spaces and -
112
67
  are ignored.
113
68
 
@@ -131,13 +86,15 @@ returns
131
86
  utm.n = 6409932.13416127
132
87
 
133
88
 
134
- == AUTHOR
89
+ = AUTHOR
135
90
 
91
+ This gem is heavily inspired and to a large extent also copied directly from by the Perl library Geo::Coordinates::UTM written by Graham Crookham.
136
92
  Graham Crookham, grahamc@cpan.org
137
93
 
138
- Ruby port by Tallak Tveide 2009
94
+ Ruby port by Tallak Tveide, Erling Linde 2009/2011
95
+
139
96
 
140
- == COPYRIGHT
97
+ = COPYRIGHT
141
98
 
142
99
  Copyright (c) 2000,2002,2004 by Graham Crookham. All rights reserved.
143
100
 
@@ -1,37 +1,8 @@
1
1
  module GeoUtm
2
+ # This class represents the ellipsoid used to convert from latitude/longitude into UTM coordinates. All
3
+ # operations default to using WGS-84.
2
4
  class Ellipsoid
3
- attr_reader :name, :radius, :eccentricity
4
-
5
-
6
- def initialize(name, radius, eccentricity)
7
- @name, @radius, @eccentricity = name, radius, eccentricity
8
- end
9
-
10
- def Ellipsoid.lookup(name)
11
- List[normalize_name(name.to_s)]
12
- end
13
-
14
- def Ellipsoid.list_names
15
- List.keys.sort.map do |k|
16
- List[k].name
17
- end
18
- end
19
-
20
- def Ellipsoid.each
21
- List.keys.sort do |k|
22
- yield List[k]
23
- end
24
- end
25
-
26
- private
27
-
28
- def Ellipsoid.normalize_name(name)
29
- name.gsub(/[\s\-\(\)]/, '').upcase
30
- end
31
-
32
- def Ellipsoid.generate_list
33
- result = {}
34
- data = [
5
+ ELLIPSOID_DATA = [
35
6
  [ "Airy", 6377563, 0.00667054],
36
7
  [ "Australian National", 6378160, 0.006694542],
37
8
  [ "Bessel 1841", 6377397, 0.006674372],
@@ -62,7 +33,64 @@ module GeoUtm
62
33
  [ "Everest Pakistan", 6377296, 0.006637534],
63
34
  [ "Indonesian 1974", 6378160, 0.006694609],
64
35
  ]
65
- data.each do |item|
36
+ attr_reader :name, :radius, :eccentricity
37
+
38
+
39
+ def initialize(name, radius, eccentricity)
40
+ @name, @radius, @eccentricity = name, radius, eccentricity
41
+ end
42
+
43
+ # Find a preconfigured ellipsoid
44
+ # @param [String] the name of the ellipsoid. Spaces, case and `-` are ignored
45
+ # @return [Ellipsoid]
46
+ def Ellipsoid.lookup(name)
47
+ result = List[normalize_name(name.to_s)]
48
+ raise GeoUtmException, 'Ellipsoid not found: ' + name.to_s unless result
49
+ result
50
+ end
51
+
52
+
53
+ # @see #lookup
54
+ def Ellipsoid.[](name)
55
+ lookup name
56
+ end
57
+
58
+ # Use this method when you get an ellipsoid-like as a parameter to convert to an ellipsoid
59
+ # @param [Ellipsoid, String, Symbol]
60
+ # @return [Ellipsoid]
61
+ def Ellipsoid.clean_parameter(ellipsoid_or_name)
62
+ case ellipsoid_or_name
63
+ when Ellipsoid
64
+ ellipsoid_or_name
65
+ else
66
+ lookup ellipsoid_or_name.to_s
67
+ end
68
+ end
69
+
70
+ # @return [Array<String>] A list of all the available ellipsoid names
71
+ def Ellipsoid.list_names
72
+ List.keys.sort.map do |k|
73
+ List[k].name
74
+ end
75
+ end
76
+
77
+ # Iterate over the ellipsoid names
78
+ def Ellipsoid.each
79
+ List.keys.sort do |k|
80
+ yield List[k]
81
+ end
82
+ end
83
+
84
+
85
+ # :nodoc:
86
+ def Ellipsoid.normalize_name(name)
87
+ name.gsub(/[\s\-\(\)]/, '').upcase
88
+ end
89
+
90
+ # :nodoc:
91
+ def Ellipsoid.generate_list
92
+ result = {}
93
+ ELLIPSOID_DATA.each do |item|
66
94
  el = Ellipsoid.new *item
67
95
  result[normalize_name(el.name)] = el
68
96
  end
@@ -70,6 +98,7 @@ module GeoUtm
70
98
  end
71
99
 
72
100
  List = Ellipsoid::generate_list
101
+ WGS84 = Ellipsoid[:wgs84]
73
102
  end
74
103
 
75
104
  end
@@ -0,0 +1,4 @@
1
+ module GeoUtm
2
+ class GeoUtmException < Exception
3
+ end
4
+ end
data/lib/geoutm/latlon.rb CHANGED
@@ -1,136 +1,35 @@
1
- require 'geoutm/constants'
2
1
  require 'geoutm/ellipsoid'
3
2
  require 'geoutm/utm'
3
+ require 'geoutm/geo_utm_exception'
4
4
 
5
5
  module GeoUtm
6
6
  class LatLon
7
7
  include Math
8
8
  attr_reader :lat, :lon
9
-
9
+
10
+ # Create a new coordinate instance based on latitude and longitude
11
+ #
12
+ # @param [Float] the coordinate latitude
13
+ # @param [Float] the coordinate longitude
10
14
  def initialize(lat, lon)
11
- throw RuntimeException.new("Invalid longitude #{lon}") unless (-180.0...180.0).member? lon
15
+ raise GeoUtmException, "Invalid longitude #{lon}" unless (-180.0...180.0).member? lon
12
16
  @lat, @lon = lat, lon
13
17
  end
14
18
 
19
+ # Textual representation of the coordinate
15
20
  def to_s
16
21
  north_south = if @lat >= 0.0 then 'N' else 'S' end
17
22
  east_west = if @lon >= 0.0 then 'E' else 'W' end
18
23
  '%0.6f%s %0.6f%s' % [@lat.abs, north_south, @lon.abs, east_west]
19
24
  end
20
25
 
21
- def to_utm(ellipsoid = Ellipsoid::lookup(:wgs84), zone = nil)
22
- lat_radian = Deg2Rad * @lat
23
- long_radian = Deg2Rad * long2
24
-
25
- k0 = 0.9996 # scale
26
-
27
- if zone
28
- zn, zl = UTM::split_zone zone
29
- else
30
- zn = calc_utm_zone_number
31
- zl = calc_utm_zone_letter
32
- end
33
-
34
- eccentricity = ellipsoid.eccentricity
35
- longorigin = (zn - 1) * 6 - 180 + 3
36
- longoriginradian = Deg2Rad * longorigin
37
- eccentprime = ellipsoid.eccentricity/(1.0-eccentricity)
38
-
39
- n = ellipsoid.radius / sqrt(1 - eccentricity * sin(lat_radian)*sin(lat_radian))
40
- t = tan(lat_radian) * tan(lat_radian)
41
- c = eccentprime * cos(lat_radian)*cos(lat_radian)
42
- a = cos(lat_radian) * (long_radian - longoriginradian)
43
- m = ellipsoid.radius * (
44
- (1 - eccentricity/4 - 3 * eccentricity * eccentricity/64 -
45
- 5 * eccentricity * eccentricity * eccentricity/256) * lat_radian -
46
- (3 * eccentricity/8 + 3 * eccentricity * eccentricity/32 +
47
- 45 * eccentricity * eccentricity * eccentricity/1024) * sin(2 * lat_radian) +
48
- (15 * eccentricity * eccentricity/256 +
49
- 45 * eccentricity * eccentricity * eccentricity/1024) * sin(4 * lat_radian) -
50
- (35 * eccentricity * eccentricity * eccentricity/3072) * sin(6 * lat_radian)
51
- )
52
-
53
- utm_easting = k0*n*(a+(1-t+c)*a*a*a/6 + (5-18*t+t*t+72*c-58*eccentprime)*a*a*a*a*a/120) + 500000.0
54
-
55
- utm_northing = k0 * ( m + n*tan(lat_radian) * ( a*a/2+(5-t+9*c+4*c*c)*a*a*a*a/24 +
56
- (61-58*t+t*t+600*c-330*eccentprime) * a*a*a*a*a*a/720))
57
- utm_northing += 10000000.0 if @lat < 0
58
-
59
- UTM.new '%d%s' % [zn, zl], utm_easting, utm_northing, ellipsoid
60
- end
61
-
62
- private
63
-
64
- def calc_utm_zone_letter
65
- case @lat
66
- when 72.0..84.0
67
- 'X'
68
- when 64.0..72.0
69
- 'W'
70
- when 56.0..64.0
71
- 'V'
72
- when 48.0..56.0
73
- 'U'
74
- when 40.0..48.0
75
- 'T'
76
- when 32.0..40.0
77
- 'S'
78
- when 24.0..32.0
79
- 'R'
80
- when 16.0..24.0
81
- 'Q'
82
- when 8.0..16.0
83
- 'P'
84
- when 0.0..8.0
85
- 'N'
86
- when -8.0..0.0
87
- 'M'
88
- when -16.0..-8.0
89
- 'L'
90
- when -24.0..-16.0
91
- 'K'
92
- when -32.0..-24.0
93
- 'J'
94
- when -40.0..-32.0
95
- 'H'
96
- when -48.0..-40.0
97
- 'G'
98
- when -56.0..-48.0
99
- 'F'
100
- when -64.0..-56.0
101
- 'E'
102
- when -72.0..-64.0
103
- 'D'
104
- when -80.0..-72.0
105
- 'C'
106
- else
107
- throw RuntimeException.new("Latitude #{@lat} out of UTM range")
108
- end
109
- end
110
-
111
- def long2
112
- @lon - ((@lon + 180)/360).to_i * 360
113
- end
114
-
115
- def calc_utm_zone_number
116
- zone = ((long2 + 180)/6).to_i + 1;
117
- if (56.0..64.0).member?(@lat) && (3.0..12.0).member?(long2)
118
- zone = 32
119
- end
120
-
121
- if (72.0..84.0).member? @lat
122
- case long2
123
- when 0.0..9.0
124
- zone = 31
125
- when 9.0..21.0
126
- zone = 33
127
- when 21.0..33.0
128
- zone = 35
129
- when 33.0..42.0
130
- zone = 37
131
- end
132
- end
133
- zone
26
+ # Convert the coordinate in latutude/longitude into the UTM coordinate system
27
+ #
28
+ # @option options [String,Symbol,Ellipsoid] :ellipsoid The ellipsoid to use
29
+ # @option options [String] :zone Force the coordiante into another UTM zone than it belongs. Use this to compare two coordinates in different zones
30
+ # @return [UTM] The converted UTM representation
31
+ def to_utm(options = {})
32
+ UTM::latlon_to_utm self, options
134
33
  end
135
34
  end
136
35
  end
data/lib/geoutm/utm.rb CHANGED
@@ -1,86 +1,124 @@
1
- require 'geoutm/constants'
2
1
  require 'geoutm/ellipsoid'
3
2
  require 'geoutm/latlon'
3
+ require 'enumerator'
4
+ require 'geoutm/geo_utm_exception'
5
+ require 'geoutm/utm_zones'
4
6
 
5
7
  module GeoUtm
6
8
  class UTM
7
- include Math
9
+ # Northing
10
+ attr_reader :n
11
+ # Easting
12
+ attr_reader :e
13
+ attr_reader :zone
14
+ attr_reader :ellipsoid
15
+
16
+ # Creates a new UTM coorrdinate
17
+ #
18
+ # @param [String] The UTM zone to use, eg. `32H`
19
+ # @param [Float] The easting component of the coordinate
20
+ # @param [Float] The northing component of the coordinate
21
+ # @param [String,Symbol,Ellipsoid] The ellipsoid to use
22
+ def initialize(zone, e, n, ellipsoid = Ellipsoid::WGS84)
23
+ @n, @e, @zone, @ellipsoid = n, e, zone, Ellipsoid::clean_parameter(ellipsoid)
24
+ end
25
+
26
+ #:nodoc:
27
+ def UTM.latlon_to_utm(latlon, options = {})
28
+ ellipsoid = (options[:ellipsoid] && Ellipsoid::clean_parameter(options[:ellipsoid])) || Ellipsoid::WGS84
8
29
 
9
- attr_reader :n, :e, :zone, :zone_number, :zone_letter, :ellipsoid
30
+ lat_rad = Math::PI / 180.0 * latlon.lat
31
+ k0 = 0.9996 # scale
32
+ zone = options[:zone] || UTMZones::calc_utm_zone(latlon.lat, latlon.lon)
10
33
 
11
- def initialize(zone, e, n, ellipsoid = Ellipsoid::lookup(:wgs84))
12
- @n, @e, @zone, @ellipsoid = n, e, zone, ellipsoid
13
- @zone_number, @zone_letter = UTM::split_zone @zone
34
+ eccentricity = ellipsoid.eccentricity
35
+ eccentprime = ellipsoid.eccentricity/(1.0-eccentricity)
36
+
37
+ n = ellipsoid.radius / Math::sqrt(1 - eccentricity * Math::sin(lat_rad)*Math::sin(lat_rad))
38
+ t = Math::tan(lat_rad) * Math::tan(lat_rad)
39
+ c = eccentprime * Math::cos(lat_rad)*Math::cos(lat_rad)
40
+ a = Math::cos(lat_rad) * Math::PI / 180.0 * (UTMZones::clean_longitude(latlon.lon) - UTMZones::lon_origin(zone))
41
+ m = ellipsoid.radius * (
42
+ (1 - eccentricity/4 - 3 * eccentricity * eccentricity/64 -
43
+ 5 * eccentricity * eccentricity * eccentricity/256) * lat_rad -
44
+ (3 * eccentricity/8 + 3 * eccentricity * eccentricity/32 +
45
+ 45 * eccentricity * eccentricity * eccentricity/1024) * Math::sin(2 * lat_rad) +
46
+ (15 * eccentricity * eccentricity/256 +
47
+ 45 * eccentricity * eccentricity * eccentricity/1024) * Math::sin(4 * lat_rad) -
48
+ (35 * eccentricity * eccentricity * eccentricity/3072) * Math::sin(6 * lat_rad)
49
+ )
50
+ utm_easting = k0*n*(a+(1-t+c)*a*a*a/6 + (5-18*t+t*t+72*c-58*eccentprime)*a*a*a*a*a/120) + 500000.0
51
+ utm_northing = k0 * ( m + n*Math::tan(lat_rad) * ( a*a/2+(5-t+9*c+4*c*c)*a*a*a*a/24 +
52
+ (61-58*t+t*t+600*c-330*eccentprime) * a*a*a*a*a*a/720))
53
+ utm_northing += 10000000.0 if latlon.lat < 0
54
+ UTM.new zone, utm_easting, utm_northing, ellipsoid
14
55
  end
15
56
 
57
+ # Returns the band letter portion of the zone, eg `H` for `32H`
58
+ def zone_letter
59
+ UTMZones::split_zone(@zone).last
60
+ end
61
+
62
+ alias :zone_band :zone_letter
63
+
64
+ # Returns the number portion of the zone, eg `32` for `32H`
65
+ def zone_number
66
+ UTMZones::split_zone(@zone).first.to_i
67
+ end
68
+
69
+
16
70
  def to_s
17
71
  '%s %.2f %.2f' % [zone, e, n]
18
72
  end
19
73
 
74
+ # Converts this position into the latitude/longitude coordinate system
75
+ # @return [LatLon]
20
76
  def to_lat_lon
21
77
  k0 = 0.9996
22
78
  x = @e - 500000 # Remove Longitude offset
23
79
  y = @n
24
80
 
25
81
  # Set hemisphere (1=Northern, 0=Southern)
26
- y -= 10000000.0 unless northern_hemisphere?
82
+ y -= 10000000.0 unless UTMZones::northern_hemisphere? @zone
27
83
 
28
- longorigin = (@zone_number - 1)*6 - 180 + 3
29
84
  ecc = @ellipsoid.eccentricity
30
85
  eccPrimeSquared = (ecc)/(1-ecc)
31
86
  m = y / k0
32
87
  mu = m / (@ellipsoid.radius * (1 - ecc / 4 - 3 * ecc ** 2 / 64 - 5 * ecc ** 3 / 256))
33
- e1 = (1 - sqrt(1 - ecc)) / (1 + sqrt(1 - ecc))
88
+ e1 = (1 - Math::sqrt(1 - ecc)) / (1 + Math::sqrt(1 - ecc))
34
89
  phi1rad = mu +
35
- (3 * e1 / 2 - 27 * e1 ** 3 / 32) * sin(2 * mu) +
36
- (21 * e1 ** 2 / 16 - 55 * e1 ** 4 / 32) * sin(4 * mu) +
37
- (151 * e1 ** 3 / 96) * sin(6 * mu)
38
- phi1 = phi1rad * Rad2Deg
39
- n1 = @ellipsoid.radius / sqrt(1 - ecc * sin(phi1rad) ** 2)
40
- t1 = tan(phi1rad) ** 2
41
- c1 = ecc * cos(phi1rad)**2
42
- r1 = @ellipsoid.radius * (1 - ecc) / ((1 - ecc * sin(phi1rad) ** 2) ** 1.5)
90
+ (3 * e1 / 2 - 27 * e1 ** 3 / 32) * Math::sin(2 * mu) +
91
+ (21 * e1 ** 2 / 16 - 55 * e1 ** 4 / 32) * Math::sin(4 * mu) +
92
+ (151 * e1 ** 3 / 96) * Math::sin(6 * mu)
93
+ n1 = @ellipsoid.radius / Math::sqrt(1 - ecc * Math::sin(phi1rad) ** 2)
94
+ t1 = Math::tan(phi1rad) ** 2
95
+ c1 = ecc * Math::cos(phi1rad)**2
96
+ r1 = @ellipsoid.radius * (1 - ecc) / ((1 - ecc * Math::sin(phi1rad) ** 2) ** 1.5)
43
97
  d = x / (n1 * k0)
44
- latitude = phi1rad-
45
- (n1 * tan(phi1rad) / r1) * (d * d / 2 -
98
+ latitude_rad = phi1rad-
99
+ (n1 * Math::tan(phi1rad) / r1) * (d * d / 2 -
46
100
  (5 + 3 * t1 + 10 * c1 - 4 * c1 * c1 - 9 * eccPrimeSquared) * d ** 4 / 24 +
47
101
  (61 + 90 * t1 + 298 * c1 + 45 * t1 * t1 -
48
102
  252 * eccPrimeSquared - 3 * c1 * c1) * d ** 6 / 720)
49
- latitude = latitude * Rad2Deg
50
- longitude = (d - (1 + 2 * t1 + c1) * d ** 3 / 6 +
103
+ latitude_deg = latitude_rad * 180.0 / Math::PI
104
+ lon_tmp = (d - (1 + 2 * t1 + c1) * d ** 3 / 6 +
51
105
  (5 - 2 * c1 + 28 * t1 - 3 * c1 * c1 + 8 * eccPrimeSquared +
52
- 24 * t1 * t1) * d ** 5 / 120) / cos(phi1rad)
53
- longitude = longorigin + longitude * Rad2Deg
54
- LatLon.new latitude, longitude
106
+ 24 * t1 * t1) * d ** 5 / 120) / Math::cos(phi1rad)
107
+ longitude_deg = UTMZones::lon_origin(@zone) + lon_tmp * 180.0 / Math::PI
108
+ LatLon.new latitude_deg, longitude_deg
55
109
  end
56
110
 
111
+ # The euclidian distance between two points (ie. `sqrt(dx * dx + dy * dy)`)
112
+ # @param [LatLon,UTM] The other coordinate. Should be in close proximity to avoid large errors
113
+ # @return [Float]
57
114
  def distance_to(other)
58
115
  if other.class == LatLon
59
- other = other.to_utm(@ellipsoid, @zone)
116
+ other = UTM::latlon_to_utm other, :ellipsoid => @ellipsoid, :zone => @zone
60
117
  end
61
118
  unless other.zone == @zone
62
- throw RuntimeError.new('Cannot calc distance for points in different zones - convert first')
119
+ raise GeoUtmException, 'Cannot calc distance for points in different zones - convert first'
63
120
  end
64
- sqrt((@n - other.n) ** 2.0 + (@e - other.e) ** 2.0)
65
- end
66
-
67
- def UTM.split_zone(zone_in)
68
- m = zone_in.match /^(\d+)([CDEFGHJKLMNPQRSTUVWX])$/
69
- throw RuntimeError.new('Illegal zone: ' + zone_in) unless m
70
- zn, zl = m[1].to_i, m[2]
71
- throw RuntimeError.new('Illegal zone: ' + zone_in) unless (1..60).member? zn
72
- return zn, zl
73
- end
74
-
75
- def UTM.validate_zone(zone_in)
76
- UTM::split_zone zone_in # throw exception
77
- true
78
- end
79
-
80
- private
81
-
82
- def northern_hemisphere?
83
- @zone_letter.match /[NPQRSTUVWX]/
121
+ Math::sqrt((@n - other.n) ** 2.0 + (@e - other.e) ** 2.0)
84
122
  end
85
123
  end
86
124
  end
@@ -0,0 +1,100 @@
1
+ require 'geoutm/geo_utm_exception'
2
+
3
+ module GeoUtm
4
+ # This module is used by the UTM class and is generally never used directly
5
+ module UTMZones
6
+ # :nodoc:
7
+ SPECIAL_ZONES = {
8
+ '31V' => {:lat => (56.0..64.0), :lon => (0.0..3.0), :lon_origin => 3.0},
9
+ '32V' => {:lat => (56.0..64.0), :lon => (3.0..12.0), :lon_origin => 15.0},
10
+ '31X' => {:lat => (72.0..84.0), :lon => (0.0..9.0), :lon_origin => 3.0},
11
+ '33X' => {:lat => (72.0..84.0), :lon => (9.0..21.0), :lon_origin => 15.0},
12
+ '35X' => {:lat => (72.0..84.0), :lon => (21.0..33.0), :lon_origin => 27.0},
13
+ '37X' => {:lat => (72.0..84.0), :lon => (33.0..42.0), :lon_origin => 39.0}
14
+ }
15
+
16
+ BANDS = {
17
+ 'X' => 72.0..84.0,
18
+ 'W' => 64.0..72.0,
19
+ 'V' => 56.0..64.0,
20
+ 'U' => 48.0..56.0,
21
+ 'T' => 40.0..48.0,
22
+ 'S' => 32.0..40.0,
23
+ 'R' => 24.0..32.0,
24
+ 'Q' => 16.0..24.0,
25
+ 'P' => 8.0..16.0,
26
+ 'N' => 0.0..8.0,
27
+ 'M' => -8.0..0.0,
28
+ 'L' => -16.0..-8.0,
29
+ 'K' => -24.0..-16.0,
30
+ 'J' => -32.0..-24.0,
31
+ 'H' => -40.0..-32.0,
32
+ 'G' => -48.0..-40.0,
33
+ 'F' => -56.0..-48.0,
34
+ 'E' => -64.0..-56.0,
35
+ 'D' => -72.0..-64.0,
36
+ 'C' => -80.0..-72.0,
37
+ }
38
+
39
+ # :nodoc:
40
+ def UTMZones.calc_utm_zone(lat, lon)
41
+ search_for_special_zones(lat, lon) || calc_utm_default_zone(lat, lon)
42
+ end
43
+
44
+ # :nodoc:
45
+ def UTMZones.search_for_special_zones(lat, lon)
46
+ result = SPECIAL_ZONES.find {|k, v| v[:lat].member?(lat) && v[:lon].member?(lon)}
47
+ result && result.first
48
+ end
49
+
50
+ # :nodoc:
51
+ def UTMZones.calc_utm_default_zone(lat, lon)
52
+ '%d%s' % [((clean_longitude(lon) + 180)/6).to_i + 1, calc_utm_default_letter(lat)]
53
+ end
54
+
55
+ # :nodoc:
56
+ def UTMZones.lon_origin(zone)
57
+ sp = SPECIAL_ZONES[zone]
58
+ (sp && sp[:lon_origin]) || (zone_number_from_zone(zone) - 1) * 6 - 180 + 3
59
+ end
60
+
61
+ # :nodoc:
62
+ def UTMZones.calc_utm_default_letter(lat)
63
+ result = BANDS.find {|letter, lats| lats.member?(lat) }
64
+ raise GeoUtmException, "Latitude #{lat} out of UTM range" unless result
65
+ result.first
66
+ end
67
+
68
+ # :nodoc:
69
+ def UTMZones.zone_number_from_zone(zone)
70
+ UTMZones.split_zone(zone).first.to_i
71
+ end
72
+
73
+ # :nodoc:
74
+ def UTMZones.split_zone(zone_in)
75
+ m = zone_in.match /^(\d+)([CDEFGHJKLMNPQRSTUVWX])$/
76
+ raise GeoUtmException, 'Illegal zone: ' + zone_in unless m
77
+ zn, zl = m[1].to_i, m[2]
78
+ raise GeoUtmException, 'Illegal zone: ' + zone_in unless (1..60).member? zn
79
+ return zn, zl
80
+ end
81
+
82
+ # :nodoc:
83
+ def UTMZones.validate_zone(zone_in)
84
+ UTMZones.split_zone zone_in # throw exception
85
+ true
86
+ end
87
+
88
+ # :nodoc:
89
+ def UTMZones.northern_hemisphere?(zone)
90
+ zone.match /[NPQRSTUVWX]$/
91
+ end
92
+
93
+ # :nodoc:
94
+ def UTMZones.clean_longitude(lon)
95
+ lon - ((lon + 180)/360).to_i * 360
96
+ end
97
+
98
+ end
99
+ end
100
+
data/lib/geoutm.rb CHANGED
@@ -5,5 +5,5 @@ require 'geoutm/utm'
5
5
  require 'geoutm/latlon'
6
6
 
7
7
  module GeoUtm
8
- VERSION = '0.0.4'
8
+ VERSION = '1.0.0'
9
9
  end
@@ -20,5 +20,9 @@ module GeoUtm
20
20
  it 'should lookup with symbol names' do
21
21
  Ellipsoid::lookup(:wgs84).should be(@wgs84)
22
22
  end
23
+
24
+ it 'should rasie an exception on bad ellipsoid names' do
25
+ lambda { Ellipsoid::lookup :dummy }.should raise_error
26
+ end
23
27
  end
24
28
  end
data/spec/geoutm_spec.rb CHANGED
@@ -19,20 +19,19 @@ module GeoUtm
19
19
  it "should convert from lat/lon to UTM" do
20
20
  @testdata.each do |sample|
21
21
  latlon = LatLon.new sample[:latitude].to_f, sample[:longitude].to_f
22
- utm = latlon.to_utm Ellipsoid::lookup(sample[:ellipsoid])
23
- utm.n.should be_close(sample[:northing].to_f, 0.01)
24
- utm.e.should be_close(sample[:easting].to_f, 0.01)
22
+ utm = latlon.to_utm :ellipsoid => sample[:ellipsoid]
23
+ utm.n.should be_within(0.01).of(sample[:northing].to_f)
24
+ utm.e.should be_within(0.01).of(sample[:easting].to_f)
25
25
  utm.zone.should == sample[:zone]
26
26
  end
27
27
  end
28
28
 
29
29
  it "should convert from UTM to lat/lon" do
30
30
  @testdata.each do |sample|
31
- utm = UTM.new sample[:zone], sample[:easting].to_f, sample[:northing].to_f,
32
- Ellipsoid::lookup(sample[:ellipsoid])
31
+ utm = UTM.new sample[:zone], sample[:easting].to_f, sample[:northing].to_f, sample[:ellipsoid]
33
32
  latlon = utm.to_lat_lon
34
- latlon.lat.should be_close(sample[:latitude].to_f, 0.01)
35
- latlon.lon.should be_close(sample[:longitude].to_f, 0.01)
33
+ latlon.lat.should be_within(0.01).of(sample[:latitude].to_f)
34
+ latlon.lon.should be_within(0.01).of(sample[:longitude].to_f)
36
35
  end
37
36
  end
38
37
  end
data/spec/latlon_spec.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper.rb'
2
2
 
3
3
  module GeoUtm
4
- # http://rspec.info/
5
4
  describe LatLon do
6
5
  before :each do
7
6
  # some random points
@@ -13,5 +12,20 @@ module GeoUtm
13
12
  @p1.to_s.should == '25.966877S 176.847283E'
14
13
  @p2.to_s.should == '62.664347N 18.131801W'
15
14
  end
15
+
16
+ it 'should raise an exception unless longitude is outside -180..180' do
17
+ lambda { LatLon.new 0.0, -200.0 }.should raise_error(GeoUtm::GeoUtmException)
18
+ lambda { LatLon.new 0.0, 200.0 }.should raise_error(GeoUtm::GeoUtmException)
19
+ end
20
+
21
+ it 'should raise an exception unless latitude is outside -80..84' do
22
+ lambda { LatLon.new(-81.0, 0.0).to_utm }.should raise_error(GeoUtm::GeoUtmException)
23
+ lambda { LatLon.new( 85.0, 0.0).to_utm }.should raise_error(GeoUtm::GeoUtmException)
24
+ end
25
+
26
+ it 'should accept an ellipsoid name in to_utm initializer' do
27
+ @p1.to_utm :ellipsoid => :wgs84
28
+ lambda {@p1.to_utm :ellipsoid => :wgs84}.should_not raise_error
29
+ end
16
30
  end
17
31
  end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ begin
3
3
  rescue LoadError
4
4
  require 'rubygems'
5
5
  gem 'rspec'
6
- require 'spec'
6
+ require 'rspec'
7
7
  end
8
8
 
9
9
  $:.unshift(File.dirname(__FILE__) + '/../lib')
data/spec/testdata.yaml CHANGED
@@ -474,11 +474,11 @@
474
474
  :longitude: "-11.4023259491394"
475
475
  :ellipsoid: Australian National
476
476
  - :zone: 32V
477
- :latitude: "58.8522081830389"
478
- :easting: "564252.730916659"
479
- :northing: "6524279.99360641"
480
- :longitude: "10.1135752098733"
481
- :ellipsoid: International
477
+ :latitude: "59.102298"
478
+ :easting: "285505.27"
479
+ :northing: "6557462.78"
480
+ :longitude: "11.254186"
481
+ :ellipsoid: WGS-84
482
482
  - :zone: 48W
483
483
  :latitude: "68.5749166287382"
484
484
  :easting: "507579.568049395"
data/spec/utm_spec.rb CHANGED
@@ -9,12 +9,30 @@ module GeoUtm
9
9
  @p2 = UTM.new '27E', 646897.012158895, 3049077.01849759, Ellipsoid::lookup(:airy)
10
10
  @p3 = UTM.new '37T', 581477.812337138, 5020289.06897684, Ellipsoid::lookup('bessel 1841')
11
11
  @p4 = UTM.new '37T', 677938.186800496, 5048262.27080925, Ellipsoid::lookup('bessel 1841')
12
+ @p5 = UTM.new '32V', 285505.3, 6557462.8
13
+ end
14
+
15
+ it "should report zone number" do
16
+ @p1.zone_number.should == 60
17
+ @p2.zone_number.should == 27
18
+ @p3.zone_number.should == 37
19
+ @p4.zone_number.should == 37
20
+ @p5.zone_number.should == 32
21
+ end
22
+
23
+
24
+ it 'should report zone bands' do
25
+ @p1.zone_band.should == 'J'
26
+ @p2.zone_band.should == 'E'
27
+ @p3.zone_band.should == 'T'
28
+ @p4.zone_band.should == 'T'
29
+ @p5.zone_band.should == 'V'
12
30
  end
13
31
 
14
32
  it "should calculate distance between points" do
15
- @p3.distance_to(@p4).should be_close(100434.575034537, 0.0001)
16
- @p4.distance_to(@p3).should be_close(100434.575034537, 0.0001)
17
- @p4.distance_to(@p4).should be_close(0.0, 0.0001)
33
+ @p3.distance_to(@p4).should be_within(0.0001).of(100434.575034537)
34
+ @p4.distance_to(@p3).should be_within(0.0001).of(100434.575034537)
35
+ @p4.distance_to(@p4).should be_within(0.0001).of(0.0)
18
36
  end
19
37
 
20
38
  it "should not calculate distances between different zones" do
@@ -22,7 +40,11 @@ module GeoUtm
22
40
  end
23
41
 
24
42
  it "should calculate distance to LatLon coordinate" do
25
- @p3.distance_to(@p4.to_lat_lon).should be_close(100434.575034537, 0.001)
43
+ @p3.distance_to(@p4.to_lat_lon).should be_within(0.001).of(100434.575034537)
44
+ end
45
+
46
+ it "should convert correctly for 32V" do
47
+ @p5.to_lat_lon.to_s.should == "59.102298N 11.254186E"
26
48
  end
27
49
 
28
50
  it 'should format as string' do
@@ -30,19 +52,28 @@ module GeoUtm
30
52
  end
31
53
 
32
54
  it 'should not accept invalid zone letters' do
33
- lambda {UTM::split_zone '56Y'}.should raise_error
34
- lambda {UTM::split_zone '56I'}.should raise_error
35
- lambda {UTM::split_zone '56O'}.should raise_error
36
- lambda {UTM::split_zone '56A'}.should raise_error
55
+ lambda {UTMZones::split_zone '56Y'}.should raise_error
56
+ lambda {UTMZones::split_zone '56I'}.should raise_error
57
+ lambda {UTMZones::split_zone '56O'}.should raise_error
58
+ lambda {UTMZones::split_zone '56A'}.should raise_error
37
59
  end
38
60
 
39
61
  it 'should not accept invalid zone numbers' do
40
- lambda {UTM::split_zone '00C'}.should raise_error
41
- lambda {UTM::split_zone '61C'}.should raise_error
62
+ lambda {UTMZones::split_zone '00C'}.should raise_error
63
+ lambda {UTMZones::split_zone '61C'}.should raise_error
42
64
  end
43
65
 
44
66
  it 'should accept valid UTM zones' do
45
- lambda {UTM::split_zone '51C'}.should_not raise_error
67
+ lambda {UTMZones::split_zone '51C'}.should_not raise_error
68
+ end
69
+
70
+ it 'should calculate correct longitude origin' do
71
+ UTMZones::lon_origin('32U').should == 9
72
+ UTMZones::lon_origin('32R').should == 9
73
+ UTMZones::lon_origin('32W').should == 9
74
+ UTMZones::lon_origin('32V').should == 15
75
+ UTMZones::lon_origin('32X').should == 9
76
+ UTMZones::lon_origin('40H').should == 57
46
77
  end
47
78
  end
48
79
  end
metadata CHANGED
@@ -1,89 +1,76 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: geoutm
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 4
9
- version: 0.0.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Tallak Tveide
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-05-24 00:00:00 +02:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rspec
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
16
+ requirement: &78899510 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
30
22
  type: :development
31
- version_requirements: *id001
23
+ prerelease: false
24
+ version_requirements: *78899510
32
25
  description: Conversion between latitude and longitude coordinates and UTM coordiantes
33
- email:
26
+ email:
34
27
  - tallak@tveide.net
35
28
  executables: []
36
-
37
29
  extensions: []
38
-
39
- extra_rdoc_files:
30
+ extra_rdoc_files:
40
31
  - README.rdoc
41
- files:
42
- - spec/utm_spec.rb
43
- - spec/spec.opts
44
- - spec/latlon_spec.rb
32
+ files:
45
33
  - spec/geoutm_spec.rb
46
34
  - spec/ellipsoid_spec.rb
47
- - spec/spec_helper.rb
35
+ - spec/spec.opts
36
+ - spec/utm_spec.rb
48
37
  - spec/testdata.yaml
49
- - lib/geoutm/utm.rb
38
+ - spec/latlon_spec.rb
39
+ - spec/spec_helper.rb
40
+ - lib/geoutm.rb
50
41
  - lib/geoutm/latlon.rb
51
- - lib/geoutm/constants.rb
52
42
  - lib/geoutm/ellipsoid.rb
53
- - lib/geoutm.rb
43
+ - lib/geoutm/utm.rb
44
+ - lib/geoutm/geo_utm_exception.rb
45
+ - lib/geoutm/utm_zones.rb
54
46
  - LICENCE
55
47
  - README.rdoc
56
48
  - History.txt
57
- has_rdoc: true
58
49
  homepage: http://www.github.com/tallakt/geoutm
59
50
  licenses: []
60
-
61
51
  post_install_message:
62
- rdoc_options:
52
+ rdoc_options:
63
53
  - --main
64
54
  - README.rdoc
65
- require_paths:
55
+ require_paths:
66
56
  - - lib
67
- required_ruby_version: !ruby/object:Gem::Requirement
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- segments:
72
- - 0
73
- version: "0"
74
- required_rubygems_version: !ruby/object:Gem::Requirement
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- segments:
79
- - 0
80
- version: "0"
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
81
69
  requirements: []
82
-
83
70
  rubyforge_project: geoutm
84
- rubygems_version: 1.3.6
71
+ rubygems_version: 1.8.10
85
72
  signing_key:
86
73
  specification_version: 3
87
74
  summary: Converting map coordinates
88
75
  test_files: []
89
-
76
+ has_rdoc: true
@@ -1,4 +0,0 @@
1
- module GeoUtm
2
- Deg2Rad = Math::PI / 180.0
3
- Rad2Deg = 180.0 / Math::PI
4
- end