Class: CocinaDisplay::Geospatial::BoundingBox

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

Overview

A bounding box defined by two corner points.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Coordinates

from_cocina, from_structured_values, parse

Constructor Details

#initialize(min_point:, max_point:) ⇒ BoundingBox

Construct a BoundingBox from two Geo::Coord points.

Parameters:

  • min_point (Geo::Coord)
  • max_point (Geo::Coord)


165
166
167
168
# File 'lib/cocina_display/geospatial.rb', line 165

def initialize(min_point:, max_point:)
  @min_point = min_point
  @max_point = max_point
end

Instance Attribute Details

#max_pointObject (readonly)

Returns the value of attribute max_point.



144
145
146
# File 'lib/cocina_display/geospatial.rb', line 144

def max_point
  @max_point
end

#min_pointObject (readonly)

Returns the value of attribute min_point.



144
145
146
# File 'lib/cocina_display/geospatial.rb', line 144

def min_point
  @min_point
end

Class Method Details

.from_coords(west:, east:, north:, south:) ⇒ BoundingBox?

Construct a BoundingBox from west, east, north, and south string values.

Parameters:

  • west (String)

    western longitude

  • east (String)

    eastern longitude

  • north (String)

    northern latitude

  • south (String)

    southern latitude

Returns:



152
153
154
155
156
157
158
159
160
# File 'lib/cocina_display/geospatial.rb', line 152

def self.from_coords(west:, east:, north:, south:)
  min_point = Geo::Coord.parse("#{north}, #{west}")
  max_point = Geo::Coord.parse("#{south}, #{east}")

  # Must be parsable
  return unless min_point && max_point

  new(min_point: min_point, max_point: max_point)
end

Instance Method Details

#as_envelopeString

Note:

Limits decimals to 6 places.

Format using the CQL ENVELOPE representation.

Examples:

“ENVELOPE(-118.243700, -117.952200, 34.199600, 34.052200)”

Returns:

  • (String)


199
200
201
202
203
# File 'lib/cocina_display/geospatial.rb', line 199

def as_envelope
  "ENVELOPE(%.6f, %.6f, %.6f, %.6f)" % [
    min_point.lng, max_point.lng, max_point.lat, min_point.lat
  ]
end

#as_pointString

Note:

Limits decimals to 6 places.

The box center point as a comma-separated latitude,longitude pair.

Examples:

“34.0522,-118.2437”

Returns:

  • (String)


209
210
211
212
213
214
# File 'lib/cocina_display/geospatial.rb', line 209

def as_point
  azimuth = min_point.azimuth(max_point)
  distance = min_point.distance(max_point)
  center = min_point.endpoint(distance / 2, azimuth)
  "%.6f,%.6f" % [center.lat, center.lng]
end

#as_wktString

Note:

Limits decimals to 6 places.

Format using the Well-Known Text (WKT) representation.



185
186
187
188
189
190
191
192
193
# File 'lib/cocina_display/geospatial.rb', line 185

def as_wkt
  "POLYGON((%.6f %.6f, %.6f %.6f, %.6f %.6f, %.6f %.6f, %.6f %.6f))" % [
    min_point.lng, min_point.lat,
    max_point.lng, min_point.lat,
    max_point.lng, max_point.lat,
    min_point.lng, max_point.lat,
    min_point.lng, min_point.lat
  ]
end

#to_sString

Note:

This format adapts the “Annex D” human representation style.

Format for display in DMS format, adapted from ISO 6709 standard.

Examples:

“118°14′37″W – 117°56′55″W / 34°03′08″N – 34°11′59″N”

Returns:

  • (String)

See Also:



175
176
177
178
179
# File 'lib/cocina_display/geospatial.rb', line 175

def to_s
  min_lat, min_lng = format_point(min_point)
  max_lat, max_lng = format_point(max_point)
  "#{min_lng} -- #{max_lng} / #{min_lat} -- #{max_lat}"
end