Class: Datacite::Mapping::GeoLocationBox

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

Overview

A latitude-longitude quadrangle containing the area where the data was gathered or about which the data is focused.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ GeoLocationBox

Initializes a new Datacite::Mapping::GeoLocationBox. The arguments can be provided either as a named-parameter hash, or as a list of four coordinates in the form lat, long, lat, long (typically south_latitude, west_longitude, north_latitude, east_longitude but not necessarily; north/south and east/west will be flipped if need be). That is, the following forms are equivalent:

GeoLocationBox.new(
  south_latitude: -33.45,
  west_longitude: -122.33,
  north_latitude: 47.61,
  east_longitude: -70.67
)

GeoLocationBox.new(-33.45, -122.33, 47.61, -70.67)

Parameters:

  • south_latitude (Numeric)

    the latitude of the south edge of the box

  • west_longitude (Numeric)

    the longitude of the west edge of the box

  • north_latitude (Numeric)

    the latitude of the north edge of the box

  • east_longitude (Numeric)

    the longitude of the east edge of the box



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/datacite/mapping/geo_location_box.rb', line 48

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

    init_from_hash(**args[0])

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

Instance Attribute Details

#east_longitudeNumeric

Returns the longitude of the east edge of the box.

Returns:

  • (Numeric)

    the longitude of the east edge of the box

Raises:

  • (ArgumentError)


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/datacite/mapping/geo_location_box.rb', line 19

class GeoLocationBox
  include Comparable

  attr_reader :south_latitude, :west_longitude, :north_latitude, :east_longitude

  # Initializes a new {GeoLocationBox}. The arguments can be provided
  # either as a named-parameter hash, or as a list of four coordinates
  # in the form `lat, long, lat, long` (typically
  # `south_latitude, west_longitude, north_latitude, east_longitude`
  # but not necessarily; north/south and east/west will be flipped if
  # need be). That is, the following forms are equivalent:
  #
  #     GeoLocationBox.new(
  #       south_latitude: -33.45,
  #       west_longitude: -122.33,
  #       north_latitude: 47.61,
  #       east_longitude: -70.67
  #     )
  #
  #     GeoLocationBox.new(-33.45, -122.33, 47.61, -70.67)
  #
  # @param south_latitude [Numeric]
  #   the latitude of the south edge of the box
  # @param west_longitude [Numeric]
  #   the longitude of the west edge of the box
  # @param north_latitude [Numeric]
  #   the latitude of the north edge of the box
  # @param east_longitude [Numeric]
  #   the longitude of the east edge of the box
  def initialize(*args)
    case args.length
    when 1
      raise ArgumentError, "Can't construct GeoLocationBox from arguments: #{args}" unless args[0].respond_to?(:keys)

      init_from_hash(**args[0])

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

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

    @south_latitude = value
  end

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

    @west_longitude = value
  end

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

    @north_latitude = value
  end

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

    @east_longitude = value
  end

  # Gets the box coordinates as a string.
  # @return [String] the coordinates of the box as a sequence of four numbers, in the order S W N E.
  def to_s
    "#{south_latitude} #{west_longitude} #{north_latitude} #{east_longitude}"
  end

  # Sorts boxes from north to south and east to west, first by south edge, then west
  # edge, then north edge, then east edge, and compares them for equality.
  # @param other [GeoLocationBox] the box to compare
  # @return [Fixnum, nil] the sort order (-1, 0, or 1), or nil if `other` is not a
  #   {GeoLocationBox}
  def <=>(other)
    return nil unless other.instance_of?(self.class)

    %i[south_latitude west_longitude north_latitude east_longitude].each do |c|
      order = send(c) <=> other.send(c)
      return order if order.nonzero?
    end
    0
  end

  # Returns a hash code consistent with {GeoLocationBox#&lt;=&gt;}
  # @return [Integer] the hash code
  def hash
    [south_latitude, west_longitude, north_latitude, east_longitude].hash
  end

  private

  def init_from_hash(south_latitude:, west_longitude:, north_latitude:, east_longitude:)
    self.south_latitude = south_latitude
    self.west_longitude = west_longitude
    self.north_latitude = north_latitude
    self.east_longitude = east_longitude
  end

  def init_from_array(coordinates)
    self.south_latitude = coordinates[0]
    self.north_latitude = coordinates[2]
    self.west_longitude = coordinates[1]
    self.east_longitude = coordinates[3]
  end
end

#north_latitudeNumeric

Returns the latitude of the north edge of the box.

Returns:

  • (Numeric)

    the latitude of the north edge of the box

Raises:

  • (ArgumentError)


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/datacite/mapping/geo_location_box.rb', line 19

