class Way

Attributes

color[R]
currentwp[RW]
nodes[R]
path[RW]

Public Class Methods

new(id, user, time, color) click to toggle source
# File lib/fgmapping/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

Public Instance Methods

<<(node) click to toggle source
# File lib/fgmapping/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) click to toggle source
# File lib/fgmapping/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) click to toggle source
# File lib/fgmapping/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_str() click to toggle source
# File lib/fgmapping/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
toGPStime() click to toggle source
# File lib/fgmapping/tile.rb, line 213
def toGPStime()
        #2009-06-18T20:32:16Z
        return @timestamp.strftime("%Y-%m-%dT%H:%M:%SZ")
end