Module: SmoWgs84ToBng

Defined in:
lib/smo_wgs84_to_bng.rb,
lib/smo_wgs84_to_bng/errors.rb,
lib/smo_wgs84_to_bng/version.rb,
lib/smo_wgs84_to_bng/constants.rb,
lib/smo_wgs84_to_bng/validator.rb,
lib/smo_wgs84_to_bng/bng_to_wgs84.rb,
lib/smo_wgs84_to_bng/wgs84_to_bng.rb

Defined Under Namespace

Modules: BngToWgs84, Constants, Validator, Wgs84ToBng Classes: Error, InvalidCoordinateError, MissingCoordinateError, MissingIdError, OutOfBoundsError

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.convert_many_to_array(points) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/smo_wgs84_to_bng.rb', line 41

def self.convert_many_to_array(points)
  points.each_with_index.map do |pt, i|
    id  = pt[:id]
    lat = pt[:lat]
    lon = pt[:lon]
    Validator.validate_wgs84!(id: id, lat: lat, lon: lon, index: i)
    e, n = Wgs84ToBng.convert(lat.to_f, lon.to_f)
    [id, e, n]
  end
end

.convert_many_to_hash(points) ⇒ Object

Batch: accepts array of hashes with :id, :lat, :lon (plus optional extras)



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smo_wgs84_to_bng.rb', line 29

def self.convert_many_to_hash(points)
  points.each_with_index.map do |pt, i|
    id  = pt[:id]
    lat = pt[:lat]
    lon = pt[:lon]
    Validator.validate_wgs84!(id: id, lat: lat, lon: lon, index: i)
    extra = pt.reject { |k, _| [:id, :lat, :lon].include?(k) }
    e, n = Wgs84ToBng.convert(lat.to_f, lon.to_f)
    { id: id, easting: e, northing: n }.merge(extra)
  end
end

.convert_many_to_json(points) ⇒ Object



52
53
54
# File 'lib/smo_wgs84_to_bng.rb', line 52

def self.convert_many_to_json(points)
  convert_many_to_hash(points).to_json
end

.convert_to_array(id:, lat:, lon:, **_extra) ⇒ Object



18
19
20
21
22
# File 'lib/smo_wgs84_to_bng.rb', line 18

def self.convert_to_array(id:, lat:, lon:, **_extra)
  Validator.validate_wgs84!(id: id, lat: lat, lon: lon)
  e, n = Wgs84ToBng.convert(lat.to_f, lon.to_f)
  [id, e, n]
end

.convert_to_hash(id:, lat:, lon:, **extra) ⇒ Object

– Forward: WGS84 -> BNG ————————————————–



12
13
14
15
16
# File 'lib/smo_wgs84_to_bng.rb', line 12

def self.convert_to_hash(id:, lat:, lon:, **extra)
  Validator.validate_wgs84!(id: id, lat: lat, lon: lon)
  e, n = Wgs84ToBng.convert(lat.to_f, lon.to_f)
  { id: id, easting: e, northing: n }.merge(extra)
end

.convert_to_json(id:, lat:, lon:, **extra) ⇒ Object



24
25
26
# File 'lib/smo_wgs84_to_bng.rb', line 24

def self.convert_to_json(id:, lat:, lon:, **extra)
  convert_to_hash(id: id, lat: lat, lon: lon, **extra).to_json
end

.reverse_many_to_array(points) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/smo_wgs84_to_bng.rb', line 86

def self.reverse_many_to_array(points)
  points.each_with_index.map do |pt, i|
    id       = pt[:id]
    easting  = pt[:easting]
    northing = pt[:northing]
    Validator.validate_bng!(id: id, easting: easting, northing: northing, index: i)
    lat, lon = BngToWgs84.convert(easting.to_f, northing.to_f)
    [id, lat, lon]
  end
end

.reverse_many_to_hash(points) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smo_wgs84_to_bng.rb', line 74

def self.reverse_many_to_hash(points)
  points.each_with_index.map do |pt, i|
    id       = pt[:id]
    easting  = pt[:easting]
    northing = pt[:northing]
    Validator.validate_bng!(id: id, easting: easting, northing: northing, index: i)
    extra = pt.reject { |k, _| [:id, :easting, :northing].include?(k) }
    lat, lon = BngToWgs84.convert(easting.to_f, northing.to_f)
    { id: id, lat: lat, lon: lon }.merge(extra)
  end
end

.reverse_many_to_json(points) ⇒ Object



97
98
99
# File 'lib/smo_wgs84_to_bng.rb', line 97

def self.reverse_many_to_json(points)
  reverse_many_to_hash(points).to_json
end

.reverse_to_array(id:, easting:, northing:, **_extra) ⇒ Object



64
65
66
67
68
# File 'lib/smo_wgs84_to_bng.rb', line 64

def self.reverse_to_array(id:, easting:, northing:, **_extra)
  Validator.validate_bng!(id: id, easting: easting, northing: northing)
  lat, lon = BngToWgs84.convert(easting.to_f, northing.to_f)
  [id, lat, lon]
end

.reverse_to_hash(id:, easting:, northing:, **extra) ⇒ Object

– Reverse: BNG -> WGS84 ————————————————–



58
59
60
61
62
# File 'lib/smo_wgs84_to_bng.rb', line 58

def self.reverse_to_hash(id:, easting:, northing:, **extra)
  Validator.validate_bng!(id: id, easting: easting, northing: northing)
  lat, lon = BngToWgs84.convert(easting.to_f, northing.to_f)
  { id: id, lat: lat, lon: lon }.merge(extra)
end

.reverse_to_json(id:, easting:, northing:, **extra) ⇒ Object



70
71
72
# File 'lib/smo_wgs84_to_bng.rb', line 70

def self.reverse_to_json(id:, easting:, northing:, **extra)
  reverse_to_hash(id: id, easting: easting, northing: northing, **extra).to_json
end