Tick when time changes rather than on event

This commit is contained in:
Ruben1729 2024-03-12 13:32:47 -04:00
parent 2ab91cfaeb
commit 35ec942d94
5 changed files with 117 additions and 106 deletions

Binary file not shown.

10
main.py
View File

@ -1,3 +1,4 @@
import json
from pathlib import Path from pathlib import Path
from hub.imports.geometry_factory import GeometryFactory from hub.imports.geometry_factory import GeometryFactory
@ -10,7 +11,7 @@ from matsim import Matsim
from matsim_visualizer import MatsimVisualizer from matsim_visualizer import MatsimVisualizer
try: try:
file_path = (Path(__file__).parent / 'input_files' / 'mtlbld_v4.geojson') file_path = (Path(__file__).parent / 'input_files' / 'summerschool_all_buildings.geojson')
construction_format = 'nrcan' construction_format = 'nrcan'
usage_format = 'nrcan' usage_format = 'nrcan'
energy_systems_format = 'montreal_custom' energy_systems_format = 'montreal_custom'
@ -18,7 +19,7 @@ try:
out_path = (Path(__file__).parent / 'output_files') out_path = (Path(__file__).parent / 'output_files')
city = GeometryFactory('geojson', city = GeometryFactory('geojson',
path=file_path, path=file_path,
height_field='ETAGE_HORS', height_field='citygml_me',
year_of_construction_field='ANNEE_CONS', year_of_construction_field='ANNEE_CONS',
function_field='CODE_UTILI', function_field='CODE_UTILI',
function_to_hub=Dictionaries().montreal_function_to_hub_function).city function_to_hub=Dictionaries().montreal_function_to_hub_function).city
@ -31,10 +32,13 @@ try:
visualizer = MatsimVisualizer( visualizer = MatsimVisualizer(
'output_files/Montreal', 'output_files/Montreal',
'output_files', 'output_files',
(3854635.608067, 6040255.047194, 3855550.392442, 6039416.667303) (3854635.608067, 6040255.047194, 3855550.392442, 6039416.667303),
True
) )
visualizer.visualize() visualizer.visualize()
except Exception as ex: except Exception as ex:
print('error: ', ex) print('error: ', ex)
print('[simulation abort]') print('[simulation abort]')

View File

@ -55,14 +55,11 @@ class Matsim:
""" """
Exports the city's facilities data to XML and shapefile formats. Exports the city's facilities data to XML and shapefile formats.
""" """
buildings_shape_data = {
'id': [],
'geometry': []
}
facilities_xml = etree.Element('facilities', name=self._facilities['name']) facilities_xml = etree.Element('facilities', name=self._facilities['name'])
for building in self._city.buildings: for building in self._city.buildings:
try:
facility = { facility = {
'id': building.name, 'id': building.name,
'x': str(building.centroid[0]), 'x': str(building.centroid[0]),
@ -109,6 +106,8 @@ class Matsim:
facility['activity'].append(activity_info) facility['activity'].append(activity_info)
self._facilities['facility'].append(facility) self._facilities['facility'].append(facility)
except Exception as ex:
print('error: ', ex)
viewport = Polygon([(MONTREAL_LEFT, MONTREAL_TOP), (MONTREAL_RIGHT, MONTREAL_TOP), (MONTREAL_RIGHT, MONTREAL_BOTTOM), (MONTREAL_LEFT, MONTREAL_BOTTOM)]) viewport = Polygon([(MONTREAL_LEFT, MONTREAL_TOP), (MONTREAL_RIGHT, MONTREAL_TOP), (MONTREAL_RIGHT, MONTREAL_BOTTOM), (MONTREAL_LEFT, MONTREAL_BOTTOM)])
@ -142,6 +141,8 @@ class Matsim:
""" """
Generates and exports the city's population data to an XML file. Generates and exports the city's population data to an XML file.
""" """
try:
population = etree.Element("population") population = etree.Element("population")
id = 0 id = 0
@ -212,6 +213,8 @@ class Matsim:
current_work += 1 current_work += 1
id += 1 id += 1
except Exception as ex:
print('error: ', ex)
# Write xml content to file # Write xml content to file
xml_content = etree.tostring(population, pretty_print=True, encoding='UTF-8').decode('utf-8') xml_content = etree.tostring(population, pretty_print=True, encoding='UTF-8').decode('utf-8')

View File

@ -92,11 +92,14 @@ class MatsimVisualizer:
with gzip.open(self._events_file_path, 'rb') as file: with gzip.open(self._events_file_path, 'rb') as file:
events_doc = etree.parse(file) events_doc = etree.parse(file)
current_time = None
self._tick = 0 self._tick = 0
for event in events_doc.xpath('/events/event'): for event in events_doc.xpath('/events/event'):
link_id = event.get('link') link_id = event.get('link')
event_type = event.get('type') event_type = event.get('type')
time = float(event.get('time')) time = float(event.get('time'))
if current_time is None:
current_time = float(event.get('time'))
ticked = False ticked = False
if link_id is not None and event_type is not None and time is not None: if link_id is not None and event_type is not None and time is not None:
@ -115,8 +118,9 @@ class MatsimVisualizer:
cumulative_traffic[link_id] -= 1 cumulative_traffic[link_id] -= 1
ticked = True ticked = True
if ticked: if ticked and current_time != time:
self._tick += 1 self._tick += 1
current_time = time
def _create_graph(self): def _create_graph(self):
for node_id, coords in self._nodes.items(): for node_id, coords in self._nodes.items():