Class: Geoptima::LocationRange

Inherits:
Object
  • Object
show all
Defined in:
lib/geoptima/locationrange.rb

Direct Known Subclasses

LocationDistance, LocationEverywhere

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ LocationRange



43
44
45
46
47
48
49
50
# File 'lib/geoptima/locationrange.rb', line 43

def initialize(spec)
  f=spec.gsub(/\.\./,':').gsub(/RANGE[\(\[]/i,'').split(/[\,\;\:]/)
  if spec =~ /\.\./
    initialize_min_max(Point.new(f[0],f[2]), Point.new(f[1],f[3]))
  else
    initialize_min_max(Point.new(f[0],f[1]), Point.new(f[2],f[3]))
  end
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



42
43
44
# File 'lib/geoptima/locationrange.rb', line 42

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



42
43
44
# File 'lib/geoptima/locationrange.rb', line 42

def min
  @min
end

Class Method Details

.everywhereObject



76
77
78
# File 'lib/geoptima/locationrange.rb', line 76

def self.everywhere
  @@everywhere ||= LocationEverywhere.new
end

.from(spec) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/geoptima/locationrange.rb', line 67

def self.from(spec)
  if spec == '*' || spec =~ /everywhere/i
    LocationEverywhere.new
  elsif spec =~ /dist[\(\[]\s*([\d\.\-\+]+)\s*\,\s*([\d\.\-\+]+)\s*\,\s*([\d\.\-\+]+)\s*[\)\]]/i
    LocationDistance.new($1.to_f,Point.new($2,$3))
  else
    LocationRange.new(spec)
  end
end

.testObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/geoptima/locationrange.rb', line 79

def self.test
  [
    '56.1..57.0,12.0..15.8',
    '56.1,12.0,57.0,15.8',
    'range[56.1..57.0,12.0..15.8]',
    'range[56.1,12.0,57.0,15.8]',
    'everywhere',
    '*',
    'dist(70,56.5,12.0)'
  ].each do |test|
    puts "Testing: #{test}"
    range = Geoptima::LocationRange.from(test)
    puts "\t#{range}"
    puts "\tTesting MIN: #{range}.include?(#{range.min}) => #{range.include?(range.min)}"
    puts "\tTesting MAX: #{range}.include?(#{range.max}) => #{range.include?(range.max)}"
    (0..10).each do |i|
      latitude = 56.0 + 0.1 * i
      longitude = 11.0 + 0.2 * i
      p = Point.new(latitude,longitude)
      puts "\tTesting #{range}.include?(#{p}) => #{range.include?(p)}"
    end
  end
end

Instance Method Details

#include?(point) ⇒ Boolean



60
61
62
63
# File 'lib/geoptima/locationrange.rb', line 60

def include?(point)
  puts "Testing point #{point} in range #{self}" if($debug)
  point && point < @max && point >= @min
end

#initialize_min_max(min, max) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/geoptima/locationrange.rb', line 51

def initialize_min_max(min,max)
  @min = min
  @max = max
  if @min > @max
    p = @min
    @min = @max
    @max = p
  end
end

#to_sObject



64
65
66
# File 'lib/geoptima/locationrange.rb', line 64

def to_s
  [min,max].join(',')
end