silva 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/silva/system/base.rb +2 -1
- data/lib/silva/system/gridref.rb +17 -9
- data/lib/silva/version.rb +1 -1
- data/test/test_data.rb +8 -1
- data/test/test_en.rb +1 -0
- data/test/test_helper.rb +1 -0
- data/test/test_location.rb +3 -2
- metadata +2 -2
data/lib/silva/system/base.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/silva/system/gridref.rb
CHANGED
@@ -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
|
-
|
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
|
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(
|
95
|
-
|
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
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
|
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
data/test/test_helper.rb
CHANGED
data/test/test_location.rb
CHANGED
@@ -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
|
-
|
30
|
-
|
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.
|
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-
|
12
|
+
date: 2012-06-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: test/unit
|