Пример #1
0
def total_pollution_cost(nodes,osm,factor): #takes in a list of nodes, returns the total pollution cost of the way
    x,y= [osm[node].lat for node in nodes],[osm[node].lon for node in nodes]

    #print x,y
#    f = interp1d(x, y)
#    f2 = interp1d(x, y, kind='cubic')
    length = int(getlengthofway(x,y)*factor) #with this calculation, each evenly-spaced point is around 50 meters from each other
    M = max(1,length) #The reason this was slow is because every way is interpolated the same number of points regardless of length, what we want to do is to make the number of interpolating points directly proportional to the length of way.
    #in order to optimise this (to make number of interpolating points proportional to the length of way) we need to calculate the length of way.
    t = np.linspace(0, len(x), M) #creates M points from 0-len(x)
    x = np.interp(t, np.arange(len(x)), x)
    y = np.interp(t, np.arange(len(y)), y)
    tol = 0.0004 #problem with this weighting algorithm is that single-node streets can be disproportionately weighted compared to streets with many nodes...
    i, idx = 0, [0]
    j=0
    while i < len(x):
        total_dist = 0
        for j in range(i+1, len(x)):
            total_dist += sqrt((x[j]-x[j-1])**2 + (y[j]-y[j-1])**2)
            if total_dist > tol:
                idx.append(j)
                break
        i = j+1
    xn = x[idx]
    yn = y[idx]
    pollution_levels = datainterp.load_func(xn,yn)
    result=0
    for i in pollution_levels: #this assumes all evenly points across all ways are actually equally spaced
        result+=i
    return result
Пример #2
0
def avg_pollution_level(xn,yn):
    pollution_levels = datainterp.load_func(xn,yn)
    return sum(pollution_levels)/len(pollution_levels)