Class: Geospatial::Filter

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

Defined Under Namespace

Classes: Range

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(curve) ⇒ Filter

Returns a new instance of Filter.



55
56
57
58
# File 'lib/geospatial/filter.rb', line 55

def initialize(curve)
	@ranges = []
	@curve = curve
end

Instance Attribute Details

#rangesObject (readonly)

Returns the value of attribute ranges.



60
61
62
# File 'lib/geospatial/filter.rb', line 60

def ranges
  @ranges
end

Instance Method Details

#add(prefix, order) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/geospatial/filter.rb', line 62

def add(prefix, order)
	if last = @ranges.last
		raise ArgumentError.new("Cannot add non-sequential prefix") unless prefix > last.max
	end
	
	unless last = @ranges.last and last.expand!(prefix, order)
		@ranges << Range.new(prefix, order)
	end
end

#apply(points) ⇒ Object Also known as: &



72
73
74
75
# File 'lib/geospatial/filter.rb', line 72

def apply(points)
	# This is a poor implementation.
	points.select{|point| @ranges.any?{|range| range.include?(point.hash)}}
end

#each(depth: 0) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/geospatial/filter.rb', line 83

def each(depth: 0)
	# TODO using a range tree might allow a significantly improved implementation.
	@curve.traverse do |child_origin, child_size, prefix, order|
		child = Box.new(Vector.elements(child_origin), Vector.elements(child_size))
		
		# puts "Considering (order=#{order}) #{child.inspect}..."
		
		min = prefix
		max = prefix | ((1 << (order*2)) - 1)
		
		if min_range = find(min)
			if order == depth # at bottom
				yield(child, prefix, order); :skip
			elsif min_range.include?(max)
				# This range completely contains the current prefix/order
				yield(child, prefix, order); :skip
			else
				# go deeper
			end
		elsif max_range = find(max)
			if order == depth # at bottom
				yield(child, prefix, order); :skip
			else
				# go deeper
			end
		elsif sub_range = @ranges.find{|range| range.min >= min && range.max <= max}
			# go deeper
		else
			:skip # out of bounds
		end
	end
end

#find(hash) ⇒ Object



116
117
118
# File 'lib/geospatial/filter.rb', line 116

def find(hash)
	@ranges.find{|range| range.include?(hash)}
end

#include?(point) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/geospatial/filter.rb', line 79

def include?(point)
	@ranges.any?{|range| range.include?(point.hash)}
end