1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2025-07-01 21:55:44 +00:00

Refactor code

This commit is contained in:
2018-12-10 17:59:26 +01:00
parent 8f69983a80
commit dc4444a24e
3 changed files with 50043 additions and 53 deletions

View File

@ -10,15 +10,6 @@ import randomTrips
# Absolute path of the directory the script is in
SCRIPTDIR = os.path.dirname(__file__)
STATICDIR = os.path.join(SCRIPTDIR, 'static')
SIMNAME = 'simul'
NETCONVERTCMD = ['netconvert', '-c', os.path.join(STATICDIR, f'{SIMNAME}.netcfg')]
POLYCONVERTCMD = ['polyconvert', '-c', os.path.join(STATICDIR, f'{SIMNAME}.polycfg')]
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')]
def load_netconvert_template(osm_input, out_name):
@ -30,37 +21,50 @@ def load_netconvert_template(osm_input, out_name):
return tree
def generate_network(osm_file, out_path, net_name):
template = load_netconvert_template(osm_file, net_name)
def load_polyconvert_template(osm_file, type_file, scenario_name):
tree = ElementTree.parse(os.path.join(STATICDIR, 'simul.polycfg'))
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):
tree = ElementTree.parse(os.path.join(STATICDIR, 'simul.sumocfg'))
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)
with tempfile.TemporaryDirectory() as tmpdirname:
netconfig = os.path.join(tmpdirname, f'{net_name}.netcfg')
template.write(netconfig)
# copy typemaps to tempdir
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
shutil.copytree(os.path.join(STATICDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
print('Creating the network…')
polyconvert_cmd = ['polyconvert', '-c', polyconfig]
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')
def generate_scenario(out_path, name):
print('Creating the network…')
subprocess.run(NETCONVERTCMD, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print('Extracting polygons…')
subprocess.run(POLYCONVERTCMD, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
print('Moving files')
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'))
# Move log files
logdir = os.path.join(out_path, 'log')
if not os.path.exists(logdir):
os.mkdir(logdir)
for f in os.listdir(STATICDIR):
if f.endswith('.log'):
shutil.move(os.path.join(STATICDIR, f), os.path.join(logdir, f))
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))
def generate_mobility(path, name):
@ -82,19 +86,12 @@ def generate_mobility(path, name):
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)
def generate_all(osm_file, output_path):
output_dir = os.path.dirname(output_path)
simulation_name = os.path.basename(output_path)
generate_scenario(osm_file, output_dir, simulation_name)
generate_mobility(output_dir, simulation_name)
if __name__ == '__main__':
# 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')
pass