silva 0.1.0 → 0.1.1
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.
- data/Rakefile +6 -2
- data/lib/silva/location.rb +3 -1
- data/lib/silva/system/base.rb +1 -16
- data/lib/silva/system/en.rb +6 -2
- data/lib/silva/system/gridref.rb +6 -2
- data/lib/silva/system/osgb36.rb +4 -0
- data/lib/silva/system/wgs84.rb +4 -0
- data/lib/silva/transform.rb +18 -17
- data/lib/silva/version.rb +1 -1
- data/lib/silva.rb +24 -14
- data/silva.gemspec +4 -3
- data/test/{test_bad_data.rb → silva/test_bad_data.rb} +2 -1
- data/test/{test_data.rb → silva/test_data.rb} +0 -0
- data/test/{test_en.rb → silva/test_en.rb} +2 -0
- data/test/{test_gridref.rb → silva/test_gridref.rb} +3 -1
- data/test/{test_location.rb → silva/test_location.rb} +3 -1
- data/test/{test_osgb36.rb → silva/test_osgb36.rb} +2 -0
- data/test/{test_wgs84.rb → silva/test_wgs84.rb} +2 -1
- data/test/test_helper.rb +3 -9
- metadata +17 -18
- data/lib/silva/exception.rb +0 -17
data/Rakefile
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require "bundler/gem_tasks"
|
3
|
+
require 'rake/testtask'
|
3
4
|
|
4
|
-
|
5
|
-
|
5
|
+
$:.unshift File.expand_path("../lib", __FILE__)
|
6
|
+
require "silva/version"
|
7
|
+
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.pattern = 'test/silva/test_*.rb'
|
6
10
|
end
|
data/lib/silva/location.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'silva/system'
|
2
|
+
|
1
3
|
module Silva
|
2
4
|
##
|
3
5
|
# A spoonful of syntactic sugar for creating location systems from the relevant parameters:
|
@@ -8,7 +10,7 @@ module Silva
|
|
8
10
|
module Location
|
9
11
|
##
|
10
12
|
# Create a location system from the given parameters.
|
11
|
-
#
|
13
|
+
#
|
12
14
|
# @param [Symbol] system_name The name of the system -- at present, :wgs84, :en, :osgb36 or :gridref.
|
13
15
|
# @param [Hash] options Parameters relevant to the system (see individual systems for details).
|
14
16
|
# @return [Silva::System] A new location system.
|
data/lib/silva/system/base.rb
CHANGED
@@ -1,20 +1,5 @@
|
|
1
1
|
module Silva
|
2
|
-
##
|
3
|
-
# A location system -- :wgs84, :osgb36, :en or :gridref.
|
4
2
|
module System
|
5
|
-
##
|
6
|
-
# A factory method to simplify and moderate creation of location systems.
|
7
|
-
# @param [Symbol] system_name The name of the system to be created.
|
8
|
-
# @param [Hash] options Parameters relevant to the given system.
|
9
|
-
# @return [Silva::System] A valid location system.
|
10
|
-
# @raises Silva::InvalidSystemError If the given system can't be created.
|
11
|
-
def self.create(system_name, options)
|
12
|
-
system = Silva::System.const_get(system_name.to_s.capitalize)
|
13
|
-
system.new(options)
|
14
|
-
rescue NameError
|
15
|
-
raise Silva::InvalidSystemError, "Can't create system: #{system_name}"
|
16
|
-
end
|
17
|
-
|
18
3
|
##
|
19
4
|
# Provides basic utility functions.
|
20
5
|
#
|
@@ -38,7 +23,7 @@ module Silva
|
|
38
23
|
|
39
24
|
##
|
40
25
|
# Transforms the base system to a different system.
|
41
|
-
#
|
26
|
+
#
|
42
27
|
# @param [Symbol] target_system_name The system to convert to.
|
43
28
|
# @param [Hash] options Parameters relevant to the target system.
|
44
29
|
# @return [Silva::System] A new location system of type target_system_name.
|
data/lib/silva/system/en.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
require 'silva/system/base'
|
2
|
+
require 'silva/system/osen'
|
3
|
+
require 'silva/transform'
|
4
|
+
|
1
5
|
module Silva
|
2
6
|
module System
|
3
7
|
##
|
@@ -9,8 +13,8 @@ module Silva
|
|
9
13
|
def to_s
|
10
14
|
[easting, northing].to_s
|
11
15
|
end
|
12
|
-
|
13
|
-
private
|
16
|
+
|
17
|
+
private
|
14
18
|
|
15
19
|
def to_osgb36(options = nil)
|
16
20
|
Silva::Transform.en_to_osgb36(self)
|
data/lib/silva/system/gridref.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
+
require 'silva/system/base'
|
2
|
+
require 'silva/system/osen'
|
3
|
+
require 'silva/transform'
|
4
|
+
|
1
5
|
module Silva
|
2
6
|
module System
|
3
7
|
##
|
4
8
|
# Location system representing Ordnance Survey Standard Grid References.
|
5
|
-
#
|
9
|
+
#
|
6
10
|
# Can be created given the options :easting => e, :northing => n or :gridref => g
|
7
11
|
#
|
8
12
|
class Gridref < Base
|
@@ -39,7 +43,7 @@ module Silva
|
|
39
43
|
n100k = (northing / 100000).floor
|
40
44
|
index = n100k * OSGB_GRID_WIDTH + e100k
|
41
45
|
prefix = OSGB_PREFIXES[index]
|
42
|
-
|
46
|
+
|
43
47
|
e = ((easting % OSGB_GRID_SCALE) / (10**(5 - @digits / 2))).round
|
44
48
|
n = ((northing % OSGB_GRID_SCALE) / (10**(5 - @digits / 2))).round
|
45
49
|
|
data/lib/silva/system/osgb36.rb
CHANGED
data/lib/silva/system/wgs84.rb
CHANGED
data/lib/silva/transform.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'silva/system'
|
2
|
+
|
1
3
|
module Silva
|
2
4
|
##
|
3
5
|
# Encapsulates the hairy maths required to perform the various transforms.
|
@@ -23,7 +25,7 @@ module Silva
|
|
23
25
|
LAMBDA0 = -2 * Math::PI / 180
|
24
26
|
|
25
27
|
# Helmert transform parameters
|
26
|
-
HELMERT_PARAMS = {
|
28
|
+
HELMERT_PARAMS = {
|
27
29
|
:tx=> -446.448, :ty=> 125.157, :tz=> -542.060, # m
|
28
30
|
:rx=> -0.1502, :ry=> -0.2470, :rz=> -0.8421, # sec
|
29
31
|
:s=> 20.4894 # ppm
|
@@ -42,7 +44,6 @@ module Silva
|
|
42
44
|
helmert_transform(osgb36, :wgs84, AIRY1830, HELMERT_PARAMS.inject({}) { |h, (k, v)| h[k] = v * -1; h }, GRS80)
|
43
45
|
end
|
44
46
|
|
45
|
-
|
46
47
|
##
|
47
48
|
# Convert a :wgs84 co-ordinate system to :osgb36
|
48
49
|
#
|
@@ -112,7 +113,7 @@ module Silva
|
|
112
113
|
|
113
114
|
nu, rho, eta2 = transverse_and_meridional_radii(phi, ellipsoid)
|
114
115
|
m = meridional_arc(phi, ellipsoid)
|
115
|
-
|
116
|
+
|
116
117
|
i = m + N0
|
117
118
|
ii = (nu / 2) * Math.sin(phi) * Math.cos(phi)
|
118
119
|
iii = (nu / 24) * Math.sin(phi) * Math.cos(phi)**3 * (5 - Math.tan(phi)**2 + 9 * eta2)
|
@@ -121,15 +122,15 @@ module Silva
|
|
121
122
|
v = (nu / 6) * Math.cos(phi)**3 * (nu / rho - Math.tan(phi)**2)
|
122
123
|
vi = (nu / 120) * Math.cos(phi)**5 * \
|
123
124
|
(5 - 18 * Math.tan(phi)**2 + Math.tan(phi)**4 + 14 * eta2 - 58 * Math.tan(phi)**4 * eta2)
|
124
|
-
|
125
|
+
|
125
126
|
n = i + ii * (lambda - LAMBDA0)**2 + iii * (lambda - LAMBDA0)**4 + iiia * (lambda - LAMBDA0)**6
|
126
127
|
e = E0 + iv * (lambda - LAMBDA0) + v * (lambda - LAMBDA0)**3 + vi * (lambda - LAMBDA0)**5
|
127
|
-
|
128
|
+
|
128
129
|
System.create(:en, :easting => e.round, :northing => n.round)
|
129
130
|
end
|
130
131
|
|
131
132
|
# Transform to/from :osgb36/:wgs84 co-ordinate systems.
|
132
|
-
# Algorithm from:
|
133
|
+
# Algorithm from:
|
133
134
|
# http://www.ordnancesurvey.co.uk/oswebsite/gps/information/coordinatesystemsinfo/guidecontents/guide6.html
|
134
135
|
# Portions of code from:
|
135
136
|
# http://www.harrywood.co.uk/blog/2010/06/29/ruby-code-for-converting-to-uk-ordnance-survey-coordinate-systems-from-wgs84/
|
@@ -138,40 +139,40 @@ module Silva
|
|
138
139
|
phi = to_rad(source_system.lat)
|
139
140
|
lambda = to_rad(source_system.long)
|
140
141
|
h = source_system.alt
|
141
|
-
|
142
|
+
|
142
143
|
a1 = ellipsoid_1[:a]
|
143
|
-
|
144
|
+
|
144
145
|
# convert co-ordinates to 3D Cartesian. See:
|
145
146
|
# http://www.ordnancesurvey.co.uk/oswebsite/gps/docs/A_Guide_to_Coordinate_Systems_in_Great_Britain.pdf
|
146
147
|
e_sq1 = eccentricity_squared(ellipsoid_1)
|
147
148
|
nu = a1 / Math.sqrt(1 - e_sq1 * Math.sin(phi)**2)
|
148
|
-
|
149
|
+
|
149
150
|
x1 = (nu + h) * Math.cos(phi) * Math.cos(lambda)
|
150
151
|
y1 = (nu + h) * Math.cos(phi) * Math.sin(lambda)
|
151
152
|
z1 = ((1 - e_sq1) * nu + h) * Math.sin(phi)
|
152
|
-
|
153
|
+
|
153
154
|
# apply Helmert transformation
|
154
155
|
tx = transform[:tx]
|
155
156
|
ty = transform[:ty]
|
156
157
|
tz = transform[:tz]
|
157
|
-
rx = transform[:rx] / 3600 * Math::PI / 180
|
158
|
+
rx = transform[:rx] / 3600 * Math::PI / 180
|
158
159
|
ry = transform[:ry] / 3600 * Math::PI / 180
|
159
160
|
rz = transform[:rz] / 3600 * Math::PI / 180
|
160
|
-
s1 = transform[:s] / 1e6 + 1
|
161
|
-
|
161
|
+
s1 = transform[:s] / 1e6 + 1
|
162
|
+
|
162
163
|
x2 = tx + x1 * s1 - y1 * rz + z1 * ry
|
163
164
|
y2 = ty + x1 * rz + y1 * s1 - z1 * rx
|
164
165
|
z2 = tz - x1 * ry + y1 * rx + z1 * s1
|
165
|
-
|
166
|
+
|
166
167
|
# convert 3D Cartesian co-ordinates back to lat, long, alt
|
167
168
|
a2 = ellipsoid_2[:a]
|
168
169
|
precision = 4 / a2
|
169
|
-
|
170
|
+
|
170
171
|
e_sq2 = eccentricity_squared(ellipsoid_2)
|
171
172
|
p = Math.sqrt(x2**2 + y2**2)
|
172
173
|
phi = Math.atan2(z2, p * (1 - e_sq2))
|
173
174
|
phi_prime = 2 * Math::PI
|
174
|
-
|
175
|
+
|
175
176
|
while ((phi - phi_prime).abs > precision) do
|
176
177
|
nu = a2 / Math.sqrt(1 - e_sq2 * Math.sin(phi)**2)
|
177
178
|
phi_prime = phi
|
@@ -180,7 +181,7 @@ module Silva
|
|
180
181
|
|
181
182
|
lambda = Math.atan2(y2, x2)
|
182
183
|
h = p / Math.cos(phi) - nu
|
183
|
-
|
184
|
+
|
184
185
|
System.create(target_system, :lat => to_deg(phi).round(DEGREE_ROUNDING_PLACES),\
|
185
186
|
:long => to_deg(lambda).round(DEGREE_ROUNDING_PLACES), :alt => h)
|
186
187
|
end
|
data/lib/silva/version.rb
CHANGED
data/lib/silva.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Silva makes it simple to convert WGS84 (GPS) location data to and from the UK
|
1
|
+
# Silva makes it simple to convert WGS84 (GPS) location data to and from the UK
|
2
2
|
# Ordnance Survey OSGB36 formats (grid references, eastings/northings, and longitude/latitude).
|
3
3
|
#
|
4
4
|
# Silva was inspired by the work of Chris Veness and Harry Wood:
|
@@ -6,9 +6,9 @@
|
|
6
6
|
# http://www.harrywood.co.uk/blog/2010/06/29/ruby-code-for-converting-to-uk-ordnance-survey-coordinate-systems-from-wgs84/
|
7
7
|
#
|
8
8
|
# Portions of Harry's code remain, especially in Silva::Transform::helmert_transform,
|
9
|
-
# but algorithms have been clarified so as to be easily comparable with those given by
|
9
|
+
# but algorithms have been clarified so as to be easily comparable with those given by
|
10
10
|
# Ordnance Survey.
|
11
|
-
#
|
11
|
+
#
|
12
12
|
# Usage:
|
13
13
|
# location = Silva::Location.from(:wgs84, data[:wgs84]).to(:gridref, :digits => 8)
|
14
14
|
#
|
@@ -17,14 +17,24 @@
|
|
17
17
|
# Requires:: Ruby 1.9
|
18
18
|
# License:: Provided under the FreeBSD License (http://www.freebsd.org/copyright/freebsd-license.html)
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
20
|
+
require 'silva/version'
|
21
|
+
require 'silva/location'
|
22
|
+
require 'silva/system'
|
23
|
+
|
24
|
+
module Silva
|
25
|
+
##
|
26
|
+
# Indicates that an invalid system name has been passed.
|
27
|
+
class InvalidSystemError < StandardError; end
|
28
|
+
##
|
29
|
+
# Indicates that an invalid transform has been attempted.
|
30
|
+
class InvalidTransformError < StandardError; end
|
31
|
+
##
|
32
|
+
# Indicates that an invalid parameter has been passed.
|
33
|
+
class InvalidParamError < StandardError; end
|
34
|
+
##
|
35
|
+
# Indicates that an invalid parameter value has been passed.
|
36
|
+
class InvalidParamValueError < StandardError; end
|
37
|
+
##
|
38
|
+
# Indicates that an insufficient parameters have been passed.
|
39
|
+
class InsufficientParamsError < StandardError; end
|
40
|
+
end
|
data/silva.gemspec
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'silva/version'
|
3
5
|
|
4
6
|
Gem::Specification.new do |gem|
|
5
7
|
gem.authors = ["Robert Dallas Gray"]
|
@@ -9,8 +11,7 @@ Gem::Specification.new do |gem|
|
|
9
11
|
gem.homepage = "http://github.com/rdallasgray/silva"
|
10
12
|
gem.license = "FreeBSD"
|
11
13
|
gem.required_ruby_version = ">= 1.9"
|
12
|
-
gem.add_development_dependency "test
|
13
|
-
|
14
|
+
gem.add_development_dependency "test-unit"
|
14
15
|
|
15
16
|
gem.files = `git ls-files`.split($\)
|
16
17
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
File without changes
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
1
3
|
class TestGridref < Test::Unit::TestCase
|
2
4
|
def test_gridref_no_digits
|
3
5
|
Silva::Test::DATA.each do |data|
|
@@ -5,7 +7,7 @@ class TestGridref < Test::Unit::TestCase
|
|
5
7
|
assert_equal(data[:gridref], l.to_s)
|
6
8
|
end
|
7
9
|
end
|
8
|
-
|
10
|
+
|
9
11
|
def test_gridref_to_en
|
10
12
|
Silva::Test::DATA.each do |data|
|
11
13
|
options = { :gridref => data[:gridref], :digits => 8 }
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,3 @@
|
|
1
|
-
require 'test
|
2
|
-
|
3
|
-
require_relative 'test_data'
|
4
|
-
require_relative 'test_location'
|
5
|
-
require_relative 'test_wgs84'
|
6
|
-
require_relative 'test_en'
|
7
|
-
require_relative 'test_osgb36'
|
8
|
-
require_relative 'test_gridref'
|
9
|
-
require_relative 'test_bad_data'
|
1
|
+
require 'test-unit'
|
2
|
+
require 'silva'
|
3
|
+
require_relative 'silva/test_data'
|
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.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,10 +9,10 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-08-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name: test
|
15
|
+
name: test-unit
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
@@ -41,7 +41,6 @@ files:
|
|
41
41
|
- README.md
|
42
42
|
- Rakefile
|
43
43
|
- lib/silva.rb
|
44
|
-
- lib/silva/exception.rb
|
45
44
|
- lib/silva/location.rb
|
46
45
|
- lib/silva/system/base.rb
|
47
46
|
- lib/silva/system/co_ordinate.rb
|
@@ -53,14 +52,14 @@ files:
|
|
53
52
|
- lib/silva/transform.rb
|
54
53
|
- lib/silva/version.rb
|
55
54
|
- silva.gemspec
|
56
|
-
- test/test_bad_data.rb
|
57
|
-
- test/test_data.rb
|
58
|
-
- test/test_en.rb
|
59
|
-
- test/test_gridref.rb
|
55
|
+
- test/silva/test_bad_data.rb
|
56
|
+
- test/silva/test_data.rb
|
57
|
+
- test/silva/test_en.rb
|
58
|
+
- test/silva/test_gridref.rb
|
59
|
+
- test/silva/test_location.rb
|
60
|
+
- test/silva/test_osgb36.rb
|
61
|
+
- test/silva/test_wgs84.rb
|
60
62
|
- test/test_helper.rb
|
61
|
-
- test/test_location.rb
|
62
|
-
- test/test_osgb36.rb
|
63
|
-
- test/test_wgs84.rb
|
64
63
|
homepage: http://github.com/rdallasgray/silva
|
65
64
|
licenses:
|
66
65
|
- FreeBSD
|
@@ -88,12 +87,12 @@ specification_version: 3
|
|
88
87
|
summary: Convert between the GPS (WGS84) location standard and UK Ordnance Survey
|
89
88
|
standards.
|
90
89
|
test_files:
|
91
|
-
- test/test_bad_data.rb
|
92
|
-
- test/test_data.rb
|
93
|
-
- test/test_en.rb
|
94
|
-
- test/test_gridref.rb
|
90
|
+
- test/silva/test_bad_data.rb
|
91
|
+
- test/silva/test_data.rb
|
92
|
+
- test/silva/test_en.rb
|
93
|
+
- test/silva/test_gridref.rb
|
94
|
+
- test/silva/test_location.rb
|
95
|
+
- test/silva/test_osgb36.rb
|
96
|
+
- test/silva/test_wgs84.rb
|
95
97
|
- test/test_helper.rb
|
96
|
-
- test/test_location.rb
|
97
|
-
- test/test_osgb36.rb
|
98
|
-
- test/test_wgs84.rb
|
99
98
|
has_rdoc:
|
data/lib/silva/exception.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
module Silva
|
2
|
-
##
|
3
|
-
# Indicates that an invalid system name has been passed.
|
4
|
-
class InvalidSystemError < StandardError; end
|
5
|
-
##
|
6
|
-
# Indicates that an invalid transform has been attempted.
|
7
|
-
class InvalidTransformError < StandardError; end
|
8
|
-
##
|
9
|
-
# Indicates that an invalid parameter has been passed.
|
10
|
-
class InvalidParamError < StandardError; end
|
11
|
-
##
|
12
|
-
# Indicates that an invalid parameter value has been passed.
|
13
|
-
class InvalidParamValueError < StandardError; end
|
14
|
-
##
|
15
|
-
# Indicates that an insufficient parameters have been passed.
|
16
|
-
class InsufficientParamsError < StandardError; end
|
17
|
-
end
|