class GeoLocationBox
  include Comparable

  attr_reader :south_latitude, :west_longitude, :north_latitude, :east_longitude

  # Initializes a new {GeoLocationBox}. The arguments can be provided
  # either as a named-parameter hash, or as a list of four coordinates
  # in the form `lat, long, lat, long` (typically
  # `south_latitude, west_longitude, north_latitude, east_longitude`
  # but not necessarily; north/south and east/west will be flipped if
  # need be). That is, the following forms are equivalent:
  #
  #     GeoLocationBox.new(
  #       south_latitude: -33.45,
  #       west_longitude: -122.33,
  #       north_latitude: 47.61,
  #       east_longitude: -70.67
  #     )
  #
  #     GeoLocationBox.new(-33.45, -122.33, 47.61, -70.67)
  #
  # @param south_latitude [Numeric]
  #   the latitude of the south edge of the box
  # @param west_longitude [Numeric]
  #   the longitude of the west edge of the box
  # @param north_latitude [Numeric]
  #   the latitude of the north edge of the box
  # @param east_longitude [Numeric]
  #   the longitude of the east edge of the box
  def initialize(*args)
    case args.length
    when 1
      raise ArgumentError, "Can't construct GeoLocationBox from arguments: #{args}" unless args[0].respond_to?(:keys)

      init_from_hash(**args[0])

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

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

    @south_latitude = value
  end

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

    @west_longitude = value
  end

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

    @north_latitude = value
  end

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

    @east_longitude = value
  end

  # Gets the box coordinates as a string.
  # @return [String] the coordinates of the box as a sequence of four numbers, in the order S W N E.
  def to_s
    "#{south_latitude} #{west_longitude} #{north_latitude} #{east_longitude}"
  end

  # Sorts boxes from north to south and east to west, first by south edge, then west
  # edge, then north edge, then east edge, and compares them for equality.
  # @param other [GeoLocationBox] the box to compare
  # @return [Fixnum, nil] the sort order (-1, 0, or 1), or nil if `other` is not a
  #   {GeoLocationBox}
  def <=>(other)
    return nil unless other.instance_of?(self.class)

    %i[south_latitude west_longitude north_latitude east_longitude].each do |c|
      order = send(c) <=> other.send(c)
      return order if order.nonzero?
    end
    0
  end

  # Returns a hash code consistent with {GeoLocationBox#&lt;=&gt;}
  # @return [Integer] the hash code
  def hash
    [south_latitude, west_longitude, north_latitude, east_longitude].hash
  end

  private

  def init_from_hash(south_latitude:, west_longitude:, north_latitude:, east_longitude:)
    self.south_latitude = south_latitude
    self.west_longitude = west_longitude
    self.north_latitude = north_latitude
    self.east_longitude = east_longitude
  end

  def init_from_array(coordinates)
    self.south_latitude = coordinates[0]
    self.north_latitude = coordinates[2]
    self.west_longitude = coordinates[1]
    self.east_longitude = coordinates[3]
  end
end

#south_latitudeNumeric

Returns the latitude of the south edge of the box.

Returns:

  • (Numeric)

    the latitude of the south edge of the box

Raises:

  • (ArgumentError)


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/datacite/mapping/geo_location_box.rb', line 19

class GeoLocationBox
  include Comparable

  attr_reader :south_latitude, :west_longitude, :north_latitude, :east_longitude

  # Initializes a new {GeoLocationBox}. The arguments can be provided
  # either as a named-parameter hash, or as a list of four coordinates
  # in the form `lat, long, lat, long` (typically
  # `south_latitude, west_longitude, north_latitude, east_longitude`
  # but not necessarily; north/south and east/west will be flipped if
  # need be). That is, the following forms are equivalent:
  #
  #     GeoLocationBox.new(
  #       south_latitude: -33.45,
  #       west_longitude: -122.33,
  #       north_latitude: 47.61,
  #       east_longitude: -70.67
  #     )
  #
  #     GeoLocationBox.new(-33.45, -122.33, 47.61, -70.67)
  #
  # @param south_latitude [Numeric]
  #   the latitude of the south edge of the box
  # @param west_longitude [Numeric]
  #   the longitude of the west edge of the box
  # @param north_latitude [Numeric]
  #   the latitude of the north edge of the box
  # @param east_longitude [Numeric]
  #   the longitude of the east edge of the box
  def initialize(*args)
    case args.length
    when 1
      raise ArgumentError, "Can't construct GeoLocationBox from arguments: #{args}" unless args[0].respond_to?(:keys)

      init_from_hash(**args[0])

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

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

    @south_latitude = value
  end

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

    @west_longitude = value
  end

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

    @north_latitude = value
  end

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

    @east_longitude = value
  end

  # Gets the box coordinates as a string.
  # @return [String] the coordinates of the box as a sequence of four numbers, in the order S W N E.
  def to_s
    "#{south_latitude} #{west_longitude} #{north_latitude} #{east_longitude}"
  end

  # Sorts boxes from north to south and east to west, first by south edge, then west
  # edge, then north edge, then east edge, and compares them for equality.
  # @param other [GeoLocationBox] the box to compare
  # @return [Fixnum, nil] the sort order (-1, 0, or 1), or nil if `other` is not a
  #   {GeoLocationBox}
  def <=>(other)
    return nil unless other.instance_of?(self.class)

    %i[south_latitude west_longitude north_latitude east_longitude].each do |c|
      order = send(c) <=> other.send(c)
      return order if order.nonzero?
    end
    0
  end

  # Returns a hash code consistent with {GeoLocationBox#&lt;=&gt;}
  # @return [Integer] the hash code
  def hash
    [south_latitude, west_longitude, north_latitude, east_longitude].hash
  end

  private

  def init_from_hash(south_latitude:, west_longitude:, north_latitude:, east_longitude:)
    self.south_latitude = south_latitude
    self.west_longitude = west_longitude
    self.north_latitude = north_latitude
    self.east_longitude = east_longitude
  end

  def init_from_array(coordinates)
    self.south_latitude = coordinates[0]
    self.north_latitude = coordinates[2]
    self.west_longitude = coordinates[1]
    self.east_longitude = coordinates[3]
  end
