Class: GeoCombine::BoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_combine/bounding_box.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(west:, south:, east:, north:) ⇒ BoundingBox

Returns a new instance of BoundingBox.

Parameters:

  • west (String, Integer, Float)
  • south (String, Integer, Float)
  • east (String, Integer, Float)
  • north (String, Integer, Float)


10
11
12
13
14
15
# File 'lib/geo_combine/bounding_box.rb', line 10

def initialize(west:, south:, east:, north:)
  @west = west.to_f
  @south = south.to_f
  @east = east.to_f
  @north = north.to_f
end

Instance Attribute Details

#eastObject (readonly)

Returns the value of attribute east.



3
4
5
# File 'lib/geo_combine/bounding_box.rb', line 3

def east
  @east
end

#northObject (readonly)

Returns the value of attribute north.



3
4
5
# File 'lib/geo_combine/bounding_box.rb', line 3

def north
  @north
end

#southObject (readonly)

Returns the value of attribute south.



3
4
5
# File 'lib/geo_combine/bounding_box.rb', line 3

def south
  @south
end

#westObject (readonly)

Returns the value of attribute west.



3
4
5
# File 'lib/geo_combine/bounding_box.rb', line 3

def west
  @west
end

Class Method Details

.from_envelope(envelope) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/geo_combine/bounding_box.rb', line 46

def self.from_envelope(envelope)
  return if envelope.nil?
  envelope = envelope[/.*ENVELOPE\(([^\)]*)/, 1].split(',')
  new(
    west: envelope[0],
    south: envelope[3],
    east: envelope[1],
    north: envelope[2]
  )
end

.from_string_delimiter(spatial, delimiter: ',') ⇒ Object

Parameters:

  • spatial (String)

    w,s,e,n or w s e n

  • delimiter (String) (defaults to: ',')

    “,” or “ ”



60
61
62
63
64
65
66
67
68
69
# File 'lib/geo_combine/bounding_box.rb', line 60

def self.from_string_delimiter(spatial, delimiter: ',')
  return if spatial.nil?
  spatial = spatial.split(delimiter)
  new(
    west: spatial[0],
    south: spatial[1],
    east: spatial[2],
    north: spatial[3]
  )
end

Instance Method Details

#to_envelopeString

Returns a bounding box in ENVELOPE syntax

Returns:

  • (String)


20
21
22
# File 'lib/geo_combine/bounding_box.rb', line 20

def to_envelope
  "ENVELOPE(#{west}, #{east}, #{north}, #{south})"
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/geo_combine/bounding_box.rb', line 24

def valid?
  [south, north].map do |coord|
    next if (-90..90).cover?(coord)
    raise GeoCombine::Exceptions::InvalidGeometry,
          "#{coord} should be in range -90 90"
  end
  [east, west].map do |coord|
    next if (-180..180).cover?(coord)
    raise GeoCombine::Exceptions::InvalidGeometry,
          "#{coord} should be in range -180 180"
  end
  if west > east
    raise GeoCombine::Exceptions::InvalidGeometry,
          "east #{east} should be greater than or equal to west #{west}"
  end
  if south > north
    raise GeoCombine::Exceptions::InvalidGeometry,
          "north #{north} should be greater than or equal to south #{south}"
  end
  true
end