Class: CocinaDisplay::Geospatial::Coordinates

Inherits:
Object
  • Object
show all
Defined in:
lib/cocina_display/geospatial.rb

Overview

Abstract class representing multiple geospatial coordinates, like a point or box.

Direct Known Subclasses

BoundingBox, Point

Class Method Summary collapse

Class Method Details

.from_cocina(cocina) ⇒ Coordinates?

Convert Cocina structured data into a Coordinates object. Chooses a parsing strategy based on the cocina structure.

Parameters:

  • cocina (Hash)

Returns:



12
13
14
15
# File 'lib/cocina_display/geospatial.rb', line 12

def from_cocina(cocina)
  return from_structured_values(cocina["structuredValue"]) if Array(cocina["structuredValue"]).any?
  parse(cocina["value"]) if cocina["value"].present?
end

.from_structured_values(structured_values) ⇒ Coordinates?

Convert structured values into the appropriate Coordinates object. Handles points and bounding boxes.

Parameters:

  • structured_values (Array<Hash>)

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cocina_display/geospatial.rb', line 21

def from_structured_values(structured_values)
  if structured_values.size == 2
    lat = structured_values.find { |v| v["type"] == "latitude" }&.dig("value")
    lng = structured_values.find { |v| v["type"] == "longitude" }&.dig("value")
    Point.from_coords(lat: lat, lng: lng)
  elsif structured_values.size == 4
    north = structured_values.find { |v| v["type"] == "north" }&.dig("value")
    south = structured_values.find { |v| v["type"] == "south" }&.dig("value")
    east = structured_values.find { |v| v["type"] == "east" }&.dig("value")
    west = structured_values.find { |v| v["type"] == "west" }&.dig("value")
    BoundingBox.from_coords(west: west, east: east, north: north, south: south)
  end
end

.parse(value) ⇒ Coordinates?

Convert a single string value into a Coordinates object. Chooses a parsing strategy based on the string format.

Parameters:

  • value (String)

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cocina_display/geospatial.rb', line 39

def parse(value)
  # Remove all whitespace for easier matching/parsing
  match_str = value.gsub(/[\s]+/, "")

  # Try each parser in order until one matches; bail out if none do
  parser_class = [
    MarcDecimalBoundingBoxParser,
    MarcDMSBoundingBoxParser,
    DecimalBoundingBoxParser,
    DMSBoundingBoxParser,
    DecimalPointParser,
    DMSPointParser
  ].find { |parser| parser.supports?(match_str) }
  return unless parser_class

  # Use the matching parser to parse the string
  parser_class.parse(match_str)
end