Class: Geoptima::Locator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sorted, options = {}) ⇒ Locator

Returns a new instance of Locator.



118
119
120
121
122
123
124
# File 'lib/geoptima/locator.rb', line 118

def initialize(sorted, options={})
  @sorted = sorted
  @options = Hash[*options.map{|k,v| [k.to_s.intern,v]}.flatten]
  @options[:algorithm] ||= 'window'
  @options[:window] ||= 60
  puts "Initialized geo-location on #{@sorted.length} events with options: #{@options.inspect}" if($debug)
end

Instance Attribute Details

#failedObject (readonly)

Returns the value of attribute failed.



117
118
119
# File 'lib/geoptima/locator.rb', line 117

def failed
  @failed
end

#locatedObject (readonly)

Returns the value of attribute located.



117
118
119
# File 'lib/geoptima/locator.rb', line 117

def located
  @located
end

#optionsObject (readonly)

Returns the value of attribute options.



117
118
119
# File 'lib/geoptima/locator.rb', line 117

def options
  @options
end

#sortedObject (readonly)

Returns the value of attribute sorted.



117
118
119
# File 'lib/geoptima/locator.rb', line 117

def sorted
  @sorted
end

Instance Method Details

#algorithmObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/geoptima/locator.rb', line 128

def algorithm
  @algorithm ||= case @options[:algorithm].to_s
  when /^\-win/
    BeforeLocatorAlgorithm.new
  when /^\+win/
    AfterLocatorAlgorithm.new
  when /closest/
    ClosestLocatorAlgorithm.new
  when /inter/
    InterpolationLocatorAlgorithm.new
  else
    LocatorAlgorithm.new
  end
end

#locateObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/geoptima/locator.rb', line 142

def locate
  gps = nil
  @located = []
  @failed = []
  locatables = []
  time_window = @options[:window].to_i
  time_window = 60 if(time_window<1)
  puts "Locating within window[#{time_window}] using algorithm:#{algorithm.class}" if($debug)
  sorted.each do |event|
    event.time ||= start + event[:timestamp].to_f / Geoptima::MSPERDAY
    if event.name === 'gps'
      gps = event
      puts "Setting GPS location point: #{gps.inspect}" if($debug)
      gps.location = Point.new(gps['latitude'].to_f, gps['longitude'].to_f)
      locatables.each do |event|
        event.set_next_if(gps,time_window)
      end
      locatables = []
    else
      event.set_previous_if(gps,time_window)
      locatables << event
      @located << event
    end
  end
  @located = @located.map do |l|
    if self.algorithm.locate(l)
      l
    else
      @failed << l
      nil
    end
  end.compact
end

#startObject



125
126
127
# File 'lib/geoptima/locator.rb', line 125

def start
  @start ||= sorted[0] && sorted[0][:time] || DateTime.now
end