Sha256: c0072663132c038aeab7f55cfe06a645ee74b88888748a97c93785c62bb81e8e

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true
module Geoblacklight
  ##
  # Transforms and parses a bounding box for various formats
  class BoundingBox
    ##
    # @param [String, Integer, Float] west
    # @param [String, Integer, Float] south
    # @param [String, Integer, Float] east
    # @param [String, Integer, Float] north
    def initialize(west, south, east, north)
      @west = west
      @south = south
      @east = east
      @north = north
      # TODO: check for valid Geometry and raise if not
    end

    ##
    # Returns a bounding box in ENVELOPE syntax
    # @return [String]
    def to_envelope
      "ENVELOPE(#{west}, #{east}, #{north}, #{south})"
    end

    ##
    # Create a Geoblacklight::BoundingBox from a Solr rectangle syntax
    # @param [String] bbox as "W S E N"
    # @return [Geoblacklight::BoundingBox]
    def self.from_rectangle(rectangle)
      rectangle_array = rectangle.split(' ')
      message = 'Bounding box should be a string in Solr rectangle syntax e.g."W S E N"'
      fail Geoblacklight::Exceptions::WrongBoundingBoxFormat, message if rectangle_array.count != 4
      new(
        rectangle_array[0],
        rectangle_array[1],
        rectangle_array[2],
        rectangle_array[3]
      )
    end

    private

    attr_reader :west, :south, :east, :north
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
geoblacklight-3.2.0 lib/geoblacklight/bounding_box.rb
geoblacklight-3.1.0 lib/geoblacklight/bounding_box.rb
geoblacklight-3.0.1 lib/geoblacklight/bounding_box.rb
geoblacklight-3.0.0 lib/geoblacklight/bounding_box.rb