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.



14
15
16
17
18
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 14

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]}]" if points.size > 1 unless points[0] == points[-1]
end

Class Method Details

.marshal_point(element, value)



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

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)



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

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)



24
25
26
27
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 24

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

#hash



29
30
31
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 29

def hash
  points.hash
end

#points=(value)



20
21
22
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 20

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

#to_s



33
34
35
36
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 33

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