Class: Way

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, user, time, color) ⇒ Way

Returns a new instance of Way.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tile.rb', line 139

def initialize(id, user, time, color)
	@id = id
	@user = user
	@color = color
	if time.kind_of? String then
		#2009-06-18T20:32:16Z
		time =~ /(\d*)-(\d*)-(\d*)T(\d*):(\d*):(\d*)Z/
		@timestamp = Time.local($1,$2,$3,$4,$5,$6)
	else
		@timestamp = time
	end
	@nodes=[]
	@currentwp=nil
	@distance_result=Hash.new(nil)
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



136
137
138
# File 'lib/tile.rb', line 136

def color
  @color
end

#currentwpObject

Returns the value of attribute currentwp.



136
137
138
# File 'lib/tile.rb', line 136

def currentwp
  @currentwp
end

#nodesObject (readonly)

Returns the value of attribute nodes.



136
137
138
# File 'lib/tile.rb', line 136

def nodes
  @nodes
end

#pathObject

Returns the value of attribute path.



136
137
138
# File 'lib/tile.rb', line 136

def path
  @path
end

Instance Method Details

#<<(node) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/tile.rb', line 155

def <<(node)
	i=@nodes.index(nil)
	if i.nil? then
		@nodes << node
		# fetch last 10 elements
		check_nodes = @nodes[-10, 10]
		if check_nodes.nil? then
			check_nodes = @nodes
		end
		avg_nodes = check_nodes.find_all{|n|
			(node.timestamp - n.timestamp) <= SPEED_AVG_TIME_SEC
		}
		speeds=[]
		t_diff = 0.0
		avg_nodes.each_with_index{|n, i|
			if i>0 then
				t_diff = n.timestamp - avg_nodes[i-1].timestamp
				if t_diff > 0.0 then
					speeds << n.distanceto(avg_nodes[i-1].lon, avg_nodes[i-1].lat) / t_diff
				end
			end
		}
		if speeds.length > 0 then
			avg_speed = speeds.inject(0){ |result, element| result + element } / speeds.length * 3.6
		else
			avg_speed = 0.0
		end
		node.speed = avg_speed
		@nodes.length
	else
		@nodes[i]=node
		i+1
	end
end

#del(lon, lat) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/tile.rb', line 190

def del(lon,lat)
	diff=[]
	@nodes.each{|n|
		diff << [ Math::sqrt((n.lat - lat) ** 2 + (n.lon - lon) ** 2) , n.lat, n.lon] if !n.nil?
	}
	mindist=999.0
	min=nil
	diff.each{|i|
		if i[0] < mindist then
			mindist = i[0]
			min=i
		end
	}
	deleted=nil
	@nodes.each_index {|i|
		if (!@nodes[i].nil?) and (@nodes[i].lat == min[1]) and (@nodes[i].lon == min[2]) then
			@nodes[i]=nil
			deleted=i
		end
	}
	return deleted+1
end

#distance(n) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/tile.rb', line 227

def distance(n)
	if @distance_result[n].nil? then
		distancenode=nil
		total_distance=0.0
		@nodes.each{|n|
			if distancenode.nil? then distancenode = n; end
			d = n.distanceto(distancenode.lon, distancenode.lat)
			if  d >= 1.0 then # ignore if smaller than 1 meter
				total_distance += d
				distancenode = n
			end
			@distance_result[n] = total_distance
		}
 		end
	return @distance_result[n]
end

#duration_strObject



218
219
220
221
222
223
224
225
# File 'lib/tile.rb', line 218

def duration_str
	if !@sec.nil? then
		return @sec
	end
	sec = (nodes.last.timestamp - nodes.first.timestamp).to_i
	min = sec / 60
	@sec = "#{min/60}:" + ("%02d" % (min % 60)) + (":%02d" % (sec % 60))
end

#toGPStimeObject



213
214
215
216
# File 'lib/tile.rb', line 213

def toGPStime()
	#2009-06-18T20:32:16Z
	return @timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
end