Working viewport

This commit is contained in:
Ruben1729 2024-02-21 12:44:08 -05:00
parent 1f6b05339f
commit d0bb062e2e
3 changed files with 28 additions and 3 deletions

View File

@ -28,7 +28,7 @@ try:
Matsim(city, 'output_files').export() Matsim(city, 'output_files').export()
MatSimEngine('output_files/Montreal_config.xml').run() 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() visualizer.visualize()
except Exception as ex: except Exception as ex:

View File

@ -15,6 +15,11 @@ class MatsimVisualizer():
self._links = [] self._links = []
self._pos = None self._pos = None
self.norm = None self.norm = None
self._max_x = None
self._min_x = None
self._max_y = None
self._min_y = None
self._viewport = viewport self._viewport = viewport
self._output_file_path = output_file_path self._output_file_path = output_file_path
@ -27,6 +32,8 @@ class MatsimVisualizer():
self._tick = 0 self._tick = 0
self._max_traffic = 0 self._max_traffic = 0
self._max_traffic_link = None
self._max_traffic_tick = None
def load_data(self): def load_data(self):
# ====== LOAD NETWORK ====== # # ====== LOAD NETWORK ====== #
@ -36,7 +43,20 @@ class MatsimVisualizer():
for node in network_doc.xpath('/network/nodes/node'): for node in network_doc.xpath('/network/nodes/node'):
x = float(node.get('x')) x = float(node.get('x'))
y = float(node.get('y')) 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) self._nodes[node.get('id')] = (x, y)
for link in network_doc.xpath('/network/links/link'): 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 # We need to find the maximum value for traffic at any given point for the heatmap
if self._max_traffic < cumulative_traffic[link_id]: if self._max_traffic < cumulative_traffic[link_id]:
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']: elif event_type in ['left link', 'vehicle leaves traffic']:
self._traffic_per_tick[self._tick][link_id] -= 1 self._traffic_per_tick[self._tick][link_id] -= 1
cumulative_traffic[link_id] -= 1 cumulative_traffic[link_id] -= 1
@ -101,7 +123,6 @@ class MatsimVisualizer():
for link_id, change in traffic_change.items(): for link_id, change in traffic_change.items():
if link_id == link['id']: if link_id == link['id']:
link['vehicles'] += change link['vehicles'] += change
break
edge_colors.append(self._cmap(link['vehicles'])) edge_colors.append(self._cmap(link['vehicles']))
edge_widths.append(1 + self.norm(link['vehicles']) * 10) edge_widths.append(1 + self.norm(link['vehicles']) * 10)
@ -121,6 +142,8 @@ class MatsimVisualizer():
fig, ax = plt.subplots() 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 = plt.cm.ScalarMappable(cmap=self._cmap, norm=self.norm)
sm.set_array([]) sm.set_array([])
plt.colorbar(sm, ax=ax, label='Traffic Density') 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 = 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) ani.save(f"{self._output_file_path}/traffic_animation.gif", writer='ffmpeg', fps=5)
plt.show() plt.show()
print(f"Largest amount of vehicles ({self._max_traffic}) seen at {self._max_traffic_tick} on link {self._max_traffic_link}")