mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-21 19:16:30 +00:00
Refactor code
This commit is contained in:
parent
ceea79035d
commit
f50a11e3e8
@ -26,34 +26,34 @@ vehicle_classes = {
|
|||||||
|
|
||||||
|
|
||||||
def load_netconvert_template(osm_input, out_name):
|
def load_netconvert_template(osm_input, out_name):
|
||||||
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg'))
|
netconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.netcfg'))
|
||||||
root = tree.getroot()
|
root = netconfig.getroot()
|
||||||
root.find('input/osm-files').set('value', osm_input)
|
root.find('input/osm-files').set('value', osm_input)
|
||||||
root.find('output/output-file').set('value', f'{out_name}.net.xml')
|
root.find('output/output-file').set('value', f'{out_name}.net.xml')
|
||||||
root.find('report/log').set('value', f'{out_name}.netconvert.log')
|
root.find('report/log').set('value', f'{out_name}.netconvert.log')
|
||||||
return tree
|
return netconfig
|
||||||
|
|
||||||
|
|
||||||
def load_polyconvert_template(osm_file, type_file, scenario_name):
|
def load_polyconvert_template(osm_file, type_file, scenario_name):
|
||||||
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.polycfg'))
|
polyconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.polycfg'))
|
||||||
root = tree.getroot()
|
root = polyconfig.getroot()
|
||||||
root.find('input/osm-files').set('value', osm_file)
|
root.find('input/osm-files').set('value', osm_file)
|
||||||
root.find('input/net-file').set('value', f'{scenario_name}.net.xml')
|
root.find('input/net-file').set('value', f'{scenario_name}.net.xml')
|
||||||
root.find('input/type-file').set('value', type_file)
|
root.find('input/type-file').set('value', type_file)
|
||||||
root.find('output/output-file').set('value', f'{scenario_name}.poly.xml')
|
root.find('output/output-file').set('value', f'{scenario_name}.poly.xml')
|
||||||
root.find('report/log').set('value', f'{scenario_name}.polyconvert.log')
|
root.find('report/log').set('value', f'{scenario_name}.polyconvert.log')
|
||||||
return tree
|
return polyconfig
|
||||||
|
|
||||||
|
|
||||||
def load_sumoconfig_template(simulation_name, routefiles=None):
|
def load_sumoconfig_template(simulation_name, routefiles=()):
|
||||||
routefiles = routefiles or (f'{simulation_name}.rou.xml',)
|
routefiles = routefiles or (f'{simulation_name}.rou.xml',)
|
||||||
tree = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
|
sumoconfig = ElementTree.parse(os.path.join(TEMPLATEDIR, 'simul.sumocfg'))
|
||||||
root = tree.getroot()
|
root = sumoconfig.getroot()
|
||||||
root.find('input/net-file').set('value', f'{simulation_name}.net.xml')
|
root.find('input/net-file').set('value', f'{simulation_name}.net.xml')
|
||||||
root.find('input/route-files').set('value', ','.join(f for f in routefiles))
|
root.find('input/route-files').set('value', ','.join(routefiles))
|
||||||
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml')
|
root.find('input/additional-files').set('value', f'{simulation_name}.poly.xml')
|
||||||
root.find('report/log').set('value', f'{simulation_name}.log')
|
root.find('report/log').set('value', f'{simulation_name}.log')
|
||||||
return tree
|
return sumoconfig
|
||||||
|
|
||||||
|
|
||||||
def generate_scenario(osm_file, out_path, scenario_name):
|
def generate_scenario(osm_file, out_path, scenario_name):
|
||||||
@ -61,12 +61,14 @@ def generate_scenario(osm_file, out_path, scenario_name):
|
|||||||
poly_template = load_polyconvert_template(osm_file, 'typemap/osmPolyconvert.typ.xml', scenario_name)
|
poly_template = load_polyconvert_template(osm_file, 'typemap/osmPolyconvert.typ.xml', scenario_name)
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmpdirname:
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
||||||
|
# Generate POLYCONVERT and NETCONVERT configuration
|
||||||
netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg')
|
netconfig = os.path.join(tmpdirname, f'{scenario_name}.netcfg')
|
||||||
polyconfig = os.path.join(tmpdirname, f'{scenario_name}.polycfg')
|
polyconfig = os.path.join(tmpdirname, f'{scenario_name}.polycfg')
|
||||||
net_template.write(netconfig)
|
net_template.write(netconfig)
|
||||||
poly_template.write(polyconfig)
|
poly_template.write(polyconfig)
|
||||||
# Copy typemaps to tempdir
|
# Copy typemaps to tempdir
|
||||||
shutil.copytree(os.path.join(TEMPLATEDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
|
shutil.copytree(os.path.join(TEMPLATEDIR, 'typemap'), os.path.join(tmpdirname, 'typemap'))
|
||||||
|
# Call POLYCONVERT and NETCONVERT
|
||||||
polyconvert_cmd = ['polyconvert', '-c', polyconfig]
|
polyconvert_cmd = ['polyconvert', '-c', polyconfig]
|
||||||
netconvertcmd = ['netconvert', '-c', netconfig]
|
netconvertcmd = ['netconvert', '-c', netconfig]
|
||||||
subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
subprocess.run(netconvertcmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
@ -76,19 +78,20 @@ def generate_scenario(osm_file, out_path, scenario_name):
|
|||||||
shutil.copytree(tmpdirname, out_path, ignore=ignore_patterns)
|
shutil.copytree(tmpdirname, out_path, ignore=ignore_patterns)
|
||||||
|
|
||||||
|
|
||||||
def generate_mobility(path, name):
|
def generate_mobility(out_path, name):
|
||||||
netfile = os.path.join(path, f'{name}.net.xml')
|
netfile = f'{name}.net.xml'
|
||||||
output = os.path.join(path, f'{name}.trips.xml')
|
netpath = os.path.join(out_path, netfile)
|
||||||
|
output = os.path.join(out_path, f'{name}.trips.xml')
|
||||||
routefiles = []
|
routefiles = []
|
||||||
end_time = 200
|
end_time = 200
|
||||||
classes = ('passenger', 'bus')
|
classes = ('passenger', 'bus')
|
||||||
for veh_class in classes:
|
for veh_class in classes:
|
||||||
# simname.bus.rou.xml, simname.passenger.rou.xml, ...
|
# simname.bus.rou.xml, simname.passenger.rou.xml, ...
|
||||||
routefile = f'{name}.{veh_class}.rou.xml'
|
routefile = f'{name}.{veh_class}.rou.xml'
|
||||||
routepath = os.path.join(path, routefile)
|
routepath = os.path.join(out_path, routefile)
|
||||||
routefiles.append(routefile)
|
routefiles.append(routefile)
|
||||||
options = {
|
options = {
|
||||||
'--net-file': netfile,
|
'--net-file': netpath,
|
||||||
'--output-trip-file': output,
|
'--output-trip-file': output,
|
||||||
'--route-file': routepath,
|
'--route-file': routepath,
|
||||||
'-e': end_time
|
'-e': end_time
|
||||||
@ -104,7 +107,7 @@ def generate_random_trips(flags, options):
|
|||||||
|
|
||||||
|
|
||||||
def generate_sumo_configuration(routefiles, path, scenario_name):
|
def generate_sumo_configuration(routefiles, path, scenario_name):
|
||||||
sumo_template = load_sumoconfig_template(routefiles, scenario_name)
|
sumo_template = load_sumoconfig_template(scenario_name, routefiles=routefiles)
|
||||||
sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg'))
|
sumo_template.write(os.path.join(path, f'{scenario_name}.sumocfg'))
|
||||||
|
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
<input>
|
<input>
|
||||||
<net-file value="simul.net.xml"/>
|
<net-file value="simul.net.xml"/>
|
||||||
<osm-files value="simul.raw.osm"/>
|
<osm-files value="simul.raw.osm"/>
|
||||||
<!--<osm.keep-full-type value="true"/>-->
|
|
||||||
<type-file value="typemap/osmPolyconvert.typ.xml"/>
|
<type-file value="typemap/osmPolyconvert.typ.xml"/>
|
||||||
</input>
|
</input>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user