2018-12-07 14:53:00 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import subprocess
|
2018-12-10 12:16:35 +00:00
|
|
|
import tempfile
|
|
|
|
from xml.etree import ElementTree
|
2018-12-07 14:53:00 +00:00
|
|
|
|
|
|
|
import randomTrips
|
|
|
|
|
2018-12-07 15:20:31 +00:00
|
|
|
# Absolute path of the directory the script is in
|
|
|
|
SCRIPTDIR = os.path.dirname(__file__)
|
2018-12-13 14:10:08 +00:00
|
|
|
TEMPLATEDIR = os.path.join(SCRIPTDIR, 'templates')
|
2018-12-07 14:53:00 +00:00
|
|
|
|
|
|
|
|
2018-12-10 12:16:35 +00:00
|
|
|
def load_netconvert_template(osm_input, out_name):
|
2018-12-13 14:10:08 +00:00
|
|
|
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg'))
|
2018-12-10 12:16:35 +00:00
|
|
|
root = tree.getroot()
|
|
|
|
root.find('input/osm-files').set('value', osm_input)
|
|
|
|
root.find('output/output-file').set('value', f'{out_name}.net.xml')
|
|
|
|
root.find('report/log').set('value', f'{out_name}.netconvert.log')
|
|
|
|
return tree
|
|
|
|
|
|
|
|
|
2018-12-10 16:59:26 +00:00
|
|
|
def load_polyconvert_template(osm_file, type_file, scenario_name):
|
2018-12-13 14:10:08 +00:00
|
|
|
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.polycfg'))
|
2018-12-10 16:59:26 +00:00
|
|
|
root = tree.getroot()
|
|
|
|
root.find('input/osm-files').set('value', osm_file)
|
|
|
|
root.find('input/net-file').set('value', f'{scenario_name}.net.xml')
|
|
|
|
root.find('input/type-file').set('value', type_file)
|
|
|
|
root.find('output/output-file').set('value', f'{scenario_name}.poly.xml')
|
|
|
|
root.find('report/log').set('value', f'{scenario_name}.polyconvert.log')
|
|
|
|
return tree
|
|
|
|
|
|
|
|
|
|
|
|
def load_sumoconfig_template(simulation_name):
|
2018-12-13 14:10:08 +00:00
|
|
|
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
|
2018-12-10 16:59:26 +00:00
|
|
|
root = tree.getroot()
|
|
|
|
root.find('input/net-file').set('value', f'{simulation_name}.net.xml')
|
|
|
|
root.find('input/route-files').set('value', f'{simulation_name}.rou.xml')
|
|
|
|
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml')
|
|
|
|
root.find('report/log').set('value', f'{simulation_name}.log')
|
|
|
|
return tree
|
|
|
|
|
|
|
|
|
|
|
|
def generate_scenario(osm_file, out_path, scenario_name):
|
|
|
|
net_template = load_netconvert_template(osm_file, scenario_name)
|
|
|
|
poly_template = load_polyconvert_template(osm_file, 'typemap/osmPolyconvert.typ.xml', scenario_name)
|
2018-12-13 14:05:37 +00:00
|
|
|
|
2018-12-10 12:16:35 +00:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
2018-12-10 16:59:26 +00:00
|
|
|
netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg')
|
|
|
|
polyconfig = os.path.join(tmpdirname, f'{scenario_name}.polycfg')
|
|
|
|
net_template.write(netconfig)
|
|
|
|
poly_template.write(polyconfig)
|
|
|
|
# Copy typemaps to tempdir
|
2018-12-13 14:10:08 +00:00
|
|
|
shutil.copytree(os.path.join(TEMPLATEDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
|
2018-12-10 16:59:26 +00:00
|
|
|
polyconvert_cmd = ['polyconvert', '-c', polyconfig]
|
2018-12-10 12:16:35 +00:00
|
|
|
netconvertcmd = ['netconvert', '-c', netconfig]
|
|
|
|
subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
2018-12-10 16:59:26 +00:00
|
|
|
subprocess.run(polyconvert_cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
|
|
# Move files to destination
|
|
|
|
files = [
|
|
|
|
f'{scenario_name}.net.xml',
|
|
|
|
f'{scenario_name}.netconvert.log',
|
|
|
|
f'{scenario_name}.poly.xml',
|
|
|
|
f'{scenario_name}.polyconvert.log'
|
|
|
|
]
|
|
|
|
for f in files:
|
|
|
|
shutil.move(os.path.join(tmpdirname, f), os.path.join(out_path, f))
|
2018-12-07 14:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_mobility(path, name):
|
|
|
|
routefile = os.path.join(path, f'{name}.rou.xml')
|
|
|
|
netfile = os.path.join(path, f'{name}.net.xml')
|
|
|
|
output = os.path.join(path, f'{name}.trips.xml')
|
|
|
|
end_time = 200
|
|
|
|
veh_class = 'passenger'
|
|
|
|
options = [
|
|
|
|
f'--net-file={netfile}',
|
|
|
|
f'--route-file={routefile}',
|
|
|
|
f'--end={end_time}',
|
|
|
|
f'--vehicle-class={veh_class}',
|
|
|
|
f'--output-trip-file={output}',
|
|
|
|
'--validate',
|
|
|
|
'--length',
|
|
|
|
]
|
|
|
|
print('Generating mobility…')
|
|
|
|
randomTrips.main(randomTrips.get_options(options))
|
|
|
|
|
|
|
|
|
2018-12-13 14:05:37 +00:00
|
|
|
def generate_sumo_configuration(path, scenario_name):
|
|
|
|
sumo_template = load_sumoconfig_template(scenario_name)
|
|
|
|
sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg'))
|
|
|
|
|
|
|
|
|
|
|
|
def generate_all(osm_file, output_path, simulation_name):
|
|
|
|
simulation_dir = os.path.join(output_path, simulation_name)
|
|
|
|
logs_dir = os.path.join(simulation_dir, 'log')
|
|
|
|
if not os.path.exists(simulation_dir):
|
|
|
|
os.mkdir(simulation_dir)
|
|
|
|
os.mkdir(logs_dir)
|
|
|
|
generate_scenario(osm_file, simulation_dir, simulation_name)
|
|
|
|
generate_mobility(simulation_dir, simulation_name)
|
|
|
|
generate_sumo_configuration(simulation_dir, simulation_name)
|
|
|
|
# Move all logs to logdir
|
|
|
|
for f in os.listdir(simulation_dir):
|
|
|
|
if os.path.splitext(f)[1] == '.log':
|
|
|
|
shutil.move(os.path.join(simulation_dir, f), logs_dir)
|
2018-12-07 14:53:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-12-13 14:05:37 +00:00
|
|
|
path = '/tmp/scenario/'
|
|
|
|
osm = '/tmp/scenario/map.osm'
|
|
|
|
generate_all(osm, path, 'foo')
|