dx-grid 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: db60093b0328e595636bf8c4509e042a8ac3d933
4
+ data.tar.gz: f2bd0cd4d34ede6d90dcac71c0f883947fbefca6
5
+ SHA512:
6
+ metadata.gz: e17134482735828c86a3b277e661cd15bb3c0edd48d923850f40579f06bc12ef963a211f3b03b6cd2401839e35d0a33f658f0beb3144dbf52349b27585509d09
7
+ data.tar.gz: d3eeb86a04d6ebfc45c5000c3fd669854527008df158077dad56e031d1f25c4ec93408fc20891310976635671e7ef66164ad63448bb1d0bac5466a0741bc15fc
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dx-grid.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rockwell Schrock
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # DX::Grid
2
+
3
+ The `DX::Grid` module provides methods for converting between lat/long coordinate pairs and amateur radio Maidenhead grid locators. It can also validate grid coordinators.
4
+
5
+ The supported grid formats are 4-character (e.g. `FN22`) and 6-character (e.g. `FN32ab`).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dx-grid'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dx-grid
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ require 'dx/grid'
27
+ ```
28
+
29
+ ### Decoding a Grid to a Coordinate
30
+
31
+ ```ruby
32
+ DX::Grid.decode('FN22') # => [42.5, -75.0]
33
+ DX::Grid.decode('FN22ab') # => [42.0625, -75.95833333333333]
34
+ ```
35
+
36
+ ### Encoding a Coordinate to a Grid
37
+
38
+ ```ruby
39
+ DX::Grid.encode([42.481076, -75.037847]) # => 'FN22'
40
+ DX::Grid.encode([42.481076, -75.037847], :length => 4) # => 'FN22'
41
+ DX::Grid.encode([42.481076, -75.037847], :length => 6) # => 'FN22cd'
42
+ ```
43
+
44
+ ### Validating a Grid
45
+
46
+ ```ruby
47
+ DX::Grid.valid?('FN22') # => true
48
+ DX::Grid.valid?('fn22') # => true
49
+ DX::Grid.valid?('AA00aa') # => true
50
+ DX::Grid.valid?('ab12xx') # => true
51
+ DX::Grid.valid?('MO00OO') # => true
52
+ DX::Grid.valid?('lo00ol') # => true
53
+
54
+ DX::Grid.valid?('FN') # => false
55
+ DX::Grid.valid?('st12') # => false
56
+ DX::Grid.valid?('fn22yz') # => false
57
+ DX::Grid.valid?('hello world') # => false
58
+ DX::Grid.valid?(nil) # => false
59
+ DX::Grid.valid?('') # => false
60
+ DX::Grid.valid?(5) # => false
61
+ DX::Grid.valid?(Math::PI) # => false
62
+ ```
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/schrockwell/dx-grid.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dx/grid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dx-grid"
8
+ spec.version = DX::Grid::VERSION
9
+ spec.authors = ["Rockwell Schrock"]
10
+ spec.email = ["schrockwell@gmail.com"]
11
+
12
+ spec.summary = %q{Amateur radio Maidenhead grid locator conversions}
13
+ spec.homepage = "https://github.com/schrockwell/dx-grid"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.0"
24
+ end
@@ -0,0 +1,114 @@
1
+ require "dx/grid/version"
2
+
3
+ module DX
4
+ module Grid
5
+ def self.encode(coord, options={})
6
+ length = options[:length] || 4
7
+ validate_grid_length!(length)
8
+ validate_coord!(coord)
9
+
10
+ lat, lon = coord[0], coord[1]
11
+ grid = ''
12
+
13
+ # Normalize from (-90, -180) to (0, 0)
14
+ lon = lon + 180.0
15
+ lat = lat + 90.0
16
+
17
+ # Map lon from 0 to 17 (A to R)
18
+ lon_index = (lon / 20.0).to_i
19
+ lat_index = (lat / 10.0).to_i
20
+
21
+ # Convert to characters
22
+ grid += "#{('A'.ord + lon_index).chr}#{('A'.ord + lat_index).chr}"
23
+
24
+ lon -= lon_index * 20.0 # 20 degrees lon per grid
25
+ lat -= lat_index * 10.0 # 10 degrees lat per grid
26
+
27
+ # Map from 0 to 9
28
+ lon_index = (lon / 2.0).to_i
29
+ lat_index = lat.to_i
30
+
31
+ grid += "#{lon_index}#{lat_index}"
32
+
33
+ # Do the long grid if desired
34
+ if length == 6
35
+ lon -= lon_index * 2.0 # Now 2 degrees lon per grid remaining
36
+ lat -= lat_index # Now 1 degree lon per grid remaining
37
+
38
+ # Map from 0 to 23 (a to x)
39
+ lon_index = (lon / (2.0 / 24.0)).to_i
40
+ lat_index = (lat / (1.0 / 24.0)).to_i
41
+
42
+ # Convert to characters
43
+ grid += "#{('a'.ord + lon_index).chr}#{('a'.ord + lat_index).chr}"
44
+ end
45
+
46
+ grid
47
+ end
48
+
49
+ def self.decode(grid)
50
+ grid = grid.to_s
51
+ validate_grid!(grid)
52
+
53
+ lon = -180
54
+ lat = -90
55
+
56
+ ord0 = grid[0].upcase.ord - 'A'.ord
57
+ ord1 = grid[1].upcase.ord - 'A'.ord
58
+ ord2 = grid[2].to_i
59
+ ord3 = grid[3].to_i
60
+
61
+ lon += (360.0 / 18.0) * ord0 + (360.0 / 18.0 / 10.0) * ord2
62
+ lat += (180.0 / 18.0) * ord1 + (180.0 / 18.0 / 10.0) * ord3
63
+
64
+ if grid.length == 4
65
+ lon += (360.0 / 18.0 / 10.0 / 2.0)
66
+ lat += (180.0 / 18.0 / 10.0 / 2.0)
67
+ else
68
+ ord4 = grid[4].downcase.ord - 'a'.ord
69
+ ord5 = grid[5].downcase.ord - 'a'.ord
70
+
71
+ lon += (360.0 / 18.0 / 10.0 / 24.0) * (ord4 + 0.5)
72
+ lat += (180.0 / 18.0 / 10.0 / 24.0) * (ord5 + 0.5)
73
+ end
74
+
75
+ return [lat, lon]
76
+ end
77
+
78
+ def self.valid?(str)
79
+ str = str.to_s
80
+
81
+ if str.length == 4
82
+ return str.match(/[A-R]{2}[0-9]{2}/i)
83
+ elsif str.length == 6
84
+ return str.match(/[A-R]{2}[0-9]{2}[A-X]{2}/i)
85
+ end
86
+
87
+ false
88
+ end
89
+
90
+ private
91
+
92
+ def self.validate_coord!(coord)
93
+ if coord[0] == nil || coord[0] < -90 || coord[0] > 90
94
+ raise ArgumentError, 'Latitude must be between -90 and 90 degrees'
95
+ end
96
+
97
+ if coord[1] == nil || coord[1] < -180 || coord[1] > 180
98
+ raise ArgumentError, 'Longitude must be between -180 and 180 degrees'
99
+ end
100
+ end
101
+
102
+ def self.validate_grid!(grid)
103
+ if !valid?(grid)
104
+ raise ArgumentError, "Invalid grid: '#{grid}'"
105
+ end
106
+ end
107
+
108
+ def self.validate_grid_length!(length)
109
+ if length != 4 && length != 6
110
+ raise ArgumentError, 'Grid length must be 4 or 6'
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,5 @@
1
+ module DX
2
+ module Grid
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dx-grid
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Rockwell Schrock
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - schrockwell@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - dx-grid.gemspec
69
+ - lib/dx/grid.rb
70
+ - lib/dx/grid/version.rb
71
+ homepage: https://github.com/schrockwell/dx-grid
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.5.1
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Amateur radio Maidenhead grid locator conversions
95
+ test_files: []