Class: Datacite::Mapping::GeoLocationPoint

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/datacite/mapping/geo_location_point.rb

Overview

A latitude-longitude point at which the data was gathered or about which the data is focused.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ GeoLocationPoint

Initializes a new Datacite::Mapping::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)

Parameters:

  • latitude (Numeric)

    the latitude

  • longitude (Numeric)

    the longitude



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/datacite/mapping/geo_location_point.rb', line 30

def initialize(*args)
  case args.length
  when 1
    raise ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}" unless args[0].respond_to?(:keys)

    init_from_hash(**args[0])

  when 2
    init_from_array(args)
  else
    raise ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}"
  end
end

Instance Attribute Details

#latitudeNumeric

Returns the latitude.

Returns:

  • (Numeric)

    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
91
92
93
94
95
# File 'lib/datacite/mapping/geo_location_point.rb', line 15

class GeoLocationPoint
  include Comparable

  attr_reader :latitude, :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
      raise ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}" unless args[0].respond_to?(:keys)

      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.instance_of?(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

#longitudeNumeric

Returns the longitude.

Returns:

  • (Numeric)

    the longitude

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
91
92
93
94
95
# File 'lib/datacite/mapping/geo_location_point.rb', line 15

class GeoLocationPoint
  include Comparable

  attr_reader :latitude, :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
      raise ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}" unless args[0].respond_to?(:keys)

      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.instance_of?(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

Instance Method Details

#<=>(other) ⇒ Fixnum?

Sorts points from north to south and from east to west, and compares them for equality.

Parameters:

Returns:



69
70
71
72
73
74
75
76
77
# File 'lib/datacite/mapping/geo_location_point.rb', line 69

def <=>(other)
  return nil unless other.instance_of?(self.class)

  %i[latitude longitude].each do |c|
    order = send(c) <=> other.send(c)
    return order if order.nonzero?
  end
  0
end

#hashInteger

Returns a hash code consistent with #<=>

Returns:

  • (Integer)

    the hash code



81
82
83
# File 'lib/datacite/mapping/geo_location_point.rb', line 81

def hash
  [latitude, longitude].hash
end

#to_sString

Gets the coordinates as a string.

Returns:

  • (String)

    the coordinates as a pair of numbers separated by a space, in the order lat long.



61
62
63
# File 'lib/datacite/mapping/geo_location_point.rb', line 61

def to_s
  "#{latitude} #{longitude}"
end