os_map_ref 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +8 -0
- data/README.md +55 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/os_map_ref/error.rb +3 -0
- data/lib/os_map_ref/input_processor.rb +89 -0
- data/lib/os_map_ref/location.rb +105 -0
- data/lib/os_map_ref/version.rb +3 -0
- data/lib/os_map_ref.rb +7 -0
- data/os_map_ref.gemspec +25 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3d3a6d62d55bbfca44fd8031064e34087eb938e9
|
4
|
+
data.tar.gz: 4793331eb8dddfd7033762b3df45bcfe188bf07a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9638109a89f0295e16fec3a4c2e03a029f9aba6c905baa21f412dda6765eb13f8d37c36d54ff3e203ba148e4bd0dc09eab0111f9f4328bc99e754778abe0fb3b
|
7
|
+
data.tar.gz: b0dcce127c708d994d3c7e96c8173a8630d911aab28434ab20324e909efc1be3eaca192a3b27dd233df12aff6f9451c49ed56a0f6c2d030910827baa7f81dfc7
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
The Open Government Licence (OGL) Version 3
|
2
|
+
|
3
|
+
Copyright (c) 2016 Environment Agency
|
4
|
+
|
5
|
+
This source code is licensed under the Open Government Licence v3.0. To view this
|
6
|
+
licence, visit www.nationalarchives.gov.uk/doc/open-government-licence/version/3
|
7
|
+
or write to the Information Policy Team, The National Archives, Kew, Richmond,
|
8
|
+
Surrey, TW9 4DU.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# OsMapRef
|
2
|
+
|
3
|
+
This gem allows you to gather U.K. Ordnance Survey Eastings, North, and Map
|
4
|
+
References from a range of text inputs.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'os_map_ref'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install os_map_ref
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
To convert a map reference to eastings and northings:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
location = OsMapRef::Location.for 'ST5880171043'
|
28
|
+
|
29
|
+
location.easting == 358801
|
30
|
+
location.northing == 171043
|
31
|
+
location.map_reference == 'ST 58801 71043'
|
32
|
+
```
|
33
|
+
|
34
|
+
To convert eastings and northings to a map reference:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
location = OsMapRef::Location.for '358801, 171043'
|
38
|
+
|
39
|
+
location.easting == 358801
|
40
|
+
location.northing == 171043
|
41
|
+
location.map_reference == 'ST 58801 71043'
|
42
|
+
```
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
47
|
+
Then, run `rake test` to run the tests. You can also run `bin/console`
|
48
|
+
for an interactive prompt that will allow you to experiment.
|
49
|
+
|
50
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/reggieb/os_map_ref.
|
55
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "os_map_ref"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
class InputProcessor
|
2
|
+
attr_reader :input
|
3
|
+
|
4
|
+
def initialize(input)
|
5
|
+
@input = input
|
6
|
+
end
|
7
|
+
|
8
|
+
def params
|
9
|
+
case input
|
10
|
+
when map_reference_pattern
|
11
|
+
map_reference_params
|
12
|
+
when easting_northing_pattern
|
13
|
+
easting_northing_params
|
14
|
+
else
|
15
|
+
raise OsMapRef::Error, "Unable to process input #{input}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def map_reference_params
|
20
|
+
{map_reference: processed_map_reference}
|
21
|
+
end
|
22
|
+
|
23
|
+
def easting_northing_params
|
24
|
+
{
|
25
|
+
easting: easting_and_northing[0],
|
26
|
+
northing: easting_and_northing[1]
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def processed_map_reference
|
31
|
+
[
|
32
|
+
grid_letters,
|
33
|
+
padded_map_reference_easting,
|
34
|
+
padded_map_reference_northing
|
35
|
+
].join(' ')
|
36
|
+
end
|
37
|
+
|
38
|
+
def map_reference_elements
|
39
|
+
@map_reference_elements ||= get_map_reference_elements
|
40
|
+
end
|
41
|
+
|
42
|
+
def get_map_reference_elements
|
43
|
+
match = map_reference_pattern.match input
|
44
|
+
(1..3).collect{|n| match[n]}
|
45
|
+
end
|
46
|
+
|
47
|
+
def grid_letters
|
48
|
+
map_reference_elements[0]
|
49
|
+
end
|
50
|
+
|
51
|
+
def map_reference_easting
|
52
|
+
map_reference_elements[1]
|
53
|
+
end
|
54
|
+
|
55
|
+
def map_reference_northing
|
56
|
+
map_reference_elements[2]
|
57
|
+
end
|
58
|
+
|
59
|
+
def padded_map_reference_easting
|
60
|
+
map_reference_easting.ljust(5, '0')
|
61
|
+
end
|
62
|
+
|
63
|
+
def padded_map_reference_northing
|
64
|
+
target_length = northing_longer_than_easting? ? 6 : 5
|
65
|
+
map_reference_northing.ljust(target_length, '0')
|
66
|
+
end
|
67
|
+
|
68
|
+
def northing_longer_than_easting?
|
69
|
+
map_reference_northing.length > map_reference_easting.length
|
70
|
+
end
|
71
|
+
|
72
|
+
def map_reference_pattern
|
73
|
+
/([a-zA-Z]{2})\s*(\d{3,5})\s*(\d{3,6})/
|
74
|
+
end
|
75
|
+
|
76
|
+
def easting_and_northing
|
77
|
+
@easting_and_northing ||= get_easting_and_northing
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_easting_and_northing
|
81
|
+
match = easting_northing_pattern.match input
|
82
|
+
(1..2).collect{|n| match[n].ljust(6, '0').to_i}
|
83
|
+
end
|
84
|
+
|
85
|
+
def easting_northing_pattern
|
86
|
+
/(\d{3,6})[\,\s]+(\d{3,6})/
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
module OsMapRef
|
3
|
+
class Location
|
4
|
+
|
5
|
+
def self.for(text)
|
6
|
+
input_processor = InputProcessor.new(text)
|
7
|
+
new input_processor.params
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(args={})
|
11
|
+
@map_reference = args[:map_reference].freeze if args[:map_reference]
|
12
|
+
@easting = args[:easting].to_i if args[:easting]
|
13
|
+
@northing = args[:northing].to_i if args[:northing]
|
14
|
+
end
|
15
|
+
|
16
|
+
def map_reference
|
17
|
+
@map_reference ||= build_map_reference.freeze
|
18
|
+
end
|
19
|
+
|
20
|
+
def build_map_reference
|
21
|
+
[grid_reference_prefix, short_easting, short_northing].join(' ')
|
22
|
+
end
|
23
|
+
|
24
|
+
def grid_easting
|
25
|
+
easting.to_s[0].to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
def short_easting
|
29
|
+
easting.to_s[1..-1].to_i
|
30
|
+
end
|
31
|
+
|
32
|
+
def long_northing?
|
33
|
+
northing.to_s.length >= 7
|
34
|
+
end
|
35
|
+
|
36
|
+
def grid_northing
|
37
|
+
northing.to_s[0..(chars_in_northing_start - 1)].to_i
|
38
|
+
end
|
39
|
+
|
40
|
+
def short_northing
|
41
|
+
northing.to_s[chars_in_northing_start..-1].to_i
|
42
|
+
end
|
43
|
+
|
44
|
+
def chars_in_northing_start
|
45
|
+
long_northing? ? 2 : 1
|
46
|
+
end
|
47
|
+
|
48
|
+
def grid_reference_prefix
|
49
|
+
grid[grid_northing][grid_easting]
|
50
|
+
end
|
51
|
+
|
52
|
+
def easting
|
53
|
+
@easting ||= easting_from_map_reference.to_i
|
54
|
+
end
|
55
|
+
|
56
|
+
def northing
|
57
|
+
@northing ||= northing_from_map_reference.to_i
|
58
|
+
end
|
59
|
+
|
60
|
+
def northing_from_map_reference
|
61
|
+
northing_easting_from_map_reference[0].to_s + map_reference_parts[2]
|
62
|
+
end
|
63
|
+
|
64
|
+
def easting_from_map_reference
|
65
|
+
northing_easting_from_map_reference[1].to_s + map_reference_parts[1]
|
66
|
+
end
|
67
|
+
|
68
|
+
# The parts should be a pair of letters then two sets of numbers
|
69
|
+
# 'ST 58901 71053' becomes ['ST', '58901', '71053']
|
70
|
+
def map_reference_parts
|
71
|
+
@map_reference_parts ||= map_reference.split
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns array of [grid_northing, grid_easting] for the gird element
|
75
|
+
# matching the map reference start. So 'ST 58901 71053' will return [1, 3]
|
76
|
+
def northing_easting_from_map_reference
|
77
|
+
@northing_easting_from_map_reference ||= matrix.index map_reference_parts[0]
|
78
|
+
end
|
79
|
+
|
80
|
+
def matrix
|
81
|
+
@matrix ||= Matrix[*grid]
|
82
|
+
end
|
83
|
+
|
84
|
+
# Grid of 100km squares as they are arranged over the UK.
|
85
|
+
# The grid is reversed so that the origin (0,0) is the
|
86
|
+
# bottom left corner ('SV').
|
87
|
+
def grid
|
88
|
+
@grid ||= [
|
89
|
+
%w[HL HM HN HO HP JL JM JH JO JP],
|
90
|
+
%w[HQ HR HS HT HU JQ JR JS JT JU],
|
91
|
+
%w[HV HW HX HY HZ JV JW JX JY JZ],
|
92
|
+
%w[NA NB NC ND NE OA OB OC OD OE],
|
93
|
+
%w[NF NG NH NJ NK OF OG OH OI OJ],
|
94
|
+
%w[NL NM NN NO NP OL OM ON OO OP],
|
95
|
+
%w[NQ NR NS NT NU OQ OR OS OT OU],
|
96
|
+
%w[NV NW NX NY NZ OV OW OX OY OZ],
|
97
|
+
%w[SA SB SC SD SE TA TB TC TD TE],
|
98
|
+
%w[SF SG SH SJ SK TF TG TH TI TJ],
|
99
|
+
%w[SL SM SN SO SP TL TM TN TO TP],
|
100
|
+
%w[SQ SR SS ST SU TQ TR TS TT TU],
|
101
|
+
%w[SV SW SX SY SZ TV TW TX TY TZ]
|
102
|
+
].reverse
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
data/lib/os_map_ref.rb
ADDED
data/os_map_ref.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'os_map_ref/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "os_map_ref"
|
8
|
+
spec.version = OsMapRef::VERSION
|
9
|
+
spec.authors = ["Rob Nichols"]
|
10
|
+
spec.email = ["rob@undervale.co.uk"]
|
11
|
+
|
12
|
+
spec.summary = %q{A tool to help handle UK Ordnance Survey map references, in particular to convert them to other coordinate systems}
|
13
|
+
spec.description = %q{This gem allows you to gather U.K. Ordnance Survey Eastings, North, and Map References from a range of text inputs; and output them in a consistent manner}
|
14
|
+
spec.homepage = "https://github.com/reggieb/os_map_ref"
|
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
|
+
spec.license = "LICENSE"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: os_map_ref
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Nichols
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-24 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: This gem allows you to gather U.K. Ordnance Survey Eastings, North, and
|
56
|
+
Map References from a range of text inputs; and output them in a consistent manner
|
57
|
+
email:
|
58
|
+
- rob@undervale.co.uk
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/os_map_ref.rb
|
72
|
+
- lib/os_map_ref/error.rb
|
73
|
+
- lib/os_map_ref/input_processor.rb
|
74
|
+
- lib/os_map_ref/location.rb
|
75
|
+
- lib/os_map_ref/version.rb
|
76
|
+
- os_map_ref.gemspec
|
77
|
+
homepage: https://github.com/reggieb/os_map_ref
|
78
|
+
licenses:
|
79
|
+
- LICENSE
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.8
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: A tool to help handle UK Ordnance Survey map references, in particular to
|
101
|
+
convert them to other coordinate systems
|
102
|
+
test_files: []
|