Class: GeoRuby::Rtree::STRBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/georuby-ext/georuby/rtree.rb

Defined Under Namespace

Classes: Slice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements, node_size = 2) ⇒ STRBuilder

Returns a new instance of STRBuilder.



19
20
21
22
# File 'lib/georuby-ext/georuby/rtree.rb', line 19

def initialize(elements, node_size = 2)
  @elements = elements
  @node_size = node_size
end

Instance Attribute Details

#elementsObject

Returns the value of attribute elements.



17
18
19
# File 'lib/georuby-ext/georuby/rtree.rb', line 17

def elements
  @elements
end

#node_sizeObject

Returns the value of attribute node_size.



17
18
19
# File 'lib/georuby-ext/georuby/rtree.rb', line 17

def node_size
  @node_size
end

Instance Method Details

#leaf_nodesObject



28
29
30
# File 'lib/georuby-ext/georuby/rtree.rb', line 28

def leaf_nodes
  slices.collect(&:nodes).flatten
end

#leaf_nodes_countObject



60
61
62
# File 'lib/georuby-ext/georuby/rtree.rb', line 60

def leaf_nodes_count
  (elements.count / node_size.to_f).ceil
end

#root_nodeObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/georuby-ext/georuby/rtree.rb', line 32

def root_node
  nodes = leaf_nodes

  while nodes.many?
    nodes = [].tap do |parent_nodes|
      nodes.each_slice(node_size) do |children|
        parent_nodes << Node.new(children)
      end
    end
  end

  nodes.first
end

#slice_sizeObject



64
65
66
# File 'lib/georuby-ext/georuby/rtree.rb', line 64

def slice_size
  Math.sqrt(leaf_nodes_count).ceil
end

#slicesObject



46
47
48
49
50
51
52
# File 'lib/georuby-ext/georuby/rtree.rb', line 46

def slices
  [].tap do |slices|
    sort_x.each_slice(slice_size) do |slice_elements|
      slices << Slice.new(slice_elements, node_size)
    end
  end
end

#sort_xObject



54
55
56
57
58
# File 'lib/georuby-ext/georuby/rtree.rb', line 54

def sort_x
  elements.sort_by do |element|
    element.bounds.center.x
  end
end

#to_rtreeObject



24
25
26
# File 'lib/georuby-ext/georuby/rtree.rb', line 24

def to_rtree
  Rtree.new root_node
end