geo_swap 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module GeoSwap
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/geo_swap.rb CHANGED
@@ -61,6 +61,20 @@ module GeoSwap
61
61
 
62
62
  module_function :lat_long_to_utm
63
63
 
64
+ def utm_to_usng(easting, northing, zone_number, zone_letter, hemisphere = 'N')
65
+ precision = 10
66
+ if hemisphere == 'S'
67
+ northing += NORTHING_OFFSET
68
+ end
69
+
70
+ letters = find_grid_letters(zone_number, northing, easting)
71
+ usngNorthing = (northing % BLOCK_SIZE).round
72
+ usngEasting = (easting % BLOCK_SIZE).round
73
+
74
+ "#{zone_number}#{zone_letter} #{letters} #{usngEasting} #{usngNorthing}"
75
+ end
76
+
77
+ module_function :utm_to_usng
64
78
 
65
79
  private
66
80
 
@@ -79,6 +93,10 @@ module GeoSwap
79
93
  EASTING_OFFSET = 500000.0
80
94
  NORTHING_OFFSET = 10000000.0
81
95
 
96
+ BLOCK_SIZE = 100000
97
+ GRIDSQUARE_SET_COL_SIZE = 8
98
+ GRIDSQUARE_SET_ROW_SIZE = 20
99
+
82
100
  def self.validate_range(lat, long)
83
101
  unless lat.between?(MIN_LATITUDE, MAX_LATITUDE) && long.between?(MIN_LONGITUDE, MAX_LONGITUDE)
84
102
  raise InputError, 'Input coordinates are invalid'
@@ -94,6 +112,61 @@ module GeoSwap
94
112
  (numerator * ecc) / denominator
95
113
  end
96
114
 
115
+ def self.find_grid_letters(zone_number, northing, easting)
116
+ row = 1
117
+ north_1m = northing.round
118
+ while north_1m >= BLOCK_SIZE do
119
+ north_1m = north_1m - BLOCK_SIZE
120
+ row += 1;
121
+ end
122
+ row = row % GRIDSQUARE_SET_ROW_SIZE
123
+
124
+ col = 0
125
+ east_1m = easting.round
126
+ while east_1m >= BLOCK_SIZE
127
+ east_1m = east_1m - BLOCK_SIZE
128
+ col += 1
129
+ end
130
+ col = col % GRIDSQUARE_SET_COL_SIZE
131
+
132
+ letters_helper(find_set(zone_number), row, col)
133
+ end
134
+
135
+ LETTERS_MAP = [
136
+ { cols: "ABCDEFGH", rows: "ABCDEFGHJKLMNPQRSTUV" },
137
+ { cols: "JKLMNPQR", rows: "FGHJKLMNPQRSTUVABCDE" },
138
+ { cols: "STUVWXYZ", rows: "ABCDEFGHJKLMNPQRSTUV" },
139
+ { cols: "ABCDEFGH", rows: "FGHJKLMNPQRSTUVABCDE" },
140
+ { cols: "JKLMNPQR", rows: "ABCDEFGHJKLMNPQRSTUV" },
141
+ { cols: "STUVWXYZ", rows: "FGHJKLMNPQRSTUVABCDE" }
142
+ ]
143
+
144
+ ZONE_TO_SET = [6, 1, 2, 3, 4, 5]
145
+
146
+ def self.find_set(zone_number)
147
+ zoneNum = zone_number % 6
148
+ ZONE_TO_SET[zoneNum.to_i] || -1
149
+ end
150
+
151
+ def self.letters_helper(set, row, col)
152
+ if row == 0
153
+ row = GRIDSQUARE_SET_ROW_SIZE - 1
154
+ else
155
+ row -= 1
156
+ end
157
+
158
+ if col == 0
159
+ col = GRIDSQUARE_SET_COL_SIZE - 1
160
+ else
161
+ col -= 1
162
+ end
163
+
164
+ l1 = l2 = nil
165
+
166
+ hash = LETTERS_MAP[set - 1]
167
+ hash[:cols][col] + hash[:rows][row]
168
+ end
169
+
97
170
  end
98
171
 
99
172
  class GeoSwap::InputError < StandardError; end
@@ -21,7 +21,7 @@ module GeoSwap
21
21
 
22
22
  it 'applys the zone formula correctly' do
23
23
  ZONE_DATA.each do |data|
24
- Zone.new(data[:long]).number.should == data[:zone_number]
24
+ Zone.new(0, data[:long]).number.should == data[:zone_number]
25
25
  end
26
26
  end
27
27
 
@@ -29,15 +29,15 @@ module GeoSwap
29
29
 
30
30
  describe 'determining the zone origin' do
31
31
  it 'can migrate from too far left' do
32
- Zone.new(-180).origin.should == -177
32
+ Zone.new(0, -180).origin.should == -177
33
33
  end
34
34
 
35
35
  it 'can migrate from too far right' do
36
- Zone.new(180).origin.should == 177
36
+ Zone.new(0, 180).origin.should == 177
37
37
  end
38
38
 
39
39
  it 'doesnt change the value when the long is already the origin' do
40
- Zone.new(3).origin.should == 3
40
+ Zone.new(0, 3).origin.should == 3
41
41
  end
42
42
  end
43
43
 
@@ -45,6 +45,12 @@ module GeoSwap
45
45
  end
46
46
  end
47
47
  end
48
+
49
+ describe 'utm_to_usng' do
50
+ it 'converts utm to usng' do
51
+ GeoSwap.utm_to_usng(560921.64, 4308133.45, 15, 'S').should == '15S WD 60922 8133'
52
+ end
53
+ end
48
54
  end
49
55
 
50
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_swap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-11 00:00:00.000000000 Z
12
+ date: 2013-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  segments:
98
98
  - 0
99
- hash: 3526420961644426813
99
+ hash: 408789171515357868
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  none: false
102
102
  requirements:
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  segments:
107
107
  - 0
108
- hash: 3526420961644426813
108
+ hash: 408789171515357868
109
109
  requirements: []
110
110
  rubyforge_project:
111
111
  rubygems_version: 1.8.23