Class: RGeoServer::BoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/rgeoserver/utils/boundingbox.rb

Constant Summary collapse

@@epsilon =
0.0001

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BoundingBox

Returns a new instance of BoundingBox.



26
27
28
29
30
31
32
# File 'lib/rgeoserver/utils/boundingbox.rb', line 26

def initialize options = {}
  reset
  if ['minx', 'miny', 'maxx', 'maxy'].all? {|k| options.include?(k)}
    add options['minx'].to_f, options['miny'].to_f # SW
    add options['maxx'].to_f, options['maxx'].to_f # NE
  end
end

Instance Attribute Details

#maxxObject (readonly)

Returns the value of attribute maxx.



5
6
7
# File 'lib/rgeoserver/utils/boundingbox.rb', line 5

def maxx
  @maxx
end

#maxyObject (readonly)

Returns the value of attribute maxy.



5
6
7
# File 'lib/rgeoserver/utils/boundingbox.rb', line 5

def maxy
  @maxy
end

#minxObject (readonly)

Returns the value of attribute minx.



5
6
7
# File 'lib/rgeoserver/utils/boundingbox.rb', line 5

def minx
  @minx
end

#minyObject (readonly)

Returns the value of attribute miny.



5
6
7
# File 'lib/rgeoserver/utils/boundingbox.rb', line 5

def miny
  @miny
end

Class Method Details

.epsilonObject



9
10
11
# File 'lib/rgeoserver/utils/boundingbox.rb', line 9

def self.epsilon
  @@epsilon
end

.epsilon=(value) ⇒ Object



13
14
15
# File 'lib/rgeoserver/utils/boundingbox.rb', line 13

def self.epsilon= value
  @@epsilon = value
end

.from_a(a) ⇒ Object

Parameters:

  • a (Array)

    in [minx, miny, maxx, maxy]



18
19
20
21
22
23
24
# File 'lib/rgeoserver/utils/boundingbox.rb', line 18

def self.from_a a
  self.class.new Hash.new('minx' => a[0].to_f,
                          'miny' => a[1].to_f,
                          'maxx' => a[2].to_f,
                          'maxy' => a[3].to_f)
    
end

Instance Method Details

#<<(point) ⇒ Object



39
40
41
# File 'lib/rgeoserver/utils/boundingbox.rb', line 39

def << point
  add point[0], point[1]
end

#add(x, y) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rgeoserver/utils/boundingbox.rb', line 43

def add x, y
  if @empty
    @minx = @maxx = x
    @miny = @maxy = y
  end

  @minx = [minx, x].min
  @miny = [miny, y].min
  @maxx = [maxx, x].max
  @maxy = [maxy, y].max

  @empty = false
end

#areaObject



87
88
89
# File 'lib/rgeoserver/utils/boundingbox.rb', line 87

def area
  width * height
end

#constrict(rate = @@epsilon) ⇒ Object



75
76
77
# File 'lib/rgeoserver/utils/boundingbox.rb', line 75

def constrict rate = @@epsilon
  expand(-rate)
end

#expand(rate = @@epsilon) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/rgeoserver/utils/boundingbox.rb', line 65

def expand rate = @@epsilon
  _minx, _miny = [minx - rate, miny - rate]
  _maxx, _maxy = [maxx + rate, maxy + rate]

  reset

  add _minx, _miny
  add _maxx, _maxy
end

#heightObject



83
84
85
# File 'lib/rgeoserver/utils/boundingbox.rb', line 83

def height
  maxy - miny
end

#inspectObject



127
128
129
# File 'lib/rgeoserver/utils/boundingbox.rb', line 127

def inspect
  "#<#{self.class} #{to_s}>"
end

#maxObject



61
62
63
# File 'lib/rgeoserver/utils/boundingbox.rb', line 61

def max
  [maxx, maxy]
end

#minObject



57
58
59
# File 'lib/rgeoserver/utils/boundingbox.rb', line 57

def min
  [minx, miny]
end

#resetObject



34
35
36
37
# File 'lib/rgeoserver/utils/boundingbox.rb', line 34

def reset
  @minx = @miny = @maxx = @maxy = 0.0
  @empty = true
end

#to_aObject



119
120
121
# File 'lib/rgeoserver/utils/boundingbox.rb', line 119

def to_a
  [minx, miny, maxx, maxy]
end

#to_geometryObject



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rgeoserver/utils/boundingbox.rb', line 96

def to_geometry
  factory = RGeo::Cartesian::Factory.new

  point_min, point_max = unless [minx, miny] == [maxx, maxy]
    [factory.point(minx, miny), factory.point(maxx, maxy)]
  else
    [factory.point(minx - @@epsilon, miny - @@epsilon),
     factory.point(maxx + @@epsilon, maxy + @@epsilon)]
  end

  line_string = factory.line_string [point_min, point_max]
  line_string.envelope
end

#to_hObject



110
111
112
113
114
115
116
117
# File 'lib/rgeoserver/utils/boundingbox.rb', line 110

def to_h
  {
    :minx => minx,
    :miny => miny,
    :maxx => maxx,
    :maxy => maxy
  }
end

#to_sObject



123
124
125
# File 'lib/rgeoserver/utils/boundingbox.rb', line 123

def to_s
  to_a.join(', ')
end

#valid?Boolean

Returns true if bounding box has non-zero area.

Returns:

  • (Boolean)

    true if bounding box has non-zero area



92
93
94
# File 'lib/rgeoserver/utils/boundingbox.rb', line 92

def valid?
  area > 0
end

#widthObject



79
80
81
# File 'lib/rgeoserver/utils/boundingbox.rb', line 79

def width
  maxx - minx
end