-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNetwork_2_graph.py
98 lines (90 loc) · 3.15 KB
/
Network_2_graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#################
# Convert Road network to a directed graph
# Generating node correlation matrix for the graph
# Document: https://arxiv.org/abs/1901.06758
# Last update: 2018/01/01
#################
import math
import csv
import os
import numpy as np
from os import listdir
from os.path import isfile, join
from datetime import datetime, timedelta
import collections
from geojson import Point, Feature, FeatureCollection
googlemap_key = ""
Features = []
term_2_link = {}
links = {}
oldlink = ''
ofile_d = open("link_latlon.csv", 'wb')
writer_d = csv.writer(ofile_d, delimiter=',', lineterminator='\n', dialect='excel')
writer_d.writerow(['Link ID', 'Location', "Latitude", "Longitude", 'Terminal_list'])
Link_geo = {}
with open("downtown_links.csv") as infile:
words = infile.readline().replace("\n", "").split(',')
link_lat = []
link_lon = []
TermList = []
for line in infile:
words = line.replace("\n", "").split(',')
Terminal = str(words[0])
link = str(words[5])
loc = str(words[2])
if link != oldlink and oldlink != '':
lat = np.average(link_lat)
lon = np.average(link_lon)
Terms = '_'.join(TermList)
writer_d.writerow([oldlink, oldloc, str(lat), str(lon), Terms])
mypoint = Point((lon, lat))
myfeature = Feature(geometry=mypoint, properties={"Link_ID": oldlink, "Terminals": Terms,
"Location": oldloc})
Features.append(myfeature)
Link_geo[oldlink] = (lat, lon)
###
link_lat = []
link_lon = []
TermList = []
link_lat.append(np.float(words[3]))
link_lon.append(np.float(words[4]))
TermList.append(Terminal)
oldlink = link
oldloc = loc
lat = np.average(link_lat)
lon = np.average(link_lon)
Terms = '_'.join(TermList)
writer_d.writerow([oldlink, oldloc, str(lat), str(lon), Terms])
mypoint = Point((lon, lat))
myfeature = Feature(geometry=mypoint, properties={"Link_ID": oldlink, "Terminals": Terms,
"Location": oldloc})
Features.append(myfeature)
Link_geo[oldlink] = (lat, lon)
output = FeatureCollection(Features)
ofile_geo = open("link_latlon.geojson", 'wb')
ofile_geo.write(str(output))
ofile_geo.close()
ofile_d.close()
import googlemaps
import json
import numpy as np
import time
gmaps = googlemaps.Client(key=googlemap_key)
station_list = Link_geo.keys()
station_list.sort()
n = len(station_list)
ofile_d = open("Dist_matrix.csv", 'wb')
writer_d = csv.writer(ofile_d, delimiter=',', lineterminator='\n', dialect='excel')
writer_d.writerow(["origin"]+ station_list)
origin = station_list[0]
dest = station_list[1]
js = gmaps.distance_matrix(origins = Link_geo[origin], destinations = Link_geo[dest], mode = 'driving')
for origin in station_list:
dist_array = []
for dest in station_list:
js = gmaps.distance_matrix(origins = Link_geo[origin], destinations = Link_geo[dest], mode = 'driving')
distance = (js['rows'][0]['elements'][0]['duration']['value'])
dist_array.append(distance)
print dist_array
time.sleep(61)
writer_d.writerow([origin]+dist_array)