Class: Datacite::Mapping::GeoLocationPolygon

Inherits:
Object
  • Object
show all
Includes:
Comparable, XML::Mapping
Defined in:
lib/datacite/mapping/geo_location_polygon.rb

Constant Summary collapse

COORD_ELEMENTS =

TODO: Figure out how to DRY this with GeoLocationPointNode

{ longitude: 'pointLongitude',
latitude: 'pointLatitude' }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(points:) ⇒ GeoLocationPolygon

Creates a new GeoLocationPolygon.

Parameters:

  • points (Array<GeoLocationPoint>)

    an array of points defining the polygon area. Per the spec, the array should contain at least four points, the first and last being identical to close the polygon.



16
17
18
19
20
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 16

def initialize(points:) # TODO: allow simple array of point args, array of hashes
  self.points = points
  warn "Polygon should contain at least 4 points, but has #{points.size}" if points.size < 4
  warn "Polygon is not closed; last and first point should be identical, but were: [#{points[0]}], [#{points[-1]}]" unless points[0] == points[-1] || points.size <= 1
end

Class Method Details

.marshal_point(element, value)



59
60
61
62
63
64
65
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 59

def self.marshal_point(element, value)
  COORD_ELEMENTS.each do |getter, element_name|
    v = value.send(getter)
    child = element.elements << REXML::Element.new(element_name)
    child.text = v
  end
end

.unmarshal_point(elem)



67
68
69
70
71
72
73
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 67

def self.unmarshal_point(elem)
  coords_hash = COORD_ELEMENTS.map do |key, element_name|
    value = elem.elements[element_name].text
    [key, value && value.to_f]
  end.to_h
  GeoLocationPoint.new(coords_hash)
end

Instance Method Details

#<=>(other)



26
27
28
29
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 26

def <=>(other)
  return nil unless other.class == self.class
  points <=> other.points
end

#hash



31
32
33
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 31

def hash
  points.hash
end

#points=(value)



22
23
24
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 22

def points=(value)
  @points = value || []
end

#to_s



35
36
37
38
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 35

def to_s
  point_hashes = points.map { |p| "{ latitude: #{p.latitude}, longitude: #{p.longitude} }" }.join(', ')
  "[ #{point_hashes} ]"
end