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:, in_polygon_point: nil) ⇒ 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
21
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 16

def initialize(points:, in_polygon_point: nil) # TODO: allow simple array of point args, array of hashes
  self.points = points
  self.in_polygon_point = in_polygon_point
  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)



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

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)



76
77
78
79
80
81
82
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 76

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)



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

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

  points <=> other.points
end

#hash



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

def hash
  points.hash
end

#points=(value)



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

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

#to_s



37
38
39
40
# File 'lib/datacite/mapping/geo_location_polygon.rb', line 37

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