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



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

def initialize(*args)
  case args.length
  when 1
    init_from_hash(args[0])
  when 2
    init_from_array(args)
  else
    fail ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}"
  end
end

Instance Attribute Details

#latitudeNumeric

Returns the latitude.

Returns:

  • (Numeric)

    the latitude



13
14
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
# File 'lib/datacite/mapping/geo_location_point.rb', line 13

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
      fail ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}"
    end
  end

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

  def longitude=(value)
    fail ArgumentError, 'Longitude cannot be nil' unless value
    fail 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
    [: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



13
14
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
# File 'lib/datacite/mapping/geo_location_point.rb', line 13

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
      fail ArgumentError, "Can't construct GeoLocationPoint from arguments: #{args}"
    end
  end

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

  def longitude=(value)
    fail ArgumentError, 'Longitude cannot be nil' unless value
    fail 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
    [: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:



63
64
65
66
67
68
69
70
# File 'lib/datacite/mapping/geo_location_point.rb', line 63

def <=>(other)
  return nil unless other.class == self.class
  [: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



74
75
76
# File 'lib/datacite/mapping/geo_location_point.rb', line 74

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.



55
56
57
# File 'lib/datacite/mapping/geo_location_point.rb', line 55

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