Class: Datacite::Mapping::GeoLocationPoint
- Inherits:
-
Object
- Object
- Datacite::Mapping::GeoLocationPoint
- 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
-
#latitude ⇒ Numeric
The latitude.
-
#longitude ⇒ Numeric
The longitude.
Instance Method Summary collapse
-
#<=>(other) ⇒ Fixnum?
Sorts points from north to south and from east to west, and compares them for equality.
-
#hash ⇒ Integer
Returns a hash code consistent with #<=>.
-
#initialize(*args) ⇒ GeoLocationPoint
constructor
Initializes a new GeoLocationPoint.
-
#to_s ⇒ String
Gets the coordinates as a string.
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)
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
#latitude ⇒ Numeric
Returns the latitude.
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#<=>} # @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 |
#longitude ⇒ Numeric
Returns the longitude.
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#<=>} # @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.
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 |
#hash ⇒ Integer
Returns a hash code consistent with #<=>
81 82 83 |
# File 'lib/datacite/mapping/geo_location_point.rb', line 81 def hash [latitude, longitude].hash end |
#to_s ⇒ String
Gets the coordinates as a string.
61 62 63 |
# File 'lib/datacite/mapping/geo_location_point.rb', line 61 def to_s "#{latitude} #{longitude}" end |