Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/osgb/string_conversions.rb

Instance Method Summary collapse

Instance Method Details

#coordinates(datum = :osgb36, options = {}) ⇒ Object

Treats the string as a coordinate pair, if that can be done. Any two decimal numbers, positive or negative, separated by any non-digit (and non -) characters are acceptable.

"54.196763, -3.093320".coordinates      # -> [54.196763, -3.093320]


48
49
50
51
52
53
54
55
# File 'lib/osgb/string_conversions.rb', line 48

def coordinates(datum=:osgb36, options={})
  if matches = self.match(/(-?\d+\.\d+)[^\d\-]+(-?\d+\.\d+)/)
    lat,lng = matches[1,2]
    Osgb::Point.new(lat, lng, datum, options[:precision])
  else
    nil
  end
end

#is_gridref?Boolean

Returns true if the string is a valid grid reference: that is, it consits of two valid square-designation letters then 4, 6, 8 or 10 digits. Invalid, malformed and just plain not a grid reference will all return false.

Returns:

  • (Boolean)


10
11
12
# File 'lib/osgb/string_conversions.rb', line 10

def is_gridref?
  !!(self.upcase =~ /^(H(P|T|U|Y|Z)|N(A|B|C|D|F|G|H|J|K|L|M|N|O|R|S|T|U|W|X|Y|Z)|OV|S(C|D|E|G|H|J|K|M|N|O|P|R|S|T|U|W|X|Y|Z)|T(A|F|G|L|M|Q|R|V)){1}\d{4}(NE|NW|SE|SW)?$|((H(P|T|U|Y|Z)|N(A|B|C|D|F|G|H|J|K|L|M|N|O|R|S|T|U|W|X|Y|Z)|OV|S(C|D|E|G|H|J|K|M|N|O|P|R|S|T|U|W|X|Y|Z)|T(A|F|G|L|M|Q|R|V)){1}(\d{4}|\d{6}|\d{8}|\d{10}))$/)
end

#is_latlng?Boolean

Returns true if the string can be decomposed into a valid lat/long co-ordinate pair.

Returns:

  • (Boolean)


31
32
33
# File 'lib/osgb/string_conversions.rb', line 31

def is_latlng?
  coordinates && coordinates.valid?
end

#lat(options = {}) ⇒ Object

Returns the latitude component of the string, however it can be found. Works for both coordinate pairs and grid references. Defaults to WGS84 if coming from a grid reference.



116
117
118
# File 'lib/osgb/string_conversions.rb', line 116

def lat(options={})
  to_latlng(options).lat
end

#lng(options = {}) ⇒ Object

Returns the longitude component of the string, however it can be found. Works for both coordinate pairs and grid references. Defaults to WGS84 if coming from a grid reference.



124
125
126
# File 'lib/osgb/string_conversions.rb', line 124

def lng(options={})
  to_latlng(options).lng
end

#resembles_gridref?Boolean

Returns true if the string looks like a grid reference, whether or not it is well-formed or valid on the ground. In validation this may allow you to distinguish between mistaken and irrelevant input.

“HD123456”.resembles_gridref? # -> true “HD123456”.is_gridref? # -> false “SD12345”.resembles_gridref? # -> true “SD12345”.is_gridref? # -> false “WC1 1AA”.resembles_gridref? # -> false

Returns:

  • (Boolean)


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

def resembles_gridref?
  !!(self.upcase =~ /^\w\w\d{2,}/)
end

#resembles_latlng?Boolean

Returns true if the string can be decomposed into a co-ordinate pair, regardless of whether the coordinates are valid.

Returns:

  • (Boolean)


38
39
40
# File 'lib/osgb/string_conversions.rb', line 38

def resembles_latlng?
  !!coordinates
end

#to_latlng(options = {}) ⇒ Object

If the string is a valid grid reference, this returns the lat/long point using the specified or default datum. Default is WGS84 for GPS compatibility.



60
61
62
63
64
65
66
# File 'lib/osgb/string_conversions.rb', line 60

def to_latlng(options={})
  if is_gridref?
    Osgb::Gridref.new(self, options).to_latlng(options[:datum])
  else
    self.coordinates(options[:datum])
  end
end

#to_latlng!(options = {}) ⇒ Object

Returns the grid reference as a lat/long pair on the specified or default datum, raising an exception if it is not valid.



71
72
73
# File 'lib/osgb/string_conversions.rb', line 71

def to_latlng!(options={})
  with_validity_check { to_latlng(options) }
end

#to_osgb36(options = {}) ⇒ Object

If the string is a valid grid reference, this returns the coordinate pair using the OSGB36 datum, which is the native representation for grid references.



78
79
80
81
82
83
84
# File 'lib/osgb/string_conversions.rb', line 78

def to_osgb36(options={})
  if is_gridref?
    Osgb::Gridref.new(self, options).to_latlng(:osgb36)
  else
    self.coordinates(:osgb36, options)
  end
end

#to_osgb36!(options = {}) ⇒ Object

Returns the grid reference as a lat/long pair on OSGB36, raising an exception if it is not valid.



89
90
91
# File 'lib/osgb/string_conversions.rb', line 89

def to_osgb36!(options={})
  with_validity_check { to_osgb36(options) }
end

#to_wgs84(options = {}) ⇒ Object

If the string is a valid grid reference, this returns the coordinate pair using the WGS84 datum, which is the most suitable representation for work with GPS or google maps.



97
98
99
100
101
102
103
# File 'lib/osgb/string_conversions.rb', line 97

def to_wgs84(options={})
  if is_gridref?
    Osgb::Gridref.new(self, options).to_latlng(:wgs84)
  else
    self.coordinates(:wgs84, options)
  end
end

#to_wgs84!(options = {}) ⇒ Object

Returns the grid reference as a lat/long pair on WGS84, raising an exception if it is not valid.



108
109
110
# File 'lib/osgb/string_conversions.rb', line 108

def to_wgs84!(options={})
  with_validity_check { to_wgs84(options) }
end