silva 0.0.8 → 0.0.9

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.
@@ -9,7 +9,8 @@ module Silva
9
9
  # @return [Silva::System] A valid location system.
10
10
  # @raises Silva::InvalidSystemError If the given system can't be created.
11
11
  def self.create(system_name, options)
12
- return Silva::System.const_get(system_name.to_s.capitalize).new(options)
12
+ system = Silva::System.const_get(system_name.to_s.capitalize)
13
+ system.new(options)
13
14
  rescue NameError
14
15
  raise Silva::InvalidSystemError, "Can't create system: #{system_name}"
15
16
  end
@@ -37,14 +37,13 @@ module Silva
37
37
  unless @gridref
38
38
  e100k = (easting / 100000).floor
39
39
  n100k = (northing / 100000).floor
40
-
41
40
  index = n100k * OSGB_GRID_WIDTH + e100k
42
41
  prefix = OSGB_PREFIXES[index]
43
42
 
44
43
  e = ((easting % OSGB_GRID_SCALE) / (10**(5 - @digits / 2))).round
45
44
  n = ((northing % OSGB_GRID_SCALE) / (10**(5 - @digits / 2))).round
46
-
47
- @gridref = prefix + e.to_s.rjust(@digits / 2) + n.to_s.rjust(@digits / 2)
45
+
46
+ @gridref = prefix + e.to_s.rjust(@digits / 2, '0') + n.to_s.rjust(@digits / 2, '0')
48
47
  end
49
48
 
50
49
  @gridref
@@ -67,16 +66,21 @@ module Silva
67
66
 
68
67
  def to_en(options = nil)
69
68
  e100k, n100k = prefix_to_en
70
- gridref.delete!(' ')
71
- en = gridref[2..-1]
69
+ en = get_suffix
72
70
  e = en[0, (en.length / 2)].ljust(5, '5').to_i + e100k
73
71
  n = en[(en.length / 2)..-1].ljust(5, '5').to_i + n100k
74
72
 
75
73
  System.create(:en, :easting => e, :northing => n )
76
74
  end
77
75
 
78
- def get_prefix
79
- gridref[0..1]
76
+ def get_prefix(g = nil)
77
+ g ||= gridref
78
+ g[0..1]
79
+ end
80
+
81
+ def get_suffix(g = nil)
82
+ g ||= gridref
83
+ g[2..-1]
80
84
  end
81
85
 
82
86
  def prefix_to_en
@@ -91,8 +95,12 @@ module Silva
91
95
  [6, 8, 10].include?(digits)
92
96
  end
93
97
 
94
- def validate_gridref(gridref)
95
- gridref.match /[HJNOST][A-Z][0-9]{3,5}[0-9]{3,5}/
98
+ def validate_gridref(g)
99
+ OSGB_PREFIXES.include?(get_prefix(g)) && valid_suffix?(get_suffix(g))
100
+ end
101
+
102
+ def valid_suffix?(suffix)
103
+ suffix.length > 1 && suffix.length % 2 == 0 && suffix =~ /[0-9]+/
96
104
  end
97
105
 
98
106
  def params_satisfied?(options)
data/lib/silva/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Silva
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/test/test_data.rb CHANGED
@@ -9,11 +9,18 @@ module Silva
9
9
  },
10
10
  {
11
11
  # greenwich
12
- :wgs84 => { :lat => 51.478017, :long => -0.001619, :alt => 0},
12
+ :wgs84 => { :lat => 51.478017, :long => -0.001619 },
13
13
  :osgb36 => { :lat => 51.477501, :long => 0, :alt => 0 },
14
14
  :en => { :easting => 538874, :northing => 177344 },
15
15
  :gridref => "TQ38877734"
16
16
  }]
17
+
18
+ DATA_BAD = {
19
+ :wgs84 => { :lat => 30, :long => 10 },
20
+ :en => { :easting => 50, :northing => 220000 },
21
+ :gridref => {:gridref => "TC00502200" }
22
+ }
23
+
17
24
  LAT_DELTA = 5e-5
18
25
  LONG_DELTA = 2.5e-5
19
26
  EN_DELTA = 10
data/test/test_en.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  class TestEn < Test::Unit::TestCase
2
+
2
3
  def test_en_to_wgs84
3
4
  Silva::Test::DATA.each do |data|
4
5
  l = Silva::Location.from(:en, data[:en]).to(:wgs84)
data/test/test_helper.rb CHANGED
@@ -6,3 +6,4 @@ require_relative 'test_wgs84'
6
6
  require_relative 'test_en'
7
7
  require_relative 'test_osgb36'
8
8
  require_relative 'test_gridref'
9
+ require_relative 'test_bad_data'
@@ -26,8 +26,9 @@ class TestLocation < Test::Unit::TestCase
26
26
  def test_co_ordinates_set_correctly
27
27
  Silva::Test::DATA.each do |data|
28
28
  l = Silva::Location.from(:wgs84, data[:wgs84]).to(:wgs84)
29
- assert(l.lat == data[:wgs84][:lat] && l.long == data[:wgs84][:long] && l.alt == data[:wgs84][:alt], \
30
- "Failed assigning co-ords to System::Wgs84")
29
+ hash_data = { :lat => l.lat, :long => l.long }
30
+ hash_data[:alt] = l.alt if data[:wgs84][:alt]
31
+ assert_equal(data[:wgs84], hash_data)
31
32
  end
32
33
  end
33
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: silva
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
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: 2012-06-16 00:00:00.000000000 Z
12
+ date: 2012-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test/unit