2024-02-05 23:11:26 -05:00
|
|
|
import math
|
2024-01-26 12:36:53 -05:00
|
|
|
import subprocess
|
|
|
|
import xmltodict
|
|
|
|
|
2024-01-26 15:11:49 -05:00
|
|
|
import geopandas as gpd
|
|
|
|
from shapely.geometry import Point
|
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
from matsim_activity_to_matsim_schedule import MatsimActivityToMatsimSchedule
|
|
|
|
from hub_function_to_matsim_activity import HubFunctionToMatsimActivity
|
|
|
|
|
2024-02-05 23:11:26 -05:00
|
|
|
|
|
|
|
# TODO: Please check this https://github.com/matsim-org/matsim-code-examples/issues/63 for the CRS in matsim should match the city
|
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
class MatSimEngine:
|
|
|
|
def __init__(self, city, output_file_path):
|
|
|
|
self._city = city
|
|
|
|
self._output_file_path = output_file_path
|
|
|
|
|
|
|
|
facilities_dict = {
|
|
|
|
'facilities': {
|
|
|
|
'@name': 'Montreal Facilities',
|
|
|
|
'facility': []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hub_function_to_matsim = HubFunctionToMatsimActivity()
|
|
|
|
matsim_schedule = MatsimActivityToMatsimSchedule()
|
|
|
|
|
2024-01-26 15:11:49 -05:00
|
|
|
buildings_shape_data = {
|
|
|
|
'id': [],
|
|
|
|
'geometry': []
|
|
|
|
}
|
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
# 1- Facilities
|
2024-02-05 23:11:26 -05:00
|
|
|
# TODO: this should come from the factories, please check idf generation
|
2024-01-26 12:36:53 -05:00
|
|
|
for building in city.buildings:
|
2024-02-01 14:35:55 -05:00
|
|
|
activity = hub_function_to_matsim.dictionary[building.function].split(',')
|
2024-01-26 12:36:53 -05:00
|
|
|
|
2024-02-05 23:11:26 -05:00
|
|
|
for surface in building.grounds:
|
2024-01-26 15:11:49 -05:00
|
|
|
for coord in surface.solid_polygon.coordinates:
|
2024-02-05 23:11:26 -05:00
|
|
|
buildings_shape_data['id'].append(f"{building.name}")
|
2024-01-26 15:11:49 -05:00
|
|
|
buildings_shape_data['geometry'].append(Point(coord[0], coord[1]))
|
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
facility = {
|
|
|
|
'@id': building.name,
|
|
|
|
'@x': str(building.centroid[0]),
|
|
|
|
'@y': str(building.centroid[1]),
|
2024-02-01 14:35:55 -05:00
|
|
|
'activity': []
|
|
|
|
}
|
2024-02-05 23:11:26 -05:00
|
|
|
|
|
|
|
if len(building.thermal_zones_from_internal_zones) > 1:
|
|
|
|
raise NotImplementedError("multi-zone buildings aren't yet supported")
|
|
|
|
|
2024-02-06 10:56:07 -05:00
|
|
|
building_schedules = []
|
|
|
|
|
|
|
|
capacity = 0
|
2024-02-05 23:11:26 -05:00
|
|
|
for thermal_zone in building.thermal_zones_from_internal_zones:
|
|
|
|
capacity = thermal_zone.occupancy.occupancy_density * building.floor_area * building.storeys_above_ground
|
|
|
|
for schedule in thermal_zone.occupancy.occupancy_schedules:
|
2024-02-06 10:56:07 -05:00
|
|
|
building_schedules.append(schedule)
|
2024-02-05 23:11:26 -05:00
|
|
|
|
2024-02-01 14:35:55 -05:00
|
|
|
for new_activity in activity:
|
2024-02-06 10:56:07 -05:00
|
|
|
activity_info = {
|
2024-02-01 14:35:55 -05:00
|
|
|
'@type': new_activity,
|
2024-01-26 12:36:53 -05:00
|
|
|
'capacity': {
|
2024-02-05 23:11:26 -05:00
|
|
|
'@value': math.ceil(capacity)
|
2024-01-26 12:36:53 -05:00
|
|
|
},
|
2024-02-06 10:56:07 -05:00
|
|
|
'opentime': []
|
|
|
|
}
|
|
|
|
|
|
|
|
for schedule in building_schedules:
|
|
|
|
opening_hour = 0
|
|
|
|
closing_hour = 0
|
|
|
|
|
|
|
|
# Find opening hour (first hour > 0)
|
|
|
|
for i, value in enumerate(schedule.values):
|
|
|
|
if value > 0:
|
|
|
|
opening_hour = i
|
|
|
|
break
|
|
|
|
|
|
|
|
for i, value in reversed(list(enumerate(schedule.values))):
|
|
|
|
if value > 0:
|
|
|
|
closing_hour = i
|
|
|
|
break
|
|
|
|
|
|
|
|
for day in schedule.day_types:
|
|
|
|
if day[0:3] != 'hol':
|
|
|
|
activity_info['opentime'].append({
|
|
|
|
'@day': day[0:3],
|
|
|
|
'@start_time': opening_hour,
|
|
|
|
'@end_time': closing_hour
|
|
|
|
})
|
|
|
|
|
|
|
|
facility['activity'].append(activity_info)
|
2024-02-01 14:35:55 -05:00
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
facilities_dict['facilities']['facility'].append(facility)
|
|
|
|
|
2024-01-26 15:11:49 -05:00
|
|
|
gdf = gpd.GeoDataFrame(
|
|
|
|
buildings_shape_data,
|
2024-02-05 23:11:26 -05:00
|
|
|
crs=city.srs_name
|
2024-01-26 15:11:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
gdf.to_file("input_files/buildings_shapefile.shp")
|
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
# Convert the Python dictionary to an XML string
|
2024-02-06 10:56:07 -05:00
|
|
|
xml_content = xmltodict.unparse(facilities_dict, pretty=True, short_empty_elements=True)
|
2024-01-26 12:36:53 -05:00
|
|
|
|
|
|
|
# Write the XML to the file
|
2024-02-05 23:11:26 -05:00
|
|
|
with open(f"{output_file_path}/{city.name}_facilities.xml", 'w') as file:
|
2024-01-26 12:36:53 -05:00
|
|
|
file.write(xml_content)
|
|
|
|
|
|
|
|
# 2- Network
|
2024-01-26 15:11:49 -05:00
|
|
|
# First get only the part of the network necessary for the simulation
|
|
|
|
java_path = "java"
|
|
|
|
jar_path = "matsim-network-from-osm.jar"
|
2024-02-05 23:11:26 -05:00
|
|
|
command = [
|
|
|
|
java_path,
|
|
|
|
"-jar", jar_path,
|
|
|
|
"input_files/merged-network.osm.pbf",
|
|
|
|
"input_files/buildings_shapefile.shp",
|
|
|
|
f"{output_file_path}/network.xml.gz"
|
|
|
|
]
|
2024-01-26 15:11:49 -05:00
|
|
|
subprocess.run(command)
|
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
# 3- Population
|
2024-02-01 14:35:55 -05:00
|
|
|
|
|
|
|
# 3.1 - Public Transport
|
|
|
|
|
2024-01-26 12:36:53 -05:00
|
|
|
# 4- Config Generation
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
java_path = "java"
|
|
|
|
jar_path = "matsim.jar"
|
|
|
|
command = [java_path, "-jar", jar_path]
|
|
|
|
|
|
|
|
# Must generate this config file first.
|
|
|
|
# command.append(config_file_path)
|
|
|
|
|
2024-02-05 23:11:26 -05:00
|
|
|
subprocess.run(command)
|