-
Notifications
You must be signed in to change notification settings - Fork 13
/
graph_loader.py
53 lines (40 loc) · 1.6 KB
/
graph_loader.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
import sys
import os
import config
class Node:
def __init__(self, panoid, pano_yaw_angle, lat, lng):
self.panoid = panoid
self.pano_yaw_angle = pano_yaw_angle
self.neighbors = {}
self.coordinate = (lat, lng)
class Graph:
def __init__(self):
self.nodes = {}
def add_node(self, panoid, pano_yaw_angle, lat, lng):
self.nodes[panoid] = Node(panoid, pano_yaw_angle, lat, lng)
def add_edge(self, start_panoid, end_panoid, heading):
start_node = self.nodes[start_panoid]
end_node = self.nodes[end_panoid]
start_node.neighbors[heading] = end_node
class GraphLoader:
def __init__(self):
self.graph = Graph()
self.node_file = config.paths['node']
self.link_file = config.paths['link']
def construct_graph(self):
with open(self.node_file) as f:
for line in f:
panoid, pano_yaw_angle, lat, lng = line.strip().split(',')
self.graph.add_node(panoid, int(pano_yaw_angle), float(lat), float(lng))
with open(self.link_file) as f:
for line in f:
start_panoid, heading, end_panoid = line.strip().split(',')
self.graph.add_edge(start_panoid, end_panoid, int(heading))
num_edges = 0
for panoid in self.graph.nodes.keys():
num_edges += len(self.graph.nodes[panoid].neighbors)
print('===== Graph loaded =====')
print('Number of nodes:', len(self.graph.nodes))
print('Number of edges:', num_edges)
print('========================')
return self.graph