end

#west_longitudeNumeric

Returns the longitude of the west edge of the box.

Returns:

  • (Numeric)

    the longitude of the west edge of the box

Raises:

  • (ArgumentError)


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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/datacite/mapping/geo_location_box.rb', line 19

class GeoLocationBox
  include Comparable

  attr_reader :south_latitude, :west_longitude, :north_latitude, :east_longitude

  # Initializes a new {GeoLocationBox}. The arguments can be provided
  # either as a named-parameter hash, or as a list of four coordinates
  # in the form `lat, long, lat, long` (typically
  # `south_latitude, west_longitude, north_latitude, east_longitude`
  # but not necessarily; north/south and east/west will be flipped if
  # need be). That is, the following forms are equivalent:
  #
  #     GeoLocationBox.new(
  #       south_latitude: -33.45,
  #       west_longitude: -122.33,
  #       north_latitude: 47.61,
  #       east_longitude: -70.67
  #     )
  #
  #     GeoLocationBox.new(-33.45, -122.33, 47.61, -70.67)
  #
  # @param south_latitude [Numeric]
  #   the latitude of the south edge of the box
  # @param west_longitude [Numeric]
  #   the longitude of the west edge of the box
  # @param north_latitude [Numeric]
  #   the latitude of the north edge of the box
  # @param east_longitude [Numeric]
  #   the longitude of the east edge of the box
  def initialize(*args)
    case args.length
    when 1
      raise ArgumentError, "Can't construct GeoLocationBox from arguments: #{args}" unless args[0].respond_to?(:keys)

      init_from_hash(**args[0])

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

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

    @south_latitude = value
  end

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

    @west_longitude = value
  end

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

    @north_latitude = value
  end

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

    @east_longitude = value
  end

  # Gets the box coordinates as a string.
  # @return [String] the coordinates of the box as a sequence of four numbers, in the order S W N E.
  def to_s
    "#{south_latitude} #{west_longitude} #{north_latitude} #{east_longitude}"
  end

  # Sorts boxes from north to south and east to west, first by south edge, then west
  # edge, then north edge, then east edge, and compares them for equality.
  # @param other [GeoLocationBox] the box to compare
  # @return [Fixnum, nil] the sort order (-1, 0, or 1), or nil if `other` is not a
  #   {GeoLocationBox}
  def <=>(other)
    return nil unless other.instance_of?(self.class)

    %i[south_latitude west_longitude north_latitude east_longitude].each do |c|
      order = send(c) <=> other.send(c)
      return order if order.nonzero?
    end
    0
  end

  # Returns a hash code consistent with {GeoLocationBox#&lt;=&gt;}
  # @return [Integer] the hash code
  def hash
    [south_latitude, west_longitude, north_latitude, east_longitude].hash
  end

  private

  def init_from_hash(south_latitude:, west_longitude:, north_latitude:, east_longitude:)
    self.south_latitude = south_latitude
    self.west_longitude = west_longitude
    self.north_latitude = north_latitude
    self.east_longitude = east_longitude
  end

  def init_from_array(coordinates)
    self.south_latitude = coordinates[0]
    self.north_latitude = coordinates[2]
    self.west_longitude = coordinates[1]
    self.east_longitude = coordinates[3]
  end
end

Instance Method Details

#<=>(other) ⇒ Fixnum?

Sorts boxes from north to south and east to west, first by south edge, then west edge, then north edge, then east edge, and compares them for equality.

Parameters:

Returns:



101
102
103
104
105
106
107
108
109
# File 'lib/datacite/mapping/geo_location_box.rb', line 101

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

  %i[south_latitude west_longitude north_latitude east_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



113
114
115
# File 'lib/datacite/mapping/geo_location_box.rb', line 113

def hash
  [south_latitude, west_longitude, north_latitude, east_longitude].hash
end

#to_sString

Gets the box coordinates as a string.

Returns:

  • (String)

    the coordinates of the box as a sequence of four numbers, in the order S W N E.



92
93
94
# File 'lib/datacite/mapping/geo_location_box.rb', line 92

def to_s
  "#{south_latitude} #{west_longitude} #{north_latitude} #{east_longitude}"
end