1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-25 04:56:30 +00:00
sumo-emissions/configurator/configurator.py

101 lines
3.7 KiB
Python
Raw Normal View History

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__)
STATICDIR = os.path.join(SCRIPTDIR, 'static')
2018-12-07 14:53:00 +00:00
SIMNAME = 'simul'
2018-12-07 15:20:31 +00:00
NETCONVERTCMD = ['netconvert', '-c', os.path.join(STATICDIR, f'{SIMNAME}.netcfg')]
POLYCONVERTCMD = ['polyconvert', '-c', os.path.join(STATICDIR, f'{SIMNAME}.polycfg')]
2018-12-07 14:53:00 +00:00
def clean():
# [os.remove(os.path.join('simul', f)) for f in os.listdir('simul')]
[os.remove(os.path.join('static', f))
for f in os.listdir('static') if f.endswith('.log')]
2018-12-10 12:16:35 +00:00
def load_netconvert_template(osm_input, out_name):
tree = ElementTree.parse(os.path.join(STATICDIR, 'simul.netcfg'))
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
def generate_network(osm_file, out_path, net_name):
template = load_netconvert_template(osm_file, net_name)
with tempfile.TemporaryDirectory() as tmpdirname:
netconfig = os.path.join(tmpdirname, f'{net_name}.netcfg')
template.write(netconfig)
# copy typemaps to tempdir
shutil.copytree(os.path.join(STATICDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
print('Creating the network…')
netconvertcmd = ['netconvert', '-c', netconfig]
subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print('Copying files')
shutil.copy(os.path.join(tmpdirname, f'{net_name}.net.xml'), os.path.join(out_path, f'{net_name}.net.xml'))
print('created')
2018-12-07 14:53:00 +00:00
def generate_scenario(out_path, name):
print('Creating the network…')
2018-12-07 15:45:11 +00:00
subprocess.run(NETCONVERTCMD, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
2018-12-07 14:53:00 +00:00
print('Extracting polygons…')
2018-12-07 15:45:11 +00:00
subprocess.run(POLYCONVERTCMD, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
2018-12-07 14:53:00 +00:00
print('Moving files')
2018-12-07 15:20:31 +00:00
shutil.move(os.path.join(STATICDIR, 'simul.net.xml'), os.path.join(out_path, f'{name}.net.xml'))
shutil.move(os.path.join(STATICDIR, 'simul.poly.xml'), os.path.join(out_path, f'{name}.poly.xml'))
shutil.copyfile(os.path.join(STATICDIR, 'simul.sumocfg'), os.path.join(out_path, f'{name}.sumocfg'))
2018-12-07 14:53:00 +00:00
# Move log files
logdir = os.path.join(out_path, 'log')
if not os.path.exists(logdir):
os.mkdir(logdir)
2018-12-07 15:20:31 +00:00
for f in os.listdir(STATICDIR):
2018-12-07 14:53:00 +00:00
if f.endswith('.log'):
2018-12-07 15:20:31 +00:00
shutil.move(os.path.join(STATICDIR, f), os.path.join(logdir, 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))
def generate_all(output_path, simulation_name):
output_path = os.path.join(output_path, simulation_name)
if not os.path.exists(output_path):
os.mkdir(output_path)
generate_scenario(output_path, simulation_name)
generate_mobility(output_path, simulation_name)
if __name__ == '__main__':
2018-12-10 12:16:35 +00:00
# clean()
# generate_all('/tmp/', 'simul')
output_file = '/tmp/simul'
if not os.path.exists(output_file):
os.mkdir(output_file)
osm_file = os.path.join(STATICDIR, 'simul.raw.osm')
generate_network(osm_file, output_file, 'test-sc')