Class: MySQLPoint::Coordinate
- Inherits:
-
Object
- Object
- MySQLPoint::Coordinate
- Defined in:
- lib/mysql_point/coordinate.rb
Overview
A struct for latitude/longitude coordinates, with serializers and deserializers for the standard WKT and WKB formats.
# WKT
‘POINT(<longitude> <latitude>)`
# WKB
-
byte 0: byte order (little endian is 01)
-
bytes 1-4: WKB type (point is 01)
-
bytes 5-12: X coordinate (longitude)
-
bytes 13-21: Y coordinate (latitude)
Instance Attribute Summary collapse
-
#latitude ⇒ Object
readonly
Returns the value of attribute latitude.
-
#longitude ⇒ Object
readonly
Returns the value of attribute longitude.
Class Method Summary collapse
- .from_wkb(binstr) ⇒ Object
-
.from_wkt(str) ⇒ Object
POINT(<longitude> <latitude>).
Instance Method Summary collapse
-
#initialize(longitude:, latitude:) ⇒ Coordinate
constructor
A new instance of Coordinate.
- #to_wkb ⇒ Object
- #to_wkt ⇒ Object
Constructor Details
#initialize(longitude:, latitude:) ⇒ Coordinate
Returns a new instance of Coordinate.
34 35 36 37 |
# File 'lib/mysql_point/coordinate.rb', line 34 def initialize(longitude:, latitude:) @longitude = longitude.to_f if longitude @latitude = latitude.to_f if latitude end |
Instance Attribute Details
#latitude ⇒ Object (readonly)
Returns the value of attribute latitude.
32 33 34 |
# File 'lib/mysql_point/coordinate.rb', line 32 def latitude @latitude end |
#longitude ⇒ Object (readonly)
Returns the value of attribute longitude.
31 32 33 |
# File 'lib/mysql_point/coordinate.rb', line 31 def longitude @longitude end |
Class Method Details
.from_wkb(binstr) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/mysql_point/coordinate.rb', line 21 def self.from_wkb(binstr) type = binstr[1, 4].unpack(binstr[0] == "\x01" ? 'V' : 'N').first return unless type == 1 x = binstr[5, 8].unpack('D').first y = binstr[13, 8].unpack('D').first new(longitude: x, latitude: y) end |
.from_wkt(str) ⇒ Object
POINT(<longitude> <latitude>)
16 17 18 19 |
# File 'lib/mysql_point/coordinate.rb', line 16 def self.from_wkt(str) md = str.match(/\APOINT\((-?[0-9.]*) (-?[0-9.]*)\)\z/) new(longitude: md[1], latitude: md[2]) if md end |
Instance Method Details
#to_wkb ⇒ Object
43 44 45 |
# File 'lib/mysql_point/coordinate.rb', line 43 def to_wkb "\x01" + [1].pack('L') + [longitude, latitude].pack('D*') end |
#to_wkt ⇒ Object
39 40 41 |
# File 'lib/mysql_point/coordinate.rb', line 39 def to_wkt "POINT(#{format('%.7f', longitude)} #{format('%.7f', latitude)})" end |