Method: Datacite::Mapping::GeoLocationPoint#latitude

Defined in:
lib/datacite/mapping/geo_location_point.rb,
lib/datacite/mapping/geo_location_point.rb

#latitudeNumeric

Returns the latitude.

Raises:

  • (ArgumentError)


15
16
17
18
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
44
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/datacite/mapping/geo_location_point.rb', line 15

class GeoLocationPoint
  include Comparable

  attr_reader :latitude
  attr_reader :longitude

  # Initializes a new {GeoLocationPoint}. The arguments can be provided
  # either as a named-parameter hash, or as a pair of coordinates in the
  # form `lat, long`. That is, the following forms are equivalent:
  #
  #     GeoLocationPoint.new(latitude: 47.61, longitude: -122.33)
  #
  #     GeoLocationPoint.new(47.61, -122.33)
  #
  # @param latitude [Numeric] the latitude
  # @param longitude [Numeric] the longitude
  def initialize(*args)
    case args.length
    when 1
      init_from_hash(args[0])
    when 2
      init_from_array(args)
    else
      raise ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}"
    end
  end

  def latitude=(value)
    raise ArgumentError, 'Latitude cannot be nil' unless value
    raise ArgumentError, "#{value} is not a valid latitude" unless value >= -90 && value <= 90
    @latitude = value
  end

  def longitude=(value)
    raise ArgumentError, 'Longitude cannot be nil' unless value
    raise ArgumentError, "#{value} is not a valid longitude" unless value >= -180 && value <= 180
    @longitude = value
  end

  # Gets the coordinates as a string.
  # @return [String] the coordinates as a pair of numbers separated by a space, in the
  #   order `lat` `long`.
  def to_s
    "#{latitude} #{longitude}"
  end

  # Sorts points from north to south and from east to west, and compares them for equality.
  # @param other [GeoLocationPoint] the point to compare
  # @return [Fixnum, nil] the sort order (-1, 0, or 1), or nil if `other` is not a
  #   {GeoLocationPoint}
  def <=>(other)
    return nil unless other.class == self.class
    %i[latitude longitude].each do |c|
      order = send(c) <=> other.send(c)
      return order if order.nonzero?
    end
    0
  end

  # Returns a hash code consistent with {GeoLocationPoint#&lt;=&gt;}
  # @return [Integer] the hash code
  def hash
    [latitude, longitude].hash
  end

  private

  def init_from_hash(latitude:, longitude:)
    self.latitude = latitude
    self.longitude = longitude
  end

  def init_from_array(args)
    self.latitude, self.longitude = args
  end
end