@@ -0,0 +1,2181 @@
+# creating a network distance OD matrix using OSRM%0A# returns the distance in metres of the route with the shortest travel time for each OD pair %0A%0Aimport csv%0Aimport requests%0Aimport time%0Aimport json%0A# import polyline%0A%0Astart_time = time.time()%0A%0Aorigins = %5B%5D%0Adests = %5B%5D%0Awith open(%22TAZ_fixed_1.csv%22, 'r') as csvfile:%0A    reader = csv.DictReader(csvfile)%0A    n = 0%0A    for row in reader:%0A        # print row%0A        origins.append(%5Brow%5B'GTA06'%5D,row%5B'X'%5D,row%5B'Y'%5D%5D)%0A        dests.append(%5Brow%5B'GTA06'%5D,row%5B'X'%5D,row%5B'Y'%5D%5D)%0A        n += 1%0A%0A%0Aprint(len(origins))%0Aprint(len(dests))%0A%0A%0A# get list of destination ids%0Adids = %5B%5D%0Afor row in dests:%0A    dids.append(row%5B0%5D)%0A%0A%0A%0A# function for one-to-many - input origin X, origin Y, and list of destinations%0Adef row_request(ox,oy,destinations):%0A%0A    # grab string of all the coordinates - for plugging into OSRM url%0A    coord_str = str(ox) + ',' + str(oy) + ';'%0A    for row in destinations:%0A        coord_str = coord_str + str(row%5B1%5D) + ',' + str(row%5B2%5D) + ';'%0A    coord_str = coord_str%5B:-1%5D%0A%0A    # grab list of destinations IDs for URL string%0A    distr = ''%0A    di = 1%0A    while di %3C= len(destinations):%0A        distr = distr + str(di) + ';'%0A        di += 1%0A    distr = distr%5B:-1%5D%0A%0A    # setting up the url to send%0A    url = 'http://localhost:5000/table/v1/driving/' + coord_str + '?sources=0&destinations=' + distr + '&annotations=duration,distance'%0A%0A%0A    # sending and recieving the data%0A    page = requests.get(url)%0A    data = json.loads(page.content)%0A%0A    # print length for testing%0A    # print(len(data%5B'distances'%5D)) # should be 1%0A    # print(len(data%5B'distances'%5D%5B0%5D)) # should be = len(destinations)%0A%0A    # print(data)%0A    # return the data as a row%0A    return data%5B'distances'%5D%5B0%5D%0A%0A%0A# do this for a bunch of origins if needed = essentially an OD matrix%0Aout_rows = %5B%5B'OD'%5D + dids%5D%0Acou = 0%0Afor person in origins:%0A    tt = row_request(person%5B1%5D,person%5B2%5D,dests)%0A    tt = %5Bperson%5B0%5D%5D + tt%0A    out_rows.append(tt)%0A    print(%22----------------------------%22)%0A    print(cou, time.time() - start_time)%0A    cou += 1%0A%0Awith open(%22TAZ_distance_matrix.csv%22, 'w') as csvfile:%0A    writer = csv.writer(csvfile)%0A    for p in out_rows:%0A        writer.writerow(p)%0A

Create osrm_OD_distance_matrix.py