diff --git a/__pycache__/matsim_visualizer.cpython-39.pyc b/__pycache__/matsim_visualizer.cpython-39.pyc index bab6254..e80f40f 100644 Binary files a/__pycache__/matsim_visualizer.cpython-39.pyc and b/__pycache__/matsim_visualizer.cpython-39.pyc differ diff --git a/main.py b/main.py index c47503b..2dfc576 100644 --- a/main.py +++ b/main.py @@ -28,7 +28,7 @@ try: Matsim(city, 'output_files').export() MatSimEngine('output_files/Montreal_config.xml').run() - visualizer = MatsimVisualizer('output_files/Montreal/output_network.xml.gz', 'output_files/Montreal/output_events.xml.gz', 'output_files', (3827652.8297388176, 6039933.844748944, 3873591.6501221485, 6042487.096594835)) + visualizer = MatsimVisualizer('output_files/Montreal/output_network.xml.gz', 'output_files/Montreal/output_events.xml.gz', 'output_files', (3830263.051512, 6043256.513982, 3866288.881858, 6012487.188767)) visualizer.visualize() except Exception as ex: diff --git a/matsim_visualizer.py b/matsim_visualizer.py index 1326f1a..1a77be0 100644 --- a/matsim_visualizer.py +++ b/matsim_visualizer.py @@ -15,6 +15,11 @@ class MatsimVisualizer(): self._links = [] self._pos = None self.norm = None + + self._max_x = None + self._min_x = None + self._max_y = None + self._min_y = None self._viewport = viewport self._output_file_path = output_file_path @@ -27,6 +32,8 @@ class MatsimVisualizer(): self._tick = 0 self._max_traffic = 0 + self._max_traffic_link = None + self._max_traffic_tick = None def load_data(self): # ====== LOAD NETWORK ====== # @@ -36,7 +43,20 @@ class MatsimVisualizer(): for node in network_doc.xpath('/network/nodes/node'): x = float(node.get('x')) y = float(node.get('y')) - if self._viewport[0] < x < self._viewport[2] and self._viewport[1] < y < self._viewport[3]: + + if self._max_x is None or x > self._max_x: + self._max_x = x + + if self._min_x is None or x < self._min_x: + self._min_x = x + + if self._max_y is None or y > self._max_y: + self._max_y = y + + if self._min_y is None or y < self._min_y: + self._min_y = y + + if self._viewport[0] < x < self._viewport[2] and self._viewport[1] > y > self._viewport[3]: self._nodes[node.get('id')] = (x, y) for link in network_doc.xpath('/network/links/link'): @@ -73,6 +93,8 @@ class MatsimVisualizer(): # We need to find the maximum value for traffic at any given point for the heatmap if self._max_traffic < cumulative_traffic[link_id]: self._max_traffic = cumulative_traffic[link_id] + self._max_traffic_link = link_id + self._max_traffic_tick = self._tick elif event_type in ['left link', 'vehicle leaves traffic']: self._traffic_per_tick[self._tick][link_id] -= 1 cumulative_traffic[link_id] -= 1 @@ -101,7 +123,6 @@ class MatsimVisualizer(): for link_id, change in traffic_change.items(): if link_id == link['id']: link['vehicles'] += change - break edge_colors.append(self._cmap(link['vehicles'])) edge_widths.append(1 + self.norm(link['vehicles']) * 10) @@ -121,6 +142,8 @@ class MatsimVisualizer(): fig, ax = plt.subplots() + ax.set_aspect((self._max_x - self._min_x) / (self._max_y - self._min_y)) + sm = plt.cm.ScalarMappable(cmap=self._cmap, norm=self.norm) sm.set_array([]) plt.colorbar(sm, ax=ax, label='Traffic Density') @@ -129,3 +152,5 @@ class MatsimVisualizer(): ani = FuncAnimation(fig, self.update, frames=len(self._traffic_per_tick), repeat=False) ani.save(f"{self._output_file_path}/traffic_animation.gif", writer='ffmpeg', fps=5) plt.show() + + print(f"Largest amount of vehicles ({self._max_traffic}) seen at {self._max_traffic_tick} on link {self._max_traffic_link}")