Module: SmoWgs84ToBng::Validator

Defined in:
lib/smo_wgs84_to_bng/validator.rb

Constant Summary collapse

LAT_MIN =
49.0
LAT_MAX =
61.0
LON_MIN =
-8.5
LON_MAX =
2.0
E_MIN =
0.0
E_MAX =
700_000.0
N_MIN =
0.0
N_MAX =
1_300_000.0
MSG_ID_REQUIRED =
"id is required"
MSG_REQUIRED =
"%s is required"
MSG_MUST_BE_NUMERIC =
"%s must be numeric"
MSG_OUTSIDE_BOUNDS =
"%s %s is outside GB bounds (%s..%s)"

Class Method Summary collapse

Class Method Details

.numeric?(val) ⇒ Boolean

Returns:

  • (Boolean)


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

def numeric?(val)
  val.is_a?(Numeric)
end

.validate_bng!(id:, easting:, northing:, index: nil) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/smo_wgs84_to_bng/validator.rb', line 45

def validate_bng!(id:, easting:, northing:, index: nil)
  ctx = index ? " for point at index #{index}" : ""

  raise MissingIdError, "#{MSG_ID_REQUIRED}#{ctx}" if id.nil?

  raise MissingCoordinateError, "#{format(MSG_REQUIRED, 'easting')}#{ctx}" if easting.nil?
  raise MissingCoordinateError, "#{format(MSG_REQUIRED, 'northing')}#{ctx}" if northing.nil?

  unless numeric?(easting)
    raise InvalidCoordinateError, "#{format(MSG_MUST_BE_NUMERIC, 'easting')}#{ctx}"
  end
  unless numeric?(northing)
    raise InvalidCoordinateError, "#{format(MSG_MUST_BE_NUMERIC, 'northing')}#{ctx}"
  end

  e_f = easting.to_f
  n_f = northing.to_f

  unless e_f.between?(E_MIN, E_MAX)
    raise OutOfBoundsError, "#{format(MSG_OUTSIDE_BOUNDS, 'easting', e_f, E_MIN, E_MAX)}#{ctx}"
  end
  unless n_f.between?(N_MIN, N_MAX)
    raise OutOfBoundsError, "#{format(MSG_OUTSIDE_BOUNDS, 'northing', n_f, N_MIN, N_MAX)}#{ctx}"
  end
end

.validate_wgs84!(id:, lat:, lon:, index: nil) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/smo_wgs84_to_bng/validator.rb', line 19

def validate_wgs84!(id:, lat:, lon:, index: nil)
  ctx = index ? " for point at index #{index}" : ""

  raise MissingIdError, "#{MSG_ID_REQUIRED}#{ctx}" if id.nil?

  raise MissingCoordinateError, "#{format(MSG_REQUIRED, 'lat')}#{ctx}" if lat.nil?
  raise MissingCoordinateError, "#{format(MSG_REQUIRED, 'lon')}#{ctx}" if lon.nil?

  unless numeric?(lat)
    raise InvalidCoordinateError, "#{format(MSG_MUST_BE_NUMERIC, 'lat')}#{ctx}"
  end
  unless numeric?(lon)
    raise InvalidCoordinateError, "#{format(MSG_MUST_BE_NUMERIC, 'lon')}#{ctx}"
  end

  lat_f = lat.to_f
  lon_f = lon.to_f

  unless lat_f.between?(LAT_MIN, LAT_MAX)
    raise OutOfBoundsError, "#{format(MSG_OUTSIDE_BOUNDS, 'lat', lat_f, LAT_MIN, LAT_MAX)}#{ctx}"
  end
  unless lon_f.between?(LON_MIN, LON_MAX)
    raise OutOfBoundsError, "#{format(MSG_OUTSIDE_BOUNDS, 'lon', lon_f, LON_MIN, LON_MAX)}#{ctx}"
  end
end